|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <stdio.h>
- #include <string.h>
- #define MAX_REMIND 50
- #define MSG_LEN 60
- int main()
- {
- char reminders[MAX_REMIND][MSG_LEN+3];
- char day_str[3],msg_str[MSG_LEN+1];
- int day[MAX_REMIND]={0},i,j=0,num_remind=0,ch,len=0;
- char *p[MAX_REMIND];
-
- /*
- for(;;)
- {
- printf("Enter the day and reminder:");
- scanf("%d",&day[j]);
- if(day[j]==0)
- break;
- 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++;
- }
- */
-
- for(i=0;i<j;i++)
- {
- p[i]=reminders[i];
- len++;
- }
- ch=len-1;
- while(ch>=0)
- {
- j=-1;
- for(i=0;i<=ch-1;i++)
- {
- j=i+1;
- if(day[i]>day[j])
- {
- p[i]=reminders[j];
- p[j]=reminders[i];
- }
-
- }
- ch=j;
- }
-
- for(i=0;i<len;i++)
- printf("%s\n",p[i]);
-
- return 0;
-
- }
复制代码
/********************************************************************************************************************************************
我的问题出在(代码里用注释标记的那个for循环)
那部分代码想要实现:输入一个数字,(空格结束数字的输入)。然后是字符串信息(用回车结束)。把这两部分统一保存为字符串保存在二维数组里。
问题来了:只输入一条信息(然后输入数字0,可以退出这个for循环),如果输入2及以上的信息,再输入0,退不出这个循环。
请大家帮我看看,谢谢。
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX_REMIND 50
- #define MSG_LEN 60
- int main(void)
- {
- char reminders[MAX_REMIND][MSG_LEN + 3] ;
- char day_str[3] , msg_str[MSG_LEN + 1] ;
- int day[MAX_REMIND]={0} , i , j = 0 , num_remind = 0 , ch , len = 0 ;
- int tx ;
- 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 = 0 ;
- msg_str[i] = '\0' ;
- while((ch = getchar()) != '\n') msg_str[i ++] = ch ;
- msg_str[i]= '\0' ;
- strcat(strcpy(reminders[j] , day_str) , msg_str) ;
- j ++ ;
- }
- for(i = 0 ; i < j ; i ++) {
- p[i] = reminders[i] ;
- len ++ ;
- }
- for(i = 1 ; i < len ; i ++) {
- for(j = i ; j > 0 && day[j] < day[j - 1] ; j --) {
- tx = day[j] ;
- day[j] = day[j - 1] ;
- day[j - 1] = tx ;
- px = p[j] ;
- p[j] = p[j - 1] ;
- p[j - 1] = px ;
- }
- }
- for(i = 0 ; i < len ; i ++) printf("%s\n" , p[i]) ;
- }
复制代码
你是怎么弄的,看看我这里的运行实况:
- C:\Bin>g++ -o x2 x2.c
- C:\Bin>x2
- Enter the day and reminder : 13 1234
- Enter the day and reminder : 12 1234
- Enter the day and reminder : 11 1234
- Enter the day and reminder : 1588 1234
- Enter the day and reminder : 1789 1234
- Enter the day and reminder : 19870 1234
- Enter the day and reminder : 291876 1234
- Enter the day and reminder : 5 1234
- Enter the day and reminder : 2 1234
- Enter the day and reminder : 0
- 2 1234
- 5 1234
- 11 1234
- 12 1234
- 13 1234
- 15 1234
- 17 1234
- 19 1234
- 29 1234
- C:\Bin>
复制代码
|
|