c - 有一个测试卡在无限循环中,你能找到它吗?

标签 c testing infinite-loop

int main() {
    int s, b;
    int hist[26] = { 0 };
    int hist2[26] = { 0 };
    char char1, char2;
    printf("Hello Netta, enter the letters you would like us to repeat ending with $:\n");
    scanf("%c", &char2);
    while (char2 != '$') {
        if (char2 >= 'A' && char2 <= 'Z')
            char2 = char2 + 32;
        int char3 = char2 - 'a';
        hist2[char3]++;
        scanf("%c", &char2);
        if (char2 < 0)
            break;
    }
    printf("How many times would you like to loop?\n");
    if (!scanf("%d", &s))
        return 0;

    printf("Enter the string you would like to be checked ending with $:\n");
    scanf("%c", &char1);
    if (char1 >= 'A' && char1 <= 'Z')
        char1 = char1 + 32;
     while (char1 != '$' && char1 > 0) {
         int char3 = char1 - 'a';
         hist[char3]++;
         scanf("%c", &char1);
     }
     for (int i = 0; i < 26; i++) {
        if (hist[i] > s * hist2[i]) {
            printf("Not enough letters\n");
            b = 0;
            break;
        } else {
            b = 1;
        }
    }
    if (b)
         printf("Congratulations! you have enough letters to create your song and win the Eurovision!\n");

    return 0;
}

//所以基本上这是我大学的一项作业,他们要求我们做程序输入是字符和一个循环,并将其与另一个输入进行比较,每个字母可以循环多少次(无需检查输入是否正确)为 true,但循环编号为 int

最佳答案

你的程序有很多问题:

  • scanf()返回成功转换的次数。将返回值与 1 进行比较在您的程序中而不是测试 0 ,这对于 "%c" 来说永远不会发生反正。此外,char2如果流位于文件末尾,则不会被修改。

  • 您必须检查 char2是索引到数组之前的一个字母,否则您可能会访问超出数组边界并出现未定义的行为。

  • 演示很重要:使用适当的缩进和间距以使程序易于阅读。

  • 包含必要的 header ,例如 <stdio.h>

这是一个改进的版本:

#include <stdio.h>

int main() {
    int hist[26] = { 0 };
    int hist2[26] = { 0 };
    int s;
    char c;

    printf("Hello Netta, enter the letters you would like us to repeat ending with $:\n");
    while ((scanf("%c", &c) == 1 && c != '$') {
        if (c >= 'A' && c <= 'Z')
            hist2[c - 'A']++;
        else if (c >= 'a' && c <= 'z')
            hist2[c - 'a']++;
    }

    printf("How many times would you like to loop?\n");
    if (scanf("%d", &s) != 1)
        return 1;

    printf("Enter the string you would like to be checked ending with $:\n");
    while (scanf("%c", &c) == 1 && c != '$') {
        if (c >= 'A' && c <= 'Z')
            hist[c - 'A']++;
        else if (c >= 'a' && c <= 'z')
            hist[c - 'a']++;
     }
     for (int i = 0; i < 26; i++) {
        if (hist[i] > s * hist2[i]) {
            printf("Not enough letters\n");
            break;
        }
    }
    if (i == 26)
         printf("Congratulations! you have enough letters to create your song and win the Eurovision!\n");

    return 0;
}

关于c - 有一个测试卡在无限循环中,你能找到它吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50268778/

相关文章:

javascript - 测试 JavaScript 僵尸事件处理程序或 DOM 元素

ios - iTunes Connect 内部测试页面损坏

django - 在同一个 Django unittest 测试用例中测试多个 IntegrityErrors

java - 使用 sc.hasNextLine() 进行无限循环

node.js - 有没有任何编程方法可以打破 NodeJS 中的无限循环?

python - 我用Python编写了一段代码,即使我使用break语句并继续,它仍然会进入Python :的无限循环

c - C 中的 strcpy() 函数遇到问题

python - 将基于 pygtk 的 C API 移植到 pygobject

c - 整数错误C

c - 如何在 'while' 循环中重新输入整数值?