实验七
实验任务四
1 #include<stdio.h> 2 #include<ctype.h> 3 4 int main(){ 5 int line=0,chars=0,ch; 6 FILE *fp; 7 8 fp=fopen("C:\\Users\\沈CL\\Desktop\\新建文件夹\\data4.txt","r"); 9 printf("data4.txt统计结果:\n"); 10 if(!fp){ 11 perror("data4.txt"); 12 return 1; 13 } 14 while((ch=fgetc(fp))!=EOF){ 15 if(!isspace(ch)){ 16 chars++; 17 } 18 if(ch=='\n'){ 19 line++; 20 } 21 } 22 ch=fgetc(fp); 23 if(ch!='\n') 24 line++; 25 26 fclose(fp); 27 printf("字符数:%d\n",chars); 28 printf("行数:%d",line); 29 return 0; 30 }

实验任务五
#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 s[], 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("从文件读入%d个考生信息...\n", N); read(stu, N); printf("\n对考生成绩进行统计...\n"); cnt = process(stu, N, stu_pass); printf("\n通过考试的名单:\n"); output(stu_pass, cnt); write(stu_pass, cnt); pass_rate = 1.0 * cnt / N; printf("\n本次等级考试通过率: %.2f%%\n", pass_rate * 100); return 0; } void output(STU st[], int n) { int i; printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t结果\n"); for (i = 0; i < n; i++) 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); } void read(STU st[], int n) { int i; FILE *fin; fin = fopen("C:\\Users\\沈CL\\Desktop\\新建文件夹\\examinee.txt", "r"); if (!fin) { printf("fail to open file\n"); return; } 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 s[], int n) { FILE *fout; int i; fout = fopen("list_pass.txt", "w"); if (!fout) { printf("无法创建文件 list_pass.txt\n"); return; } fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t结果\n"); for (i = 0; i < n; i++) { 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].result); } fclose(fout); } int process(STU st[], int n, STU st_pass[]) { int i, pass_cnt = 0; for (i = 0; i < n; i++) { st[i].sum = st[i].objective + st[i].subjective; if (st[i].sum >= 60) { strcpy(st[i].result, "通过"); st_pass[pass_cnt] = st[i]; pass_cnt++; } else { strcpy(st[i].result, "不通过"); } } return pass_cnt; }

实验任务六
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <time.h> 5 6 #define N 80 7 #define M 5 8 9 int main() { 10 FILE *fin, *fout; 11 char students[N][50]; 12 int flag[N] = {0}; 13 int chosen[M]; 14 char filename[100]; 15 int i, index, count = 0; 16 17 fin = fopen("C:\\Users\\沈CL\\Desktop\\新建文件夹\\list.txt", "r"); 18 if (!fin) { 19 printf("无法打开文件 list.txt\n"); 20 return 1; 21 } 22 for (i = 0; i < N; i++) { 23 if (fgets(students[i], 50, fin) == NULL) break; 24 25 students[i][strcspn(students[i], "\n")] = 0; 26 } 27 fclose(fin); 28 29 srand(time(NULL)); 30 31 while (count < M) { 32 index = rand() % N; 33 if (flag[index] == 0) { 34 flag[index] = 1; 35 chosen[count] = index; 36 count++; 37 } 38 } 39 printf("请输入输出文件名:"); 40 scanf("%s", filename); 41 42 fout = fopen(filename, "w"); 43 if (!fout) { 44 printf("无法创建文件 %s\n", filename); 45 return 1; 46 } 47 printf("中奖名单如下:\n"); 48 fprintf(fout, "中奖名单如下:\n"); 49 for (i = 0; i < M; i++) { 50 printf("%d. %s\n", i + 1, students[chosen[i]]); 51 fprintf(fout, "%d. %s\n", i + 1, students[chosen[i]]); 52 } 53 54 fclose(fout); 55 printf("结果已保存到文件 %s\n", filename); 56 57 return 0; 58 }

浙公网安备 33010602011771号