当前位置:网站首页>【C补充】【字符串】按日期排序显示一个月的日程
【C补充】【字符串】按日期排序显示一个月的日程
2022-07-01 16:28:00 【一苇以航fp】
1、程序说明
要求: 用户按固定格式输入一系列日程(输入0时停止),程序将其按日期排序,并逐行显示。
输入格式: “日期 事件”。例如 “24 Mom’s birthday”
2、思路
2.1 字符串存储形式
- 将读入的字符串中的日期和事件分开存储(
scanf读取日期,getchar逐个读取事件字符串),便于之后的比较和排序。 - 排序(
strcpy函数)结束后,二者合并(strcat函数)存储,存储在二维字符数组中; - 将排序好且合并后的字符数组逐行输出。
2.2 排序存储的方法
- 提取日期,单独存储,将其与此前的日期比较(
strcmp函数);
此前的日期已被存储在日程字符串的前2位,由strcmp函数的比较机制,不必额外单独提取 - 找到比其大的那一行,余下各行各下移一行,空出该行的位置;
由数组存储机制,下移实际是后移 - 将该日期对应的完整内容存储在空出的该行之中。
3、代码
/************************************ * 显示一个月的日程列表 * * **********************************/
#include<stdio.h>
#include<string.h>
#define MAX_REMIND 50 //事件个数上限
#define MSG_LEN 60 //各事件描述的长度上限
int read_line(char str[], int n); //读取事件内容并存储
int main(void)
{
char reminders[MAX_REMIND][MSG_LEN+3]; //+3是预留日期和字符串空字符的位置
char day_str[3], msg_str[MSG_LEN+1]; //临时存储日期和事件
int day, i, j;
int num_remind = 0; //记录已读取的日程数量
//设置死循环,保证用户可持续输入
for(;;){
//限制读取数量
if(num_remind == MAX_REMIND){
printf("———————————— 日程已达上限 ————————————\n");
break;
}
printf("Enter day and reminder: ");
scanf("%2d", &day); //读取前两位的数字
if(day == 0) break; //读取终止
sprintf(day_str, "%2d", day); //将日期转换为字符串存储,占两格右对齐
read_line(msg_str, MSG_LEN); //读取事件并存储
/********按日期排序********/
//逐行比较,找到适合适合的位置(i)
for(i=0; i<num_remind; i++){
if( strcmp(day_str, reminders[i]) < 0 ) break;
}
//空出该位置所在行(其下方字符串各下移一行)
for(j=num_remind; j>i; j--){
strcpy( reminders[j], reminders[j-1] );
}
//将日期和事件合并存储
strcpy( reminders[i], day_str );
strcat( reminders[i], msg_str );
num_remind++;
}
//逐行显示排序后的日程
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' ) //空格不会被跳过(与scanf不同)
if(i<n) str[i++] = ch;
str[i] = '\0'; //字符数组末位加空字符,成为字符串
return i; //返回字符数组长度
}
4、运行结果示例

边栏推荐
- Alibaba cloud, Zhuoyi technology beach grabbing dialogue AI
- Redis distributed lock
- Research and investment strategy report of hydroxypropyl beta cyclodextrin industry in China (2022 Edition)
- 数据库系统原理与应用教程(005)—— yum 离线安装 MySQL5.7(Linux 环境)
- sql刷题1050. 合作过至少三次的演员和导演
- Detailed explanation of activity life cycle and startup mode
- Is the programmer's career really short?
- [nodemon] app crashed - waiting for file changes before starting... resolvent
- 【flask入门系列】Cookie与Session
- Leetcode 77 combination -- backtracking method
猜你喜欢

How to repair the laptop that cannot connect to the wireless network

Hi Fun Summer, play SQL planner with starrocks!

Dataframe gets the number of words in the string

數據庫系統原理與應用教程(006)—— 編譯安裝 MySQL5.7(Linux 環境)

String class

VMware virtual machine failed during startup: VMware Workstation is incompatible with hyper-v

VMware 虛擬機啟動時出現故障:VMware Workstation 與 Hyper-v 不兼容...

SQL question brushing 627 Change gender

巴比特 | 元宇宙每日必读:奈雪币、元宇宙乐园、虚拟股票游戏...奈雪的茶这波“操作拉满”的营销活动你看懂了吗?...

想做软件测试的女孩子看这里
随机推荐
China sorbitol Market Forecast and investment strategy report (2022 Edition)
C语言输入/输出流和文件操作
如何使用 etcd 实现分布式 /etc 目录
P2893 [USACO08FEB] Making the Grade G(dp&优先队列)
P2893 [usaco08feb] making the grade g (DP & priority queue)
MLPerf Training v2.0 榜单发布,在同等GPU配置下百度飞桨性能世界第一
想做软件测试的女孩子看这里
How to use phpipam to manage IP addresses and subnets
红队第10篇:coldfusion反序列化过waf改exp拿靶标的艰难过程
How to restore the system with one click on Lenovo laptop
巴比特 | 元宇宙每日必读:奈雪币、元宇宙乐园、虚拟股票游戏...奈雪的茶这波“操作拉满”的营销活动你看懂了吗?...
[jetsonnano] [tutorial] [introductory series] [III] build tensorflow environment
Bugku's file contains
Leetcode 216 combined summation III -- backtracking method
How to solve the problem that the battery icon of notebook computer does not display
Template Engine Velocity Foundation
Tutorial on the principle and application of database system (003) -- MySQL installation and configuration: manually configure MySQL (Windows Environment)
Redis6.0 new features
嗨 FUN 一夏,与 StarRocks 一起玩转 SQL Planner!
UML旅游管理系统「建议收藏」