工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 3036|回复: 8

c语言的问题

[复制链接]
发表于 2008-7-27 15:13 | 显示全部楼层 |阅读模式
在其他的论坛发现一段代码,统计大写字母时说是比实际多一个,不知怎么回事,代码如下(不知为什么有些符号在这里显示不出来,下面是代码载图):

#include<stdio.h>
void main()
{
  char c,str[3][80];
  int i,j,bw=0,lw=0,num=0,ept=0,other=0;/*bw大写字母,lw小写字母,num数字,ept空格,other其他*/
  for(i=0;i<3;i++)
  gets(str);
  for(i=0;i<3;i++)
   { for(j=0;j<80;j++)
      {
       c=str[j];
       if(c>='a' && c<='z')
       lw++;
       else if(c>='A' && c<='Z')
       bw++;
       else if(c>='0' && c<='9')
       num++;
       else if(c==' ')
       ept++;
       else other++;
      }
    }
  printf("lw=%d,bw=%d,num=%d,ept=%d,other=%d\n",lw,bw,num,ept,other);
}


我的测试结果如下:


[ 本帖最后由 六月飞霜 于 2008-7-27 15:17 编辑 ]
Snap1.jpg
Snap1.jpg
发表于 2008-7-27 15:38 | 显示全部楼层
类似的,有无参考价值~~?

题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
1.程序分析:利用while语句,条件为输入的字符不为'\n'.
      
2.程序源代码:
#include "stdio.h"
main()
{char c;
 int letters=0,space=0,digit=0,others=0;
 printf("please input some characters\n");
 while((c=getchar())!='\n')
 {
 if(c>='a'&&c<='z'||c>='A'&&c<='Z')
  letters++;
 else if(c==' ')
  space++;
   else if(c>='0'&&c<='9')
       digit++;
     else
       others++;
}
printf("all in all:char=%d space=%d digit=%d others=%d\n",letters,
space,digit,others);
}
回复

使用道具 举报

 楼主| 发表于 2008-7-27 16:09 | 显示全部楼层
原帖由 于 2008-7-27 15:38 发表
类似的,有无参考价值~~?

题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
1.程序分析:利用while语句,条件为输入的字符不为'\n'.
      
2.程序源代码:
#include "stdio ...

现在问题是为什么其他的统计都正确,唯独大写字母的统计不正确?
回复

使用道具 举报

发表于 2008-7-27 16:30 | 显示全部楼层
你的数组都没初始化,编译器随机赋的值谁也不知道是什么,说不定在你的机器上跑刚好就给赋了个大写字母也说不定。
把for循环改为while循环,把循环条件改为判断是否\n,或者是否串结束试试看。
回复

使用道具 举报

发表于 2008-7-27 16:35 | 显示全部楼层
关键在于数组没初始化,而作者偏偏要把80个字节遍历一遍。
回复

使用道具 举报

 楼主| 发表于 2008-7-27 17:36 | 显示全部楼层
原帖由 gyCai 于 2008-7-27 16:35 发表
关键在于数组没初始化,而作者偏偏要把80个字节遍历一遍。

的确是这个原因,谢了!!
回复

使用道具 举报

 楼主| 发表于 2008-8-4 20:44 | 显示全部楼层
原帖由 abcd932 于 2008-8-4 20:04 发表
China's registered urban and township unemployment rate stood at four percent in the first half, down 0.2 percentage point from the same world of warcraft goldperiod last year, the Ministry of Human R ...

ls的你在这里发了什么鬼东西??
回复

使用道具 举报

发表于 2008-8-5 22:06 | 显示全部楼层
你得代码加一句(红色的那一句),对于系统初始化的数据不测试,只测试那些那读入的

  1. #include<iostream>
  2. #include<stdio.h>
  3. int main()
  4. {
  5.   char c,str[3][80];
  6.   int i,j,bw=0,lw=0,num=0,ept=0,other=0;/*bw大写字母,lw小写字母,num数字,ept空格,other其他*/
  7.   for(i=0;i<3;i++)
  8.     gets(str[i]);
  9.    
  10.   for(i=0;i<3;i++)
  11.    { for(j=0;j<80&&str[i][j];j++)
  12.       {
  13.        c=str[i][j];
  14.        if(c>='a' && c<='z')
  15.        lw++;
  16.        else if(c>='A' && c<='Z')
  17.        bw++;
  18.        else if(c>='0' && c<='9')
  19.        num++;
  20.        else if(c==' ')
  21.        ept++;
  22.        else other++;
  23.       }
  24.     }
  25.   printf("lw=%d,bw=%d,num=%d,ept=%d,other=%d\n",lw,bw,num,ept,other);
  26.   system("pause");
  27.   return 0;
  28. }
复制代码
回复

使用道具 举报

头像被屏蔽
发表于 2008-9-8 13:55 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2025-5-14 00:47

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表