|

楼主 |
发表于 2019-8-13 14:36:47
|
显示全部楼层
这个程序很奇怪,有时运行结果正确,有时错误。但是我的思路应该是没问题的。下面是我最后修改的代码。
- #include <stdio.h>
- #include <string.h>
- #define MAX_REMIND 50
- #define MSG_LEN 60
- int main()
- {
- char reminders[MAX_REMIND][MSG_LEN+3]={0};
- char day_str[3]={0},msg_str[MSG_LEN+1]={0};
- int day[MAX_REMIND]={0},i,j=0,num_remind=0,ch,len=0,tx,suoyin;
- char *p[MAX_REMIND],*px;
-
- for(;;)
- {
- printf("Enter the day and reminder:");
- scanf("%d",&day[j]);
- if(day[j]==0)
- break;
- while(day[j]>99)
- day[j]/=10;
- sprintf(day_str,"%2d",day[j]);
- i=1;
- msg_str[0]=' ';
- while((ch=getchar())!='\n')
- msg_str[i++]=ch;
- msg_str[i]='\0';
- strcat(strcpy(reminders[j],day_str),msg_str);
- j++;
- }
- len=j;
- for(i=0;i<len;i++)
- {
- p[i]=reminders[i];
- }
- for(i=0;i<len-1;i++)
- {
- int min=day[i]; //先找到数组day[]最小的值,标记索引位置
- for(j=i+1;j<len;j++)
- {
- if(min>day[j])
- {
- min=day[j];
- suoyin=j;
- }
- }
- tx=day[i]; //将day[]中每个数字以及其对应信息同时移动位置
- day[i]=min;
- day[suoyin]=tx;
-
- px=p[i];
- p[i]=reminders[suoyin];
- p[suoyin]=px;
- }
-
-
- for(i=0;i<len;i++)
- printf("%-7d%s\n",day[i],p[i]);
- return 0;
- }
复制代码 |
|