当前位置:网站首页>[C supplement] [string] display the schedule of a month by date
[C supplement] [string] display the schedule of a month by date
2022-07-01 17:31:00 【One reed to sail FP】
1、 Program description
requirement : The user enters a series of schedules in a fixed format ( Input 0 Stop when ), The program sorts them by date , And display line by line .
Input format : “ date event ”. for example “24 Mom’s birthday”
2、 Ideas
2.1 String storage form
- Store the date and event in the read string separately (
scanf Read date ,getchar Read the event strings one by one), For later comparison and sorting . - Sort (
strcpy function) After the end , The two merge (strcat function) Storage , Stored in a two-dimensional character array ; - Output the sorted and merged character array line by line .
2.2 Method of sorting storage
- Date of withdrawal , Separate storage , Compare it with previous dates (
strcmp function);
The previous date has been stored before the schedule string 2 position , fromstrcmp functionComparison mechanism , There is no need to extract separately - Find a bigger line , Move the remaining lines down one line , Vacate the position of the line ;
By the array storage mechanism , Moving down is actually moving back - Store the complete content corresponding to the date in the empty line .
3、 Code
/************************************ * Show the schedule list of a month * * **********************************/
#include<stdio.h>
#include<string.h>
#define MAX_REMIND 50 // Maximum number of events
#define MSG_LEN 60 // The upper limit of the length of each event description
int read_line(char str[], int n); // Read the event content and store
int main(void)
{
char reminders[MAX_REMIND][MSG_LEN+3]; //+3 Is to reserve the position of date and empty character of string
char day_str[3], msg_str[MSG_LEN+1]; // Temporarily store dates and events
int day, i, j;
int num_remind = 0; // Record the number of schedules read
// Set up a dead cycle , Ensure that users can input continuously
for(;;){
// Limit the number of reads
if(num_remind == MAX_REMIND){
printf("———————————— The schedule has reached the upper limit ————————————\n");
break;
}
printf("Enter day and reminder: ");
scanf("%2d", &day); // Read the first two digits
if(day == 0) break; // Read terminated
sprintf(day_str, "%2d", day); // Convert date to string storage , Align right in two spaces
read_line(msg_str, MSG_LEN); // Read events and store
/******** Sort by date ********/
// Line by line comparison , Find the right place (i)
for(i=0; i<num_remind; i++){
if( strcmp(day_str, reminders[i]) < 0 ) break;
}
// Leave the line where the position is located ( The string below it moves down one line )
for(j=num_remind; j>i; j--){
strcpy( reminders[j], reminders[j-1] );
}
// Merge and store dates and events
strcpy( reminders[i], day_str );
strcat( reminders[i], msg_str );
num_remind++;
}
// Show the sorted schedule line by line
printf("\n\nDay Reminder: \n");
for(i=0; i<num_remind; i++){
printf(" %s\n", reminders[i]);
return 0;
}
int read_line(char str[], int n)
{
int i=0, ch;
while( (ch=getchar()) != '\n' ) // Spaces are not skipped ( And scanf Different )
if(i<n) str[i++] = ch;
str[i] = '\0'; // Add empty characters at the end of the character array , Become a string
return i; // Returns the length of the character array
}
4、 Examples of run results

边栏推荐
- Official announcement! Hong Kong University of science and Technology (Guangzhou) approved!
- Basic usage of Frida
- JDBC: deep understanding of Preparedstatement and statement[easy to understand]
- Gameframework eating guide
- China acetonitrile market forecast and strategic consulting research report (2022 Edition)
- Determine whether the linked list is a palindrome linked list
- China biodegradable plastics market forecast and investment strategy report (2022 Edition)
- String的trim()和substring()详解
- 中国PBAT树脂市场预测及战略研究报告(2022版)
- 美国国家安全局(NSA)“酸狐狸”漏洞攻击武器平台技术分析报告
猜你喜欢

Free lottery | explore the future series of blind box digital copyright works of "abadou" will be launched on the whole network!

Kia recalls some K3 new energy with potential safety hazards

机器学习11-聚类,孤立点判别

美国国家安全局(NSA)“酸狐狸”漏洞攻击武器平台技术分析报告

DNS

(1) CNN network structure

Shenyu gateway development: enable and run locally

存在安全隐患 起亚召回部分K3新能源

(十七)DAC转换实验

Jojogan practice
随机推荐
Openlayers 自定义气泡框以及定位到气泡框
redis -- 数据类型及操作
反射型XSS漏洞
荣威 RX5 的「多一点」产品策略
Transition technology from IPv4 to IPv6
Depth first traversal and breadth first traversal [easy to understand]
China PBAT resin Market Forecast and Strategic Research Report (2022 Edition)
Soft test software designer full truth simulation question (including answer analysis)
Determine whether the linked list is a palindrome linked list
Redis distributed lock
Intel's open source deep learning tool library openvino will increase cooperation with local software and hardware parties and continue to open
Report on research and investment prospects of UHMWPE industry in China (2022 Edition)
如何使用 etcd 实现分布式 /etc 目录
[wrung Ba wrung Ba is 20] [essay] why should I learn this in college?
重磅披露!上百个重要信息系统被入侵,主机成为重点攻击目标
Alibaba cloud Li Feifei: China's cloud database has taken the lead in many mainstream technological innovations abroad
[C language supplement] judge which day tomorrow is (tomorrow's date)
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
Openlayers customize bubble boxes and navigate to bubble boxes
Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse