实验7

 1 // 将file1.txt中小写字母转换成大写后,另存为file2.txt 
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 
 5 int main() {
 6     FILE *fin, *fout; // 定义文件类型指针
 7     int ch;
 8     
 9     fin = fopen("file1.txt", "r"); // 以只读文本方式打开文件file1.txt
10     if (fin == NULL) {
11         printf("fail to open file1.txt\n");
12         exit(0);    
13     } 
14     
15     fout = fopen("D:\\学习\\c\\实验7\\7.1\\file3.txt", "w"); // 以写文本方式打开文件file2.txt, 如果文件不存在,就创建一个
16     if (fout == NULL) {
17         printf("fail to open or create file2.txt\n");
18         exit(0);
19     } 
20     
21     while( !feof(fin) ) {
22         ch = fgetc(fin);  // 从fin指向的文件file1.txt中读取单个字符,暂存在字符变量ch中 
23         
24         if(ch >= 'a' && ch <= 'z')  // 如果是小写字母,则转换成大写 
25             ch -= 32;
26         
27         fputc(ch, fout); // 将字符变量ch中的字符写入fout指向的文件file2.txt中 
28     }
29     
30     fclose(fin);    
31     fclose(fout);
32     
33     return 0;
34 }

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include<stdlib.h>
  4 #define N 10
  5 
  6 // 定义结构体类型struct student,并定义其别名为STU 
  7 typedef struct student {
  8     long int id;
  9     char name[20];
 10     float objective;    /*客观题得分*/
 11     float subjective;    /*操作题得分*/
 12     float sum;
 13     char level[10];    
 14 }STU; 
 15 
 16 // 函数声明
 17 void input(STU s[], int n);
 18 void output(STU s[], int n);
 19 void process(STU s[], int n);
 20 
 21 int main() {
 22     STU stu[N];
 23     
 24     printf("录入%d个考生信息: 准考证号,姓名,客观题得分(<=40),操作题得分(<=60)\n", N); 
 25     input(stu, N);
 26     
 27     printf("\n对考生信息进行处理: 计算总分,确定等级\n");
 28     process(stu, N);
 29     
 30     printf("\n打印考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级\n");
 31     output(stu, N); 
 32     
 33     return 0;
 34 } 
 35 
 36 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
 37 void input(STU s[], int n) {
 38     // 补足代码
 39     FILE *fp1;
 40     
 41     int i;
 42     fp1=fopen("D:\\学习\\c\\实验7\\7.5\\examinee.txt","r");
 43     if(!fp1){
 44         printf("cannot open");
 45         exit(0);
 46         }
 47     
 48     for(i=0;i<n;i++){
 49         
 50         fscanf(fp1,"%ld %s %f %f",&s[i].id,s[i].name,&s[i].objective,&s[i].subjective);//输入整型,输出浮点型结果是0.0 
 51     }
 52     
 53     fclose(fp1);
 54     
 55 }
 56 
 57 // 输出考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级
 58 // 不仅输出到屏幕上,还写到文本文件result.txt中 
 59 void output(STU s[], int n) {
 60     // 补足代码
 61      FILE *fp2;
 62      fp2=fopen("result.txt","w");
 63      if(!fp2){
 64          printf("Error\n");
 65          exit(0);
 66      }
 67      
 68      
 69      
 70      
 71      int i;
 72      for(i=0;i<n;i++){
 73      
 74      printf("%20ld %10s %5.1f %5.1f %5.1f %10s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level);
 75      fprintf(fp2,"%20ld %10s %5.1f %5.1f %5.1f %10s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level);
 76 
 77 
 78 }
 79        
 80        
 81        
 82        
 83        
 84        fclose(fp2);
 85        
 86 
 87 
 88 
 89 }
 90 
 91 // 对考生信息进行处理:计算总分,排序,确定等级
 92 void process(STU s[], int n) {
 93     // 补足代码
 94     int i,j;
 95     STU temp;
 96     
 97     
 98     for(i=0;i<n;i++){
 99         
100         s[i].sum=s[i].objective+s[i].subjective;
101         
102     }
103     
104     
105     for(i=0;i<n-1;i++){
106         for(j=0;j<n-1-i;j++){
107             if(s[j+1].sum>s[j].sum){
108             
109             temp=s[j];
110             s[j]=s[j+1];
111             s[j+1]=temp;
112             
113             
114         }
115     }
116 }
117             
118             for(i=0;i<n;i++){
119                 
120                 if(i<=(0.1*n-1))
121                    strcpy(s[i].level,"nice");
122                 else if(i>(0.1*n-1)&&i<=(0.5*n-1))
123                    strcpy(s[i].level,"good");
124                 else
125                    strcpy(s[i].level,"bad");
126                    
127                 
128                 
129                 
130                 
131             }
132         }
133         
134         
135         
136     
137         
138         
139     
140         
141         

posted @ 2021-06-15 16:00  Jackynuist  阅读(31)  评论(1)    收藏  举报