我試圖創建一個程序使用基本的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;
}
看來我以前的圖形錯誤是我操作錯誤的簡單順序。感謝您的幫助!
感謝您一步一步的指導和建議。他們真的幫我把頭繞在目標身上! – 2013-03-22 01:50:27