task4.c
1 #include<stdio.h> 2 3 int main(){ 4 FILE *fp; 5 6 int k = 0; 7 char ch; 8 9 fp = fopen("data4.txt","r"); 10 11 if(fp == NULL) { 12 printf("fail to open file\n"); 13 return 1; 14 } 15 16 while(1){ 17 ch = fgetc(fp); 18 19 if(ch == EOF) 20 break; 21 22 if(ch != ' '&&ch !='\n'&&ch != '\r') 23 k++; 24 25 } 26 27 printf("data4.txt中共包含字符数:%d",k); 28 29 fclose(fp); 30 31 return 0; 32 }View Code
task5.c
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 #define N 10 6 7 typedef struct { 8 long int id; 9 char name[20]; 10 float objective; // 客观题得分 11 float subjective; // 操作题得分 12 float sum; // 总分 13 char ans[10]; // 考试结果 14 } STU; 15 16 // 函数声明 17 void finput(STU st[], int n); 18 void foutput(STU st[], int n); 19 void output(STU st[], int n); 20 int process(STU st[], int n, STU st_pass[]); 21 22 int main() { 23 STU stu[N], stu_pass[N]; 24 int cnt; 25 double pass_rate; 26 27 printf("从文件读入%d个考生信息...\n", N); 28 finput(stu, N); 29 30 printf("\n对考生成绩进行统计...\n"); 31 cnt = process(stu, N, stu_pass); 32 33 printf("\n通过考试的名单:\n"); 34 output(stu, N); // 输出到屏幕 35 foutput(stu, N); // 输出到文件 36 37 pass_rate = 1.0 * cnt / N; 38 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100); 39 40 return 0; 41 } 42 43 // 把通过考试的考生完整信息输出到屏幕上 44 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 45 void output(STU st[], int n) { 46 int i; 47 48 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 49 for (i = 0; i < n; i++) 50 printf("%ld\t\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].ans); 51 } 52 53 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 54 void finput(STU st[], int n) { 55 int i; 56 FILE *fin; 57 58 fin = fopen("examinee.txt", "r"); 59 if (fin == NULL) { 60 printf("fail to open file\n"); 61 exit(0); 62 } 63 64 while (!feof(fin)) { 65 for (i = 0; i < n; i++) 66 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 67 } 68 69 fclose(fin); 70 } 71 72 // 把通过考试的考生完整信息写入文件list_pass.txt 73 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 74 void foutput(STU s[], int n) { 75 FILE *fout; 76 int i; 77 78 // 保存到文件 79 fout = fopen("list_pass.txt", "w"); 80 if (!fout) { 81 printf("fail to open or create list_pass.txt\n"); 82 exit(0); 83 } 84 85 fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 86 87 for (i = 0; i < n; i++) 88 fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].ans); 89 90 fclose(fout); 91 } 92 93 94 95 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 96 int process(STU st[], int n, STU st_pass[]) { 97 // 待补足 98 // ××× 99 100 FILE *fp; 101 int i, k = 0; 102 103 fp = fopen("examinee.txt", "r"); 104 if (!fp) { 105 printf("fail to open or create list_pass.txt\n"); 106 exit(0); 107 } 108 109 for(i = 0;i < n;i++){ 110 fscanf(fp,"%ld%s%f%f",&st[i].id,st[i].name,&st[i].objective,&st[i].subjective); 111 st[i].sum = st[i].objective + st[i].subjective; 112 113 if(st[i].sum >= 60){ 114 st_pass[k] = st[i]; 115 k++; 116 strcpy(st[i].ans,"pass"); 117 } 118 else 119 strcpy(st[i].ans,"fail"); 120 } 121 122 fclose(fp); 123 124 return k; 125 126 }View Code
task6.c
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #define N 80 5 6 typedef struct { 7 int id; 8 char name[20]; 9 char class[100]; 10 }info; 11 12 13 void finput(info stu[], int n) { 14 15 FILE* fp; 16 fp = fopen("list.txt", "r"); 17 while (!feof(fp)) { 18 for (int i = 0; i < N; i++) { 19 fscanf(fp, "%d%s%s", &stu[i].id, stu[i].name, stu[i].class); 20 } 21 } 22 fclose(fp); 23 } 24 25 void output(info stu[], int n) { 26 for (int i = 0; i < n; i++) 27 printf("%d\t%s\t%s\n", stu[i].id, stu[i].name, stu[i].class); 28 } 29 30 void translate(info stu[], info sts[], int n) { 31 srand((unsigned)time(NULL)); 32 int judge[80] = { 0 }; 33 for (int i = 0; i < 5; i++) { 34 int j = rand() % 80 + 1; 35 if (judge[j] == 1) { 36 i--; 37 continue; 38 } 39 sts[i].id = stu[j].id; 40 strcpy(sts[i].name, stu[j].name); 41 strcpy(sts[i].class, stu[j].class); 42 judge[j] = 1; 43 } 44 } 45 46 void sort(info sts[], int n) { 47 for (int i = 0; i < 5; i++) { 48 for (int j = i; j < 5; j++) { 49 if (sts[i].id > sts[j].id) { 50 int l = sts[i].id; 51 sts[i].id = sts[j].id; 52 sts[j].id = l; 53 char p[100]; 54 strcpy(p, sts[i].name); 55 strcpy(sts[i].name, sts[j].name); 56 strcpy(sts[j].name, p); 57 } 58 } 59 } 60 } 61 62 int main() 63 { 64 info stu[N], sts[5]; 65 finput(stu, N); 66 output(stu, N); 67 printf("\n"); 68 translate(stu, sts, N); 69 sort(sts, 5); 70 output(sts, 5); 71 72 return 0; 73 }View Code
标签:fp,int,st,stu,实验,sts,id From: https://www.cnblogs.com/zyqJ/p/17917413.html