实验7

实验7

任务4

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 #include<stdlib.h>
 4 #define MAX 1000
 5 
 6 int main(){
 7     
 8     FILE *fp;
 9     char line[MAX];
10     int lines = 0;
11     int chars = 0;
12     int i;
13     
14     fp =fopen("data4.txt","r");
15     if(fp == NULL){
16         printf("无法打开文件 data4.txt\n");
17         return 1;
18     }
19     
20     while (fgets(line, sizeof(line), fp) != NULL){
21         lines++;
22         
23         
24         for( i = 0; line[i] != '\0'; i++){
25             char c = line[i];
26             
27             if (c != ' ' && c != '\n' && c != '\t') {
28                 chars++;
29             }
30         }
31     } 
32     
33     fclose(fp);
34     
35     printf("data4.txt统计结果: \n");
36     printf("行数:%d\n", lines);
37     printf("字符数(不及空白符):%d\n", chars);
38     
39     system("pause");
40     return 0;
41 }

屏幕截图 2026-06-23 135415

实验任务5

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include<stdlib.h>
 4 #define N 10
 5 typedef struct {
 6 long id;
 7 char name[20];
 8 float objective;
 9 float subjective;
10 float sum;
11 char result[10];
12 } STU;
13 void read(STU st[], int n);
14 void write(STU st[], int n);
15 void output(STU st[], int n);
16 int process(STU st[], int n, STU st_pass[]);
17 int main() {
18 STU stu[N], stu_pass[N];
19 int cnt;
20 double pass_rate;
21 printf("从文件读入%d个考生信息:已完成\n", N);
22 read(stu, N);
23 printf("\n对考生成绩进行统计:已完成\n");
24 cnt = process(stu, N, stu_pass);
25 printf("\n所有考生完整信息:\n");
26 output(stu, N);
27 printf("\n通过考试的名单写入文件:已完成!\n");
28 write(stu_pass, cnt);
29 pass_rate = 1.0 * cnt / N;
30 printf("\n本次等级考试通过率:%.2f%%\n", pass_rate*100);
31 system("pause");
32 return 0;
33 }
34 void output(STU st[], int n) {
35 int i;
36 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
37 for (i = 0; i < n; i++)
38 printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", st[i].id, st[i].name,
39 st[i].objective, st[i].subjective, st[i].sum, st[i].result);
40 }
41 void read(STU st[], int n) {
42 st[0].id = 1001; strcpy(st[0].name, "桃乐丝"); st[0].objective = 36; st[0].subjective = 55;
43 st[1].id = 1002; strcpy(st[1].name, "稻草人"); st[1].objective = 28; st[1].subjective = 40;
44 st[2].id = 1003; strcpy(st[2].name, "千寻"); st[2].objective = 39; st[2].subjective = 55;
45 st[3].id = 1004; strcpy(st[3].name, "白龙"); st[3].objective = 35; st[3].subjective = 60;
46 st[4].id = 1005; strcpy(st[4].name, "汤婆婆"); st[4].objective = 20; st[4].subjective = 35;
47 st[5].id = 1006; strcpy(st[5].name, "无脸男"); st[5].objective = 33; st[5].subjective = 50;
48 st[6].id = 1007; strcpy(st[6].name, "希达"); st[6].objective = 40; st[6].subjective = 32;
49 st[7].id = 1008; strcpy(st[7].name, "巴鲁"); st[7].objective = 28; st[7].subjective = 30;
50 st[8].id = 1009; strcpy(st[8].name, "苏菲"); st[8].objective = 37; st[8].subjective = 60;
51 st[9].id = 1010; strcpy(st[9].name, "哈尔"); st[9].objective = 35; st[9].subjective = 57;
52 }
53 int process(STU st[], int n, STU st_pass[]) {
54 int i, pass_cnt = 0;
55 for (i = 0; i < n; i++) {
56 st[i].sum = st[i].objective + st[i].subjective;
57 if (st[i].sum >= 60) {
58 strcpy(st[i].result, "通过");
59 st_pass[pass_cnt] = st[i];
60 pass_cnt++;
61 } else {
62 strcpy(st[i].result, "未通过");
63 }
64 }
65 return pass_cnt;
66 }
67 void write(STU st[], int n) {
68 int i;
69 FILE *fout = fopen("list_pass.txt", "w");
70 if (!fout) {
71 printf("无法创建输出文件\n");
72 return;
73 }
74 fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
75 for (i = 0; i < n; i++) {
76 fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n",
77 st[i].id, st[i].name,
78 st[i].objective, st[i].subjective,
79 st[i].sum, st[i].result);
80 }
81 fclose(fout);
82 }

屏幕截图 2026-06-23 141046

实验任务6

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #include <string.h>
 5 #define N 10
 6 typedef struct {
 7 char id[20];
 8 char name[20];
 9 char cls[30];
10 } Student;
11 int main() {
12 Student stu[N] = {
13 {"204942001", "抖森", "星际联盟2049(1)班"},
14 {"204942002", "卷福", "星际联盟2049(1)班"},
15 {"204942003", "毛怪", "星际联盟2049(1)班"},
16 {"204942004", "大眼仔", "星际联盟2049(1)班"},
17 {"204942005", "辛巴", "星际联盟2049(1)班"},
18 {"204942006", "裘花", "星际联盟2049(1)班"},
19 {"204942007", "小李子", "星际联盟2049(1)班"},
20 {"204942008", "甜茶", "星际联盟2049(1)班"},
21 {"204942009", "囧瑟夫", "星际联盟2049(1)班"},
22 {"204942010", "霉霉", "星际联盟2049(1)班"}
23 };
24 int idx[N];
25 char filename[50];
26 FILE *fp;
27 int i;
28 for (i = 0; i < N; i++) idx[i] = i;
29 srand((unsigned)time(NULL));
30 
31 for (i = N - 1; i > 0; i--) {
32 int j = rand() % (i + 1);
33 int tmp = idx[i];
34 idx[i] = idx[j];
35 idx[j] = tmp;
36 }
37 printf("--------中奖名单--------\n");
38 for (i = 0; i < 5; i++) {
39 int k = idx[i];
40 printf("%s\t%s\t%s\n", stu[k].id, stu[k].name, stu[k].cls);
41 }
42 printf("------------------------\n");
43 printf("输入文件名:");
44 scanf("%s", filename);
45 fp = fopen(filename, "w");
46 if (fp == NULL) {
47 printf("文件创建失败\n");
48 return 1;
49 }
50 for (i = 0; i < 5; i++) {
51 int k = idx[i];
52 fprintf(fp, "%s\t%s\t%s\n", stu[k].id, stu[k].name, stu[k].cls);
53 }
54 fclose(fp);
55 printf("保存成功!\n");
56 return 0;
57 }

屏幕截图 2026-06-23 141937

 

posted @ 2026-06-23 14:20  luvfz  阅读(7)  评论(0)    收藏  举报