B. Lord of the Values 思維數學建構
阿新 • • 發佈:2021-06-14
任務一
無
任務二
無
任務三
執行程式,在螢幕上正確輸出了按分數由高→低排序的資訊。
同時,在當前路徑下,生成了文字檔案file3.dat。
嘗試用記事本程式開啟檔案file3.dat,裡面的資料資訊是正確的,並且是直觀可讀的。
任務四
子任務1:
執行程式,在螢幕上正確輸出了按分數由高→低排序的資訊。
同時,在當前路徑下,生成了文字檔案file4.dat。
嘗試用記事本程式開啟檔案file4.dat,裡面的資料資訊不是正確的,並且不是直觀可讀的。
子任務2:
#include<stdio.h> #define N 10 typedef struct student{int num; char name[20]; int score; }STU; int main(){ FILE *fp; int i; STU stu[N]; fp = fopen("file4.dat","rb"); if(!fp){ printf("fail to open file4.dat\n"); return 0; } for(i=0;i<N;i++){ fread(&stu[i],sizeof(STU),N,fp); printf("%-6d %-10s %3d\n",stu[i].num,stu[i].name,stu[i].score); } fclose(fp);return 0; }
任務五
#include <stdio.h> #include <string.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; } void input(STU s[], int n) { int i; for(i=0;i<n;++i){ scanf("%ld %s %f %f",&s[i].id,s[i].name,&s[i].objective,&s[i].subjective); } } //輸出考生完整資訊: 准考證號,姓名,客觀題得分,操作題得分,總分,等級 void output(STU s[], int n) { int i; printf("准考證號 姓名 客觀題得分 操作題得分 總分 等級\n"); for(i=0;i<n;++i){ printf("%5ld %6s %8.2f %8.2f %8.2f %4s\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,j; for(i=0;i<n;++i) s[i].sum=(s[i].objective+s[i].subjective); STU temp; for(i=0;i<n;++i){ for(j=0;j<n-i-1;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(s[i].sum>95) strcpy(s[i].level,"優秀"); else if(s[i].sum>90&&s[i].sum<=95) strcpy(s[i].level,"合格"); else strcpy(s[i].level,"不合格"); } }