C++利用二维数组的类存储学生成绩

将学生成绩储存在二维数组中,计算每位学生的成绩和,再输出

#include<stdio.h>
//#include<fstream>
#include<string>
//#include<vector>
#include<iostream>
#include<iomanip>
using namespace std;
class GradeBook{
	public:
		const static int students=10;
		const static int tests=3;
		GradeBook(string,const int [][tests]);
		void setCourseName(string);
		string getCourseName();
		void diaplayMessage();
		void processGrades();
		double getSum(const int[],const int);
//		void outputBarChart();
		void outputGrades();
	private:
		string courseName;
		int grades[students][tests];
};
GradeBook::GradeBook(string name,const int gradesArray[][tests])
{
	setCourseName(name);
	for(int student=0;student<students;student++)
	{
		for(int test=0;test<tests;test++)
		{
			grades[student][test]=gradesArray[student][test];
		}
	}
}


void GradeBook::setCourseName(string name)
{
	courseName=name;
}

string GradeBook::getCourseName()
{
	return courseName; 
 } 
 
void GradeBook::diaplayMessage()
{
	cout<<"成绩如下:"<<endl;
}

void GradeBook::processGrades()
{
	outputGrades();	
}

double GradeBook::getSum(const int setOfGrades[],const int grades)
{
	int total=0;
	for(int grade=0;grade<grades;grade++)
	{
		total+=setOfGrades[grade];
		
	}
	return total;
}


void GradeBook::outputGrades()
{
	cout<<"列表:\n\n";
	cout<<"           ";
//	tests[4]={"数学成绩","英语成绩","语文成绩","总成绩"};
	for(int test=0;test<tests;test++)
	{
		cout<<"Tests"<<test+1<<"   ";
		
	}
	cout<<"总成绩"<<endl;
	for(int student=0;student<students;student++)
	{
		cout<<"Student"<<setw(2)<<student+1;
		for(int test=0;test<tests;test++)
		{
			cout<<setw(8)<<grades[student][test];
		}
		double sum=getSum(grades[student],tests);
		cout<<setw(9)<<setprecision(2)<<fixed<<sum<<endl;
	}
}

int main()
{
	int gradesArray[GradeBook::students][GradeBook::tests]=
	{{87,96,70},
	  {68,87,90},
	  {94,100,90},
	  {100,81,82},
	  {83,65,65},
	  {78,87,65},
	  {85,75,83},
	  {91,94,100},
	  {76,72,84},
	  {87,93,73}	
	};
	GradeBook myGradeBook(
	"CS101 C++",gradesArray);
	myGradeBook.diaplayMessage();
	myGradeBook.processGrades();
	return 0;
}

效果如下所示:

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

抵扣说明:

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

余额充值