1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| #include <stdio.h> #include <string.h>
#define N 10
typedef struct { long id; char name[20]; float objective; float subjective; float sum; char result[10]; } STU;
void read(STU st[], int n); void write(STU st[], int n); void output(STU st[], int n); int process(STU st[], int n, STU st_pass[]);
int main() { STU stu[N], stu_pass[N]; int cnt; double pass_rate;
printf("Add %d students from file...\n", N); read(stu, N);
printf("\nFiguring...\n"); cnt = process(stu, N, stu_pass);
printf("\nStudents passed the exam:\n"); output(stu, N); write(stu, N);
pass_rate = 1.0 * cnt / N; printf("\nPass Rank: %.2f%%\n", pass_rate * 100);
return 0; }
void output(STU st[], int n) { int i;
printf("Number\tName\tObj_Score\tOpe_Score\tTotal_Score\tResult\n"); for (i = 0; i < n; i++) printf("%ld\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result); }
void read(STU st[], int n) { int i; FILE *fin;
fin = fopen("examinee.txt", "r"); if (!fin) { printf("fail to open file\n"); return; }
while (!feof(fin)) { for (i = 0; i < n; i++) fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); }
fclose(fin); }
void write(STU st[], int n) { int i, cnt = 0; FILE *fp;
fp = fopen("list_pass.txt", "w"); if (fp == NULL) { printf("fail to open\n"); return; }
for (i = 0; i < n; i++) { if (st[i].sum >= 60) { cnt++; fprintf(fp, "%ld %s %.2f %.2f %.2f %s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result); } }
fclose(fp);
printf("%d students passed the exam\n", cnt); }
int process(STU st[], int n, STU st_pass[]) { int i, cnt = 0;
for (i = 0; i < n; i++) { st[i].sum = st[i].objective + st[i].subjective; if (st[i].sum >= 60) { cnt++; strcpy(st[i].result, "Passed"); strcpy(st_pass[cnt - 1].name, st[i].name); st_pass[cnt - 1].id = st[i].id; st_pass[cnt - 1].objective = st[i].objective; st_pass[cnt - 1].subjective = st[i].subjective; st_pass[cnt - 1].sum = st[i].sum; strcpy(st_pass[cnt - 1].result, st[i].result); } else { strcpy(st[i].result, "Failed"); } }
return cnt; }
|