C 句子的平均字长
我想计算一个句子的平均字长 例如,给定输入C 句子的平均字长,c,C,我想计算一个句子的平均字长 例如,给定输入abc def ghi,平均字长将为3.0 #include <stdio.h> #include <conio.h> int main() { char ch,temp; float avg; int space = 1,alphbt = 0,k = 0; printf("Enter a sentence: "); while((ch = getchar()) != '\n') { temp = ch;
abc def ghi
,平均字长将为3.0
#include <stdio.h>
#include <conio.h>
int main()
{
char ch,temp;
float avg;
int space = 1,alphbt = 0,k = 0;
printf("Enter a sentence: ");
while((ch = getchar()) != '\n')
{
temp = ch;
if( ch != ' ')
{
alphbt++;
k++; // To ignore spaces before first word!!!
}
else if(ch == ' ' && k != 0)
space++;
}
if (temp == ' ') //To ignore spaces after last word!!!
printf("Average word lenth: %.1f",avg = (float) alphbt/(space-1));
else
printf("Average word lenth: %.1f",avg = (float) alphbt/space);
getch();
}
程序可以运行,但我想忽略单词之间的额外空格。因此,给出以下句子:
abc def
(单词之间有两个空格),平均单词长度计算为2.0
,而不是3.0
#include <stdio.h>
#include <conio.h>
int main()
{
char ch,temp;
float avg;
int space = 1,alphbt = 0,k = 0;
printf("Enter a sentence: ");
while((ch = getchar()) != '\n')
{
temp = ch;
if( ch != ' ')
{
alphbt++;
k++; // To ignore spaces before first word!!!
}
else if(ch == ' ' && k != 0)
space++;
}
if (temp == ' ') //To ignore spaces after last word!!!
printf("Average word lenth: %.1f",avg = (float) alphbt/(space-1));
else
printf("Average word lenth: %.1f",avg = (float) alphbt/space);
getch();
}
如何考虑单词之间的额外空格?在上面的示例中,这些将被忽略,这将给出平均字长3.0
,而不是错误计算的2.0
#include <stdio.h>
#include <conio.h>
int main()
{
char ch,temp;
float avg;
int space = 1,alphbt = 0,k = 0;
printf("Enter a sentence: ");
while((ch = getchar()) != '\n')
{
temp = ch;
if( ch != ' ')
{
alphbt++;
k++; // To ignore spaces before first word!!!
}
else if(ch == ' ' && k != 0)
space++;
}
if (temp == ' ') //To ignore spaces after last word!!!
printf("Average word lenth: %.1f",avg = (float) alphbt/(space-1));
else
printf("Average word lenth: %.1f",avg = (float) alphbt/space);
getch();
}
#包括
#包括
int main()
{
温度;
浮动平均值;
int空间=1,alphbt=0,k=0;
printf(“输入一个句子:”);
而((ch=getchar())!='\n')
{
温度=ch;
如果(ch!='')
{
alphbt++;
k++;//忽略第一个单词前的空格!!!
}
else如果(ch=''&&k!=0)
空间++;
}
if(temp='')//忽略最后一个单词后的空格!!!
printf(“平均字长:%.1f”,平均值=(浮点)alphbt/(空格-1));
其他的
printf(“平均字长:%.1f”,平均值=(浮点)alphbt/空格);
getch();
}
显然,计算非空格字符很容易,您的问题是计算单词。为什么像你这样把单词算作空格呢?或者更重要的是,一个词的定义是什么
在我看来,单词的定义是从空格字符到非空格字符的过渡。所以,如果你能检测到,你就可以知道你有多少单词,你的问题就解决了
我有一个实现,有很多可能的实现方法,我不认为你会有困难想出一个。我以后可能会以编辑的形式发布我的实现
*编辑:我的实现
#include <stdio.h>
int main()
{
char ch;
float avg;
int words = 0;
int letters = 0;
int in_word = 0;
printf("Enter a sentence: ");
while((ch = getchar()) != '\n')
{
if(ch != ' ') {
if (!in_word) {
words++;
in_word = 1;
}
letters++;
}
else {
in_word = 0;
}
}
printf("Average word lenth: %.1f",avg = (float) letters/words);
}
#包括
int main()
{
char ch;
浮动平均值;
int字=0;
整数字母=0;
int in_word=0;
printf(“输入一个句子:”);
而((ch=getchar())!='\n')
{
如果(ch!=''){
如果(!in_word){
words++;
in_word=1;
}
字母++;
}
否则{
in_word=0;
}
}
printf(“平均字长:%.1f”,平均=(浮点数)字母/单词);
}
考虑以下输入:(连字符表示空格)
当前忽略初始空间和结束空间,但计算每个中间空间,即使它们彼此相邻。只要对您的程序稍作修改,特别是对“k”的修改,我们就可以处理此案例
#include <stdio.h>
#include <conio.h>
#include <stdbool.h>
int main()
{
char ch;
float avg;
int numWords = 0;
int numLetters = 0;
bool prevWasASpace = true; //spaces at beginning are ignored
printf("Enter a sentence: ");
while((ch = getchar()) != '\n')
{
if( ch != ' ')
{
prevWasASpace = false;
numLetters++;
}
else if(ch == ' ' && !prevWasASpace)
{
numWords++;
prevWasASpace = true; //EDITED this line until after the if.
}
}
avg = numLetters / (float)(numWords);
printf("Average word lenth: %.1f",avg);
getch();
}
#包括
#包括
#包括
int main()
{
char ch;
浮动平均值;
int numWords=0;
int numleters=0;
bool prevWasASpace=true;//忽略开头的空格
printf(“输入一个句子:”);
而((ch=getchar())!='\n')
{
如果(ch!='')
{
prevWasASpace=false;
numLetters++;
}
else if(ch=''&&!prevWasASpace)
{
numWords++;
prevWasASpace=true;//编辑此行直到if之后。
}
}
平均值=numLetters/(浮动)(numWords);
printf(“平均字长:%.1f”,平均值);
getch();
}
您可能需要稍微修改前面的内容(尚未测试)
然而,仅仅根据单词之间的空格来计算句子中的单词,可能并不是你想要的一切。请考虑以下句子:
约翰说:“马上接电话!”
电视播音员刚刚提供了一个“买一送一”的优惠,同时说他们是全天候营业的
他们每月的花费不会超过100.99美元(3.25欧元)
我马上用他的电话打(555)555-5555
A(n)=A(n-1)+A(n-2)--换句话说,顺序是:0,1,1,2,3,5
你需要决定一个单词的构成,这不是一个容易的问题(顺便说一句,所有的例子都不包括所有的英语变体)。计算空格在英语中是一个很好的估计,但它不会让你一路走下去
请查看上的维基百科页面。这篇文章四次使用“非平凡”这个短语。计数逻辑是错误的。此代码似乎可以正确处理前导空格和尾随空格,以及单词之间的多个空格等。请注意
int ch的用法
以便代码可以准确地检查EOF(getchar()
返回一个int
)
在最后一个示例中,我输入了Control-D两次(第一次将“阿尔及尔的人类”刷新到程序中,第二次输入EOF),在最后一个示例中输入了一次。请注意,此代码将选项卡计算为“非空格”;您需要使用#包含和if(isspace(ch))
(或if(isblank(ch))
)来代替if(ch='')
,以便更好地处理选项卡
getchar()
返回一个int
我不明白您为什么使用int ch
和EOF
这个答案有几个部分
使用int ch
的第一个原因是getchar()
函数返回一个int
。它可以返回任何有效字符加上一个单独的EOF值;因此,它的返回值不能是任何类型的char
,因为它必须返回比char
更多的值。它实际上返回一个int
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
int ch;
int numWords = 0;
int numLetters = 0;
bool prevWasASpace = true; //spaces at beginning are ignored
printf("Enter a sentence: ");
while ((ch = getchar()) != EOF && ch != '\n')
{
if (ch == ' ')
prevWasASpace = true;
else
{
if (prevWasASpace)
numWords++;
prevWasASpace = false;
numLetters++;
}
}
if (numWords > 0)
{
double avg = numLetters / (float)(numWords);
printf("Average word length: %.1f (C = %d, N = %d)\n", avg, numLetters, numWords);
}
else
printf("You didn't enter any words\n");
return 0;
}
为什么这很重要?假设getchar()
中的值被分配给char ch
。现在,对于大多数角色,在大多数情况下,它工作正常。然而,有两件事会发生。如果plainchar
是有符号类型,则有效字符(通常为ÿ、y-umlaut、0xFF、正式的Unicode U+00FF、带分音符的拉丁文小写字母y)会被错误识别为EOF。或者,如果plainchar
是无符号类型,则永远不会检测到EOF
为什么检测EOF很重要?因为您的输入代码可以在您不期望的情况下获得EOF。如果你
int ch;
while ((ch = getchar()) != '\n')
...