实验7

实验4
 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<string.h>
 5 int main()
 6 {   int line=0;
 7     int word=0;
 8     char ch;
 9     FILE *fp;
10     fp=fopen("C:\\Users\\ASUS\\Downloads\\实验7数据文件及部分代码1\\实验7数据文件及部分代码\\data4.txt","r");
11     if (fp == NULL) {
12         printf("fail to open file to write\n");
13         return;
14     }
15     while ((ch = fgetc(fp))!= EOF) {
16     if (ch!= '\n' && ch!= '\t' && ch!= ' ') {
17         word++;
18     }
19     if (ch == '\n') {
20         line++;
21     }
22 }
23     if(word>0)
24         line++;
25 
26 
27     fclose(fp);
28     fp=NULL;
29     printf("data4.txt统计结果:\n");
30     printf("行数:%d\n", line);
31     printf("字符数(不计空白符):%d\n", word);
32     system("pause");
33     return 0;
34 }

实验5
  1 #include <stdio.h>
  2 #include <string.h>
  3 #include<stdlib.h>
  4 
  5 #define N 10
  6 
  7 typedef struct {
  8     long id;            // 准考证号
  9     char name[20];      // 姓名
 10     float objective;    // 客观题得分
 11     float subjective;   // 操作题得分
 12     float sum;          // 总分
 13     char result[10];    // 考试结果
 14 } STU;
 15 
 16 // 函数声明
 17 void read(STU st[], int n);
 18 void write(STU st[], int n);
 19 void output(STU st[], int n);
 20 int process(STU st[], int n, STU st_pass[]);
 21 
 22 int main() {
 23     STU stu[N], stu_pass[N];
 24     int cnt;
 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     system("pause");
 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("C:\\Users\\ASUS\\Downloads\\实验7数据文件及部分代码1\\实验7数据文件及部分代码\\examinee.txt", "r");
 59     if (!fin) {
 60         printf("fail to open file\n");
 61         return;
 62     }
 63 
 64     while (!feof(fin)) {
 65         for (i = 0; i < n; i++)
 66             fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
 67     }
 68 
 69     fclose(fin);
 70 }
 71 
 72 // 把通过考试的考生完整信息写入文件list_pass.txt
 73 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 74 void write(STU s[], int n) {
 75     // 待补足
 76     // xxx
 77     int i;
 78     FILE *fp;
 79     fp = fopen("list_pass.txt","w");
 80 
 81     if(!fp)
 82         printf("failed to open file");
 83 
 84     fprintf(fp,"准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 85     for(i=0;i<N;i++){
 86         if(s[i].sum>=60)
 87         fprintf(fp,"%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].result);
 88     }
 89 
 90 
 91 
 92 }
 93 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
 94 int process(STU st[], int n, STU st_pass[]) {
 95     // 待补足
 96     int cnt=0,i;
 97     for(i=0;i<N;i++){
 98         st[i].sum=st[i].objective+st[i].subjective;
 99         if(st[i].sum>=60){
100             strcpy(st[i].result,"通过");
101             st_pass[cnt++]=st[i];
102         }
103         if(st[i].sum<60)
104         strcpy(st[i].result,"不通过");
105         }
106     return cnt;
107 
108 
109 }

 

任务6

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<time.h>
 5 #include<stdlib.h>
 6 #define N 80
 7 typedef struct STUINFO {
 8     char number[20];
 9     char name[20];
10     char classes[20];
11 }STU;
12 
13 int main() {
14     char s[N];
15     srand((unsigned)time(NULL));
16     STU x[N];
17     FILE* fp, * fin;
18     int i, j, n[5];
19     fp = fopen("C:\\Users\\ASUS\\Downloads\\实验7数据文件及部分代码1\\实验7数据文件及部分代码\\list.txt", "r");
20 
21     for (i = 0;i < N;i++)
22         fscanf(fp, "%s %s %s", x[i].number, x[i].name, x[i].classes);
23 
24     fclose(fp);
25 
26 
27     for (i = 0;i < 5;) {
28         n[i] = rand() % 80;
29         for (j = 0;j < i;j++)
30             if (n[i] == n[j])
31                 break;
32         if (j == i)
33             i++;
34     }
35 
36     printf("----------随机抽点名单----------\n");
37     for (i = 0;i < 5;i++)
38         printf("%-20s %-10s %-20s\n", x[n[i]].number, x[n[i]].name, x[n[i]].classes);
39 
40     printf("----------保存到文件----------\n");
41     printf("输入文件名:\n");
42     scanf("%s", &s);
43 
44     fin = fopen(s, "w");
45     for (i = 0;i < 5;i++) {
46         fprintf(fin, "%-20s %-10s %-20s\n", x[n[i]].number, x[n[i]].name, x[n[i]].classes);
47     }
48 
49     fclose(fin);
50 
51     printf("保存成功!");
52 
53     return 0;
54 }

 

posted @ 2024-12-30 00:49  格拉默老兵  阅读(15)  评论(0)    收藏  举报