实验7
任务4
#include<stdio.h> int main() { FILE*fp; int ch; int l=0; int c=0; fp=fopen("data4.text","r"); if(fp==NULL) { printf("无法打开文件\n"); return 1; } while((ch=fgetc(fp))!=EOF) { if(ch=='\n') { l++; } if(ch==' ') { continue; } if(ch=='\n') { continue; } if(ch=='\t') { continue; } c++; } printf("文件总行数;%d\n",l); printf("有效英文字符数;%d\n",c); fclose(fp); return 0; }
任务5
#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("从文件读入%d个考生信息: 已完成\n", N); read(stu, N); printf("\n对考生成绩进行统计: 已完成\n"); cnt = process(stu, N, stu_pass); printf("\n所有考生完整信息:\n"); output(stu, N); printf("\n通过考试的名单写入文件: 已完成!\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\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); } // 从文本文件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); } // 对考生信息进行处理:计算每位考生考试总分、结果;统计并返回通过考试的人数 int process(STU st[], int n, STU st_pass[]) { int pass_num = 0; for(int i = 0; i < n; i++) { st[i].sum = st[i].objective + st[i].subjective; if(st[i].sum >= 60) { strcpy(st[i].result, "通过"); pass_num++; } else { strcpy(st[i].result, "未通过"); } } return pass_num; } // 把通过考试的考生完整信息写入文件list_pass.txt // 准考证号,姓名,客观题得分,操作题得分,总分,结果 void write(STU st[], int n) { FILE *fout; fout = fopen("list_pass.txt", "w"); if(!fout) { printf("文件打开失败,无法写入\n"); return; } fprintf(fout, "准考证号\t姓名\t客观题\t操作题\t总分\t结果\n"); for(int i = 0; i < n; i++) { fprintf(fout, "%ld\t%s\t%.2f\t%.2f\t%.2f\t%s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result); } fclose(fout); }
任务6
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #define N 80 #define PICK 5 #define LEN 128 typedef struct { long id; char name[32]; char class_name[64]; } Student; int read_students(Student st[], int maxn); void make_date_filename(char filename[], int size); void pick_students(int selected[], int n, int count); void sort_by_id(Student st[], int n); void output_students(FILE *fp, Student st[], int n); int main(int argc, char *argv[]) { Student students[N], winners[PICK]; int selected[PICK]; int n, i; char filename[LEN]; FILE *fout; n = read_students(students, N); if (n < PICK) { printf("学生人数不足,无法抽取%d位中奖名单\n", PICK); return 1; } if (argc > 1) { strncpy(filename, argv[1], LEN - 1); filename[LEN - 1] = '\0'; } else { printf("请输入保存中奖名单的文件名(直接回车则使用系统日期): "); if (fgets(filename, LEN, stdin) == NULL) filename[0] = '\0'; filename[strcspn(filename, "\r\n")] = '\0'; if (filename[0] == '\0') make_date_filename(filename, LEN); } srand((unsigned int)time(NULL) ^ (unsigned int)clock()); pick_students(selected, n, PICK); for (i = 0; i < PICK; i++) winners[i] = students[selected[i]]; sort_by_id(winners, PICK); fout = fopen(filename, "w"); if (fout == NULL) { printf("fail to open file\n"); return 1; } printf("中奖名单如下:\n"); output_students(stdout, winners, PICK); output_students(fout, winners, PICK); fclose(fout); printf("中奖名单已写入文件: %s\n", filename); return 0; } int read_students(Student st[], int maxn) { int n = 0; FILE *fin; fin = fopen("list.txt", "r"); if (fin == NULL) { printf("fail to open file\n"); return 0; } while (n < maxn && fscanf(fin, "%ld %31s %63s", &st[n].id, st[n].name, st[n].class_name) == 3) { ++n; } fclose(fin); return n; } void make_date_filename(char filename[], int size) { time_t now; struct tm *local; now = time(NULL); local = localtime(&now); if (local == NULL || strftime(filename, (size_t)size, "%Y%m%d.txt", local) == 0) strncpy(filename, "winners.txt", (size_t)size); filename[size - 1] = '\0'; } void pick_students(int selected[], int n, int count) { int flag[N] = {0}; int i = 0; int index; while (i < count) { index = rand() % n; if (!flag[index]) { flag[index] = 1; selected[i] = index; ++i; } } } void sort_by_id(Student st[], int n) { int i, j; Student temp; for (i = 0; i < n - 1; i++) { for (j = 0; j < n - 1 - i; j++) { if (st[j].id > st[j + 1].id) { temp = st[j]; st[j] = st[j + 1]; st[j + 1] = temp; } } } } void output_students(FILE *fp, Student st[], int n) { int i; fprintf(fp, "学号\t\t姓名\t班级\n"); for (i = 0; i < n; i++) fprintf(fp, "%ld\t%s\t%s\n", st[i].id, st[i].name, st[i].class_name); }
浙公网安备 33010602011771号