设计一个学生类Student,成员数据包含学号(int Id),姓名(char name[40])和成绩(int score)。 程序功能:首先读入n个学生数据;然后按照学生成绩升序排列,如果有成绩相同的则按照学号升序排列;最后输出所有学生的信息。 main函数已给定,提交时只需要提交main函数外的代码部分。 #include<iostream> #include<cstring> using namespace std; //你提交的代码在这里 int main() { int i,j,n; Student t; cin>>n; Student *stu=new Student[n]; for(i=0;i<n;i++) cin>>stu[i]; for(i=0;i<n-1;i++) for(j=0;j<n-i-1;j++) if(stu[j]>stu[j+1]) { t=stu[j]; stu[j]=stu[j+1]; stu[j+1]=t; } for(i=0;i<n;i++) cout<<i+1<<":"<<stu[i]<<endl; delete[]stu; return 0; }输入3 1001 Alice 85 1002 White 75 1003 Tom 90输出1:id=1002,name=White,score=75 2:id=1001,name=Alice,score=85 3:id=1003,name=Tom,score=90请补充完整代码
时间: 2024-03-26 17:36:51 浏览: 132
```
#include<iostream>
#include<cstring>
using namespace std;
class Student{
private:
int Id;
char name[40];
int score;
public:
Student(){}
friend istream& operator>>(istream& in, Student& s);
friend ostream& operator<<(ostream& out, const Student& s);
bool operator>(const Student& s) const;
};
istream& operator>>(istream& in, Student& s){
in >> s.Id >> s.name >> s.score;
return in;
}
ostream& operator<<(ostream& out, const Student& s){
out << "id=" << s.Id << ",name=" << s.name << ",score=" << s.score;
return out;
}
bool Student::operator>(const Student& s) const{
if(score == s.score) return Id < s.Id;
return score < s.score;
}
int main(){
int i,j,n;
Student t;
cin >> n;
Student *stu = new Student[n];
for(i = 0; i < n; i++) cin >> stu[i];
for(i = 0; i < n-1; i++)
for(j = 0; j < n-i-1; j++)
if(stu[j] > stu[j+1]){
t = stu[j];
stu[j] = stu[j+1];
stu[j+1] = t;
}
for(i = 0; i < n; i++) cout << i+1 << ":" << stu[i] << endl;
delete []stu;
return 0;
}
```
阅读全文