實驗六不曉得是啥相關程式碼反正就是實驗六
阿新 • • 發佈:2020-12-23
實驗任務一
// P280例8.15 // 對教材上的程式作了微調整,把輸出學生資訊單獨編寫成一個函式模組 // 列印不及格學生資訊和所有學生資訊程分別呼叫 #include<stdio.h> #include<stdlib.h> #include<string.h> #define N 3 // 執行程式輸入測試時,可以把N改小一些輸入測試 typedef struct student { int id; /*學生學號 */ char name[20]; /*學生姓名 */ char subject[20]; /*考試科目 */ float perf; /*平時成績 */ float mid; /* 期中成績 */ float final; /* 期末成績 */ float total; /* 總評成績 */ char level[10]; /* 成績等級 */ } STU; void input(STU[],int ); /*輸入學生資訊 */ void calc(STU[],int); /*計算總評和等級 */ int fail(STU[],STU [],int); /*不及格學生統計 */ void sort(STU[],int); /*排序 */ void print(STU[], int); /*輸出學生資訊*/ int main() { STU st[N],fst[N]; // 陣列st記錄學生資訊,fst記錄不及格學生資訊 int k; // 用於記錄不及格學生個數 printf("錄入學生成績資訊:\n"); input(st,N); printf("\n成績處理...\n"); calc(st,N); k= fail(st,fst,N); sort(st, N); printf("\n學生成績排名情況:\n"); print(st, N); printf("\n不及格學生資訊:\n"); print(fst, k); return 0; } // 輸入學生資訊 void input(STU s[],int n) { int i; for(i=0;i<n;i++) scanf("%d %s %s %f %f %f",&s[i].id,s[i].name,s[i].subject,&s[i].perf,&s[i].mid,&s[i].final); } // 計算總評和等級 void calc(STU s[],int n) { int i; for(i=0;i<n;i++) { s[i].total=s[i].perf*0.2+s[i].mid*0.2+s[i].final*0.6; if(s[i].total>=90) strcpy(s[i].level,"優"); // 注意:等級是字元型陣列,要使用strcpy完成賦值 else if(s[i].total>=80 && s[i].total<90) strcpy(s[i].level,"良"); else if(s[i].total>=70 && s[i].total<80) strcpy(s[i].level,"中"); else if(s[i].total>=60 && s[i].total<70) strcpy(s[i].level,"及格"); else strcpy(s[i].level,"不及格"); } } // 不及格學生統計 // 陣列s存放的是所有學生資訊,陣列t存放不及格學生資訊,n是陣列s中元素個數 // 函式返回值:返回的是不及格人數 int fail(STU s[],STU t[],int n) { int i,k=0; for(i=0;i<n;i++) if(s[i].total<60) t[k++]=s[i]; return k; } // 根據總評成績對學生記錄資訊排序 // 使用的是氣泡排序演算法 void sort(STU s[],int n) { int i,j; STU temp; for(i=0;i<n-1;i++) for(j=0;j<n-1-i;j++) if(s[j].total<s[j+1].total) { temp = s[j]; s[j] = s[j+1]; s[j+1] = temp; } } // 輸出學生資訊 void print(STU s[], int n) { int i; printf("-----------------\n"); printf("學號 姓名 考試科目 平時成績 期中成績 期末成績 總評成績 成績等級\n"); for(i=0;i<n;i++) printf("%5d%5s%8s %8.1f %8.1f %8.1f %8.1f %10s\n",s[i].id,s[i].name,s[i].subject,s[i].perf,s[i].mid,s[i].final,s[i].total,s[i].level); }
總結:
1.為使成績顯示介面更加清晰可視,因多次除錯控制好輸出間距;
2.為方便使用者操作,可以在第一行加上註釋,為需要輸入的資料的輸入順序;
3.學生學號最好使用字串輸入,避免出現首位為0的情況。
實驗任務二
#include <stdio.h> const int 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 i,j=0; int min=s[0].score; for(i=0;i<n;i++) { if(s[i].score<min){ min = s[i].score; } } for(i=0;i<n;i++){ if(s[i].score==min){ t[j]=s[i]; j++; } } return j; }
實驗任務三
#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) { for (int 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) { for (int i = 0; i < n; i++) { printf("%20ld %10s %12f %10f %10f %10s\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) { for (int i = 0; i < n; i++) { s[i].sum = s[i].objective + s[i].subjective; if (s[i].sum < 60) { strcpy(s[i].level, "不合格"); } else if (s[i].sum < 80) { strcpy(s[i].level, "合格"); } else { strcpy(s[i].level, "優秀"); } } for (int i = 0; i < n - 1; i++) { STU temp ; for (int 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; } } } }