【发布时间】:2020-10-25 14:08:50
【问题描述】:
我对 C 语言还是很陌生,并且在如何缩进代码方面确实存在问题,所以我提前为此道歉。我试图让程序输入字符串并输出可读性分数。我已经把代码全部写出来了,但是,每当输入一个阶段并按下回车键时,程序不会执行其余的代码,而是我必须在继续之前输入其他内容。我不知道为什么!请问有人可以帮忙吗?
#include <string.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
char str[500]; // think of this like an array
int alphabet =0 , i =0, word=0, special_characters =0, vowels = 0;
printf("Input the string:\n");
fgets(str,500,stdin); // <== issue here where I have to input string twice for while loop to occur!
/* Checks each character of string*/
while(str[i] !='\0')// while there is a character and not empty space
{
if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' ||
str[i] == 'o' || str[i] == 'u' || str[i] == 'A' ||
str[i] == 'E' || str[i] == 'I' || str[i] == 'O' ||
str[i] == 'U')
{
++vowels;
}
else if((str[i] ==' ' || str[i]=='\n' || str[i]=='\t'))
{ // if there is not space, tab or enter, then logically there is a word
word++; // after not finding a space it is told to look for next one
}
else if((str[i] =='?' || str[i]=='!' || str[i]=='.')) // looks for sentance enders
{
special_characters++; // keeps going until text ends
}
else if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
{
// only i is just a counter of each letter in
// this case. alphabet is told to increase
alphabet++; // made this a loop that goes onto next letter
}
i++; // tells i to scan over ALL if loops by being INSDIE the while loop but not
// part of the IF statements!
}
//L = Letters ÷ Words × 100 = 639 ÷ 119 × 100 ? 537
//S = Sentences ÷ Words × 100 = 5 ÷ 119 × 100 ? 4.20
double letters = vowels + alphabet;
double w = word;
double d = 100.0;
double L = (letters/w)*d;
double e = special_characters; // sentances
double S= (e/w)*100;
scanf("%lf",&S);
double index = (0.0588 * L) -(0.296 * S)- 15.8;
scanf("%lf",&index);
double round(double index);
if(index <=1)
{
printf("Below Grade 1\n");
}
else if(index >=3 && index <4)
{
printf(" Grade 3\n");
}
else if(index >=5 && index <6)
{
printf(" Grade 5\n");
}
else if(index >=7 && index <8)
{
printf(" Grade 7\n");
}
else if(index >=8 && index <9)
{
printf(" Grade 8\n");
}
else if(index >=9 && index <10)
{
printf(" Grade 9\n");
}
else if(index >=10 && index <11)
{
printf(" Grade 9\n");
}
else if(index >=16)
{
printf(" Grade 16+\n");
}
}
【问题讨论】: