实验7
任务4
源代码
#include <stdio.h> int main() {FILE*fp=fopen("C:\\Users\\27367\\Downloads\\实验7数据文件及部分代码_gbk\\实验7数据文件及部分代码_gbk\\data4.txt","r"); if(fp=NULL) {perror("open file failed"); return 1; } char ch; int line=0; int char_cnt=0; int newline=1; while((ch=fgetc(fp))!=EOF) {if(ch=='\n') {line++; newline=1; continue; } if(ch==' '||ch=='\t') continue; char_cnt++; newline=0; } if(!newline) line++; fclose(fp); printf("data4.txt统计结果:\n"); printf("行数: %d\n",line); printf("字符数(不计空白):%d\n",char_cnt); return 0; }
运行结果

任务5
源代码
include <stdio.h> #include <string.h> #define MAX_N 100 typedef struct { char id[20]; char name[20]; int obj; int op; int total; char res[10]; } STU; void read(STU st[], int n) { FILE *fp = fopen("examinee.txt", "r"); if (fp == NULL) { perror("open examinee.txt error"); return; } int i = 0; while (i < n && fscanf(fp, "%s %s %d %d", st[i].id, st[i].name, &st[i].obj, &st[i].op) == 4) { i++; } fclose(fp); } int process(STU st[], int n, STU st_pass[]) { int pass_cnt = 0; for (int i = 0; i < n; i++) { st[i].total = st[i].obj + st[i].op; if (st[i].total >= 60) { strcpy(st[i].res, "通过"); st_pass[pass_cnt++] = st[i]; } else { strcpy(st[i].res, "未通过"); } } return pass_cnt; } void write(STU st_pass[], int pass_num) { FILE *fp = fopen("list_pass.txt", "w"); if (fp == NULL) { perror("open list_pass.txt error"); return; } fprintf(fp, "准考证号\t姓名\t客观题\t操作题\t总分\t结果\n"); for (int i = 0; i < pass_num; i++) { fprintf(fp, "%s\t%s\t%d\t%d\t%d\t%s\n", st_pass[i].id, st_pass[i].name, st_pass[i].obj, st_pass[i].op, st_pass[i].total, st_pass[i].res); } fclose(fp); } void output(STU st_pass[], int pass_num) { printf("===== 通过考试考生信息 =====\n"); printf("准考证号\t姓名\t客观题\t操作题\t总分\n"); for (int i = 0; i < pass_num; i++) { printf("%s\t%s\t%d\t%d\t%d\n", st_pass[i].id, st_pass[i].name, st_pass[i].obj, st_pass[i].op, st_pass[i].total); } } int main(void) { STU stu[MAX_N]; STU pass_stu[MAX_N]; int total_stu = 3; read(stu, total_stu); int pass = process(stu, total_stu, pass_stu); write(pass_stu, pass); output(pass_stu, pass); double rate = (double)pass / total_stu * 100; printf("\n本次考试通过率:%.2f%%\n", rate); return 0; }
运行结果
任务6
源代码
#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 80 typedef struct { char id[15]; char name[20]; char cls[30]; } Student; int main() { Student s[N], lucky[5]; int flag[N] = {0}; FILE *fp; int i, n = 0; int cnt = 0; char fname[50]; fp = fopen("list.txt", "r"); if (fp == NULL) { printf("无法打开 list.txt\n"); return 1; } while (fscanf(fp, "%s%s%[^\n]", s[n].id, s[n].name, s[n].cls) != EOF) { n++; } fclose(fp); srand(time(NULL)); while (cnt < 5) { int r = rand() % n; if (flag[r] == 0) { lucky[cnt] = s[r]; flag[r] = 1; cnt++; } } printf("--- 中奖名单 ---\n"); for (i = 0; i < 5; i++) { printf("%s %s %s\n", lucky[i].id, lucky[i].name, lucky[i].cls); } printf("保存到文件\n"); printf("输入文件名:"); scanf("%s", fname); fp = fopen(fname, "w"); if (fp == NULL) { printf("文件保存失败\n"); return 1; } for (i = 0; i < 5; i++) { fprintf(fp, "%s %s %s\n", lucky[i].id, lucky[i].name, lucky[i].cls); } fclose(fp); printf("保存成功!\n"); return 0; }
运行结果


浙公网安备 33010602011771号