Last active
August 29, 2015 14:22
-
-
Save johnhmj/c42e3448109ea7e99d70 to your computer and use it in GitHub Desktop.
Quiz 3 & 4(IP = 120.117.156.61, STUST), https://www.ptt.cc/bbs/C_and_CPP/M.1433693969.A.3FD.html
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> | |
#include <limits> | |
#define STUDENTNUMBER 8 | |
#define STUDENTDATA 4 | |
void ScoreInput(int a[][STUDENTDATA]); | |
void ScorePrint(int a[][STUDENTDATA]); | |
void ScoreSorting(int a[][STUDENTDATA], int f); | |
using std::cin; | |
using std::cout; | |
using std::endl; | |
int main(int argc, char* argv[]) | |
{ | |
int iScore[STUDENTNUMBER][STUDENTDATA]={0}; | |
ScoreInput(iScore); | |
ScorePrint(iScore); | |
// | |
//Pause | |
cin.clear(); | |
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); | |
cout << "press any key to continue..."; | |
cin.get(); | |
return 0; | |
} | |
void ScoreInput(int a[][STUDENTDATA]) | |
{ | |
for (int i = 0; i < STUDENTNUMBER; i ++) | |
{ | |
cout << "Input Student " << (i + 1) << " ID: "; | |
cin >> a[i][0]; | |
cout << "Input Student " << (i + 1) << " Chinese: "; | |
cin >> a[i][1]; | |
cout << "Input Student " << (i + 1) << " Math: "; | |
cin >> a[i][2]; | |
a[i][3] = a[i][1] + a[i][2]; | |
} | |
} | |
void ScorePrint(int a[][STUDENTDATA]) | |
{ | |
int c; | |
cout << "Input sorting code[ID=0/Chinese=1/Math=2/Total=3]: "; | |
cin >> c; | |
if ((c < 0)|(c >= STUDENTDATA)) | |
{ | |
return; | |
} | |
ScoreSorting(a, c); | |
for (int i = 0; i < STUDENTNUMBER; i ++) | |
{ | |
cout << "ID = " << a[i][0] << " Chinese = " << a[i][1] << " Math = " << a[i][2] << " Total = " << a[i][3] << endl; | |
} | |
} | |
void ScoreSorting(int a[][STUDENTDATA], int f) | |
{ | |
int b[STUDENTNUMBER][STUDENTDATA]; | |
for (int i = 0; i < STUDENTNUMBER; i ++) | |
{ | |
for (int j = 0; j < STUDENTNUMBER; j ++) | |
{ | |
if (a[i][f] > a[j][f]) | |
{ | |
int z; | |
for (z = 0; z < STUDENTDATA; z ++) | |
{ | |
b[i][z] = a[i][z]; | |
} | |
for (z = 0; z < STUDENTDATA; z ++) | |
{ | |
a[i][z] = a[j][z]; | |
} | |
for (z = 0; z < STUDENTDATA; z ++) | |
{ | |
a[j][z] = b[i][z]; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment