Coleman-Liau 索引程序的未知输出
Unknown output for Coleman-Liau index program
我这周一直在做CS50课程,我被这个简单的问题卡住了,因为我不知道如何找到我的错误,每次我输入测试文本如:
哈利波特在很多方面都是一个非常不寻常的男孩。一方面,他比一年中的任何其他时间都更讨厌暑假。再者,他很想做作业,却被逼得偷偷摸摸,在夜深人静的时候做。而且他恰好也是个巫师
应该输出 Grade 5 而不是 Grade 2。
这是演练的 link:
https://cs50.harvard.edu/x/2020/psets/2/readability/
我为此编写了这段代码,我也想知道是否有解决 SIZE 的方法,因为文本越多 space 我们需要的越多,因此我怀疑 1024 是不够的。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SIZE 1024
char text[SIZE];
int main(void){
gets(text);
int length,letters=0, sentences=1, words=1; //word and sentence start from 1 as it's the minimum value
length = strlen(text);
for (int i = 0; i < length; ++i) {
if(isalpha(text[i])){
letters++;
}
if(text[i]==('.'|'!'|','|';')) {
sentences++;
}
if(isspace(text[i])) {
words++;
}
}
double index= ((5.88*(letters/words))-(0.296*sentences/words)-(15.8));
if(index<0){
printf("Before grade 1\n");
} else if (index>16){
printf("Grade 16+\n");
}else{
printf("Grade: %.f",index);
}
// printf("l: %d\ns: %d\nw: %d\n",letter,sentence,word); this was used to check if counters were correct
}
谢谢你们,周末过后我有时间检查你们的答案。
最终代码是:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#define SIZE 1024
char text[SIZE];
int main(void){
gets(text);
int length,letters=0, sentences=0, words=1; //word and sentence start from 1 as it's the minimum value
length = strlen(text);
for (int i = 0; i < length; ++i) {
if(isalpha(text[i])){
letters++;
}
if(text[i]=='.'|| text[i]=='!'||text[i]=='?') {
sentences++;
}
if(isspace(text[i])) {
words++;
}
}
float L=(letters*100.0f/words);
float S=(sentences*100.0f/words);
double index= (0.0588*L-0.296*S-(15.8));
index=round(index);
if(index<0){
printf("Before grade 1\n");
} else if (index>16){
printf("Grade 16+\n");
}else{
printf("Grade: %.f\n",index);
}
// printf("l: %d\ns: %d\nw: %d\n",letter,sentence,word); this was used to check if counters were correct
}
我这周一直在做CS50课程,我被这个简单的问题卡住了,因为我不知道如何找到我的错误,每次我输入测试文本如:
哈利波特在很多方面都是一个非常不寻常的男孩。一方面,他比一年中的任何其他时间都更讨厌暑假。再者,他很想做作业,却被逼得偷偷摸摸,在夜深人静的时候做。而且他恰好也是个巫师
应该输出 Grade 5 而不是 Grade 2。 这是演练的 link: https://cs50.harvard.edu/x/2020/psets/2/readability/
我为此编写了这段代码,我也想知道是否有解决 SIZE 的方法,因为文本越多 space 我们需要的越多,因此我怀疑 1024 是不够的。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SIZE 1024
char text[SIZE];
int main(void){
gets(text);
int length,letters=0, sentences=1, words=1; //word and sentence start from 1 as it's the minimum value
length = strlen(text);
for (int i = 0; i < length; ++i) {
if(isalpha(text[i])){
letters++;
}
if(text[i]==('.'|'!'|','|';')) {
sentences++;
}
if(isspace(text[i])) {
words++;
}
}
double index= ((5.88*(letters/words))-(0.296*sentences/words)-(15.8));
if(index<0){
printf("Before grade 1\n");
} else if (index>16){
printf("Grade 16+\n");
}else{
printf("Grade: %.f",index);
}
// printf("l: %d\ns: %d\nw: %d\n",letter,sentence,word); this was used to check if counters were correct
}
谢谢你们,周末过后我有时间检查你们的答案。 最终代码是:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#define SIZE 1024
char text[SIZE];
int main(void){
gets(text);
int length,letters=0, sentences=0, words=1; //word and sentence start from 1 as it's the minimum value
length = strlen(text);
for (int i = 0; i < length; ++i) {
if(isalpha(text[i])){
letters++;
}
if(text[i]=='.'|| text[i]=='!'||text[i]=='?') {
sentences++;
}
if(isspace(text[i])) {
words++;
}
}
float L=(letters*100.0f/words);
float S=(sentences*100.0f/words);
double index= (0.0588*L-0.296*S-(15.8));
index=round(index);
if(index<0){
printf("Before grade 1\n");
} else if (index>16){
printf("Grade 16+\n");
}else{
printf("Grade: %.f\n",index);
}
// printf("l: %d\ns: %d\nw: %d\n",letter,sentence,word); this was used to check if counters were correct
}