AO·GU612 2022-04-19 23:26 采纳率: 66.7%
浏览 591
已结题

面向对象程序设计实验1:类和对象基础

要求的是按宿舍进行名额分配,每个宿舍要评比出一名学生获得奖学金,每个宿舍的学生不超过6人;评比规则是:以高数、英语、C++三门课程的成绩为评比标准,按总分排序,如果总分一致,按高数成绩评比,如果高数一致,按英语成绩评比;数据保证一个宿舍没有完全三门课成绩一样的情况。定义一个学生类 Student,有学号,姓名,高数成绩、英语成绩、C++成绩,以及宿舍号,评比出每个宿舍的获奖学生名单。

输入格式:
首先输入测试模式mode,如果mode=1,测试输入一个学生的信息,并显示该学生的信息;如果mode=2,输入接着输入两个学生的信息,并测试比较函数,显示成绩排名在前面的学生的信息;如果mode=3,则继续输入一个整型数 n (1≤∗n∗≤10
4
),表示有 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
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

这是我写的,但是有很多错误,感觉实在是不会了,特别是bool和数组那里,求指导一下

#include<iostream>
#include<iomanip>
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:
    string name;
    Course course;
public:
    int dorm;//寝室编号
    int num;//提示:学号均为非负;可用负数来表示该宿舍没有人
    Student();//默认构造函数
   Student(int dor,int nu,string nam,Course course){
    dorm=dor;num=nu;name=nam;
}
    bool betterThan(Student b);//比较this和b学生的成绩,为true表示this优于b
    void disp();//显示该学生的信息
   void input();//输入学生信息
void changedata(Student& stu){
string c_name;
        int c_dorm, c_num, c_course1,c_course2,c_course3;
        c_dorm= this->dorm; c_num = this->num; c_name = this->name; c_course1 = this->course.math;c_course2 = this->course.english;c_course3= this->course.cplus;
        this->dorm = stu.dorm; this->num = stu.num; this->name = stu.name; 
        this->course.math = stu.course.math;this->course.english = stu.course.english;this->course.cplus = stu.course.cplus;
        stu.dorm = c_dorm;stu.num = c_num; stu.name = c_name; 
        stu.course.math = c_course1;stu.course.english= c_course2;stu.course.cplus = c_course3;
    };
};
void Course::setScore(double m,double e,double c){
    math=m;english=e;cplus=c;
}
void Student::disp(){
    cout << setw(4) << setfill('0') <<dorm;//按照格式输出宿舍门牌号 
    cout << " " << num << " " << name << " " << course.math <<" " << course.english<<" " << course.cplus<<endl;
}
void Student::input(){
    cin>>dorm>>num>>name>>course.math>>course.english>>course.cplus;
}
void Course::disp(){
    cout << " " << math<< " " << english << " " << cplus <<endl;
}
void inputAward(Student stu[],int n){
cin>>n;
   int dorm,num;
   double m,e,c;
   string name;
   Student stu[MAX_SIZE];
   cin>>dorm>>num>>name>>m>>e>>c;
    for (int i = 0; i <=n; i++) {
        stu[i].Student(int dor,int nu,string nam,Course course);
    }
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            if (stu[i].room > stu[j].room) 
            { stu[i].changedata(stu[j]); }
            
};}
for (int i = 0, j = 0, max = 0; i < n;) {
        max = j;
        for (; stu[i].dorm== stu[j].dorm; j++) {
            if ((stu[j].m+stu[j].e+stu[j].c) >(stu[max].m+stu[max].e+stu[max].c)) 
            {
                max = j;
            }
                else if((stu[j].m+stu[j].e+stu[j].c) <(stu[max].m+stu[max].e+stu[max].c))max=max;
            else if((stu[j].m+stu[j].e+stu[j].c) ==(stu[max].m+stu[max].e+stu[max].c)){
                if(stu[j].m >stu[max])
                max = j;
                else if(stu[j].m <stu[max].m)max=max;
                else{
                    if(stu[j].e >stu[max].e){
                        max = j;
                    }
                    else if(stu[j].e <stu[max].e)max=max;
                    else{
                    if(stu[j].c >stu[max].c){max = j;
                }
                else if(stu[j].e <stu[max].e)max=max;
            }
        }
    }}}
    void show(Student stu[]);
    i=j;
}
void show(Student stu[]){
    cout << setfill('0') << setw(4)  << stu[max].dorm << " " <<stu[max].num << " " << stu[max].name << " " << stu[max].math <<  " " << stu[max].english << " " << stu[max].cplus <<endl;
}

