Created
January 3, 2018 15:22
-
-
Save goodjack/5805cce01acaff3fc69f8b96d5d64975 to your computer and use it in GitHub Desktop.
成績查詢系統 參考解答
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main() { | |
cout << "歡迎進入成績查詢系統" << endl; | |
cout << "==============================" << endl; | |
int classSize; | |
cout << "班級人數:"; | |
cin >> classSize; | |
int numbersOfSubject; | |
cout << "科目數:"; | |
cin >> numbersOfSubject; | |
// 宣告成績表為一個二維陣列 | |
int score[classSize][numbersOfSubject]; | |
cout << "==============================" << endl; | |
for (int i = 0; i < classSize; i++) { | |
for (int j = 0; j < numbersOfSubject; j++) { | |
cout << i + 1 << " 號第 " << j + 1 << " 科的分數:"; | |
// 顯示時把數字都加 1,比較符合人的習慣(從 1 開始數) | |
cin >> score[i][j]; | |
} | |
cout << endl; // 每換一個人就隔一行空白行 | |
} | |
cout << "==============================" << endl; | |
char func; | |
while (true) { | |
cout << "請輸入欲查詢的功能(a.單科平均 b.個人平均 x.結束):"; | |
cin >> func; | |
if (func == 'a') { | |
// ### 單科平均 ### | |
int targetSubject; | |
cout << "[單科平均] 請輸入欲查詢的科目:"; | |
cin >> targetSubject; | |
int total = 0; // 加總前先歸零 | |
for (int i = 0; i < classSize; i++) { | |
total += score[i][targetSubject - 1]; | |
// 想想看:為什麼 targetSubject 要減 1? | |
} | |
cout << "第 " << targetSubject << " 科的平均分數為:" << (float)total/classSize << " 分" << endl; | |
// 想想看:為什麼 targetSubject 不用加 1 或減 1? | |
cout << endl; | |
} else if (func == 'b') { | |
// ### 個人平均 ### | |
int targetNumber; | |
cout << "[個人平均] 請輸入欲查詢的座號:"; | |
cin >> targetNumber; | |
int total = 0; // 加總前先歸零 | |
for (int i = 0; i < numbersOfSubject; i++) { | |
total += score[targetNumber - 1][i]; | |
// 想想看:為什麼 targetNumber 要減 1? | |
} | |
cout << targetNumber << " 號的平均分數為:" << (float)total/numbersOfSubject << " 分" << endl; | |
// 想想看:為什麼 targetNumber 不用加 1 或減 1? | |
cout << endl; | |
} else if (func == 'x') { | |
break; | |
} else { | |
cout << "查無此功能,請重新輸入" << endl; | |
} | |
} | |
cout << "==============================" << endl; | |
cout << "掰掰~"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment