![]() |
#2
rjsp2017-05-08 08:39
op[t].set; 报错:error C3867: 'student::set': function call missing argument list; use '&student::set' to create a pointer to member其它的,也一塌糊涂,算法也错误。更重要的是,这不是C++的写法。 ![]() #include <iostream> 输入#include <string> struct student { std::string name; float eng_score; float cpt_score; student() : name(), eng_score(), cpt_score() { } float sum() const { return eng_score+cpt_score; } }; std::istream& operator>>( std::istream& in, student& stu ) { return in >> stu.name >> stu.eng_score >> stu.cpt_score; } std::ostream& operator<<( std::ostream& out, const student& stu ) { return out<<"name:"<<stu.name<<"\tenglish score:"<<stu.eng_score<<"\tcomputer score:"<<stu.cpt_score; } #include <vector> #include <algorithm> using namespace std; int main( void ) { size_t n; cout << "请输入学生人数:"; cin >> n; std::vector<student> stus( n ); cout << "请输入学生学生姓名,英语成绩,计算机成绩\n"; for( size_t i=0; i!=stus.size(); ++i ) cin >> stus[i]; std::sort( stus.begin(), stus.end(), [](const student& a,const student& b){return a.sum()>b.sum();} ); for( size_t i=0; i!=stus.size(); ++i ) cout << stus[i] << '\n'; } 3 a 1 1 b 3 3 c 2 2 输出 name:b english score:3 computer score:3 name:c english score:2 computer score:2 name:a english score:1 computer score:1 |
/*编写一个程序:输入几个学生的姓名、英语和计算机成绩,
然后按照总分从高到低排序。要求定义一个student类,用友元函数实现排序。*/
#include <iostream>
using namespace std;
#include<string>
class student
{
public:
void set()
{
cout<<"请输入学生学生姓名,英语成绩,计算机成绩"<<'\n';
cin>>name>>score1>>score2;
sum=score1+score2;
}
void show()
{
cout<<"姓名:"<<name<<'\t'<<"英语成绩:"<<score1<<'\t'<<"计算机成绩:"<<score2<<'\n';}
friend void panxu(student op[],int n)
{
float temp;
for(int i=0;i<n-1;i++)
for(int j=1;j<n;j++)
{
if(op[i].sum<op[j].sum)
{
temp=op[j].sum;
op[i].sum=op[j].sum;
op[j].sum=temp;
op[i].name=op[j].name;
op[i].score1=op[j].score1;
op[i].score2=op[j].score2;
}
}
}
private:
string name;
float score1;
float score2;
float sum;
};
int main()
{
int n;
student op[50];
cout<<"请输入学生人数:";
cin>>n;
for(int t=0;t<n;t++)
{
op[t].set;
}
cout<<"排序后....."<<'\n';
panxu(op,n);
for(int k=0;k<n;k++)
{
op[k].show();}
return 0;
}