[OOP]hw002 Student2

3595 ワード

Write a CLI program that reads scores and name of students, and prints out a summary sheet.
The user can input as many students as possible. One students can have as many courses as possible.
One course consists the name of the course and the marks the student got.
各学生のカリキュラム数が指定されていない場合は、可変配列(コンテナverctor)を使用してカリキュラム名とカリキュラムスコアを格納します.
// name: student.h
// author: Amrzs
// date: 2014/03/13

#ifndef STUDENT_H
#define STUDENT_H

#include 
#include 

using namespace std;

class Student{

private:
    string name;
    struct Course{
        string name;
        int score;
    };
    vector courses;

public:
    Student(string name);
    ~Student();
    
    int getSumScore();
    
    void setCourse(string name, int score);

    void printSummary();
};


#endif //STUDENT_H
// name: student.cpp
// author: Amrzs
// date: 2014/03/13

#include 
#include 

#include "student.h"

using namespace std;


Student::Student(string name):
    name(name){

}

Student::~Student(){

}

int Student::getSumScore(){
    
    int sum = 0, courseNum = courses.size();
    for(int i = 0; i < courseNum; i++)
        sum += courses[i].score;
    
    return sum;
}

void Student::setCourse(string name, int score){

    Course course;
    course.name = name;
    course.score = score;
    courses.push_back(course);        
}

void Student::printSummary(){

    cout << "Name: " << name << '\t' << endl;
    cout << "Courses: ";
    for(int i = 0; i < courses.size(); i++)    
        cout << courses[i].name << " " << courses[i].score << '\t';
    cout << "Sum: " << getSumScore() << '\t' << endl;
}
// name: main.cpp
// author: Amrzs
// date: 2014/03/13

#include 
#include 
#include 

#include "student.h"

using namespace std;

int main(){

    vector stuVec;
    Student *pStu = NULL;

    string name;
    int score;

    while(true){
        cout << "Please input a student name, " 
            << "if you want to quit, input quit" << endl;
        
        cin >> name;
        if("quit" == name)
            break;
        pStu = new Student(name);
        
        cout << "Please input courses' name and score, "
            << "if end then input 0 0" << endl;

        cin >> name >> score;
        while(name != "0"){
            pStu->setCourse(name, score);
            cin >> name >> score;
        }

        stuVec.push_back(pStu);
    }

    for(int i = 0; i < stuVec.size(); i++){
        stuVec[i]->printSummary();
        delete stuVec[i];
    }
    
    return 0;
}

makefile:
CPP = g++
OFLAG = -o
TARGET = a.out 
OBJS = main.o student.o

$(TARGET): $(OBJS)
	$(CPP) $(OFLAG) $(TARGET) $(OBJS)
main.o: main.cpp student.o
student.o: student.cpp

.PHONY: clean
clean:
	-rm $(TARGET) $(OBJS)

プロセスの実行:
$ make
$ ./a.out < testData.txt
....
$make clean
大功は成し遂げたが、いくつかの不足がある.
1.机械室でmakefileの书き方を忘れて、帰ってきた时に本と陈皓を読んでMakefileを勉强してくれました.今のmakefileも间に合うしかありません.
2.main.cppに新しい学生は関数を追加し、可読性を高めるべきで、main()に多くのことを書くのはよくありません.
3.1つの容器を使用しているが、なんだか規範的ではない
4.コードの速度はやはり少し遅くて、給油しなければなりません