[OOP]hw001 Students

来源:互联网 发布:js获取url参数中文乱码 编辑:程序博客网 时间:2024/06/07 00:36

Write a program that asks you 10 records of students. Each record consists of a name (w/o space), and scores for three courses (in integer, 1 to 5). Output a list as the next page and calculate average score (in float) of each student and each course. Output the lowest and highest score for each course.

no      name    score1  score2  score3  average1       K.Weng  5       5       5       52       T.Dixon 4       3       2       33       V.Chu   3       4       5       44       L.Tson  4       3       4       3.666675       L.Lee   3       4       3       3.333336       I.Young 4       2       5       3.666677       K.Hiro  4       2       1       2.333338       G.Ping  4       4       4       49       H.Gu    2       3       4       310      J.Jon   5       4       3       4        average 3.8     3.4     3.6        min     2       2       1        max     5       5       5

/*  Name: student.h  Copyright:   Author: Amrzs  Date: 27/02/14 08:11  Description: the defination of student class */#ifndef STUDENT_H#define STUDENT_H#include <string>using namespace std;const int COURSE_NUM = 3;class Student {private:string name;int score[COURSE_NUM];public:Student(string name, int *score);string getName();int getScore(int k); //get the kth course's scoredouble getAveScore(); };#endif //STUDENT_H

/*  Name: student.cpp  Copyright:   Author: Amrzs  Date: 27/02/14 08:17  Description: the function of student class*/#include "student.h"#include <iostream>using namespace std;Student::Student(string name, int *score){this->name = name;for(int i=0; i<COURSE_NUM; i++)this->score[i] = score[i];}string Student::getName(){return this->name;}int Student::getScore(int k){if(k >= COURSE_NUM || k < 0){cout << "Dosen't exist this course!" << endl;return -1;}return this->score[k];}double Student::getAveScore(){double sum = 0;for(int i=0; i<COURSE_NUM; i++)sum += this->score[i];return sum/COURSE_NUM;}
/*  Name: main.cpp  Copyright:   Author: Amrzs  Date: 27/02/14 08:23  Description: the main program*/#include "student.h"#include <iostream>#include <cstring>#include <iomanip>using namespace std;const int STU_NUM = 10;//the number of studentsStudent *stu[STU_NUM];int main(){string name;int score[COURSE_NUM];int sumScore[COURSE_NUM], maxScore[COURSE_NUM], minScore[COURSE_NUM];//init arraysmemset(sumScore, 0, COURSE_NUM * sizeof(sumScore[0]));memset(maxScore, 0, COURSE_NUM * sizeof(maxScore[0]));memset(minScore, 0xE, COURSE_NUM * sizeof(minScore[0]));//init the student arrayfor(int i=0; i<STU_NUM; i++){cin >> name;for(int j=0; j<COURSE_NUM; j++)cin >> score[j];stu[i] = new Student(name, score);}//ouput the students' informationcout << "no\t" << "name\t" << "score1\t" << "score2\t" << "score3\t" << "average\t" << endl;for(int i=0; i<STU_NUM; i++){cout << i+1 << "\t";cout << stu[i]->getName() << "\t";for(int j=0; j<COURSE_NUM; j++){score[j] = stu[i]->getScore(j);sumScore[j] += score[j];maxScore[j] = max(maxScore[j], score[j]);minScore[j] = min(minScore[j], score[j]);cout << score[j] << "\t";}cout << stu[i]->getAveScore() << endl;delete stu[i];//free the memory}cout << "\t" << "average\t";for(int i=0; i<COURSE_NUM; i++){//printf("%.1f\t", (double)sumScore[i]/STU_NUM);//can't make sure how to use cout       cout << fixed << setprecision(1) << (double)sumScore[i]/STU_NUM << "\t";    }    cout << endl; cout << "\t" << "min\t";for(int i=0; i<COURSE_NUM; i++)cout << minScore[i] << "\t";cout << endl;cout << "\t" << "max\t";for(int i=0; i<COURSE_NUM; i++)cout << maxScore[i] << "\t";cout << endl;return 0;}


TestData.txt

K.Weng  5       5       5 T.Dixon 4       3       2       V.Chu   3       4       5       L.Tson  4       3       4       L.Lee   3       4       3       I.Young 4       2       5       K.Hiro  4       2       1       G.Ping  4       4       4       H.Gu    2       3       4       J.Jon   5       4       3

Readme.txt

firstly:     g++ main.cpp student.cppsecondly:     a.out < TestData.txtfinally:nonamescore1score2score3average1K.Weng55552T.Dixon43233V.Chu34544L.Tson4343.666675L.Lee3433.333336I.Young4253.666677K.Hiro4212.333338G.Ping44449H.Gu234310J.Jon5434average3.83.43.6min221max555



这是今天上午在机房所作的题目,当时用了dev c++,采用tab缩进,导致文本非常难看(在源代码中应当将tab改为4空格,而文本文件中采用tab缩进隔离)。

vim拷贝文本到剪贴板无法实现,最后用gedit复制了文本。

程序么,怎么说呢?我觉得类设计的还行,可以扩展,一些格式(除了tab缩进)都较为标准。特别是在写cout设置浮点精度的时候还查了一下,有所小收获。

最大的不足就是花费的时间太长,这个小程序整整花了2个小时,非常令我不满。不过要将自己的风格朝标准靠拢非常重要,还需要一段时间磨合。

还有就是这次准备写个makefile的,可是没有时间去查和学了,希望下次写程序能使用makefile。


本次程序收获:

用预编译命令保护头文件 #ifndef #define #endif

设计一个类,实现封装(属性隐蔽,暴露接口)

常量定义设计: 如果在类的外面用到,最后定义在类之外(COURSE_NUM),如果只在类内用到,那么定义在内部

使用cout实现精度控制:#include <iomanip> fixed setprecision(int) 等等


暴露出的不足:

tab缩进问题

vim多窗口切换不熟练

写程序时间过长

0 0
原创粉丝点击