实验7
实验任务4:
源代码:
#include <stdio.h> int main() { FILE *fp; int line = 1; int count = 0; char ch; fp = fopen("data4.txt","r"); if(fp == NULL) { printf("Fail to open file to read\n"); return 1; } while((ch = fgetc(fp)) != EOF) { if(ch == '\n') line++; else if(ch != ' ' && ch != '\t') count++; } fclose(fp); printf("data4.txt统计结果:\n"); printf("行数: %d\n",line); printf("字符数(不计空白符): %d\n",count); 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 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); } 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); } void write(STU st[],int n) { FILE *fout = fopen("list_pass.txt", "w"); if (!fout) { printf("Fail to open the list_pass\n"); return; } fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); int i; for (i = 0; i < n; i++) if (strcmp(st[i].result, "通过") == 0) fprintf(fout, "%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); fclose(fout); printf("通过考生信息已成功写入list_pass.txt\n"); } int process(STU st[],int n,STU st_pass[]) { int cnt = 0; for (int i = 0; i < n; i++) { st[i].sum = st[i].objective + st[i].subjective; if (st[i].sum >= 60.0) { strcpy(st[i].result, "通过"); st_pass[cnt] = st[i]; cnt++; } else strcpy(st[i].result, "未通过"); } return cnt; }
代码运行结果:

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



浙公网安备 33010602011771号