```c++

```c++





  • 写回答

1条回答 默认 最新

  • 一只爱算法的猫 2022-04-25 17:26
    关注

    直接给程序吧,具体哪里不明白再问我。
    show函数没看明白,但是代码中注释了数据保存方式

    #include<iostream>
    #include<iomanip>
    using namespace std;
    #define MAX_SIZE 10000
    struct Course
    {
        double math, english, cplus;
        void setScore(double m, double e, double c)
        {
            math = m;
            english = e;
            cplus = c;
            return;
        }
        void disp()
        {
            printf("Math: %f, English: %f, Cplus: %f", math, english, cplus);
            return;
        }
    };
    class Student
    {
    private:
        char name[20];
        Course course;
    public:
        int dorm;
        long long num;
        Student()
        {
            name[0] = '\0';
            course = { -1,-1,-1 };
            dorm = 0;
            num = 0;
        }
        Student(int dor, long long nu, char nam[20], Course cours)
        {
            for (int i = 0; i < 20; i++)
            {
                name[i] = nam[i];
            }
            course = cours;
            dorm = dor;
            num = nu;
        }
        //比较this和b学生的成绩,为true表示this优于b
        bool betterThan(Student b)
        {
            if (course.cplus + course.english + course.math > b.course.cplus + b.course.english + b.course.math)
            {
                return true;
            }
            if (course.cplus + course.english + course.math < b.course.cplus + b.course.english + b.course.math)
            {
                return false;
            }
            if (course.math > b.course.math)
            {
                return true;
            }
            if (course.math < b.course.math)
            {
                return false;
            }
            if (course.english > b.course.english)
            {
                return true;
            }
            if (course.english < b.course.english)
            {
                return false;
            }
            if (course.cplus > b.course.cplus)
            {
                return true;
            }
            if (course.cplus < b.course.cplus)
            {
                return false;
            }
        }
        void disp()
        {
            cout << "Name: " << name;
            cout << ", Number: " << num << ", DoorNum: ";
            int dn = dorm;
            int cnt = 4;
            while (dn)
            {
                dn /= 10;
                cnt--;
            }
            for (int i = 1; i <= cnt; i++)
            {
                cout << 0;
            }
            printf("%d, Course: ",dorm);
            course.disp();
            return;
        }
        void input()
        {
            cin >> name >> num >> dorm;
            double m, e, c;
            cin >> m >> e >> c;
            course.setScore(m, e, c);
        }
    };
    Student room[MAX_SIZE];
    bool flag[MAX_SIZE];
    int roomsize;
    //获奖名单记录在room中,为防止有空宿舍,没人的宿舍flag为0。roomsize记录输入的最大的房间编号
    void inputAward(Student stu[], int n)
    {
        roomsize = -1;
        for (int i = 0; i < n; i++)
        {
            flag[i] = 0;
            room[i] = Student();
        }
        for (int i = 1; i <= n; i++)
        {
            roomsize = max(roomsize, stu[i].dorm);
            if ((!flag[stu[i].dorm]) || stu[i].betterThan(room[stu[i].dorm]))
            {
                room[stu[i].dorm] = stu[i];
            }
        }
    }
    int main()
    {
        return 0;
    }
    

    如果有帮助点个解决,谢谢啦

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 5月31日
  • 已采纳回答 5月23日
  • 创建了问题 4月19日

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画