hahahia

Class GradeBook Using an Array to Store Grades 본문

Language/C++

Class GradeBook Using an Array to Store Grades

hahahia 2011. 5. 25. 17:33

#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
Comments