实验7

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <ctype.h>
 5 
 6     char c;
 7     int a=1, b=0;
 8 
 9 int main() {
10     FILE* fp;
11     fp = fopen("C:\\Users\\zk\\Downloads\\实验7数据文件及部分代码_gbk\\实验7数据文件及部分代码_gbk\\data4.txt", "r");
12     if (!fp) {
13         perror("C:\\Users\\zk\\Downloads\\实验7数据文件及部分代码_gbk\\实验7数据文件及部分代码_gbk\\data4.txt");
14         return 1;
15     }
16 
17     while ((c = fgetc(fp) )!= EOF) {
18         if (c == '\n')
19         {
20             a++;
21         }
22         else if (!isspace(c))
23         {
24             b++;
25         }
26         
27     }
28     fclose(fp);
29     printf("data4.txt统计结果:\n");
30     printf("行数:%d\n", a);
31     printf("字符数(不计空白符):%d", b);
32     
33     return 0;
34 
屏幕截图 2025-12-30 222021

 

 

task5

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

屏幕截图 2025-12-30 222413

屏幕截图 2025-12-30 222422

 

 

task6

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 100
int main() {
char x[N][N];

int n=0,i=0,l;
int q[N] = { 0 };
FILE* fp;
FILE* lp;
fp = fopen("list.txt", "r");
if (!fp) {
perror("list.txt");
return 1;
}

i = 0;
while (fgets(x[i], N, fp) != NULL) {
++i;

}
n = i;
srand(time(0));
for (i = 0; i <5; i++) {
l = rand() % n;
if (q[l]) {
continue;
}
q[l] = 1;
printf("%s", x[l]);

}
char filename[80];
printf("Enter filename:");
gets(filename);
lp = fopen(filename, "w");
if (!lp) {
perror(filename);
return 1;
}
for (i = 0; i <n; i++) {
if(q[i]==1)
fprintf(lp,"%s",x[i]);
}
fclose(fp);
fclose(lp);
return 0;
}

 

 

屏幕截图 2025-12-30 222635

屏幕截图 2025-12-30 222642

 

 

 

posted @ 2025-12-30 22:27  叶永祺  阅读(2)  评论(0)    收藏  举报