c++程序每个步骤解释下

来源:百度知道 编辑:UC知道 时间:2024/05/26 04:44:59
#include<stdio.h>
#include<string.h>
main()
{
char str[1024];
int blank=0;
int num=0;
int letter=0;
int word=0;
int numword=0;
int i,len=0;
clrscr();
for(i=0;;i++)
{
str[i]=getchar();
if(str[i]=='\n') break;
}
len=strlen(str);
i=0;
do
{
if(str[i]==' ')
{
blank++;
}
if(str[i]>='0'&&str[i]<='9')
{
num++;
}
if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))
{
letter++;
}
if(str[i]==' ')
{
word=0;
numword++;
}
i++;
}
while(i<len);
printf("there are %d num(s) in the line,the percent is %f\n",num,(float) num/len);
printf("there are %d blank(s) in the line,the percent is %f\n",blank,(float) blank/len);
printf("there are %d letter(s) i

调试过了

void main()
{
char str[1024]; //定义字符数组
int blank=0; //空格个数
int num=0; //数字个数
int letter=0; //字母个数
int word=0; //是否在一个单词中
int numword=0; //单词个数
int i,len=0;
for(i=0;;i++)
{
str[i]=getchar(); //读入字符,直到输入回车
if(str[i]=='\n') break;
}
str[i]='\0';
len=strlen(str); //取字符串长度,输入了len个字符
i=0;
do
{
if(str[i]==' ') //如果为' ',空格数+1
{
blank++;
}
if(str[i]>='0'&&str[i]<='9') //如果为数字,num+1
{
num++;
}
if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')) //如果为字母,letter+1
{
letter++;
}
if(str[i]==' ') //如果是空格,说明开始一个单词,单词数加1
{
word=0;
numword++;
}
i++;
}
while(i<len);
//输出统计的个数,和百分比
printf("there are %d num(s) in the line,the percent is %f%