//_7_10_main.cpp #include "GradeBook.h" #include <ctime> #include <cstdlib> int main() { int gradesArray[GradeBook::students][GradeBook::tests]; //随机产生学生成绩 srand((unsigned)time(NULL)); for(int i=0;i<GradeBook::students;i++) for(int j=0;j<GradeBook::tests;j++) gradesArray[i][j] = rand()%101; GradeBook myGradeBook("C++",gradesArray); myGradeBook.displayMessage(); myGradeBook.processGrades(); system("pause >> cout"); return 0; }
//GradeBook.h #include <string> using namespace std; class GradeBook { public: //要设置成静态的const常量,,, static const int students = 10;//参加考试的学生 static const int tests = 3;//测试的次数 GradeBook(string,const int[][tests]); void setCourseName(string); string getCourseName(); void displayMessage();//打印一条欢迎信息! void processGrades();//综合下面函数的功能,,,, int getMaximum();//得到学生成绩中的最大值 int getMinimum();//得到学生成绩中的最小值 //计算该门课程学生成绩的平均值 double getAverage(const int[],const int); void outputBarChart();//输出学生成绩的柱状图 void outputGrades();//输出学生的成绩 private: string courseName;//专业名称 int grades[students][tests];//学生成绩的二维数组 };
//GradeBook.cpp #include "GradeBook.h" #include <iostream> #include <iomanip> using namespace std; GradeBook::GradeBook(string name,const int a[][tests]) { setCourseName(name); for(int i=0;i<students;i++)//将调用的数组信息复制到类的对象的数组中 for(int j=0;j<tests;j++) grades[i][j] = a[i][j]; } 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 << "Lowest grade is " << getMinimum() << endl; cout << "Highest grade is " << getMaximum() << endl; outputBarChart(); } int GradeBook::getMaximum()//得到学生成绩中的最大值 { int max = 0; for(int k=0;k<students;k++) for(int h=0;h<tests;h++) { if(max<grades[k][h]) max = grades[k][h]; } return max; } int GradeBook::getMinimum()//得到学生成绩中的最小值 { int min = 100; for(int k=0;k<students;k++) for(int h=0;h<tests;h++) { if(min>grades[k][h]) min = grades[k][h]; } return min; } //计算该门课程学生成绩的平均值 double GradeBook::getAverage(const int setOfGrades[],const int grades) { int sum = 0; for(int k=0;k<grades;k++) sum+=setOfGrades[k]; return (static_cast<double>(sum))/grades ; } void GradeBook::outputBarChart()//输出学生成绩的柱状图 { cout << "\nGrades distribution:" << endl;//学生的成绩分布 const int frequencySize = 11; int frequency[frequencySize] = {0}; for(int i=0;i<students;i++) for(int j=0;j<tests;j++) { frequency[grades[i][j]/10] ++ ; } 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 star=0;star<frequency[count];star++) cout << "*" ; cout << endl; } } void GradeBook::outputGrades()//输出学生的成绩 { cout << "\nThe grades are :" << endl; cout << " "; for(int i=0;i<tests;i++) cout << "Test" << i+1 << " "; cout << "Average" << 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 average = getAverage(grades[student],tests); cout << setw(9) << setprecision(2) << fixed << average << endl; }//end for }//end function