尝试使用文件中的信息在C中创建条形图

时间:2013-03-21 20:17:49

标签: c graph codeblocks

我正在尝试使用基本的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(内部文件指针)来实现这一点,但我不太清楚如何实现它们来读取程序的各个行,因为这是我第一次使用输入文件下进行。

**第四编辑 - 目标完成!

#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;
}

我的上一个图形错误似乎是我操作错误的简单顺序。谢谢你们的帮助!

2 个答案:

答案 0 :(得分: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...
}

这有意义吗?

答案 1 :(得分:0)

如果您可以指望您的输入文件始终按照您描述的方式进行格式化,那么您不必担心格式检查(尽管这总是一个好主意)。仍然在适当的地方检查错误(文件操作,fgets等)我认为你不需要使用switch case语句。 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;

希望有所帮助。