2013-03-21 19 views
0

我試圖創建一個程序使用基本的c結構和循環,從文件中讀取數學測驗分數並打印星星(如條形圖)的分數。該程序將嘗試讀取該文件並直觀地描繪學生在不同的數學領域(加法,減法,乘法和 分區)中的表現。試圖創建使用文件中的信息在C中的條形圖

輸入文件看起來是這樣的:

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

第一行代表佔學生總數的文件的數量。之後,每個學生將有5行個人數據。這些行中的第一行是學生姓名,接下來的4分是數學的各個領域的分數 (5中的6位,80中的70位等)

而im試圖接收類似於下面這個例子:

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

我知道我需要使用循環和IFP(內部文件指針)來實現這一點,但即時通訊不太清楚如何實現它們讀入程序的各條線,因爲這是我的第一次使用C中的輸入文件。

**第四個Edit- Objective已完成!

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

看來我以前的圖形錯誤是我操作錯誤的簡單順序。感謝您的幫助!

回答

0

那麼,我會做什麼,在找到與數學的平均分數後,我會做一個for循環,並將循環變量(我或j或其他)改爲平均值,然後在for循環。

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

這是否有意義?

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; 

希望有所幫助。

+0

感謝您一步一步的指導和建議。他們真的幫我把頭繞在目標身上! – 2013-03-22 01:50:27