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

试图使用文件中的信息在C中创建条形图,c,graph,codeblocks,C,Graph,Codeblocks,我试图创建一个使用基本c结构和循环的程序,从文件中读取数学测验分数,并将分数打印为星号(如条形图)。该程序将尝试读取文件并直观地描述学生在不同领域的表现 数学领域(加法、减法、乘法和 分部) 输入文件如下所示: 2 Bobby 6 10 70 80 50 60 4 5 Joel 7 12 20 25 4 5 3 10 第一行表示文件中的学生总数。在此之后,每个学生将有5行个人数据。第一行是学生的名字,接下来的四行是各个数学领域的分数 (六分之五、八十分之七十等) 我正在尝试接收与此示

我试图创建一个使用基本c结构和循环的程序,从文件中读取数学测验分数,并将分数打印为星号(如条形图)。该程序将尝试读取文件并直观地描述学生在不同领域的表现 数学领域(加法、减法、乘法和 分部)

输入文件如下所示:

2 
Bobby 
6 10 
70 80
50 60 
4 5 
Joel
7 12
20 25
4 5
3 10
第一行表示文件中的学生总数。在此之后,每个学生将有5行个人数据。第一行是学生的名字,接下来的四行是各个数学领域的分数 (六分之五、八十分之七十等)

我正在尝试接收与此示例类似的输出:

Bobby
+: ******** 
-: ****** 
*: ***** 
/: **** 
Joel
+: **** 
-: ******** 
*: *** 
/: ******* 
我知道我需要使用循环和ifp(内部文件指针)来实现这一点,但我不太确定如何实现它们来读取程序的各个行,因为这是我第一次使用C语言中的输入文件

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

#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;
}
#包括
#包括
#包括
//int main
int main(){
文件*ifp;
ifp=fopen(“input.txt”,“r”);
文件*ofp;
ofp=fopen(“output.txt”,“w”);
int学生=0,i,j;
整数和=0;
int perc;
智力得分1分,得分2分;
字符名[10];
//为#个学生读第一行
fscanf(ifp、%d、&学生);
//为两个学生循环

for(i=0;i好吧,我会做的是,在用数学找到分数的平均值后,我会做一个for循环,将循环变量(i或j或其他)更改为平均值,并在for循环中打印星号

//Here is an example of a for loop that loops 20 times
    for ( n=0; n < 20; n++ )
{
   // whatever here...
}
//下面是循环20次的for循环示例
对于(n=0;n<20;n++)
{
//不管这里发生了什么。。。
}

这有意义吗?

如果您可以指望输入文件总是按照您描述的方式格式化,那么您就不必担心太多的格式检查问题(尽管这总是一个好主意)。仍然可以在适当的地方检查错误(文件操作、FGET等)我认为您不需要使用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;
/*声明变量*/
int num_students//第一行的学生人数
char name[50],line[100]//学生姓名,输入文件中的一行
int addition\u分数,addition\u max//学生的加法分数,可能的最大加法分数
整数减法分数,减法最大值
/*整数表示乘和除分数*/
浮动百分比//用于_分数/_最大值(重复用于+-*和/)
文件输入文件,输出文件
//打开输入文件进行读取
//打开输出文件进行写入
//输入文件中的fgets行
//sscanf提取num_学生

//感谢你一步一步的指导和建议。它们真的帮助我把头脑集中在目标上!