很菜的蔡鸟请教,请各位大侠稍作停留,帮忙!
1。输入一行字符,分别统计其中英文字母、空格、数字和其他字符的个数。
#include"stdio.h"
#include"math.h"
void main()
{
char c;
int letters=0,space=0,digit=0,others=0;
printf("please input some characters\n");
while((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);
}
以上程序编译的时候警告说c没有初始化,就那样运行的话,所有的输入都被统计成others了。
2.将一个正整数分解质因数。
#include"stdio.h"
#include"math.h"
void main()
{
int n,i;
printf("put in a number:");
scanf("%d",&n);
for(i=2;i<n;i++)
{
  if(n%i==0)
   {
      printf("%d,",i);/*可以被整除的话,输出i*/
      n=n/i;/*用n除以i的商作为新的n*/
    }
      else 
      break;
}
printf("%d",n);
}
以上程序编译正确,运行不正确,比如输入90,结果只有2,3。
[此贴子已经被作者于2005-11-2 15:38:26编辑过]

 
											





 
	     
											


