好的!我真的是編程新手,所以我真的不知道發生了什么......我的程式編譯得很好,但它只說'1年級之前',我用debug50看到float L和float S不是做他們應該做的數學,我不知道出了什么問題這是我的代碼
`
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
int count_sentences(string);
int count_words(string);
int count_letters(string);
int letters;
int sentences;
int words;
int main(void)
{
// prompt user for text
string text = get_string("Text: ");
// calcutate the reading level
float L = 100 * (float) letters / words;
float S = 100 * (float) sentences / words;
int index = round(0.0588 * L - 0.296 * S - 15.8);
//output the result
if (index < 1)
{
printf("Before Grade 1\n");
}
else if (index > 16)
{
printf ("Grade 16 \n");
}
else
{
printf("Grade %i\n", index);
}
}
//string is an array of characters, just go through each letter and check if it is a char or not, and add to the counter
int letters = 0;
int count_letters(string text)
{
for (int i = 0; i < strlen(text); i )
{
if (isalpha(text[i]) != 0)
{
letters ;
}
}
return letters;
}
//calculation for words is made by counting the number of spaces 1
int words = 1;
int count_words(string text)
{
for (int i = 1; i < strlen(text); i )
{
if (isspace (text[i]) != 0)
{
words ;
}
}
return words;
}
// sentences end with ./!/?, so just count them
int sentences = 0;
int count_sentences(string text)
{
for (int i = 0; i < strlen(text); i )
{
if (text[i] == '.' || text[i] == '!' || text[i] == '?')
{
sentences ;
}
}
return sentences;
}
`謝謝!
uj5u.com熱心網友回復:
實際上,我不久前回答了類似型別的編碼問題。基本上,缺少的是字母、單詞和句子計數的推導,因為在您當前的程式中,即使定義了計數函式,您也不會呼叫它們。在 C 中僅僅定義函式是不夠的。在某個地方,它們需要被呼叫。在查看您的代碼時,最有可能的位置是在您的代碼提示并接收到來自用戶的文本字串之后。
// prompt user for text
string text = get_string("Text: ");
letters = count_letters(text); /* The functions need to be called to derive letters, words, and sentences */
words = count_words(text);
sentences = count_sentences(text);
可能還有其他需要測驗和改進的部分,但您可能希望從它開始。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/522232.html
標籤:Ccs50可读性
