0

私は、ファイルから数学クイズのスコアを読み取り、スコアを星(棒グラフのように)として出力する基本的なc構造とループを使用してプログラムを作成しようとしています。プログラムはファイルを読み取ろうとし、数学のさまざまな領域(加算、減算、乗算、除算)で生徒がどのように実行しているかを視覚的に表現します。

入力ファイルは次のようになります。

2 
Bobby 
6 10 
70 80
50 60 
4 5 
Joel
7 12
20 25
4 5
3 10

最初の行は、ファイル内の学生の総数を表しています。この後、各生徒は5行の個別データを取得します。これらの行の最初は生徒の名前で、次の4つは数学の個々の領域のスコアです(6つのうち5つ、80つのうち70つなど)。

そして、この例のような出力を受信しようとしています:

Bobby
+: ******** 
-: ****** 
*: ***** 
/: **** 
Joel
+: **** 
-: ******** 
*: *** 
/: ******* 

これを実現するにはループとifp(内部ファイルポインタ)を使用する必要があることはわかっていますが、Cで入力ファイルを使用するのは初めてなので、プログラムの個々の行を読み取るためにそれらを実装する方法がよくわかりません。

** 4番目の編集-目的が完了しました!

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

//int main
int main() {

    FILE * ifp;
    ifp = fopen("input.txt", "r");

    FILE * ofp;
    ofp = fopen("output.txt", "w");

    int students = 0, i, j;
    int sum = 0;
    int perc;
    int score1,score2;
    char name [10];

    //read the first line for # of students
    fscanf(ifp, "%d", &students);

    //Loop for both students
    for (i=0; i<students; i++) {

                fscanf(ifp, "%s", &name);
                fprintf(ofp, "%s:", name);

                fscanf(ifp, "%d %d", &score1, &score2);
                perc = (10 * score1/score2);
                fprintf(ofp, "\n +:");
                for(j=0; j<perc; j++){
                    fprintf(ofp, "*");
                }
                fscanf(ifp, "%d %d", &score1, &score2);
                perc = (10 * score1/score2);
                fprintf(ofp, "\n -:");
                for(j=0; j<perc; j++){
                    fprintf(ofp, "*");
                }

                fscanf(ifp, "%d %d", &score1, &score2);
                perc = (10 * score1/score2);
                fprintf(ofp, "\n *:");
                for(j=0; j<perc; j++){
                    fprintf(ofp, "*");
                }
                fscanf(ifp, "%d %d", &score1, &score2);
                perc = (10 * score1/score2);
                fprintf(ofp, "\n /:");
                for(j=0; j<perc; j++){
                    fprintf(ofp, "*");
                }
                    fprintf(ofp, "\n");
    }
    fclose(ifp);
    fclose(ofp);

return 0;
}

以前のグラフエラーは、私の側の単純な演算子の優先順位エラーだったようです。助けてくれてありがとう!

4

2 に答える 2

0

入力ファイルが常に説明どおりにフォーマットされていることを期待できる場合は、フォーマットチェックについてあまり心配する必要はありません(ただし、常にそれは良い考えです)。必要に応じてエラー(ファイル操作、fgetsなど)をチェックします。switchcaseステートメントを使用する必要はないと思います。1つの学生レコードを処理するために必要な手順を実行してから、残りの学生に対してforループでそれらの手順を繰り返すことができるため、forループで十分です。私は次のことをします:

/* declare variables */
int num_students    // the number of students on the first line
char name[50], line[100]    // student name, a line from the input file
int addition_score, addition_max    // student's addition score, max addition score possible
int subtraction_score, subtraction max
/* ints for multiply and divide scores as well */
float percent   // used for _score / _max (re-use for + - * and /)
FILE input_file, output_file

// open input file for reading
// open output file for writing
// fgets line from input file
// sscanf to extract num_students
// for(c=0; c<num_students; ++c) {

    //fgets name

    //fgets the line containing the addition scores
    //sscanf line for addition_score and addition_max
    //same two commands for -, *, and /

    //percent = (float)addition_score / addition_max 
    //output the student's name to output file
    //figure out how to represent the float in stars (I'll leave this up to you)
    //output a line containing "+:" and your stars to the output file

    //repeat that procedure for -, *, and /
   } // end for loop
// close input file
// close output file
return 0;

お役に立てば幸いです。

于 2013-03-22T01:20:29.713 に答える
0

さて、数学でスコアの平均を見つけた後、forループを作成し、ループ変数(iまたはjなど)を平均に変更し、forループに星を出力します。

//Here is an example of a for loop that loops 20 times
    for ( n=0; n < 20; n++ )
{
   // whatever here...
}

それは理にかなっていますか?

于 2013-03-21T20:37:30.453 に答える