1. 程式人生 > 其它 >獎學金評比(類和物件基礎)

獎學金評比(類和物件基礎)

題目描述

​ 理工大學決定一年一度的獎學金評比,為了公平起見,規則與往年有所不同,要求的是按宿舍進行名額分配,每個宿舍要評比出一名學生獲得獎學金,每個宿舍的學生不超過6人;評比規則是:以高數、英語、C++三門課程的成績為評比標準,按總分排序,如果總分一致,按高數成績評比,如果高數一致,按英語成績評比;資料保證一個宿舍沒有完全三門課成績一樣的情況。定義一個學生類 Student,有學號,姓名,高數成績、英語成績、C++成績,以及宿舍號,評比出每個宿舍的獲獎學生名單。

輸入格式:

首先輸入測試模式mode,如果mode=1,測試輸入一個學生的資訊,並顯示該學生的資訊;如果mode=2,輸入接著輸入兩個學生的資訊,並測試比較函式,顯示成績排名在前面的學生的資訊;如果mode=3,則繼續輸入一個整型數 n

 (1n104),表示有 n 位同學。

緊跟著 n 行輸入,每一行格式為:宿舍號(dorm)、學號(num)、姓名(name)、高數(math)、英語(english)、C++(cplus)
宿舍號的區間為 [0, 9999]的整數, 學號是10位的整數(如2021190101),name 由字母組成,長度小於 20,各科成績均為實數[0,100]。

輸出格式:

mode=1,輸出一個學生的資訊;mode=2,輸出兩個學生中排名靠前的學生的資訊;mode=3,按宿舍號從小到大排序,輸出每間宿舍獲獎的同學資訊。注意,不管mode的值是多少,輸出宿舍號的時候,注意宿舍號不足 4 位的,要按 4 位補齊前導 0。

類和函式介面定義:

 
#include<iostream>
using namespace std;
#define MAX_SIZE 10000

struct Course
{
    double math,english,cplus;
    void setScore(double m,double e,double c);
    void disp();//顯示成績
};
class Student
{
private:
    char name[20];
    Course course;
public:
    int num;//提示:學號均為非負;可用負數來表示該宿舍沒有人
    int dorm;//寢室編號
    Student();//預設建構函式
    Student(int dor,int num,char name[20],Course course);
    bool betterThan(Student b);//比較this和b學生的成績,為true表示this優於b
    void disp();//顯示該學生的資訊
    void input();//輸入學生資訊
};
//輸入n個學生資訊並評獎
void inputAward(Student stu[],int n);
//顯示獲獎學生資訊
void show(Student stu[]);
 

裁判測試程式樣例:

 
在這裡給出函式被呼叫進行測試的例子:
int main()
{
    int n,mode;
    cin>>mode;
    Student stu[MAX_SIZE];
    Student s1,s2;

    switch(mode)
    {
    case 1:
        s1.input();
        s1.disp();
        break;
    case 2:
        s1.input();
        s2.input();
        if(s1.betterThan(s2))
            s1.disp();
        else
            s2.disp();
        break;
    case 3:
        //輸入n個學生的資料,並評獎
        cin>>n;
        inputAward(stu,n);
        //顯示獲獎學生的資訊
        show(stu);
        break;
    }
    return 0;
}


/* 請在這裡填寫答案 */
 

輸入樣例1:

1
0 2021190101 Tom 95 90 88
 

輸出樣例1:

0000 2021190101 Tom 95 90 88
 

輸入樣例2:

2
0000 2021190101 Tom 95 90 88
0001 2021190102 Jack  89 98 87
 

輸出樣例2:

0001 2021190102 Jack 89 98 87
 

輸入樣例3:

3
7
0000 2021190101 Tom 95 90 88
0001 2021190102 Jack  89 98 87
0000 2021190103 Marry  95 91 87
0000 2021190104 Jerry 88 50 61
0003 2021190105 ETAF 78 89 86
0001 2021190106 Mickey 85 90 87
0001 2021190107 Hale 60 40 50
 

輸出樣例:

0000 2021190103 Marry 95 91 87
0001 2021190102 Jack 89 98 87
0003 2021190105 ETAF 78 89 86
  程式碼長度限制 16 KB 時間限制 400 ms 記憶體限制 64 MB
 
Student::Student(){};
void Student::disp() {
    printf("%04d %d %s %.0lf %.0lf %.0lf", dorm, num, name, course.math,
           course.english, course.cplus);
}
void Student::input() {
    cin >> dorm >> num >> name >> course.math >> course.english >> course.cplus;
}
bool Student::betterThan(Student b) {
    if (this->course.cplus + this->course.english + this->course.math ==
        b.course.cplus + b.course.english + b.course.math) {
        if (this->course.math == b.course.math) {
            if (this->course.english == b.course.english) {
                return this->course.cplus > b.course.cplus;            } else {
                return this->course.english > b.course.english;            }        } else {
            return this->course.math > b.course.math;       }    } else {
        return this->course.cplus + this->course.english + this->course.math >
               b.course.cplus + b.course.english + b.course.math;    }}
void inputAward(Student stu[], int n) {
    for (int i = 0; i < n; ++i) { stu[i].input(); }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n - i - 1; ++j) {
            if (stu[j].dorm == stu[j + 1].dorm) {
                if (stu[j + 1].betterThan(stu[j])) {
                    Student t = stu[j + 1];
                    stu[j + 1] = stu[j];
                    stu[j] = t;                }            } else if (stu[j].dorm > stu[j + 1].dorm) {
                Student t = stu[j + 1];
                stu[j + 1] = stu[j];
                stu[j] = t;            }        }    }
    stu[n].dorm = -5;
}
void show(Student stu[]) {
    int flag = -1;
    for(int i = 0;;++i){
        if(stu[i].dorm==-5) break;
        if(stu[i].dorm!=flag){
            flag = stu[i].dorm;
            stu[i].disp();
            cout<<"\n";        }   }}