实验7

4.

 1 #include <stdio.h>
 2 int main() 
 3 {
 4     char ch;
 5     int count=0; 
 6     //open file
 7     FILE *fp;
 8     
 9     fp = fopen("data4.txt", "r");
10     
11     if(fp == NULL) 
12     {
13     printf("fail to open file\n");
14         return 1;
15     }
16     //openfile end
17     ch = fgetc(fp);
18 
19     while(ch != EOF)
20      {
21         switch(ch)
22         {
23             case' ':
24                 ch = fgetc(fp);
25                 break;
26             case'\n':
27                 ch = fgetc(fp);    
28                 break;
29             default:
30                 {count++;
31                 ch = fgetc(fp);
32                 break;
33                 }
34         }
35     }
36         fclose(fp);
37         printf("共有%d个字符",count); 
38     return 0;
39 }

 

5.  1 #define _CRT_SECURE_NO_WARNINGS

  2 #include <stdio.h>
  3 #include <string.h>
  4 #include <stdlib.h>
  5 
  6 #define N 10
  7 
  8 typedef struct {
  9     long int id;
 10     char name[20];
 11     float objective;    // 客观题得分
 12     float subjective;   // 操作题得分
 13     float sum;
 14     char level[10];
 15 } STU;
 16 
 17 // 函数声明
 18 void input(STU s[], int n);
 19 void output(STU s[], int n);
 20 void process(STU s[], int n);
 21 
 22 int main() {
 23     STU stu[N];
 24 
 25     printf("从文件读入%d个考生信息: 准考证号,姓名,客观题得分(<=40),操作题得分(<=60)\n", N);
 26     input(stu, N);
 27 
 28     printf("\n对考生信息进行处理: 计算总分,确定等级\n");
 29     process(stu, N);
 30 
 31     printf("\n打印考生完整信息, 并保存到文件中");
 32     output(stu, N);
 33 
 34     return 0;
 35 }
 36 
 37 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
 38 void input(STU s[], int n) {
 39     int i;
 40     FILE *fin;
 41 
 42     fin = fopen("examinee.txt", "r");
 43     if (fin == NULL) {
 44         printf("fail to open file\n");
 45         exit(0);
 46     }
 47 
 48     while (!feof(fin)) {
 49         for (i = 0; i < n; i++)
 50             fscanf(fin, "%ld %s %f %f", &s[i].id, s[i].name, &s[i].objective, &s[i].subjective);
 51     }
 52 
 53     fclose(fin);
 54 }
 55 
 56 // 输出考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级
 57 // 不仅输出到屏幕上,还写到文本文件result.txt中
 58 void output(STU s[], int n) {
 59     FILE *fout;
 60     int i;
 61     
 62     // 输出到屏幕
 63     printf("\n");
 64     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n");
 65     for (i = 0; i < n; i++)
 66         printf("%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].level);
 67     
 68     // 保存到文件 
 69     fout = fopen("result.txt", "w");
 70     if (!fout) {
 71         printf("fail to open or create result.txt\n");
 72         exit(0);
 73     }
 74     
 75     fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n");
 76 
 77     for (i = 0; i < n; i++)
 78         fprintf(fout, "%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].level);
 79 
 80     fclose(fout);
 81 }
 82 
 83 // 对考生信息进行处理:计算总分,排序,确定等级
 84 void process(STU s[], int n) {
 85     float sum = 0;
 86     int i=0, j=0, k=0;
 87     int flag1 = 0.0, flag2 = 0.0;
 88     for (int i = 0; i < n; i++)
 89     {
 90         s[i].sum = s[i].objective + s[i].subjective;
 91     } //计算总分
 92   //排序
 93     for (int i = 0; i < n; i++)
 94     {
 95         for (int j = 0; j < n - i - 1; j++)
 96         {
 97             if (s[j].sum < s[j + 1].sum)
 98             {
 99                 STU temp = s[j];
100                 s[j] = s[j + 1];
101                 s[j + 1] = temp;
102             }
103         }
104     }
105     flag1 = (int)((float)(n) / 10);
106     flag2 = (int)((float)(n) / 2);
107     //赋予等级
108     
109     
110     for (i = 0; i < n; i++)
111             {
112                if (i < flag1)
113                   {strcpy(s[i].level, "优秀");
114                 }
115                else {
116            
117                             if (i >= flag2)
118                            strcpy(s[i].level, "不及格");
119             
120                           else
121                            strcpy(s[i].level, "合格");
122            
123       }
124         
125       }
126     
127     
128 }

 

6.

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #define TOTAL 80   //学生人数
 3 #define NUM 5
 4 #include <stdlib.h>
 5 #include <stdio.h>
 6 #include <time.h>
 7 void readFile();
 8 void saveFile();
 9 void Namedandsavefile(int num);
10 struct Student
11 {
12     int num;
13     char name[30];  
14     char clas[40];
15 } stu[TOTAL];       //整个班级的学生信息数组
16 void readFile()
17 {
18     int i; 
19     FILE* fp = fopen("list.txt", "rb");
20     if (fp == NULL)
21     {
22         printf("打开文件失败\n");
23         exit(1);
24     }
25     else
26     {
27         for ( i = 0; i < TOTAL; i++)
28         {
29             fscanf(fp, "%d %s %s\n", &stu[i].num, &stu[i].name, &stu[i].clas);
30         }
31     }
32     fclose(fp);
33 }
34 
35 
36 void Namedandsavefile(int num)
37 {
38     int id = -1,k=0,i=0;
39     int no[NUM];
40     //配置随机数
41     srand((unsigned)time(NULL));
42 
43     printf("\n开始点名\n");
44     //点名功能
45    
46         for ( i = 0; i < 5; i++)
47         {
48            
49                 id = rand() % 80;
50                 printf("%d %s %s\n",stu[id].num, stu[id].name, stu[id].clas);
51                 printf("\n");
52                 no[i]=id;
53         }
54        
55     FILE *fout;
56     
57     fout=fopen("lucky.txt","w");
58 
59     if(!fout)
60         {
61         printf("fail to create txt\n");
62         exit(0);
63         }
64 
65     for (i = 0; i < NUM; i++)
66         fprintf(fout,"%d %s %s\n",stu[no[i]].num, stu[no[i]].name, stu[no[i]].clas);
67 
68 
69     fclose(fout);
70         
71     
72 }
73 int main()
74 {
75     int num = 0;
76     char c='\0';
77     printf("点名程序 按Q退出 按其他开始 \n");
78     if((c=getchar())!='Q') 
79     {
80         
81         readFile();//读取文件
82         Namedandsavefile(NUM); //点名函数并保存 
83      } 
84     else
85         exit(1);
86 
87     return 0;
88 }

 

posted @ 2023-06-08 17:07  Orangpetofi  阅读(29)  评论(0)    收藏  举报