CS50可读性2022

n3ipq98p  于 2023-08-03  发布在  其他
关注(0)|答案(3)|浏览(162)

我正在服用CS50 x,在PSet 2中:可读性在编译时引发以下错误:
Image of the Error message below, easier to read
可读性/ $使可读性。c:52:24:错误:如果((txt[i] >= '97' && txt[i] <= '122'),则返回多字符字符常量[-Werror,-Wmultichar]||(txt[i] >= '65' && txt[i] <= '90'))^致命错误:发出的错误太多,现在停止[-ferror-limit=]产生2个错误。制造商:* [:可读性]错误1
我认为这一定是一个问题,97和我使用的所有其他ASCII代码都不被识别为整数,我需要特别声明它们吗?如果是,如何做到的?
下面是我的代码:

#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <math.h>

int count_letters(string);
int count_words(string);
int count_sentences(string);

float l;
float w;
float s;

int main(void)
{
    // Ask user for string, store in txt.
    string txt = get_string("Enter your text: ");

    int i = strlen(txt);

    // Convert letters and sentences to avg / 100 w.
    float L = 100 * (l / w);
    float S = 100 * (s / w);

    // Calc coleman-liau index
    int clindex = round(0.0588 * L - 0.296 * S -15.8);

    // Printf "Grade X" if X > 16, printf "Grade 16+".
    if (clindex < 1)
    {
        printf("Grade < 1\n");
    }
    else if (clindex > 16)
    {
        printf("Grade 16+\n");
    }
    else
    {
        printf("Grade %i\n", clindex);
    }

}

int count_letters(string txt)
{
    // Count letters
    l = 0;
    for (int i = 0, n = strlen(txt); i < n; i++)
    {
        // If the txt is between a-z (97 - 122) or A-Z (65 - 90), increase letter count.
        if ((txt[i] >= '97' && txt[i] <= '122') || (txt[i] >= '65' && txt[i] <= '90'))
        {
            l++;
        }
    }
    return l;
}

int count_words(string txt)
{
    // Count words
    w = 1;
    for (int i = 0, n = strlen(txt); i < n; i++)
    {
        // If there is a space (ascii 32), then increase word count.
        if (txt[i] == 32)
        {
            w++;
        }
    }
    return w;
}

int count_sentences(string txt)
{
    // Count sentences
    s = 0;
    for (int i = 0, n strlen(txt); i < n; i++)
    {
        // If txt is . (period 46), ! (exclamation 33), or ? (question 63), inscrease sentence count.
        if (txt[i] == 46 || txt[i] == 33 || txt[i] == 63)
        {
            s++;
        }
    }
    return s;
}

字符串
谢谢大家的帮助。

x6h2sr28

x6h2sr281#

我修复并简化了你的代码,这是我得到的。看起来工作正常。

#include <cs50.h>
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <string.h>

int count_letters(string);
int count_words(string);
int count_sentences(string);

float l;
float w;
float s;

int main(void)
{
// Ask user for string, store in txt.
string text = get_string("Text: ");

int i = strlen(text);

// Convert letters and sentences to avg / 100 w.
float L = 100 * (l / w);
float S = 100 * (s / w);

// Calc coleman-liau index
int clindex = round(0.0588 * L - 0.296 * S - 15.8);

// Printf "Grade X" if X > 16, printf "Grade 16+".
if (clindex < 1)
{
    printf("Grade < 1\n");
}
else if (clindex > 16)
{
    printf("Grade 16+\n");
}
else
{
    printf("Grade %i\n", clindex);
}
}

int count_letters(string text)
{
// Count letters
l = 0;
for (int i = 0, n = strlen(text); i < n; i++)
{
    // If the txt is between a-z (97 - 122) or A-Z (65 - 90), increase 
letter count.
    if (isalpha(text[i]))
    {
        l++;
    }
}
return l;
}

int count_words(string text)
{
// Count words
w = 1;
for (int i = 0, n = strlen(text); i < n; i++)
{
    // If there is a space (ascii 32), then increase word count.
    if (isblank(text[i]))
    {
        w++;
    }
}
return w++;
}

int count_sentences(string text)
{
// Count sentences
s = 0;
for (int i = 0, n = strlen(text); i < n; i++)
{
    // If txt is . (period 46), ! (exclamation 33), or ? (question 63), 
inscrease sentence count.
    if (ispunct(text[i]))
    {
        s++;
    }
}
return s;
}

字符串

ilmyapht

ilmyapht2#

这段代码就是问题所在

if ((txt[i] >= '97' && txt[i] <= '122') || (txt[i] >= '65' && txt[i] <= '90'))

字符串
试试这个

if (isalpha(txt[i]))

ql3eal8s

ql3eal8s3#

''引号用于单字符文字。
'a''d'
'123'是多个字符文字。
您可能希望使用普通整数:

(txt[i] >= 97 && txt[i] <= 122)

字符串

相关问题