task4.c
1 #include <stdio.h> 2 int hangshu(); 3 int zifushu(); 4 int main() { 5 printf("data4.txt统计结果:\n"); 6 printf("行数:%d\n",hangshu()); 7 printf("字符数(不包括空格):%d\n",zifushu()); 8 return 0; 9 } 10 int hangshu() { 11 FILE *fp; 12 fp = fopen("C:/Users/金天/Desktop/vscode/实验7数据文件及部分代码/data4.txt","r"); 13 if(!fp) { 14 printf("文件打开失败!\n"); 15 return 0; 16 } 17 int count = 0; 18 char ch; 19 while ((ch = getc(fp))!= EOF) { 20 if (ch == '\n') { 21 count++; 22 } 23 } 24 fclose(fp); 25 return count+=1; 26 } 27 int zifushu() { 28 FILE *fp; 29 fp = fopen("C:/Users/金天/Desktop/vscode/实验7数据文件及部分代码/data4.txt","r"); 30 if(!fp) { 31 printf("文件打开失败!\n"); 32 return 0; 33 } 34 int count = 0; 35 char ch; 36 while ((ch = getc(fp))!= EOF) { 37 if (ch!=' '&&ch!='\n') { 38 count++; 39 } 40 } 41 fclose(fp); 42 return count; 43 }
task5.c
1 #include <stdio.h> 2 #include <string.h> 3 4 #define N 10 5 6 typedef struct { 7 long id; // 准考证号 8 char name[20]; // 姓名 9 float objective; // 客观题得分 10 float subjective; // 操作题得分 11 float sum; // 总分 12 char result[10]; // 考试结果 13 } STU; 14 15 // 函数声明 16 void read(STU st[], int n); 17 void write(STU st[], int n); 18 void output(STU st[], int n); 19 int process(STU st[], int n, STU st_pass[]); 20 21 int main() { 22 STU stu[N], stu_pass[N]; 23 int cnt; 24 double pass_rate; 25 26 printf("从文件读入%d个考生信息...\n", N); 27 read(stu, N); 28 29 printf("\n对考生成绩进行统计...\n"); 30 cnt = process(stu, N, stu_pass); 31 32 printf("\n通过考试的名单:\n"); 33 output(stu, N); // 输出所有考生完整信息到屏幕 34 write(stu, N); // 输出考试通过的考生信息到文件 35 36 pass_rate = 1.0 * cnt / N; 37 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100); 38 39 return 0; 40 } 41 42 // 把所有考生完整信息输出到屏幕上 43 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 44 void output(STU st[], int n) { 45 int i; 46 47 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 48 for (i = 0; i < n; i++) 49 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].result); 50 } 51 52 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 53 void read(STU st[], int n) { 54 int i; 55 FILE *fin; 56 57 fin = fopen("examinee.txt", "r"); 58 if (!fin) { 59 printf("fail to open file\n"); 60 return; 61 } 62 63 while (!feof(fin)) { 64 for (i = 0; i < n; i++) 65 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 66 } 67 68 fclose(fin); 69 } 70 71 // 把通过考试的考生完整信息写入文件list_pass.txt 72 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 73 void write(STU st[], int n) { 74 int i, count = 0; 75 FILE*fp; 76 fp=fopen("list_pass.txt","w"); 77 if(fp==NULL) 78 { 79 printf("fail to open file\n"); 80 return; 81 } 82 for(i=0;i<n;i++) 83 { 84 if(st[i].sum>=60) 85 { 86 count++; 87 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); 88 } 89 } 90 fclose(fp); 91 } 92 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 93 int process(STU st[], int n, STU st_pass[]) { 94 int i,count=0; 95 for (i = 0; i < n; i++) { 96 st[i].sum = st[i].objective + st[i].subjective; 97 if (st[i].sum >= 60) { 98 strcpy(st[i].result, "通过"); 99 count++; 100 st_pass[count-1]=st[i]; 101 } else { 102 strcpy(st[i].result, "不通过"); 103 } 104 } 105 return count; 106 107 }
task7.c
1 #include <stdio.h> 2 #include <time.h> 3 #include <stdlib.h> 4 5 struct student { 6 long long xuehao; 7 char name[12]; 8 char banji[24]; 9 }; 10 11 int main() { 12 FILE* fp; 13 struct student STU[80]; 14 int total_students = 0; 15 fp = fopen("list.txt", "r"); 16 if (fp == NULL) { 17 printf("文件打开失败!"); 18 return 0; 19 } 20 while (total_students < 80 && fscanf(fp, "%lld %s %s", &STU[total_students].xuehao, STU[total_students].name, STU[total_students].banji) != EOF) { 21 total_students++; 22 } 23 fclose(fp); 24 25 srand(time(NULL)); 26 int num[5]; 27 int count = 0; 28 struct student chu[5]; 29 while (count < 5) { 30 int temp = rand() % total_students; 31 int flag = 0; 32 for (int i = 0; i < count; i++) { 33 if (temp == num[i]) { 34 flag = 1; 35 break; 36 } 37 } 38 if (flag == 0) { 39 num[count] = temp; 40 chu[count] = STU[temp]; 41 count++; 42 } 43 } 44 45 printf("抽取的学生信息如下:\n"); 46 for (int i = 0; i < 5; i++) { 47 printf("%lld %s %s\n", chu[i].xuehao, chu[i].name, chu[i].banji); 48 } 49 50 char filename[100]; 51 printf("请输入文件名:"); 52 scanf("%99s", filename); 53 fp = fopen(filename, "w"); 54 if (fp == NULL) { 55 printf("文件打开失败!"); 56 return 0; 57 } 58 for (int i = 0; i < 5; i++) { 59 fprintf(fp, "%lld %s %s\n", chu[i].xuehao, chu[i].name, chu[i].banji); 60 } 61 fclose(fp); 62 return 0; 63 }
标签:fp,count,int,st,STU,实验,printf From: https://www.cnblogs.com/JackSusan/p/18630118