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

边栏推荐
- Intelligent operation and maintenance practice: banking business process and single transaction tracking
- Report on Market Research and investment prospects of ammonium dihydrogen phosphate industry in China (2022 Edition)
- Alibaba cloud Li Feifei: China's cloud database has taken the lead in many mainstream technological innovations abroad
- 中国酶制剂市场预测与投资战略研究报告(2022版)
- China BMS battery management system Market Research Report (2022 Edition)
- Petrv2: a unified framework for 3D perception of multi camera images
- Redis 分布式鎖
- 两数之和c语言实现[通俗易懂]
- [mathematical modeling] [matlab] implementation of two-dimensional rectangular packing code
- Why should you consider using prism
猜你喜欢

vulnhub靶场-hacksudo - Thor

Girls who want to do software testing look here

Intel's open source deep learning tool library openvino will increase cooperation with local software and hardware parties and continue to open

走进微信小程序

Countdownlatch blocking wait for multithreading concurrency

Gameframework eating guide

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"

Basic usage of Frida
荣威 RX5 的「多一点」产品策略

Intelligent operation and maintenance practice: banking business process and single transaction tracking
随机推荐
Is it safe to open a stock account by mobile phone? What do you need to bring with you to open an account?
(28) Shape matching based on contour features
【C語言補充】判斷明天是哪一天(明天的日期)
China biodegradable plastics market forecast and investment strategy report (2022 Edition)
剑指 Offer II 105. 岛屿的最大面积
如何使用 etcd 实现分布式 /etc 目录
线上开通ETF基金账户安全吗?有哪些步骤?
Intelligent operation and maintenance practice: banking business process and single transaction tracking
vulnhub靶场-Hacker_Kid-v1.0.1
提交review时ReviewBoard出现500错误解决方法
Iommu/smmuv3 code analysis (10) page table operation
Redis Distributed Lock
Code example of libcurl download file
Please, stop painting star! This has nothing to do with patriotism!
ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing
In depth evaluation and development trend prediction report of China's ice cream market (2022 Edition)
Soft test network engineer full truth simulation question (including answer and analysis)
Report on research and investment prospects of China's silicon nitride ceramic substrate industry (2022 Edition)
Object. fromEntries()
Shenyu gateway development: enable and run locally