第七次實驗

ex2函數

1 #include <stdio.h>
  2 #include <string.h>
  3 const int N = 10;
  4 
  5 // 定義結構體類型struct student,並定義其別名爲STU 
  6 typedef struct student {
  7     long int id;
  8     char name[20];
  9     float objective;    /*客觀題得分*/
 10     float subjective;    /*操做題得分*/
 11     float sum;
 12     char level[10];    
 13 }STU; 
 14 
 15 // 函數聲明
 16 void input(STU s[], int n);
 17 void output(STU s[], int n);
 18 void process(STU s[], int n);
 19 
 20 int main() {
 21     STU stu[N];
 22     
 23     printf("錄入%d個考生信息: 准考證號,姓名,客觀題得分(<=40),操做題得分(<=60)\n", N); 
 24     input(stu, N);
 25     
 26     printf("\n對考生信息進行處理: 計算總分,肯定等級\n");
 27     process(stu, N);
 28     
 29     printf("\n打印考生完整信息: 准考證號,姓名,客觀題得分,操做題得分,總分,等級\n");
 30     output(stu, N); 
 31     
 32     return 0;
 33 } 
 34 
 35 // 從文本文件examinee.txt讀入考生信息:准考證號,姓名,客觀題得分,操做題得分
 36 void input(STU s[], int n) {
 37     // 補足代碼
 38     FILE *fp;
 39     int i;
 40     
 41     fp = fopen("examinee.txt", "r"); 
 42     if (fp == NULL) {
 43         printf("fail to open examinee.txt\n");    
 44     } 
 45     
 46     for(i=0; i<n; i++){ 
 47         fscanf(fp, "%d %s %f %f", &s[i].id, s[i].name, &s[i].objective, &s[i].subjective);
 48                                                                                
 49     } 
 50     fclose(fp);
 51 }
 52 
 53 // 對考生信息進行處理:計算總分,排序,肯定等級
 54 void process(STU s[], int n) {
 55     // 補足代碼
 56     STU t;
 57     int i;
 58     
 59     for(i=0; i<n; i++)
 60     {
 61         s[i].sum=s[i].objective+s[i].subjective;
 62     }
 63     
 64     for(i=0; i<n; i++)
 65       for(j=0;j<n-i-1;j++)
 66         if(s[j].sum<s[j+1].sum)
 67          {
 68           t=s[j];
 69           s[j]=s[j+1];
 70           s[j+1]=t;
 71          }
 72 
 73  for(i=0;i<n;i++)
 74     {
 75         if((i+1)<=n*0.1)
 76           strcpy(s[i].level,"優秀"); 
 77         else if((i+1)>n*0.1&&(i+1)<=n*0.5)
 78           strcpy(s[i].level,"合格");
 79         else
 80           strcpy(s[i].level,"不合格");    
 81     }
 82 }
 83 
 84 // 輸出考生完整信息: 准考證號,姓名,客觀題得分,操做題得分,總分,等級
 85 // 不只輸出到屏幕上,還寫到文本文件result.txt中 
 86 void output(STU s[], int n) {
 87     // 補足代碼
 88      FILE *fp;
 89      int i;
 90     
 91     fp = fopen("result.txt", "w"); 
 92     if (fp == NULL) {
 93         printf("fail to open result.txt\n");   
 94     } 
 95     
 96     for(i=0; i<n; i++) {
 97         printf("%d %s %.2lf %.2lf %.2lf %s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].level);
 98         fprintf(fp, "%d %s %.2lf %.2lf %.2lf %s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].level);
 99     }
100     
101     fclose(fp);
102 }

  

誒呀最後截止日期快到了纔想起來作,太匆忙了3d

讀二進制文件blog

#include <stdio.h>
#include <stdlib.h>
#define N 10

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

  

相關文章
相關標籤/搜索