ex7

#include <stdio.h> 
#include <stdlib.h>
#define N 10 
typedef struct student
 { 
 int num; 
 char name[20];
  int score;
  }STU;
   int main()
    { 
    STU st, stmax, stmin; 
    int i;
     FILE *fp; 
     fp = fopen("file1.dat", "r");
      if( !fp ) { 
       printf("fail to open file1.dat\n"); 
       exit(0); }
       stmax.score = 0;
       stmin.score = 100 ; 
       for(i=0; i<N; i++) {
       fscanf(fp, "%d %s %d", &st.num, st.name, &st.score); 
       if(st.score > stmax.score) 
       stmax = st;
        else if(st.score < stmin.score)
         stmin = st;
         }
         fclose(fp);
          printf("最高分学生信息: %5d%15s%5d\n", stmax.num, stmax.name, stmax.score); 
          printf("最低分学生信息: %5d%15s%5d\n", stmin.num, stmin.name, stmin.score);
           return 0;
    }

 

// 将file1.txt中小写字母转换成大写后,另存为file2.txt 
#include <stdio.h> 
#include <stdlib.h>
 int main() { 
 FILE *fin, *fout; 
  int ch;
  fin = fopen("file1.txt", "r"); 
   if (fin == NULL) 
   { 
   printf("fail to open file1.txt\n");
    exit(0);
    }
    fout = fopen("file2.txt", "w"); 
     if (fout == NULL) 
     { 
     printf("fail to open or create file2.txt\n");
      exit(0);
      }
      while( !feof(fin) ) { ch = fgetc(fin);
       if(ch >= 'a' && ch <= 'z')
       ch -= 32; 
       fputc(ch, fout);
       }
       fclose(fin); 
       fclose(fout);
        return 0;
    }

只写

文本形式

任务3

#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;
 fin = fopen("file1.dat", "r"); 
 if( !fin ) { 
  printf("fail to open file1.dat\n");
  exit(0); 
  }
  for(i=0; i<N; i++) 
  fscanf(fin, "%d %s %d", &st[i].num, st[i].name, &st[i].score);
   fclose(fin);
    sort(st, N); 
     fout = fopen("file3.dat", "w");
      if( !fout ) { 
      printf("fail to open file1.dat\n"); 
      exit(0); } 
      for(i=0; i<N; i++) { 
      printf("%-6d%-10s%3d\n", st[i].num, st[i].name, st[i].score);
       fprintf(fout, "%-6d%-10s%3d\n", st[i].num, st[i].name, st[i].score);
        }
        fclose(fout); 
         return 0;
         }
         
         
         
  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;
            
            }
            } 

生成了file3,如图

 

任务4.2

#include <stdio.h> 
#include <stdlib.h>

#define N 10

// 定义一个结构体类型STU 
typedef struct student {
    int num;
    char name[20];
    int score;
}STU;


int main() {
    FILE *fin;
    STU st[N];
    int i;

    fin = fopen("file4.dat", "rb");
    if( !fin ) { 
        printf("fail to open file1.dat\n");
        exit(0);
    }
     
    fread(st,sizeof(STU),10,fin);
    fclose(fin);
    for(i=0; i<N; i++) 
        printf("%-6d%-10s%3d\n", st[i].num, st[i].name, st[i].score);
    
    return 0;
}

生成了file4.dat  数据不直观不可读

任务5

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
const int N = 10;
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;
} 
void input(STU s[], int n) {
    FILE *pt;
    int i;
    pt = fopen("examinee.txt","r");
    if (!pt) {
        printf("fail to open examinee.txt\n");
        exit(0);    
    } 
    for(i=0; i<n; i++) {
        fscanf(pt,"%ld %s %f %f ",&s[i].id,s[i].name,&s[i].objective,&s[i].subjective);
    }
    fclose(pt);
}
void output(STU s[], int n) {
    int i;
    FILE *fout;
    printf("准考证号     姓名    客观题得分    操作题得分   总分     等级\n");
    fout = fopen("result.tet","w");
    if (!fout) {
        printf("fail to open result.tet\n");
        exit(0);    
    } 
    for(i=0; i<n; i++) {
        printf("%-13d %-9s %-13.2f %-9.2f %-8.2f %-5s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level);
    }
    fprintf(fout,"准考证号     姓名    客观题得分    操作题得分   总分     等级\n");
    for(i=0; i<n; i++){
    fprintf(fout,"%-13d %-9s %-14.2f %-10.2f %-9.2f %-6s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level);
    }
    fclose(fout);
}
    

void process(STU s[], int n) {
    int i,j;
    STU temp;
    for(i=0; i<n; i++) {
        s[i].sum = s[i].objective+s[i].subjective;
    }
    for(i=0; i<n-1; i++) {
        for(j=0; j<n-1-i; j++) {
            if(s[j].sum<s[j+1].sum){
                temp = s[j];
                s[j] = s[j+1];
                s[j+1] = temp;
            }
        }
    }
    for(i=0; i<n; i++) {
        if((i+1)<=n*0.1)
            strcpy(s[i].level,"优秀");
        else if(((i+1)<=n*0.5)&&((i+1)>n*0.1))
            strcpy(s[i].level,"及格");
        else
            strcpy(s[i].level,"不及格"); 
    }
}

 

posted @ 2020-12-31 12:12  编程同学入魔了  阅读(40)  评论(2)    收藏  举报