当前位置:网站首页>[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 function
Comparison 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
边栏推荐
- Research Report on development monitoring and investment prospects of China's smart environmental protection industry (2022 Edition)
- 中国超高分子量聚乙烯产业调研与投资前景报告(2022版)
- (16) ADC conversion experiment
- Redis 分布式鎖
- 求求你们,别再刷 Star 了!这跟“爱国”没关系!
- [Verilog quick start of Niuke network question brushing series] ~ priority encoder circuit ①
- 中国乙腈市场预测与战略咨询研究报告(2022版)
- Encryption and decryption of tinyurl in leetcode
- [pyg] document summary and project experience (continuously updated
- 手机开户股票开户安全吗?那么开户需要带些什么?
猜你喜欢
Leetcode records - sort -215, 347, 451, 75
6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单
官宣!香港科技大学(广州)获批!
How to use JMeter function and mockjs function in metersphere interface test
Vulnhub range hacksudo Thor
(十六)ADC转换实验
Sword finger offer 20 String representing numeric value
Shenyu gateway development: enable and run locally
Girls who want to do software testing look here
String class
随机推荐
Sword finger offer 20 String representing numeric value
荣威 RX5 的「多一点」产品策略
【C补充】【字符串】按日期排序显示一个月的日程
换掉UUID,NanoID更快更安全!
阿里云李飞飞:中国云数据库在很多主流技术创新上已经领先国外
【Try to Hack】vulnhub DC4
多线程并发之CountDownLatch阻塞等待
重磅披露!上百个重要信息系统被入侵,主机成为重点攻击目标
RadHat搭建内网YUM源服务器
Research Report on development monitoring and investment prospects of China's smart environmental protection industry (2022 Edition)
FRP intranet penetration, reverse proxy
China BMS battery management system Market Research Report (2022 Edition)
Encryption and decryption of tinyurl in leetcode
Official announcement! Hong Kong University of science and Technology (Guangzhou) approved!
Research Report on China's enzyme Market Forecast and investment strategy (2022 Edition)
Report on Market Research and investment prospects of ammonium dihydrogen phosphate industry in China (2022 Edition)
(28) Shape matching based on contour features
【牛客网刷题系列 之 Verilog快速入门】~ 优先编码器电路①
(17) DAC conversion experiment
Gameframework eating guide