输入3个学生的学号、姓名和三门功课成绩,输出平均分最高的同学

新建一个学生类Student.h:属性有姓名、学号、三门功课成绩

#include<string>
using namespace std;
class Student
{
public:
	Student():classA_score(0), classB_score(0), classC_score(0){ }
	
	void setClassA_score(double A_score);
	void setClassB_score(double B_score);
	void setClassC_score(double C_score);
	void setName(string name);
	void setNo_number(string no_num);

	double getClassA_score();
	double getClassB_score();
	double getClassC_score();
	double getAverage_score();

	string getName();
	string getNo_number();
private:
	string stu_name;
	double classA_score;
	double classB_score;
	double classC_score;
	string No_number;
};


类功能实现Student.cpp

#include"Student.h"

void Student::setName(string name)
{
	stu_name = name;
}
void Student::setNo_number(string no_num)
{
	No_number = no_num;
}
void Student::setClassA_score(double A_score)
{
	classA_score = A_score;
}
void Student::setClassB_score(double B_score)
{
	classB_score = B_score;
}
void Student::setClassC_score(double C_score)
{
	classC_score = C_score;
}
double Student::getClassA_score()
{
	return classA_score;
}
double Student::getClassB_score()
{
	return classB_score;
}
double Student::getClassC_score()
{
	return classC_score;
}

double Student::getAverage_score()
{
	return 1.0 / 3 * (getClassA_score() + getClassB_score() + getClassC_score());
}
string Student::getName( )
{
	return stu_name;
}
string Student::getNo_number()
{
	return No_number;
}

main.cpp

#include<iostream>
#include"Student.h"
using namespace std;

/*输入3个学生的学号、姓名和三门功课成绩,输出平均分最高的同学。*/

int main()
{
	Student student[3];
	for (int i = 0; i < 3; i++)
	{
		string name;
		string no_number;
		cout << "请输入第" << i + 1 << "个学生的姓名、学号:" << endl;
		cout << "姓名:";
		cin >> name;
		student[i].setName(name);
		cout << "学号:";
		cin >> no_number;
		student[i].setNo_number(no_number);
		
		cout << "第" << i + 1 << "个学生的成绩:" << endl;
		cout << "input class A score:" ;
		double a;
		cin >> a;
		student[i].setClassA_score(a);
		cout << "input class B score:" ;
		double b;
		cin >> b;
		student[i].setClassB_score(b);
		cout << "input class C score:" ;
		double c;
		cin >> c;
		student[i].setClassC_score(c);
		student[i].getAverage_score();
	}
	double aver_max = 0;
	for (int i = 0; i < 3; i++)
	{
		if (student[i].getAverage_score() > aver_max)
		{
			aver_max = student[i].getAverage_score();
		}
	}
	for (int i = 0; i < 3; i++)
	{
		if (student[i].getAverage_score() == aver_max)
		{
			cout << "最大的平均分为:" << aver_max << endl;
			cout << "获得最大平均分的人是:" << student[i].getName() << endl;
		}
	}
}






©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值