c++ - C语言实现输入多行英文句子然后统计单词数和行数,如何输入?我的代码问题在哪里?
PHPz
PHPz 2017-04-17 13:11:55
0
3
1132
int main(){
    char s[100]={0};
    char *p=s;
    int num=0,flag=0,wnum=0,line=0;
    while ((gets(s))!=EOF) {
        if ((*p>='a'||*p<='z')||(*p>='Z'||*p<='A')) {
            wnum++;
            if (flag==0) {
                *p-=32;
            }
            flag=1;
        }
        else{
            if (flag==1){
                num++;
                flag=0;
                
            }
            if (*p=='\n'||*p=='\0') {
                line++;
            }
            
        }
        p++;
    }
    int ave=wnum/num;
    printf("Number of lines: %d\nNumber of words: %d\nAverage length of a word: %d",line,num,ave);
}

输入多行之后,command+z还是结束不了输入
请问问题在哪里?
有什么更好的接受多行文字的方法吗?
这是原题,希望不是我错误理解题意了……
谢谢...

PHPz
PHPz

学习是最好的投资!

reply all(3)
迷茫

Your main problem is that eof is entered incorrectly. It is ctrl z under win, but not under mac, but ctrl d. Note that it is not common

伊谢尔伦

The author tries to output printf before num, there is a high probability that it is equal to 0.
The most serious problem:

、*(如果以下看不懂请移动至最下面,对gets()函数的分析)*、

1. gets(s)Return one line at a time instead of returning all characters before EOF at once! The poster may have thought this wrong. If not, please read the analysis below.
2. Why num=0? gets(s)Read one line each time, s is a string. The author only judged one character *p each time, and then continued the next cycle. The input example is likely to cause num =0 means that the num++ sentence will not be executed. Another loop while(*p!='

伊谢尔伦

This question is answered by the book written by the father of the C language.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!