2017-10-10 2 views
1

私は現在作業中のCプログラミング課題を持っています。収穫した作物のポンドを表す5つの数値を読み込んで棒グラフを表示するプログラムそれらの値を表す。これを行うには、for、while、do whileステートメントを使用する必要があります。文字列を解析するグラフィック、配列、コマンドを使用することはできません。ここで配列や文字列を使用しないCの水平バーグラフ

は私が複製する必要がある出力のサンプルです:

Pounds of Beans: 1 
    Pounds of Carrots: 2 
    Pounds of Lettuce: 3 
    Pounds of Peas: 4 
    Pounds of Squash: 5 

    Beans: B 
    Carrots: CC 
    Lettuce: LLL 
    Peas: PPPP 
    Squash: SSSSS 

私は成功した5つの入力を取ることができました。私の問題は、適切な構文とフロー構造から始まり、必要に応じて棒グラフを印刷できます。私は現在、最初の作物のために棒グラフを適切に印刷することができますが、各作物について再帰的に行う方法は非常に混乱します。このプログラムを正しく実行するために私が理解しなければならない流れ、私が作っている構文上の誤り、正確には "ガーディング"の意味について説明してください。ここで

は、私がこれまで持っているコードです:

#include <stdio.h> 

    // Initialize main 
    int main(void) 
    { 

    // Introduce variables 

    int count,  b, c, l, p, s; 

    // Prompts & obtaining of pounds of crops 
    printf("Pounds of Beans: "); 
    scanf("%d", &b); 
    printf("Pounds of Carrots: "); 
    scanf("%d", &c); 
    printf("Pounds of Lettuce: "); 
    scanf("%d", &l); 
    printf("Pounds of Peas: "); 
    scanf("%d", &p); 
    printf("Pounds of Squash: "); 

    // Print empty line between input and output 
    printf("\n"); 

    // Precursory print of "Beans:" 
    printf("Beans: "); 

    // Display pounds of beans using for statment 
    for (count=0; count<b; count++) 
      { 
        printf("%.*s", b, "B"); 
      } 
      printf("\n"); 

    printf("Carrots: "); 

    // Display lbs of carrots using for statment 
    for (count=0; count<c; count++); 
      { 
        printf("%.*s", c, "C"); 
      } 
      printf("\n"); 

    } 

私は現在、エラーを取得しています:あなたが提供することができます

graph.c:44:2: warning: this ‘for’ clause does not guard... [-Wmisleading-indentation] 
    for (count=0; count<c; count++); 
    ^~~ 
    graph.c:45:3: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the ‘for’ 
    { 
    ^

すべてのヘルプは大歓迎されます。

+5

その行の 'for'文の後ろにある漂白セミコロンを削除します。 –

+1

あなたは文字列を使用しています。 – EOF

+0

はい私は必要な行を表示するために文字列を使用していることを知っています。しかし、例えばstringlengthのような文字列を解析するコマンドは使用できません。 –

答えて

1

マイバージョンです。コメントが必要な場合は教えてください。

#include <stdio.h> 

void obtain_crop(char *str, int *num) { 
    printf("Pounds of %-10s", str); 
    scanf("%d", num); 
} 

void print_bar(char *name, char *ch, int num) { 
    printf("%-10s", name); 
    while(num > 0) { 
     printf("%s", ch); 
     num--; 
    } 
    puts(""); 
} 

int main() { 
    int b, c, l, p, s; 

    obtain_crop("Beans:", &b); 
    obtain_crop("Carrots:", &c); 
    obtain_crop("Lettuce:", &l); 
    obtain_crop("Peas:", &p); 
    obtain_crop("Squash:", &s); 

    puts(""); 

    print_bar("Beans:", "B", b); 
    print_bar("Carrots:", "C", c); 
    print_bar("Lettuce:", "L", l); 
    print_bar("Peas:", "P", p); 
    print_bar("Squash:", "S", s); 
    return 0; 
} 

テスト

Pounds of Beans: 8 
Pounds of Carrots: 6 
Pounds of Lettuce: 1 
Pounds of Peas:  4 
Pounds of Squash: 9 

Beans: BBBBBBBB 
Carrots: CCCCCC 
Lettuce: L 
Peas:  PPPP 
Squash: SSSSSSSSS 
1

まず、私はあなたにいくつかのアドバイスを与えてみましょう:あなたが任意の定期的な操作があるとき、それはおそらく今の方法

する必要がありますが、ここにあります作業バージョン:

#include <stdio.h> 

int read_value(const char * name, int * value) { 
    printf("Pounds of %s: ", name); 
    scanf("%d", value); 
} 

int draw_value(const char * name, char c, int value) { 
    printf("%-*s", 10, name); 
    for (int i = 0; i < value; i++) 
    printf("%c", c); 
    printf("\n"); 
} 

// Initialize main 
int main(void) 
{ 
    // Introduce variables 

    int b, c, l, p, s; 

    // Prompts & obtaining of pounds of crops 
    read_value("Beans", &b); 
    read_value("Carrots", &c); 
    read_value("Lettuce", &l); 
    read_value("Peas", &p); 
    read_value("Squash", &s); 

    // Print empty line between input and output 
    printf("\n"); 

    draw_value("Beans:", 'B', b); 
    draw_value("Carrots:", 'C', c); 
    draw_value("Lettuce:", 'L', l); 
    draw_value("Peas:", 'P', p); 
    draw_value("Squash:", 'S', s); 
} 
+0

私が例を挙げてくれてありがとうございます。 –

関連する問題