作业:
- 整理思维导图
- 定义一个Student结构体,里面的成员有公有成员name、age,私有成员:score,从堆区连续分配3个结构体大小的空间,从键盘上输入3个学生的信息,分别存放到对应的位置上,按成绩的升序排序后输出三名学生的信息。
#include <iostream>
using namespace std;
typedef struct Student
{
public:
string name;
int age;
private:
float score;
public:
float set_score(float s)
{
score = s;
}
float get_score()
{
return score;
}
};
int main()
{
Student* p = new Student[3];
for (int i=0; i<3; i++) {
float s;
cout << "请输入第" << i+1 << "个学生的name" << endl;
cin >> p[i].name;
cout << "请输入第" << i+1 << "个学生的age" << endl;
cin >> p[i].age;
cout << "请输入第" << i+1 << "个学生的score" << endl;
cin >> s;
p[i].set_score(s);
}
//冒泡排序
for (int i=0;i<3-1;i++) {
Student temp;
for (int j=0;j<3-i-1; j++) {
if(p[j].get_score() > p[j+1].get_score())
{
temp = p[j];
p[j] = p[j+1];
p[j+1] = temp;
}
}
}
for (int i=0; i<3;i++) {
cout << "name:" << p[i].name <<endl;
cout << "age:" << p[i].age <<endl;
cout << "score:" << p[i].get_score() <<endl;
}
delete [] p;
return 0;
}