【发布时间】:2020-08-06 09:31:28
【问题描述】:
1- 我不知道我的代码有什么问题,这是 ## help ## readability CS50 pset2。 2-问题似乎出在浮点等级(字符串等级)功能上。 3- 我是初学者,这是我第二次用 C 编写代码,所以如果我遗漏了一些重要的东西,请告诉我,或者如果您有任何提示可以使我的代码看起来更好,谢谢!
//Libraries
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
//Functions
int count_letters(string letter);
int count_words(string word);
int count_sentences(string sentence);
float grade(string grade);
int main(void)
{
string par = get_string("text: ");
}
int count_letters(string letter)
{
int letters = 0;
for (int i = 0, n = strlen(letter); i < n; i++)
{
if (isalpha(letter[i]))
letters++;
}
return letters;
}
int count_words(string word)
{
int words = 1;
int spaces = 0;
int nword;
nword = strlen(word);
for (int i = 0; i < nword; i++)
{
if (isspace(word[i]))
spaces++;
words = spaces + 1;
}
return words;
}
int count_sentences(string sentence)
{
int sentences = 0;
int nsentence;
nsentence = strlen(sentence);
for (int i = 0; i < nsentence; i++)
{
if (sentence[i] == '.' || sentence[i] == '!' || sentence[i] == '?')
sentences++;
}
return sentences;
}
float grade(string grade)
{
//here comes my problem. variables like letters, words, and sentences are "undeclared identifier"
float x = 0.0588 * (100 * letters / words) - 0.296 * (100 * sentences / words) - 15.8;
if (x < 16 && x >= 0)
{
printf("Grade %i\n", (int) round(x));
}
else if (x >= 16)
{
printf("Grade 16+\n");
}
else
{
printf("Before Grade 1");
}
}
【问题讨论】:
-
CS50 是一门 C 课程。 C 和 C++ 是非常不同的语言。如果您不询问 C++,请不要标记 C++。
-
你需要在你最喜欢的书中阅读关于变量作用域的内容。
-
谁应该调用所有这些函数?在
main中,你不要打电话给他们中的任何一个。
标签: c logic cs50 readability