实验7
任务4
源代码
#include <stdio.h> #define N 80 #define M 100 int a = 0; int n = 0; void read() { char ch; FILE *fp = fopen("C:\\Users\\wsj\\Desktop\\实验7数据文件及部分代码\\data4.txt", "r"); if (fp == NULL) { perror("Unable to open file"); return; } while ((ch = fgetc(fp)) != EOF) { if (ch != ' ' && ch != '\n' && ch != '\t' && ch != '\r') { n++; } if (ch == '\n') { a++; } } if (ch != '\n') { a++; } fclose(fp); } int main() { read(); printf("data4.txt统计结果\n"); printf("行数: %17d\n", a); printf("字符数(不计空白符):%d\n", n); 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); // 输出所有考生完整信息到屏幕 write(stu, N); // 输出考试通过的考生信息到文件 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; } while (!feof(fin)) { 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 st[], int n) { FILE *fp; int i; fp=fopen("list_pass.txt","w"); if(!fp) { printf("fail to open file to write\n"); return; } fprintf(fp,"准考证号 姓名 客观题得分 操作题得分 总分 结果\n"); for (i = 0; i < N; i++) if(st[i].sum>=60) fprintf(fp,"%-20ld%-10s\t%-20.2f%-20.2f%-20.2f%s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result); fclose(fp); } int process(STU st[], int n, STU st_pass[]) { int i,t=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[t].id=st[i].id; strcpy(st_pass[t].name,st[i].name); st_pass[t].objective=st[i].objective; st_pass[t].subjective=st[i].subjective; st_pass[t].sum=st[i].sum; strcpy(st_pass[t].result,st[i].result); t++; } else strcpy(st[i].result,"不通过"); } return t; }
运行结果
任务六
源代码
#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 100 #define M 100 typedef struct { int id; char name[N]; char ban[N]; } stu; void read(stu s[],int n); void write(stu s[],int n); void process(stu s[],int n,stu ss[]); void output(stu ss[],int n); int main(){ stu s[N],ss[N]; int i; int n; n=5; read(s,N); process(s,n,ss); printf("----------随机抽点名单----------\n"); output(ss,n); printf("\n"); printf("----------保存到文件----------\n"); write(ss,n); return 0; } void read(stu s[],int n) { int i; FILE *fp; fp = fopen("list.txt", "r"); if (!fp) { printf("fail to open file\n"); return; } while (!feof(fp)) { for (i = 0; i < n; i++) fscanf(fp, "%20d%10s%20s\n",&s[i].id,s[i].name,s[i].ban); } fclose(fp); } void output(stu s[],int n){ int i; for(i=0;i<n;i++) { printf("%-20d%-10s%20s\n",s[i].id,s[i].name,s[i].ban); } } void process(stu s[], int n, stu ss[]) { int i, j; int used[N] = {0}; srand((unsigned)time(NULL)); for (i = 0; i < n; i++) { do { j = rand() % 79; } while (used[j]); ss[i] = s[j]; used[j] = 1; } } void write(stu ss[],int n){ char x[N]; int i; printf ("输入文件名:"); scanf("%s",x); FILE *fp; fp=fopen(x,"w"); if(fp==NULL){ printf("fail"); return; } for(i=0;i<n;i++) fprintf(fp,"%-20d%-10s%-20s\n",ss[i].id,ss[i].name,ss[i].ban); printf("保存成功!"); fclose(fp); }
运行结果
选做
源代码
#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 100 #define M 100 typedef struct { int id; char name[N]; char ban[N]; } stu; void read(stu s[],int n); void write(stu s[],int n); void process(stu s[],int n,stu ss[]); void output(stu ss[],int n); int main(){ stu s[N],ss[N]; int i; int n; n=5; read(s,N); process(s,n,ss); printf("----------20241228抽点名单----------\n"); output(ss,n); printf("\n"); write(ss,n); return 0; } void read(stu s[],int n) { int i; FILE *fp; fp = fopen("list.txt", "r"); if (!fp) { printf("fail to open file\n"); return; } while (!feof(fp)) { for (i = 0; i < n; i++) fscanf(fp, "%20d%10s%20s\n",&s[i].id,s[i].name,s[i].ban); } fclose(fp); } void output(stu s[],int n){ int i; for(i=0;i<n;i++) { printf("%-20d%-10s%20s\n",s[i].id,s[i].name,s[i].ban); } } void process(stu s[], int n, stu ss[]) { int i, j; stu t; int used[N] = {0}; srand((unsigned)time(NULL)); for (i = 0; i < n; i++) { do { j = rand() % 79; } while (used[j]); ss[i] = s[j]; used[j] = 1; } for(i = 0; i < n - 1; i++) { for(j = 0; j < n - 1 - i; j++) { if(ss[j].id > ss[j + 1].id) { t = ss[j]; ss[j] = ss[j + 1]; ss[j + 1] = t; } } } } void write(stu ss[],int n){ char filename[N]; int i; time_t t; struct tm *tm; time(&t); tm = localtime(&t); strftime(filename, N, "%Y%m%d.txt", tm); FILE *fp = fopen(filename, "w"); if (fp == NULL) { printf("fail"); return; } for (i = 0; i < n; i++) { fprintf(fp, "%-20d%-15s%-20s\n", ss[i].id, ss[i].name, ss[i].ban); } printf("文件保存成功!"); fclose(fp); }
运行结果