1. 程式人生 > 其它 >實驗7 結構體和檔案

實驗7 結構體和檔案

實驗7 結構體和檔案 實驗一
#include <stdio.h>
#include <string.h>
#define N 5
// 定義結構體型別struct student,並定義STU為其別名
typedef struct student {
    long no;
    char name[20];
    int score;
} STU; // 函式宣告

void input(STU s[], int n);
int findMinlist(STU s[], STU t[], int n);
void output(STU s[], int n);

int
main() { STU stu[N], minlist[N]; int count; printf("錄入%d個學生資訊\n", N); input(stu, N); printf("\n統計最低分人數和學生資訊...\n"); count = findMinlist(stu, minlist, N); printf("\n一共有%d個最低分,資訊如下:\n", count); output(minlist, count); return 0; }// 輸入n個學生資訊,存放在結構體陣列s中 void input(STU s[], int
n) { int i; for (i = 0; i < n; i++) scanf("%ld %s %d", &s[i].no, s[i].name, &s[i].score); }// 輸出結構體s中n個元素資訊 void output(STU s[], int n) { int i; for (i = 0; i < n; i++) printf("%ld %s %d\n", s[i].no, s[i].name, s[i].score); } // 在結構體陣列s中,查詢最低分學生的記錄,將其存入結構體陣列t中
// 形參n是結構體陣列s中元素個數 // 函式返回最低分的學生人數 int findMinlist(STU s[], STU t[], int n) { int num = 0,i = 0,score = 1e8 ; for(i=0;i<n;++i){ if(s[i].score < score) score = s[i].score; } printf("\n***score=%d***\n",score); for(int i=0;i<n;++i){ if(s[i].score==score){ t[num].score=s[i].score; memcpy(t[num].name,s[i].name,sizeof t[num].name); t[num].no = s[num].no; num ++; } } return num; }

實驗二

#include <stdio.h>
#include <string.h>
#define 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;
}// 錄入考生資訊:准考證號,姓名,客觀題得分,操作題得分
void input(STU s[], int n) {
    int i=0;
    for(i=0;i<n;++i){
        scanf("%d%s%f%f",&s[i].id,s[i].name,&s[i].objective,&s[i].subjective);
    } 
}
void output(STU s[], int n) {
    int i=0;
    printf("准考證號\t姓名\t客觀題得分\t操作題得分\t總分\t等級\n");
    for(i=0;i<n;++i){
        printf("%d\t%s\t%.2f\t%.2f\t%.2f\t%s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level);
    } 
}

void swap(STU *a,STU *b){
    STU tmp;
    tmp.id = a->id; memcpy(tmp.name,a->name,sizeof tmp.name);
    tmp.objective = a->objective; tmp.subjective = a->subjective;
    tmp.sum = a->sum; memcpy(tmp.level,a->level,sizeof tmp.level);
    
    a->id = b->id; memcpy(a->name,b->name,sizeof a->name);
    a->objective = b->objective; a->subjective = b->subjective;
    a->sum = b->sum; memcpy(a->level,b->level,sizeof a->level);
    
    b->id = tmp.id; memcpy(b->name,tmp.name,sizeof tmp.name);
    b->objective = tmp.objective; b->subjective = tmp.subjective;
    b->sum = tmp.sum; memcpy(b->level,tmp.level,sizeof tmp.level);
}

void process(STU s[], int n) { // 補足程式碼 // ×××
    int j=0,i=0;
    double good = 0.1, med = 0.5;
    char gd[10] =  "優秀";char md[10]="合格"; char bd[10]="不合格";
    for(i=0;i<n;++i){
        s[i].sum = s[i].objective + s[i].subjective;
    }
    for(i=0;i<n;++i) for(j=i+1;j<n;++j) if(s[i].sum > s[j].sum){
        swap(&s[i],&s[j]);
    }
    for(int i=0;i<n;++i){
        if((double)(i+1)/n<=good) memcpy(s[i].level,gd,sizeof gd);
        else if((double)(i+1)/n<=med) memcpy(s[i].level,md,sizeof md);
        else memcpy(s[i].level,bd,sizeof bd);
    }
}

實驗三

#include <stdio.h>
#include <string.h>
#define 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() {
    freopen("examinee.txt", "r", stdin);
    freopen("list.txt", "w", stdout); 
    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) {
//    freopen(""); 
    int i=0;
    for(i=0;i<n;++i){
        scanf("%d%s%f%f",&s[i].id,s[i].name,&s[i].objective,&s[i].subjective);
    } 
}
void output(STU s[], int n) {
    int i=0;
    printf("准考證號  姓名  客觀題得分  操作題得分  總分  等級\n");
    for(i=0;i<n;++i){
        printf("%d\t %s\t%.2f\t%.2f\t%.2f\t%s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level);
    } 
}

void swap(STU *a,STU *b){
    STU tmp;
    tmp.id = a->id; memcpy(tmp.name,a->name,sizeof tmp.name);
    tmp.objective = a->objective; tmp.subjective = a->subjective;
    tmp.sum = a->sum; memcpy(tmp.level,a->level,sizeof tmp.level);
    
    a->id = b->id; memcpy(a->name,b->name,sizeof a->name);
    a->objective = b->objective; a->subjective = b->subjective;
    a->sum = b->sum; memcpy(a->level,b->level,sizeof a->level);
    
    b->id = tmp.id; memcpy(b->name,tmp.name,sizeof tmp.name);
    b->objective = tmp.objective; b->subjective = tmp.subjective;
    b->sum = tmp.sum; memcpy(b->level,tmp.level,sizeof tmp.level);
}

void process(STU s[], int n) { // 補足程式碼 // ×××
    int j=0,i=0;
    double good = 0.1, med = 0.5;
    char gd[10] =  "優秀";char md[10]="合格"; char bd[10]="不合格";
    for(i=0;i<n;++i){
        s[i].sum = s[i].objective + s[i].subjective;
    }
    for(i=0;i<n;++i) for(j=i+1;j<n;++j) if(s[i].sum > s[j].sum){
        swap(&s[i],&s[j]);
    }
    for(int i=0;i<n;++i){
        if((double)(i+1)/n<=good) memcpy(s[i].level,gd,sizeof gd);
        else if((double)(i+1)/n<=med) memcpy(s[i].level,md,sizeof md);
        else memcpy(s[i].level,bd,sizeof bd);
    }
}