fig7.23.cpp

GradeBook.h

#pragma once
#include<array>
#include<string>
class GradeBook
{
public:
	static const size_t students = 10;
	static const size_t tests = 3;

	GradeBook(const std::string &,
		std::array<std::array<int, tests>, students > &);

	void setCourseName(const std::string &);
	std::string getCourseName() const;
	void displayMessage() const;
	void processGrades() const;
	int getMinimum() const;
	int getMaximum() const;
	double getAverage(const std::array <int, tests> &) const;
	void outputBarChart() const;
	void outputGrades() const;
private:
	std::string courseName;
	std::array<std::array<int, tests>, students> grades;

};

fig7.23.cpp

#include<iostream>
#include<iomanip>
using namespace std;

#include"GradeBook.h"

GradeBook::GradeBook(const string &name,
	std::array<std::array<int, tests>, students> &gradesArray)
	:courseName(name), grades(gradesArray)
{

}

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

}

string GradeBook::getCourseName() const
{
	return courseName;
}

void GradeBook::displayMessage()const
{
	cout << "Welcome to the grade book for\n" << getCourseName() << "!"
		<< endl;
}

void GradeBook::processGrades() const
{
	outputGrades();
	cout << "\nLowest grade in the grade book is " << getMinimum()
		<< "\nHighest grade in the grade book is: " << getMaximum() << endl;

	outputBarChart();
}

int GradeBook::getMinimum() const
{
	int lowGrade = 100;

	for (auto const &student : grades)
	{
		for (auto const &grade : student)
		{
			if (grade < lowGrade)
				lowGrade = grade;
		}
	}
	return lowGrade;
}

int GradeBook::getMaximum() const
{
	int highGrade = 0;
	for (auto const &student : grades)
	{
		for (auto const &grade : student)
		{
			if (grade > highGrade)
				highGrade = grade;

		}
	}
	return highGrade;
}

double GradeBook::getAverage(const array<int, tests> &setOfGrades) const
{
	int total = 0;
	for (int grade : setOfGrades)
		total += grade;

	return static_cast<double>(total) / setOfGrades.size();

}

void GradeBook::outputBarChart() const
{
	cout << "\nOverall grade distribution:" << endl;

	const size_t frequencySize = 11;
	array<unsigned int, frequencySize> frequency = {};

	for (auto const &student : grades)
		for (auto const &test : student)
			++frequency[test / 10];

	for (size_t count = 0; count < frequencySize; ++count)
	{
		if (0 == count)
			cout << " 0-9: ";
		else if (10 == count)
			cout << " 100: ";
		else
			cout << count * 10 << "-" << (count * 10) + 9 << ": ";
		for (unsigned int stars = 0; stars < frequency[count]; ++stars)
			cout << '*';
		cout << endl;
	}
}

void GradeBook::outputGrades() const
{
	cout << "\nThe grades are:\n\n";
	cout << "          ";
	for (size_t test = 0; test < tests; ++test)
		cout << "Test" << test + 1 << "  ";
	cout << "Average" << endl;

	for (size_t student = 0; student < grades.size(); ++student)
	{
		cout << "Student " << setw(2) << student + 1;

		for (size_t test = 0; test < grades[student].size(); ++test)
			cout<<setw(8)<<grades[student][test];

		double average = getAverage(grades[student]);
		cout << setw(9) << setprecision(2) << fixed << average << endl;

	}
}


fig7.24.cpp

#include<array>
#include"GradeBook.h"

using namespace std;

int main()
{
	array<array<int, GradeBook::tests>, GradeBook::students> grades =
	{
		87,96,70,
		68,87,90,
		94,100,90,
		100,81,82,
		83,65,85,
		78,87,65,
		85,75,83,
		91,94,100,
		76,72,84,
		87,93,73
	};

	GradeBook myGradeBook(
		"CS101 Introduction to C++ Programming", grades);

	myGradeBook.displayMessage();
	myGradeBook.processGrades();

	system("pause");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值