using?namespace?std;
const?unsigned?int?g_nStudents?=?10;?//?學生總數
//?學生成績統計類
class?StudentsScoreStatistics
{
public:
StudentsScoreStatistics()?{?Clear();?} //?重置學生分數 void?Clear() { for?(int?i?=?0;?i?<?g_nStudents;?i++) {mScores[i]?=?0.0;
} } //?設置學生分數 bool?InitScore(double?aCore,?int?aIndex) { if?(aCore?<?0.0?||?aCore?>?100.0)return?false;
mScores[aIndex]?=?aCore; return?true; } //?排序 void?Sort() { //?采用插入排序 for?(int?i?=?1;?i?<?g_nStudents;?i++) {double?nCurrent?=?mScores[i];
int?j?=?i?-?1;
while?(j?>=?0?&&?mScores[j]?<?nCurrent)
{
mScores[j?+?1]?=?mScores[j];
j--;
}
mScores[j?+?1]?=?nCurrent;
} } //?得到所有學生分數 const?double*?GetScores()?const { return?mScores; }private:
double?mScores[g_nStudents];//?學生分數數組};
int?main()
{
StudentsScoreStatistics?stOperator; //?輸入成績 cout?<<?"請輸入"?<<?g_nStudents?<<?"位學生成績:"?<<?endl; for?(int?i?=?0;?i?<?g_nStudents;?i++) { double?dScore?=?0; cin?>>?dScore; if?(!stOperator.InitScore(dScore,?i)) {cout?<<?"成績輸入錯誤!請重新輸入:"?<<?endl;
i--;
} } //?逆序輸出 stOperator.Sort(); cout?<<?"排序後的學生成績為:"?<<?endl; for?(int?i?=?0;?i?<?g_nStudents;?i++) { cout?<<?*(stOperator.GetScores()?+?i)?<<?""; } cout?<<?endl; cout?<<?endl; system("pause");return?0;
}
運行結果: