일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
- Kafka
- 투자
- beans
- API
- C
- UTF-8
- windows
- Call-by-reference
- Sort
- request
- array
- HTML
- System
- query
- OOP
- JavaScript
- meta
- jsp
- 악성코드
- 포인터
- WebProgramming
- 윈도우즈
- 자료구조
- function
- CLASS
- algorithm
- c++
- java
- CSS
- 노드
- Today
- Total
hahahia
Class GradeBook Using an Array to Store Grades 본문
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class GradeBook{
public:
static const int students = 10;
GradeBook(string, const int []);
void setCourseName(string);
string getCourseName();
void displayMessage();
void processGrades();
int getMinimum();
int getMaximum();
double getAverage();
void outputBarChart();
void outputGrades();
private:
string courseName;
int grades[students];
};
GradeBook::GradeBook(string name, const int gradesArray[])
{
setCourseName(name);
for(int grade=0; grade < students; grade++)
grades[grade] = gradesArray[grade];
}
void GradeBook::setCourseName(string name)
{
courseName = name;
}
string GradeBook::getCourseName()
{
return courseName;
}
void GradeBook::displayMessage()
{
cout << "Welcome to the grade book for\n" << getCourseName() << "!" << endl;
}
void GradeBook::processGrades()
{
outputGrades();
cout << "\nClass average is " << setprecision(2) << fixed <<
getAverage() << "\nLowest grade is " << getMinimum() <<
"\nHighest grade is " << getMaximum() << endl;
outputBarChart();
}
int GradeBook::getMinimum()
{
int lowGrade = 100;
for(int grade = 0; grade < students; grade++)
{
if(grades[grade] < lowGrade)
{
lowGrade = grades[grade];
}
}
return lowGrade;
}
int GradeBook::getMaximum()
{
int MaxGrade = 0;
for(int grade = 0; grade < students; grade++)
{
if(grades[grade] > MaxGrade)
{
MaxGrade = grades[grade];
}
}
return MaxGrade;
}
double GradeBook::getAverage()
{
int total = 0;
for(int grade = 0; grade < students; grade++)
total += grades[grade];
return static_cast<double>(total)/students;
}
void GradeBook::outputBarChart()
{
cout << "\nGrade distribution:" << endl;
const int frequencySize = 11;
int frequency[frequencySize] = {};
for(int grade = 0; grade < students; grade++)
{
frequency[grades[grade] / students ] ++;
}
for(int count=0; count < frequencySize; count++)
{
if(count == 0)
{
cout << "0~9: ";
}
else if(count == 10)
{
cout << "100: " ;
}
else
{
cout << count * 10 << "~" << count*10 + 9 << ": " ;
}
for(int stars = 0 ; stars < frequency[count]; stars++)
{
cout << "*";
}
cout << endl;
}
}
void GradeBook::outputGrades()
{
cout << "\nThe grades are:\n\n";
for(int student = 0; student < students; student++)
{
cout << "Student : " << setw(2) << student + 1 << ": " << setw(3)
<< grades[student] << endl;
}
}
int main()
{
int gradesArray[GradeBook::students] = {87, 68, 94, 100, 83, 78, 85, 91, 76, 87};
GradeBook myGradeBook("CS101 Introduction to C++ Programming", gradesArray);
myGradeBook.displayMessage();
myGradeBook.processGrades();
return 0;
}
'Language > C++' 카테고리의 다른 글
Operator Overloading(연산자 오버로딩) (0) | 2012.09.01 |
---|---|
1주차 (0) | 2011.06.27 |
재귀함수를 이용한 fibonacci (0) | 2011.05.17 |
간단한 Class프로그램을통한 Class의 개념과 특징 (0) | 2011.05.10 |
5주차 객체 과제 (0) | 2011.05.10 |