实验7

Part1: 验证性实验

  • 验证性实验2   更改后运行结果正确
     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:\\file2.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 }
    验证实验2

     

  • 对比验证性实验3和验证性实验4的程序源码及运行结果,总结比较二进制文件与文本文件的区别。 

观察file3与file4可以找到端倪,但是二进制存储方式不可直接可读。

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 #define N 10
 5 
 6 typedef struct student {
 7     int num;
 8     char name[20];
 9     int score;
10 }STU;
11 
12 int main() {
13     FILE *fin;
14     int i;
15     STU st[N];
16     
17     fin = fopen("file4.dat", "rb"); // 以只读文本方式打开文件file4.txt
18     if (fin == NULL) {
19         printf("fail to open file\n");
20         exit(0);    
21     } 
22 while(!feof(fin)){
23     fread(&st[i],sizeof(struct student),1,fin); 
24     printf("%-6d %-10s %3d\n", st[i].num, st[i].name, st[i].score);
25     i++;
26 }
27     fclose(fin);
28     
29 
30     }
读取file4.dat

代码最后一行出现一串奇怪的数字?这是为什么呢?

 

Part2: 编程练习

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 const int 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     FILE *fp;
 39     fp = fopen("examinee.txt", "r");
 40     if( !fp ) { // 如果打开失败,则输出错误提示信息,然后退出程序
 41     printf("fail to open examinee.txt\n");
 42     exit (0);
 43 }
 44     int i;
 45     for(i=0;i<n;i++){
 46         //从fp指定的文件中格式化读取学生信息
 47         if(fscanf(fp,"%ld %s %f %f",&s[i].id,s[i].name,&s[i].objective,&s[i].subjective)==0) 
 48            printf("读取错误!"); 
 49          
 50     }
 51     fclose(fp);  //关闭文件是个好习惯
 52 }
 53 
 54 // 输出考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级
 55 // 不仅输出到屏幕上,还写到文本文件result.txt中 
 56 void output(STU s[], int n) {
 57     int i;
 58     FILE *fout;
 59     //只以写文本方式打开 
 60     fout = fopen("result.txt", "w");
 61 
 62  
 63 //在屏幕和文件中打印表头    
 64     printf("准考证号    姓名    客观题得分  操作题得分    总分     等级  \n ");
 65     fprintf(fout,"准考证号   姓名   客观题得分  操作题得分   总分    等级\n");
 66     
 67     for(i=0;i<n;i++){  //使用循环输出数据
 68         printf("%-9ld%-10s%-12.2f%-12.2f%-10.2f%-8s\n",
 69         s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level);
 70         fprintf(fout,"%-9ld%-10s%-1.2f%-12.2f%-10.2f%-8s\n",
 71         s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level);
 72 }
 73 fclose(fout);
 74 }
 75 
 76 // 对考生信息进行处理:计算总分,排序,确定等级
 77 void process(STU s[], int n) {
 78     int i,j;
 79     int high=0,mid=0;
 80     STU temp;
 81     
 82     for(i=0;i<n;i++){
 83         s[i].sum=s[i].objective + s[i].subjective;    
 84     }
 85     for(i=0;i<n-1;i++){ //使用冒泡排序法对成绩做排名
 86         for(j=0;j<n-1-i;j++)
 87            if(s[j].sum<s[j+1].sum){
 88                temp=s[j];
 89                s[j]=s[j+1];
 90                s[j+1]=temp;
 91            }
 92            
 93     }
 94     high=(int)n*0.1;mid=(int)n*0.5;//进行等级划分
 95     
 96     for(i=0;i<high-1;i++)  //因为数组第一个数字是0
 97     strcpy(s[i].level,"优秀");
 98     
 99     for(i=high-1;i<mid-1;i++) 
100     strcpy(s[i].level,"合格");
101     
102     for(i=mid-1;i<n;i++)
103     strcpy(s[i].level,"不合格"); 
104 }
编程练习(冒泡法排序)

直接在d盘中创建file2,老师给的源代码可能是修改后未更正,导致我在第一次试验中的原路径里未找到文件。

实验总结:

  1. 库函数遗漏
  2. 打印出的结果不美观,调整字节数
  3. 阅读源程序粗心

 

https://www.cnblogs.com/zys-0119/p/11046073.html#4283965

https://www.cnblogs.com/weiyuyang/p/11046961.html#4283960

https://www.cnblogs.com/aoliaoliao/p/11050192.html#4283957

posted @ 2019-06-20 15:35  yy-Siri  阅读(179)  评论(2编辑  收藏  举报