C编程练习?

标签 c

我正在阅读一本关于 C 编程的书并尝试做这些练习:

编写一个程序,打印由星号 (*) 组成的水平直方图。 0 到 70 之间的每个数字都可以有一颗星。

#include <stdio.h>

void draw_stars(int number_of_stars) {
    int counter = 0;

    for(counter = 0; counter < number_of_stars; counter++) {
        printf("*");
    }

    printf("\n");
}

int main(void) {

    int counter1, counter2, ones, tens, zero, one, two, three, four, five, six, seven, eight, nine = 0;

    for(tens = 0; tens < 7; tens++) {
         if(tens == 0)
             zero = zero + 1;
         if(tens == 1)
             one = one + 10;
         if(tens == 2)
             two = two + 10;
         if(tens == 3)
             three = three + 10;
         if(tens == 4)
             four = four + 10;
         if(tens == 5)
             five = five + 10;
         if(tens == 6)
             six = six + 10;
         if(tens == 7)
             seven = seven + 10;

             for(ones = 0; ones < 9; ones++) {
                 if(ones == 0)
                     zero++;
                 if(ones == 1)
                     one++;
                 if(ones == 2)
                     two++;
                 if(ones == 3)
                     three++;
                 if(ones == 4)
                     four++;
                 if(ones == 5)
                     five++;
                 if(ones == 6)
                     six++;
                 if(ones == 7)
                     seven++;
                 if(ones == 8)
                     eight++;
                 if(ones == 9)
                     nine++;
        }
    }

draw_stars(zero);
draw_stars(one);
draw_stars(two);
draw_stars(three);
draw_stars(four);
draw_stars(five);
draw_stars(six);
draw_stars(seven);
draw_stars(eight);
draw_stars(nine);

}

由于某种原因,我的程序进入了打印星星的无限循环。但我找不到原因?

我还没有想出任何其他解决方案,但我仍然认为它既丑陋又臃肿。真正的 C 程序员会如何解决这个问题?

编辑:

在阅读和理解书中关于数组的章节后,我能够编写一个更干净的程序版本。我将它张贴在这里,因为它可能会帮助其他初学者了解数组的使用。编写输出相同的程序,但使用语言的不同功能是一种很好的学习体验。

#include <stdio.h>

#define TENS 7

void draw_stars(int stars) {
    int star_counter = 0;

    for (star_counter = 0; star_counter < stars; star_counter++)
        printf("%c", '*');
}

int main(void) {

    int number_array[10];
    int tens_counter, ones_counter;

    for (ones_counter = 0; ones_counter < 10; ones_counter++)
        number_array[ones_counter] = 0;

    for (tens_counter = 0; tens_counter < TENS; tens_counter++) {
        if (tens_counter != 0)
            number_array[tens_counter] += 10;
        else
            number_array[tens_counter] += 1;

        for (ones_counter = 0; ones_counter < 10; ones_counter++)
            number_array[ones_counter]++;
    }

    for (ones_counter = 0; ones_counter < 10; ones_counter++) {
            draw_stars(number_array[ones_counter]);
            printf("\n");
    }
}

最佳答案

初始化所有变量:

int counter1=0, counter2=0, ones=0, tens=0, zero=0, one=0..

顺便说一句,为了获得更好的性能, 将 if 替换为 else if 除了 block 中的第一个。当您已经知道只有一个条件为真时,为什么要检查所有 if 条件?

仅供引用,当 Else if 中的条件为真时,将跳过所有其他 if

关于C编程练习?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22422128/

相关文章:

arrays - 如何检查数组中的所有值是否相同?

为 .txt 文件中的每个条目创建一个新节点

c - 为什么结构的大小应反射(reflect)其对齐方式?

c - 在位移和或运算之后检索原始位

c - 如何编写返回指针数组的回调函数?

c - 将汇编代码逆向工程为 C 代码

c - 如何处理错误的输入 C?

c++ - Windows中的互斥问题

c - 参数未传递给 c 内核中的函数

c - 在 C 中的 main() 函数之前打印 "Hello world"