1 //输出一个月提醒 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 7 #define MAX_REMIND 50 //提醒的数量 8 #define MSG_LEN 60 //提醒的长度 9 int main(void){ 10 int read_line(char str[], int n); 11 char *reminders[MAX_REMIND]; 12 char day_str[3], msg_str[MSG_LEN+1]; 13 int day, i ,j, num_remind = 0; 14 15 for(;;){ 16 if(num_remind == MAX_REMIND){ 17 printf("--No space left--\n"); 18 break; 19 } 20 21 printf("Enter day and reminder:"); 22 scanf("%2d",&day); 23 if (day == 0) break; 24 sprintf(day_str, "%2d",day); 25 read_line(msg_str,MSG_LEN); //读取字符串 26 27 for(i=0;i<num_remind;i++) //将日期靠前的提醒放在前面 28 if(strcmp(day_str,reminders[i]) < 0) break; 29 for(j=num_remind;j>i;j--) 30 reminders[j]=reminders[j-1]; 31 32 reminders[i] = malloc(2 + strlen(msg_str) + 1 ); 33 if(reminders[i] == NULL) printf("--No space left--\n"); 34 35 strcpy(reminders[i],day_str); 36 strcat(reminders[i],msg_str); 37 38 num_remind++; 39 40 } 41 printf("\n Day Remind\n"); 42 for(i=0;i<num_remind;i++) 43 printf("%s\n",reminders[i]); 44 45 return 0; 46 } 47 48 int read_line(char str[], int n){ 49 int ch, i=0; 50 while ( (ch=getchar()) !=‘\n‘) 51 if(i<n) 52 str[i++]=ch; 53 str[i]=‘\n‘; 54 return i; 55 }
时间: 2024-11-14 06:55:38