当前位置:网站首页>[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

边栏推荐
- Determine whether the linked list is a palindrome linked list
- Intel's open source deep learning tool library openvino will increase cooperation with local software and hardware parties and continue to open
- ShenYu 网关开发:在本地启用运行
- Transition technology from IPv4 to IPv6
- The difference between the lazy mode of singleton mode and the evil mode
- Kia recalls some K3 new energy with potential safety hazards
- Vulnhub range hacker_ Kid-v1.0.1
- Petrv2: a unified framework for 3D perception of multi camera images
- Redis 分布式鎖
- 【C语言基础】12 字符串
猜你喜欢

【splishsplash】关于如何在GUI和json上接收/显示用户参数、MVC模式和GenParam

Pytest learning notes (13) -allure of allure Description () and @allure title()

官宣!香港科技大学(广州)获批!

(十六)ADC转换实验

6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单

【C语言基础】12 字符串

(17) DAC conversion experiment

多线程使用不当导致的 OOM

Babbitt | yuan universe daily must read: Naixue coin, Yuan universe paradise, virtual stock game Do you understand Naixue's tea's marketing campaign of "operation pull full"
![[pyg] document summary and project experience (continuously updated](/img/b4/75da8c3e657069be4e3e3bfd5b2dc0.png)
[pyg] document summary and project experience (continuously updated
随机推荐
ACL 2022 | decomposed meta learning small sample named entity recognition
Leetcode records - sort -215, 347, 451, 75
pyqt5中,在控件上画柱状图
SQL注入漏洞(Mysql与MSSQL特性)
Computed property “xxx“ was assigned to but it has no setter.
ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing
China BMS battery management system Market Research Report (2022 Edition)
Research Report on China's enzyme Market Forecast and investment strategy (2022 Edition)
可迭代对象与迭代器、生成器的区别与联系
(1) CNN network structure
FRP intranet penetration, reverse proxy
在MeterSphere接口测试中如何使用JMeter函数和MockJS函数
Maizeer: the two batches of products reported by the media have been taken off the shelves and sealed, and consumer appeals are accepted
SQL question brushing 584 Looking for user references
Petrv2: a unified framework for 3D perception of multi camera images
机器学习11-聚类,孤立点判别
【C語言補充】判斷明天是哪一天(明天的日期)
How to use JMeter function and mockjs function in metersphere interface test
(12) About time-consuming printing
Girls who want to do software testing look here