实验七

实验七

 

实验四:

源代码:

 1 #include<stdio.h>
 2 #include<ctype.h>
 3 
 4 int main(){
 5     FILE *fp;
 6     char a;
 7     int b=0,c=0;
 8     
 9     fp=fopen("data4.txt","r");
10     if(fp==NULL){
11         printf("fail to open the text");
12         return -1;
13     }
14     
15     while((a=fgetc(fp))!=EOF){
16         if(isalnum(a)){
17             c++;
18         }
19         if(a=='-'){
20             c++;
21         }
22         if(a=='\n'){
23             b++;
24         }
25     }
26     
27     fclose(fp);
28     printf("data4.txt统计结果:\n");
29     printf("行数:%d\n",b+1);
30     printf("字符数(不计入空白符):%d\n",c);
31     
32     return 0;
33 }

 

与逆行结果:

实验五:

源代码:

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

 

运行结果:

实验六:

源代码:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 
 5 // 定义学生结构体
 6 typedef struct {
 7     int id;
 8     char name[20];
 9     char class[20];
10 } Student;
11 
12 // 读取学生信息
13 void readStudents(Student students[], int *n) {
14     FILE *fp;
15     fp = fopen("list.txt", "r");
16     if (fp == NULL) {
17         printf("无法打开文件\n");
18         exit(1);
19     }
20     *n = 0;
21     while (fscanf(fp, "%d %s %s", &students[*n].id, students[*n].name, students[*n].class) == 3) {
22         (*n)++;
23     }
24     fclose(fp);
25 }
26 
27 // 随机抽取学生
28 void selectStudents(Student students[], int n, Student selected[], int m) {
29     int i, j, index;
30     srand((unsigned int)time(NULL));
31     for (i = 0; i < m; i++) {
32         index = rand() % n;
33         selected[i] = students[index];
34         for (j = index; j < n - 1; j++) {
35             students[j] = students[j + 1];
36         }
37         n--;
38     }
39 }
40 
41 // 显示学生信息
42 void displayStudents(Student selected[], int m) {
43     int i;
44     printf("随机抽取名单:\n");
45     for (i = 0; i < m; i++) {
46         printf("%d %s %s\n", selected[i].id, selected[i].name, selected[i].class);
47     }
48 }
49 
50 // 写入文件
51 void writeStudents(Student selected[], int m, char *filename) {
52     FILE *fp;
53     int i;
54     fp = fopen(filename, "w");
55     if (fp == NULL) {
56         printf("无法打开文件\n");
57         exit(1);
58     }
59     for (i = 0; i < m; i++) {
60         fprintf(fp, "%d %s %s\n", selected[i].id, selected[i].name, selected[i].class);
61     }
62     fclose(fp);
63 }
64 
65 int main() {
66     Student students[80];
67     Student selected[5];
68     int n;
69 
70     readStudents(students, &n);
71     selectStudents(students, n, selected, 5);
72     displayStudents(selected, 5);
73     writeStudents(selected, 5, "20241222.list");
74 
75     return 0;
76 }

 

运行结果:

 

posted on 2024-12-29 19:52  安宁的空白  阅读(16)  评论(0)    收藏  举报