c++ 定义一个结构体,输入学生的信息,并输出学生的总成绩与名次
答案:2 悬赏:60 手机版
解决时间 2021-01-29 20:36
- 提问者网友:ミ烙印ゝ
- 2021-01-29 14:22
c++ 定义一个结构体,输入学生的信息,并输出学生的总成绩与名次
最佳答案
- 五星知识达人网友:鱼芗
- 2021-01-29 14:46
#include
#include
class Student{
friend std::ostream& operator << (std::ostream&, const Student&);
friend std::istream& operator >> (std::istream&, Student&);
friend bool compare_score(const Student&,const Student&);
public:
Student() = default;
Student(const std::string& name, const std::string& id) :_name(name), _id(id){ }
private:
std::string _name, _id;
double _score[3];
};
std::ostream& operator << (std::ostream& os, const Student& stu)
{
os << stu._id << " " << stu._name << std::endl;
for (const auto& score : stu._score) {
os << score << " ";
}
return os;
}
std::istream& operator >> (std::istream& is, Student& stu)
{
is >> stu._id >> stu._name;
for (auto& score : stu._score) {
is >> score;
}
if (!is){
stu = Student();
}
return is;
}
bool compare_score(const Student& lhs, const Student& rhs)
{
return (lhs._score[0] + lhs._score[1] + lhs._score[2] > rhs._score[0] + rhs._score[1] + rhs._score[2]);
}
#include
#include
int main()
{
std::vector students;
Student tmp;
while (std::cin >> tmp) {
students.push_back(tmp);
}
std::sort(students.begin(), students.end(), compare_score);
std::cout << std::endl;
for (auto stu : students) {
std::cout << stu << std::endl;
}
system("pause>nul");
}
//喵~
#include
class Student{
friend std::ostream& operator << (std::ostream&, const Student&);
friend std::istream& operator >> (std::istream&, Student&);
friend bool compare_score(const Student&,const Student&);
public:
Student() = default;
Student(const std::string& name, const std::string& id) :_name(name), _id(id){ }
private:
std::string _name, _id;
double _score[3];
};
std::ostream& operator << (std::ostream& os, const Student& stu)
{
os << stu._id << " " << stu._name << std::endl;
for (const auto& score : stu._score) {
os << score << " ";
}
return os;
}
std::istream& operator >> (std::istream& is, Student& stu)
{
is >> stu._id >> stu._name;
for (auto& score : stu._score) {
is >> score;
}
if (!is){
stu = Student();
}
return is;
}
bool compare_score(const Student& lhs, const Student& rhs)
{
return (lhs._score[0] + lhs._score[1] + lhs._score[2] > rhs._score[0] + rhs._score[1] + rhs._score[2]);
}
#include
#include
int main()
{
std::vector
Student tmp;
while (std::cin >> tmp) {
students.push_back(tmp);
}
std::sort(students.begin(), students.end(), compare_score);
std::cout << std::endl;
for (auto stu : students) {
std::cout << stu << std::endl;
}
system("pause>nul");
}
//喵~
全部回答
- 1楼网友:深街酒徒
- 2021-01-29 16:03
#include
#include
#define STU 2
typedef struct
{
char name [100];
int ID;
float s[3];
float Ascore;
int rank;
}student;
void ave(student *a) //平均成绩计算函数
{
int i,j;
float sum=0;
for(i=0;i {
for(j=0;j<3;j++)
{
sum+=a[i].s[j];
}
a[i].Ascore=sum;
sum=0;
}
}
void RANK(student *a) //学生成绩排名函数
{
int i,max=0,j;
float score[STU];
float temp;
for(i=0;i score[i]=a[i].Ascore;
for(i=0;i {
max=i;
for(j=i+1;j if(score[j]>score[i]) max=j;
if(i!=max) {temp=score[i];score[i]=score[max];score[max]=temp;}
}
for(i=0;i {
for(j=0;;j++)
if(a[i].Ascore>=score[j]) break;
a[i].rank=j+1;
}
}
void Input(student *a)
{
int i;
for(i=0;i {
printf("依次请输入学生的姓名,学号,成绩1,成绩2,成绩3\n");
scanf("%s%d",&a[i].name,&a[i].ID);
scanf("%f%f%f",&a[i].s[0],&a[i].s[1],&a[i].s[2]);
}
}
void Output(student *a)
{
int i;
for(i=0;i printf(" 学生姓名%s,学号%d,总成绩%f,排名%d\n",a[i].name,a[i].ID,a[i].Ascore,a[i].rank);
}
void main()
{
student stu[STU];
Input(stu);
ave(stu);
RANK(stu);
Output(stu);
}
#include
#define STU 2
typedef struct
{
char name [100];
int ID;
float s[3];
float Ascore;
int rank;
}student;
void ave(student *a) //平均成绩计算函数
{
int i,j;
float sum=0;
for(i=0;i
for(j=0;j<3;j++)
{
sum+=a[i].s[j];
}
a[i].Ascore=sum;
sum=0;
}
}
void RANK(student *a) //学生成绩排名函数
{
int i,max=0,j;
float score[STU];
float temp;
for(i=0;i
for(i=0;i
max=i;
for(j=i+1;j
if(i!=max) {temp=score[i];score[i]=score[max];score[max]=temp;}
}
for(i=0;i
for(j=0;;j++)
if(a[i].Ascore>=score[j]) break;
a[i].rank=j+1;
}
}
void Input(student *a)
{
int i;
for(i=0;i
printf("依次请输入学生的姓名,学号,成绩1,成绩2,成绩3\n");
scanf("%s%d",&a[i].name,&a[i].ID);
scanf("%f%f%f",&a[i].s[0],&a[i].s[1],&a[i].s[2]);
}
}
void Output(student *a)
{
int i;
for(i=0;i
}
void main()
{
student stu[STU];
Input(stu);
ave(stu);
RANK(stu);
Output(stu);
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