实验七

实验三

输出了从高到低的数 而且生成了file3.dat文件 正确而且可读

实验四

#include <stdio.h> 
#include <stdlib.h>
 #define N 10 // 定义一个结构体类型STU 
 typedef struct student { 
 int num;
  char name[20];
   int score;
    }STU;
     void sort(STU *pst, int n);
      // 函数声明 
      int main() { 
      FILE *fin, *fout; 
      STU st[N];
       int i; // 以只读文本方式打开文件file1.dat
        fin = fopen("file1.dat", "r"); 
        if( !fin ) {
         // 如果打开失败,则输出错误提示信息,然后退出程序 
         printf("fail to open file1.dat\n"); exit(0); }
// 从fin指向的数据文件file1.dat中读取数据到结构体数组st 
for(i=0; i<N; i++) 
fscanf(fin, "%d %s %d", &st[i].num, st[i].name, &st[i].score); 
fclose(fin); // 关闭fin指向的文件file1.dat
sort(st, N); // 以写方式打开/创建二进制文件file4.dat
 fout = fopen("file4.dat", "wb"); 
 if( !fout ) {
  // 如果打开失败,则输出错误提示信息,然后退出程序
   printf("fail to open file1.dat\n"); 
   exit(0);
    }// 将排序后的数组st中数据输出到屏幕 
    for(i=0; i<N; i++) 
    printf("%-6d%-10s%3d\n", st[i].num, st[i].name, st[i].score);
     // 将排序后的数组st中数据写到二进制文件file4.dat
      fwrite(st, sizeof(STU), N, fout); 
      // 将从地址st开始的sizeof(STU)×N个 字节信息写入fout指向的文件file4.dat中
       fclose(fout); // 关闭fout指向的文件file4.dat
        return 0;
         }// 函数功能描述:对pst指向的n个STU结构体数据进行排序,按成绩数据项由高到底排序 // 排序算法:选择排序算法
          void sort(STU *pst, int n)
           { STU *pi, *pj, t; 
           for(pi = pst;
            pi < pst+n-1;
             pi++) 
for(pj = pi+1; 
pj < pst+n; pj++) 
if(pi->score < pj->score) { 
t = *pi; 
*pi = *pj;
 *pj = t; 
 } 
 }

 

 

#include <stdio.h>
 #include <string.h>
#include<stdlib.h>
  const int N = 10; // 定义结构体类型struct student,并定义其别名为STU
   typedef struct student {
    long int id;
     char name[20];
      float objective;
       /*客观题得分*/ 
       float subjective; /*操作题得分*/
        float sum; 
        char level[10];
         }STU; // 函数声明 
         void input(STU s[], int n);
          void output(STU s[], int n); 
          void process(STU s[], int n); 
          int main() { 
          STU stu[N];
           printf("录入%d个考生信息: 准考证号,姓名,客观题得分(<=40),操作题得分 (<=60)\n", N); 
           input(stu, N);
            printf("\n对考生信息进行处理: 计算总分,确定等级\n");
             process(stu, N); 
             printf("\n打印考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级 \n");
             output(stu, N);
              return 0; 
              }
              // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 
             void input(STU s[], int n) { 
             // 补足代码 
             // ×××
            FILE *fin;
    int i;
    fin=fopen("examinee.txt","r");
    if(!fin){
        printf("fail to open examinee.txt\n");
        exit(0);
    }
    for(i=0;i<n;i++)
    fscanf(fin,"%ld %s %f %f",&s[i].id,s[i].name,&s[i].objective,&s[i].subjective);
    fclose(fin);
}
              // 输出考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级 
              // 不仅输出到屏幕上,还写到文本文件result.txt中
              void output(STU s[], int n) 
{
// 补足代码 
// ×××
  FILE *fout;
    int i,j;
    fout=fopen("result.txt","w");
    if(!fout){
        printf("fail to open result.txt\n");
        exit(0);
}
    printf("准考证号      姓名      客观题得分      操作题得分      总分       等级\n");
    for(i=0;i<n;i++)
        printf("%ld %14s %13.2f %13.2f %13.2f %10s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level);
       for(j=0;j<n;j++)
    fprintf(fout,"%ld %s %f %f %f %s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level); 
}
 // 对考生信息进行处理:计算总分,排序,确定等级 
void process(STU s[], int n) { 
// 补足代码
 // ××× 
int i;
    STU *x, *y, t;
    for(i=0;i<n;i++){
    s[i].sum=s[i].objective+s[i].subjective;
    }
    for(x = s; x < s+n-1; x++)
        for(y = x+1; y < s+n; y++) 
            if(x->sum< y->sum) {
                t = *x;
                *x = *y;
                *y = t; 
            }
    for(i=0;i<n;i++){
       if(i==0)
       strcpy(s[i].level,"优秀");
    else if(i>0&&i<5)
    strcpy(s[i].level,"合格");
    else
    strcpy(s[i].level,"不及格"); 
   }    
}

 

posted @ 2020-12-31 13:41  Zzzch  阅读(85)  评论(1编辑  收藏  举报