实验7

实验任务4

代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main() {
 5     FILE *fp = fopen("data99.txt", "r");
 6     int lines = 0, chars = 0;
 7     int i;
 8     char s[100];
 9     
10     if (fp == NULL) {
11         printf("fail to open file\n");
12         system("pause");
13         return 1;
14     }
15     
16     while (fgets(s, 100, fp)) {
17         lines++;
18         i = 0;
19         while (s[i] != '\0') {
20             if (s[i] != ' ' && s[i] != '\t' && s[i] != '\n') {
21                 chars++;
22             }
23             i++;
24         }
25     }
26     
27     fclose(fp);
28     printf("data99.txt统计结果:\n");
29     printf("行数: %d\n", lines);
30     printf("字符数(不计空白符): %d\n", chars);
31 
32     system("pause");
33     return 0;
34 }
4.c

截图

 

实验任务5

代码

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include<stdlib.h>
  4 #define N 10
  5 
  6 typedef struct {
  7     long id;            // 准考证号
  8     char name[20];      // 姓名
  9     float objective;    // 客观题得分
 10     float subjective;   // 操作题得分
 11     float sum;          // 总分
 12     char result[10];    // 考试结果
 13 } STU;
 14 
 15 // 函数声明
 16 void read(STU st[], int n);
 17 void write(STU st[], int n);
 18 void output(STU st[], int n);
 19 int process(STU st[], int n, STU st_pass[]);
 20 
 21 int main() {
 22     STU stu[N], stu_pass[N];
 23     int cnt;
 24     double pass_rate;
 25 
 26     printf("从文件读入%d个考生信息...\n", N);
 27     read(stu, N);
 28 
 29     printf("\n对考生成绩进行统计...\n");
 30     cnt = process(stu, N, stu_pass);
 31 
 32     printf("\n通过考试的名单:\n");
 33     output(stu, N);   // 输出所有考生完整信息到屏幕
 34     write(stu, N);    // 输出考试通过的考生信息到文件
 35 
 36     pass_rate = 1.0 * cnt / N;
 37     printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100);
 38     system("pause");
 39     return 0;
 40 }
 41 
 42 // 把所有考生完整信息输出到屏幕上
 43 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 44 void output(STU st[], int n) {
 45     int i;
 46     
 47     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 48     for (i = 0; i < n; i++)
 49         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);
 50 }
 51 
 52 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
 53 void read(STU st[], int n) {
 54     int i;
 55     FILE *fin;
 56 
 57     fin = fopen("examinee.txt", "r");
 58     if (!fin) {
 59         printf("fail to open file\n");
 60         return;
 61     }
 62 
 63     while (!feof(fin)) {
 64         for (i = 0; i < n; i++)
 65             fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
 66     }
 67 
 68     fclose(fin);
 69 }
 70 
 71 // 把通过考试的考生完整信息写入文件list_pass.txt
 72 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 73 void write(STU s[], int n) {
 74     int i;
 75     FILE *fout = fopen("list_pass.txt", "w");
 76     if (!fout) {
 77         printf("fail to open file for writing\n");
 78         return;
 79     }
 80 
 81     fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 82     for (i = 0; i < n; i++) {
 83         if (strcmp(s[i].result, "通过") == 0){
 84         fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n",
 85                s[i].id, s[i].name, s[i].objective,
 86                s[i].subjective, s[i].sum, s[i].result);}
 87     }
 88     fclose(fout);
 89     printf("通过考生信息已保存到list_pass.txt\n");
 90 }
 91 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
 92 int process(STU st[], int n, STU st_pass[]) {
 93     int cnt = 0;
 94     int i;
 95     for (i = 0; i < n; i++) {
 96         st[i].sum = st[i].objective + st[i].subjective;
 97         if (st[i].sum >= 60) {
 98             strcpy(st[i].result, "通过");
 99             st_pass[cnt++] = st[i];
100         } else {
101             strcpy(st[i].result, "未通过");
102         }
103     }
104     return cnt;
105 }
5.c

截图

 

实验任务6

代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 #define N 80
 5 #define M 50
 6 
 7 void write();
 8 
 9 
10 int main(){
11     FILE*fp;
12     char info[N][80];
13     int flag[N]={0};
14     int i;
15     int lucky;
16     int selected[5];
17     int count=0;
18     char filename[M];
19     fp=fopen("d:\\list.txt","r");
20     if(fp==NULL){
21         perror("Failed to open file");
22         return 1;
23     }
24     for(i=0;i<N;i++){
25         fgets(info[i],80,fp);
26     }
27     fclose(fp);
28 
29     srand(time(NULL));
30 
31     printf("--------中奖名单--------\n");
32     while(count<5){
33         lucky=rand()%N;
34         if(!flag[lucky]){
35             flag[lucky]=1;
36             selected[count]=lucky;
37             printf("%s",info[lucky]);
38             count++;
39         }
40     }
41     printf("--------保存到文件--------\n");
42     printf("输入文件名: ");
43     scanf("%s",filename);
44     write(filename,info,selected);
45     system("pause");
46     return 0;
47 }
48 
49 void write(char*filename,char(*info)[80],int*selected){
50     FILE*fp;
51     int i=0;
52     fp=fopen(filename,"w");
53     if(fp==NULL){
54         perror("Failed to open file for writing");
55         return;
56     }
57     for(i=0;i<5;i++){
58         fputs(info[selected[i]],fp);
59     }
60     fclose(fp);
61     printf("保存成功!\n");
62 }
6.c

截图

 

posted @ 2025-06-03 13:01  在下广  阅读(24)  评论(0)    收藏  举报