编写一个学生类,包含学号(string)、姓名(string)和成绩(double)三个私有属性,以及设置姓名、学号和成绩值,获得成绩值,输出姓名、学号和成绩等的公有成员函数。根据输入的人数,定义学生类对象数组,并读入学生信息,然后按照成绩由高低顺序排序并输出。
输入格式:
第1行输入学生人数n(0<n<100),随后n行分别输入学生学号、姓名、成绩。
输出格式:
按成绩由高到低输出每个学生的信息
输入样例:
在这里给出一组输入。例如:
3
001 张东 78.5
002 李岚 88
003 肖天 76
输出样例:
在这里给出相应的输出。例如:
1 002 李岚 88
2 001 张东 78.5
3 003 肖天 76
思路一:
#include<iostream>
#include <string>
using namespace std;
class Student{
public:
string getnum(){
return num;
}
string getname(){
return name;
}
double getscore(){
return score;
}
Student(string n,string na,double s)
:num(n),name(na),score(s){}
Student() {} // 添加默认构造函数,确保了 Student 类在没有提供参数的情况下也能被正确创建。
private:
string num,name;
double score;
};
void swap(Student &a,Student &b){
Student temp=a;
a=b;
b=temp;
}
int main(){
int n;
cin>>n;
Student s[n];
for(int i=0;i<n;i++)
{
string num,name;
double score;
cin>>num>>name>>score;
s[i] = Student(num, name, score);
}
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(s[i].getscore()<s[j].getscore())
swap(s[i],s[j]);
}
}
for(int i=0;i<n;i++)
{
cout<<i+1<<" "<<s[i].getnum()<<" "<<s[i].getname()<<" "<<s[i].getscore()<<endl;
}
}
改进:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Student{
public:
string getnum() const { // 在 getnum()、getname() 和 getscore() 成员函数中添加了 const 修饰符。
return num;
}
string getname() const {
return name;
}
double getscore() const {
return score;
}
Student(string n, string na, double s)
: num(n), name(na), score(s){}
Student() {} // 添加默认构造函数
private:
string num, name;
double score;
};
int main(){
int n;
cin >> n;
vector<Student> students; //使用了 std::vector<Student> 替代了静态数组,这样可以支持动态大小,并且可以利用 reserve 方法提前分配好存储空间,避免不必要的重新分配。
students.reserve(n); // 提前分配好空间
for (int i = 0; i < n; i++){
string num, name;
double score;
cin >> num >> name >> score;
students.emplace_back(num, name, score);
} // 使用了 emplace_back 方法替代了 push_back 方法,这样可以直接在 vector 中构造对象,避免了额外的拷贝操作。
sort(students.begin(), students.end(), [](const Student& a, const Student& b) {
return a.getscore() > b.getscore(); //按成绩由高到低排序,在比较函数中使用了 lambda 表达式,这样可以更简洁地定义比较规则。
}); //使用了 std::sort 算法替代了手动实现的选择排序,这样可以提高排序的效率。
for (int i = 0; i < n; i++){
cout << i + 1 << " " << students[i].getnum() << " " << students[i].getname() << " " << students[i].getscore() << endl;
}
return 0;
}
思路二:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Student {
private:
string studentID;
string name;
double score;
public:
Student() {} // 默认构造函数
Student(const string& id, const string& n, double s) : studentID(id), name(n), score(s) {}
void setID(const string& id) { //类中的公有接口,用于设置学生对象的私有成员变量。将对象的私有成员变量设为外部传入的值,引用类型参数可以避免不必要的拷贝,提高效率。
studentID = id;
}
void setName(const string& n) {
name = n;
}
void setScore(double s) {
score = s;
}
string getID() const { //用于获取学生的学号。通过这个函数,可以在外部访问学生对象的学号
return studentID;
}
string getName() const {
return name;
}
double getScore() const {
return score; //const 成员函数,保证不会修改对象的状态。
}
// 重载小于操作符,用于sort()函数从高到低排序
bool operator<(const Student& other) const {
return score > other.score;
}
};
int main() {
int n;
cin >> n;
vector<Student> students; //名为 students 的向量,是存储了多个 Student 对象的容器
for (int i = 0; i < n; ++i) {
string id, name;
double score;
cin >> id >> name >> score;
students.push_back(Student(id, name, score)); //添加了新的 Student 对象,
}
// 对学生数组按成绩由高到低排序
sort(students.begin(), students.end()); //students.begin() 返回指向 students 向量中第一个元素的迭代器,students.end() 返回指向 students 向量中最后一个元素之后的位置的迭代器,表示结束位置。
// 输出排序后的学生信息
for (int i = 0; i < n; ++i) {
cout << i + 1 << " " << students[i].getID() << " " << students[i].getName() << " " << students[i].getScore() << endl;
}
return 0;
}