实验7
实验任务4
#include <stdio.h> #include <ctype.h> int main() { FILE *fp; char ch; int line_count = 0; int char_count = 0; fp=fopen("C:\\Users\\li\\Downloads\\data4.txt","r"); if (fp == NULL) { printf("fail to open file data4.txt\n"); return 1; } while ((ch = fgetc(fp)) != EOF) { if (!isspace(ch)) { char_count++; } if (ch == '\n') { line_count++; } } if (char_count > 0) { line_count++; } printf("data4.txt统计结果:\n"); printf("行数:%d\n", line_count); printf("字符数(不计空白符):%d\n", char_count); fclose(fp); return 0; }
运行结果

实验任务5
#include <stdio.h> #include <string.h> #define N 10 // 考生人数,对应示例中的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, N); // 输出所有考生完整信息到屏幕 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%s\t%.2f\t\t%.2f\t\t%.2f\t%s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result); } } // 从文本文件examinee.txt读入考生信息 void read(Stu st[], int n) { int i; FILE *fin; fin = fopen("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); } // 补全:把通过考试的考生信息写入文件list_pass.txt void write(Stu s[], int n) { int i; FILE *fout = fopen("list_pass.txt", "w"); if (!fout) { printf("fail to open list_pass.txt\n"); return; } // 写入文件表头 fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t结果\n"); // 写入通过考生的信息 for(i = 0; i < n; i++){ fprintf(fout, "%ld\t%s\t%.2f\t\t%.2f\t\t%.2f\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 pass_count = 0; int i; // 记录通过人数 for (i = 0; i < n; i++) { // 计算总分 st[i].sum = st[i].objective + st[i].subjective; // 判断是否通过(总分≥60为通过) if (st[i].sum >= 60.0) { strcpy(st[i].result, "通过"); st_pass[pass_count] = st[i]; // 存入通过考生数组 pass_count++; } else { strcpy(st[i].result, "不通过"); } } return pass_count; // 返回通过人数 }
运行结果

实验任务6
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #define NUM 80 #define SELECT 5 #define N 100 int main(){ FILE *fp; fp=fopen("list.txt","r"); if(!fp){ perror("list.txt"); return 1; } char s[NUM][N]; int count=0; while(fgets(s[count],N,fp)!=NULL&&count<NUM) { count++; } fclose(fp); srand((unsigned int)time(NULL)) ; int flag[NUM]={0}; int selected[SELECT]; int i=0,random; while(i<SELECT){ random=rand()%count; if(flag[random]==0){ flag[random]=1; selected[i]=random; i++; } } printf("------------中奖名单------------\n"); int j; for(j=0;j<SELECT;++j){ printf("%s",s[selected[j]]); } char file[N]; printf("------------保存到文件------------\n"); printf("输入文件名:") ; scanf("%s",file) ; fp=fopen(file,"w"); if(!fp){ perror("file"); return 1; } for(j=0;j<SELECT;++j){ fprintf(fp,"%s\n",s[selected[j]]); } fclose(fp); printf("保存成功!\n"); return 0; }
运行结果


浙公网安备 33010602011771号