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还是结束不了输入
请问问题在哪里?
有什么更好的接受多行文字的方法吗?
这是原题,希望不是我错误理解题意了……
谢谢...
迷茫2017-04-17 13:13:55
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
伊谢尔伦2017-04-17 13:13:55
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!=' ')
should be added to determine each character in s.
3. Every time a line of numbers is judged, p should start from the beginning of s, so char *p=s
should be placed in the while(get(s))
loop.
For other methods, I recommend using while((ch=getchar())!=EOF)
. This is very consistent with your idea. You can judge it character by character. You can search for this sentence. It is quite classic.
int getchar ( void)
The return value is the ASCII code entered by the user. Reading to the end of the file returns EOF. The value of EOF is -1
In terms of method, you can use spaces or newlines to determine the number of words, and 'n' to determine the number of lines. According to the title, there should be no periods or commas.
For example
char ch;
while((ch=getchar())!=EOF){
if(ch=='\n') {
lineNum++;
wordNum++;
}
else if(ch==' ') {
wordNum++;
}
printf("%d %d",lineNum,wordNum);
while(1); // 按Ctrz+z后卡死在这里可以看输出的结果
I tested it and the visual inspection is correct.
Sorry, I didn’t look at the code carefully before, I just looked at one line while(gets(s)!=EOF)
. I just wrote the answer. I just remembered that there seems to be no problem, so I took a look at your code.
Previous answer:
I think the poster is using a C compiler, but I can’t compile it using a C++ compiler. ERROR:ISO C++ forbids comparison between pointer and integer
.
gets() is wrong, gets() returns a pointer, EOF is an integer, you should use while(gets(s)!=NULL)
.
As follows:
The function prototype is char * gets (char * buffer); if the read is successful, the same pointer as the buffer will be returned. If an error or EOF is encountered, NULL will be returned.
When executing, continuously read characters from stdin, stop when encountering a newline character or EOF, and store the read results in the buffer. Note that newline characters will be converted to ‘
伊谢尔伦2017-04-17 13:13:55
This question is answered by the book written by the father of the C language.