| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 423 人关注过本帖
标题:感激不尽!希望大家检查这个程序有什么毛病,谢谢!
只看楼主 加入收藏
江东周公瑾
Rank: 1
来 自:火星
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-6-18
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
感激不尽!希望大家检查这个程序有什么毛病,谢谢!
#include<stdio.h>
int main(){
    int c1,c2;                                                          /*c1 c2是字符串数量*/
    scanf("%d%d",&c1,&c2);                                              /*得到c1c2数值*/
    char ch1[20],ch2[20];                                               /*两个字符串*/
    int a,b,k=0;                                                            /*循环变量 K记录相等数量*/
    for(a=0;a<c1;a++)
    scanf("%c",ch1[a]);
    for(b=0;b<c2;b++)
    scanf("%c",ch2[b]);                                                 /*得到字符串*/
                        if(c1<c2){                                                         /*比较c1c2大小*/
                                   for(a=0;a<c1;a++)
                                   printf("%c",ch1[a]);
                                   printf("\n");
                                   for(b=0;b<c2;b++)
                                   printf("%c",ch2[b]);
    }
    else               if(c1>c2){
                                   for(b=0;b<c2;b++)
                                   printf("%c",ch2[b]);
                                   printf("\n");
                                   for(a=0;a<c1;a++)
                                   printf("%c",ch1[a]);
    }
    else     for(a=0;a<c1;a++){
             if(ch1[a]>ch2[a]){
                               for(b=0;b<c1;b++)
                                                printf("%c",ch2[b]);
                               printf("\n");
                               for(b=0;b<c1;b++)
                               printf("%c",ch1[b]);
                               break;
                               }
             if(ch2[a]>ch1[a]){
                               for(b=0;b<c1;b++)
                                                printf("%c",ch1[b]);
                               printf("\n");
                               for(b=0;b<c1;b++)
                               printf("%c",ch2[b]);
                               break;
                               }
              else ++k;
              if(k==c1){
                        for(b=0;b<c1;b++)
                                                printf("%c",ch2[b]);
                               printf("\n");
                               for(b=0;b<c1;b++)
                               printf("%c",ch1[b]);
                               break;
                               }

}      
return 0;
}      






此程序目的是得到两个字符串,并按照ASCII的顺序输出。编译通过,但在运行时提示错误,这是为什么呢?
搜索更多相关主题的帖子: 毛病 感激不尽 检查 
2010-06-18 09:41
myhnuhai
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:3
帖 子:425
专家分:1725
注 册:2010-3-17
收藏
得分:2 
细节错误改了一下,可以运行,但不知道你这个的目的是什么:#include<stdio.h>
int main(){
    int c1,c2;
    int a,b,k=0;  
    char ch1[20],ch2[20];                                                       /*c1 c2是字符串数量*/
    scanf("%d%d",&c1,&c2);                                             /*得到c1c2数值*/                                                /*两个字符串*/                                                            /*循环变量 K记录相等数量*/
    for(a=0;a<c1;a++)
    scanf("%c",&ch1[a]);
    for(b=0;b<c2;b++)
    scanf("%c",&ch2[b]);                                                 /*得到字符串*/
                        if(c2>c1){                                                         /*比较c1c2大小*/
                                   for(a=0;a<c1;a++)
                                   printf("%c",ch1[a]);
                                   printf("\n");
                                   for(b=0;b<c2;b++)
                     
                                   printf("%c",ch2[b]);
    }
    else               if(c1>c2){
                                   for(b=0;b<c2;b++)
                                   printf("%c",ch2[b]);
                                   printf("\n");
                                   for(a=0;a<c1;a++)
                                   printf("%c",ch1[a]);
    }
    else     for(a=0;a<c1;a++){
             if(ch1[a]>ch2[a]){
                               for(b=0;b<c1;b++)
                                                printf("%c",ch2[b]);
                               printf("\n");
                               for(b=0;b<c1;b++)
                               printf("%c",ch1[b]);
                               break;
                               }
             if(ch2[a]>ch1[a]){
                               for(b=0;b<c1;b++)
                                                printf("%c",ch1[b]);
                               printf("\n");
                               for(b=0;b<c1;b++)
                               printf("%c",ch2[b]);
                               break;
                               }
              else ++k;
              if(k==c1){
                        for(b=0;b<c1;b++)
                                                printf("%c",ch2[b]);
                               printf("\n");
                               for(b=0;b<c1;b++)
                               printf("%c",ch1[b]);
                               break;
                               }

}      
return 0;
}

不要让肮脏的记忆,迷失了原本纯洁的心灵!
2010-06-18 09:59
heartnheart
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:335
专家分:1096
注 册:2009-7-10
收藏
得分:9 
你的错误在于scanf("%c",&ch1[a]);和scanf("%c",&ch2[b]);没加&,加了之后能运行,但是不能忽略空白符啊

你就想排序字符串是吧

  for(a=0;a<c1;a++)
    scanf("%c",&ch1[a]);
    for(b=0;b<c2;b++)
    scanf("%c",&ch2[b]);                                                
改为

    scanf("%s",ch1);
  
    scanf("%s",ch2);   
就行啦   

[ 本帖最后由 heartnheart 于 2010-6-18 10:11 编辑 ]
2010-06-18 10:09
dominator
Rank: 2
等 级:论坛游民
帖 子:82
专家分:29
注 册:2009-2-16
收藏
得分:9 
for(a=0;a<c1;a++)
    scanf("%c",ch1[a]);
    for(b=0;b<c2;b++)
    scanf("%c",ch2[b]);
没加地址符吧·!
  改为scanf("%c",&ch1[a]);
      scanf("%c",&ch2[b]);也可以啊·
2010-06-18 13:06
快速回复:感激不尽!希望大家检查这个程序有什么毛病,谢谢!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028499 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved