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

边栏推荐
- 中国氮化硅陶瓷基板行业研究与投资前景报告(2022版)
- 麦趣尔:媒体报道所涉两批次产品已下架封存,受理消费者诉求
- 中国冰淇淋市场深度评估及发展趋势预测报告(2022版)
- Is it safe to open an ETF account online? What are the steps?
- 中国锦纶长丝缝纫线发展预测与投资方向研究报告(2022版)
- 如何写出好代码 — 防御式编程指南
- China sorbitol Market Forecast and investment strategy report (2022 Edition)
- The amazing open source animation library is not only awesome, but also small
- mysql -- explain性能优化
- GameFramework食用指南
猜你喜欢

Why should you consider using prism

Cookies and session keeping technology

Intelligent operation and maintenance practice: banking business process and single transaction tracking

How wild are hackers' ways of making money? CTF reverse entry Guide

Detailed explanation of string's trim() and substring()

(27) Open operation, close operation, morphological gradient, top hat, black hat

SQL question brushing 584 Looking for user references

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

字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化

DNS
随机推荐
In aks, use secret in CSI driver mount key vault
Develop those things: easycvr cluster device management page function display optimization
如何使用 etcd 实现分布式 /etc 目录
Pyqt5, draw a histogram on the control
SystemVerilog structure (II)
Sword finger offer 20 String representing numeric value
Soft test software designer full truth simulation question (including answer analysis)
pyqt5中,在控件上画柱状图
Iommu/smmuv3 code analysis (10) page table operation
(27) Open operation, close operation, morphological gradient, top hat, black hat
(十七)DAC转换实验
[mathematical modeling] [matlab] implementation of two-dimensional rectangular packing code
深度优先遍历和广度优先遍历[通俗易懂]
字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化
Integer array merge [JS]
【C语言补充】判断明天是哪一天(明天的日期)
JDBC:深入理解PreparedStatement和Statement[通俗易懂]
Report on Market Research and investment prospects of ammonium dihydrogen phosphate industry in China (2022 Edition)
JDBC: deep understanding of Preparedstatement and statement[easy to understand]
[pyg] document summary and project experience (continuously updated