| 函數 | 功能 | 參數 |
| input() | 輸入學生資料 | stud, 4 |
| aver() | 計算各生平均分存入 score[3] | stud, 4 |
| order() | 冒泡排序依 score[3] 遞增 | stud, 4 |
| output() | 表格輸出所有學生資料 | stud, 4 |
| out_row() | 計算並輸出各科平均與總平均 | stud, 4 |
核心函數實作詳解
1. 輸入函數 input()
void input(struct stu arr[], int n) {
int i, j;
char temp[30];
for (i = 0; i < n; i++) {
printf("\nInput Name,Number,English,Math,Physics\n");
gets(arr[i].name);
gets(temp);
arr[i].number = atol(temp); // 字串轉長整數
for (j = 0; j < 3; j++) {
gets(temp);
arr[i].score[j] = atof(temp); // 字串轉浮點數
}
}
}
關鍵:結構體成員型態不一,採字串輸入 + 型態轉換函數:
atoi:字串 → int
atol:字串 → long
atof:字串 → double
需 #include 。
2. 平均計算 aver()
void aver(struct stu arr[], int n) {
int i, j;
for (i = 0; i < n; i++) {
arr[i].score[3] = 0;
for (j = 0; j < 3; j++)
arr[i].score[3] += arr[i].score[j];
arr[i].score[3] /= 3;
}
}
3. 排序函數 order() - 冒泡法
void order(struct stu arr[], int n) {
struct stu temp;
int i, j;
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - 1 - i; j++)
if (arr[j].score[3] > arr[j+1].score[3]) { // 比較平均分
temp = arr[j]; // 結構體整體指派(合法!)
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
重要特性:結構體變數允許整體指派(temp = arr[j]),但不允許整體輸入/輸出(需逐成員)。
4. 表格輸出 output()
void output(struct stu arr[], int n) {
int i, j;
printf("********************TABLE********************\n");
printf("------------------------------------------------\n");
printf("| %10s | %8s | %7s | %7s | %7s | %7s |\n",
"Name", "Number", "English", "Math", "Physics", "Average");
printf("------------------------------------------------\n");
for (i = 0; i < n; i++) {
printf("| %10s | %8ld |", arr[i].name, arr[i].number);
for (j = 0; j < 4; j++)
printf(" %7.2f |", arr[i].score[j]);
printf("\n------------------------------------------------\n");
}
}
5. 各科平均統計 out_row()
void out_row(struct stu arr[], int n) {
float row[4] = {0};
int i, j;
for (i = 0; i < 4; i++) {
for (j = 0; j < n; j++)
row[i] += arr[j].score[i];
row[i] /= n;
}
printf("|%19c|", ' ');
for (i = 0; i < 4; i++)
printf(" %7.2f |", row[i]);
printf("\n------------------------------------------------\n");
}
邏輯:row[0~2] 為三科平均,row[3] 為總平均(即各生平均之平均)。
主程式整合
#include
#include
struct stu {
char name[20];
long number;
float score[4];
};
void input(struct stu[], int);
void aver(struct stu[], int);
void order(struct stu[], int);
void output(struct stu[], int);
void out_row(struct stu[], int);
main() {
struct stu stud[4];
input(stud, 4);
aver(stud, 4);
order(stud, 4);
output(stud, 4);
out_row(stud, 4);
}
關鍵技術總結
- 結構體陣列:
struct stu stud[4] 管理多筆異質資料
- 平均分內嵌:
score[3] 存平均,排序直接比較成員
- 字串輸入轉型:
gets + atoi/atol/atof 處理異質成員輸入
- 結構體整體指派:排序交換時
temp = arr[j] 一次複製所有成員
- 陣列傳函數:形參
struct stu arr[] 不指定大小,接收首元素位址
- 表格化輸出:
printf 格式控制寬度、精度、對齊
執行結果示意
********************TABLE********************
------------------------------------------------
| Name | Number | English | Math | Physics | Average |
------------------------------------------------
| Amy | 100001 | 85.00 | 92.00 | 78.00 | 85.00 |
------------------------------------------------
| Bob | 100002 | 76.00 | 88.00 | 90.00 | 84.67 |
------------------------------------------------
| Cat | 100003 | 92.00 | 85.00 | 88.00 | 88.33 |
------------------------------------------------
| Dan | 100004 | 88.00 | 90.00 | 92.00 | 90.00 |
------------------------------------------------
| | 85.25 | 88.75 | 87.00 | 87.00 |
------------------------------------------------
- 上一篇文章: C語言指標陣列完全指南:定義、應用場景、字串排序與動態記憶體配置
- 下一篇文章: C語言結構體陣列排序與統計:冒泡排序、表格輸出、欄位平均完整實作
Copyright ©
程式碼之家 All Rights Reserved