wisc cs368

帮基友撸了个C++的大作业。不得不说国内外的cs课程的质量还是有点差别的。记得当时自己学c++的时候,大作业最多也就是实现个算法吧。根本没有这个作业这样,完整的把面向对象的思想以及纯虚函数、继承等等整合的这么好了。仔细写的话还是蛮有收获的。

GradStudent.hpp

///////////////////////////////////////////////////////////////////////////////

// File Name:      GradStudent.hpp

//

// Author:         Gerald

// CS email:       gerald@cs.wisc.edu

//

// Description:    The header file for the GradStudent class.

//

// IMPORTANT NOTE: This file should NOT be modified.

///////////////////////////////////////////////////////////////////////////////



#ifndef A3_GRADSTUDENT_HPP

#define A3_GRADSTUDENT_HPP



#include "Student.hpp"



/**

 * @brief A Graduate Student class.

 */

class GradStudent : public Student {

private:

    std::string researchArea;

    std::string advisor;

    static int numGradStudents;



public:

    /**

     * @brief Constructor for a graduate student.

     *

     * @param name Graduate student's name.

     * @param yearOfBirth Graduate student's YOB.

     * @param assignmentsScore Graduate student's assignment scores.

     * @param projectScore Graduate student's project score.

     * @param researchArea Graduate student's research area.

     * @param advisor Graduate student's advisor.

     */

    GradStudent(std::string name,

                int yearOfBirth,

                const std::vector<double> &assignmentsScore,

                double projectScore,

                std::string researchArea,

                std::string advisor);



    /**

     * @brief Getter for the student's research area.

     *

     * @return The research area of the student.

     */

    std::string getResearchArea();



    /**

     * @brief Getter for the student's advisor.

     *

     * @return The advisor of the student.

     */

    std::string getAdvisor();



    /**

     * @brief Get the total number of graduate students.

     *

     * @return The number of graduate students.

     */

    static int getNumStudents();



    virtual void printDetails() override;



    virtual double getTotal() override;



    virtual std::string getGrade() override;

};



#endif //A3_GRADSTUDENT_HPP

GradStudent.cpp



#include "GradStudent.hpp"

#include <iostream>
#include <algorithm>
#include <cctype>
#include <fstream>
#include <sstream>
#include <string>
#include <typeinfo>
#include <numeric>
using namespace std;


int GradStudent::numGradStudents = 0;

GradStudent::GradStudent(std::string name_,
                int yearOfBirth_,
                const std::vector<double> &assignmentsScore_,
                double projectScore_,
                std::string researchArea_,
                std::string advisor_):Student(name_,
                yearOfBirth_,
                assignmentsScore_,
                projectScore_)
{

   researchArea = researchArea_;
   advisor = advisor_;
   ++numGradStudents;
}

std::string GradStudent::getResearchArea() {
    return researchArea;
}

std::string GradStudent::getAdvisor() {
    return advisor;
}

int GradStudent::getNumStudents(){
    return numGradStudents;
}

void GradStudent::printDetails() {
    Student::printDetails();
//    cout << "researchArea = " << getResearchArea()<< endl;
    cout << "Type = Graduate Student" << endl;
    cout << "Research Area = " << getResearchArea() << endl;
    cout << "Advisor = " << getAdvisor() << endl;
}

double GradStudent::getTotal() {
    std::vector<double> v = getAssignmentsScore();
    double average = accumulate(v.begin(), v.end(), 0.0)/v.size();
    double total_grade = (average + getProjectScore())/2.0;
    return total_grade;
}

    /**
     * @brief Get the letter grade obtained by a student.
     *
     * @return The letter grade of the student. The possible letter grades are "CR" and "N".
     */

std::string GradStudent::getGrade() {
    double tot_grade = getTotal();
    if ( tot_grade >= 80) {
        return "CR";
    } else {
        return "N";
    }
}

UndergradStudent.hpp

///////////////////////////////////////////////////////////////////////////////

// File Name:      UndergradStudent.hpp

//

// Author:         Gerald

// CS email:       gerald@cs.wisc.edu

//

// Description:    The header file for the UndergradStudent class.

//

// IMPORTANT NOTE: This file should NOT be modified.

///////////////////////////////////////////////////////////////////////////////



#ifndef A3_UNDERGRADSTUDENT_HPP

#define A3_UNDERGRADSTUDENT_HPP



#include "Student.hpp"



#include <string>

#include <vector>



/**

 * @brief An undergraduate student class.

 */

class UndergradStudent : public Student {

private:

    std::string residenceHall;

    std::string yearInCollege;

    static int numUndergradStudents;



public:

    /**

     * @brief Constructor for an undergraduate student.

     *

     * @param name Undergraduate student's name

     * @param yearOfBirth Undergraduate student's YOB

     * @param assignmentsScore Undergraduate student's assignment scores.

     * @param projectScore Undergraduate student's project score.

     * @param residenceHall Undergraduate student's residence hall.

     * @param yearInCollege Undergraduate student's year in college.

     */

    UndergradStudent(std::string name,

                     int yearOfBirth,

                     const std::vector<double> &assignmentsScore,

                     double projectScore,

                     std::string residenceHall,

                     std::string yearInCollege);
//    ::Student(name,
//.
//    yearOfBirth,
//
//    &assignmentsScore,
//
//    projectScore);



    /**

     * @brief Getter for a student's residence hall.

     *

     * @return The residence hall in which the student resides.

     */

    std::string getResidenceHall();



    /**

     * @brief Getter for a student's year in college.

     *

     * @return The year in college of the student.

     */

    std::string getYearInCollege();



    /**

     * @brief Get the total number of undergraduate students.

     *

     * @return The number of undergraduate students.

     */

    static int getNumStudents();



    virtual void printDetails() override;



    virtual double getTotal() override;



    virtual std::string getGrade() override;

};



#endif //A3_UNDERGRADSTUDENT_HPP

UndergradStudent.cpp

//
// Created by Adalbert Gerald Soosai Raj on 10/26/16.
//


#include "UndergradStudent.hpp"


#include <iostream>
#include <algorithm>
#include <cctype>
#include <fstream>
#include <sstream>
#include <string>
#include <typeinfo>
#include <numeric>
using namespace std;

int UndergradStudent::numUndergradStudents = 0;

UndergradStudent::UndergradStudent(std::string name_,

                                   int yearOfBirth_,

                                   const std::vector<double> &assignmentsScore_,

                                   double projectScore_,

                                   std::string residenceHall_,

                                   std::string yearInCollege_):Student(name_, yearOfBirth_, assignmentsScore_, projectScore_)
{

//    name = name_;
//    yearOfBirth = yearOfBirth_;
//    assignmentsScore.assign(assignmentsScore_.begin(), assignmentsScore_.end());
//    projectScore = projectScore_;
    residenceHall = residenceHall_;
    yearInCollege = yearInCollege_;
    ++numUndergradStudents;
}


//UndergradStudent::UndergradStudent(std::string name,
//                     int yearOfBirth,
//                     const std::vector<double> &assignmentsScore,
//                     double projectScore,
//                     std::string residenceHall,
//                     std::string yearInCollege) : Student(std::string name,
//            int yearOfBirth,
//            const std::vector<double> &assignmentsScore,
//            double projectScore) {
//    UndergradStudent::residenceHall = residenceHall;
//    UndergradStudent::yearInCollege = yearInCollege;
//    ++numUndergradStudents;
//}//blank?


std::string UndergradStudent::getResidenceHall() {
    return residenceHall;
}

std::string UndergradStudent::getYearInCollege() {
    return yearInCollege;
}

int UndergradStudent::getNumStudents() {
    return numUndergradStudents;
}

void UndergradStudent::printDetails() {
    Student::printDetails();
    cout << "Type = Undergraduate Student" << endl;
    cout << "Residence Hall = " << getResidenceHall() << endl;
    cout << "Year in College = " << getYearInCollege() << endl;
}

double UndergradStudent::getTotal() {
    const std::vector<double> &v = getAssignmentsScore();
    double average = accumulate(v.begin(), v.end(), 0.0)/v.size();
    double total_grade = average * 0.7 + getProjectScore() * 0.3;
    return total_grade;
}

    /**
     * @brief Get the letter grade obtained by a student.
     *
     * @return The letter grade of the student. The possible letter grades are "CR" and "N".
     */
std::string UndergradStudent::getGrade() {
    double tot_grade = getTotal();
    if ( tot_grade >= 70) {
        return "CR";
    } else {
        return "N";
    }
}

Student.hpp

///

// File Name:      Student.hpp

//

// Author:         Gerald

// CS email:       gerald@cs.wisc.edu

//

// Description:    The header file for the Student class.

//

// IMPORTANT NOTE: You should NOT add/modify/remove any PUBLIC methods.

//                 If you need, you may add some PRIVATE methods.

///



#ifndef A3_STUDENT_HPP

#define A3_STUDENT_HPP



#include <string>

#include <vector>



const int current_year = 2016;



/**

 * @brief An abstract base class for a Student.

 */

class Student {

private:

    // a student's unique id.

    int id;

    std::string name;

    int yearOfBirth;

    std::vector<double> assignmentsScore;

    double projectScore;

    static int numStudents;



/*Private methods, if any should be added BELOW this line*/





/*Private methods, if any should be added ABOVE this line*/



public:

    /**

     * @brief A parameterised constructor for a Student.

     *

     * @param name Student's name.

     * @param yearOfBirth Student's year of birth.

     * @param assignmentsScore Student's assignment scores.

     * @param projectScore Student's project score.

     */

    Student(std::string name,

            int yearOfBirth,

            const std::vector<double> &assignmentsScore,

            double projectScore);

//    Student();


    /**

     * @brief Getter for student's id.

     *

     * @return The id of the student.

     */

    int getId();



    /**

     * @brief Getter for student's name.

     *

     * @return The name of the student.

     */

    std::string getName();



    /**

     * @brief Getter for student's YOB.

     *

     * @return The YOB of the student.

     */

    int getYearOfBirth();



    /**

     * @brief Get the age of a student.

     *

     * @return The student's age.

     */

    int getAge();



    /**

     * @brief Getter for student's assignment scores.

     *

     * @return A const reference to the vector of student's assignment scores.

     */

    const std::vector<double> &getAssignmentsScore();



    /**

     * @brief Getter for student's project score.

     *

     * @return The project score of the student.

     */

    double getProjectScore();



    /**

     * @brief Get the total number of students.

     *

     * @return The total number of students.

     */

    static int getNumStudents();



    /**

     * @brief Prints the details of the student.

     *

     * @example This method prints the following details of a student.

     * Id = 10

     * Name = Rex Fernando

     * Age =  19

     * Assignments = [57, 90, 81, 96, 80, 95, 78]

     * Project = 98

     * Total = 90.2143

     * Grade = CR

     */

    virtual void printDetails();



    /**

     * @brief Get the total score of a student.

     *

     * @return The total score of the student.

     */

    virtual double getTotal() = 0;



    /**

     * @brief Get the letter grade obtained by a student.

     *

     * @return The letter grade of the student. The possible letter grades are "CR" and "N".

     */

    virtual std::string getGrade() = 0;

};



#endif //A3_STUDENT_HPP

Student.cpp

///
// File Name:     Student.cpp
//
// Author:         1
// CS email:       1
//
// Description:    Methods to perform some processing on student objects.
///


#include "Student.hpp"

#include <iostream>
#include <algorithm>
#include <cctype>
#include <fstream>
#include <sstream>
#include <string>
#include <typeinfo>
#include <numeric>
#include <vector>
using namespace std;


int Student::numStudents = 1;
//const int current_year = 2016;
//int dd = 0;
//Student::Student() {
//    ++Student::numStudents;
//}

//Student::Student();

Student::Student(std::string name_,

                 int yearOfBirth_,

                 const std::vector<double> &assignmentsScore_,

                 double projectScore_){
    id = numStudents++;
    name = name_;
    yearOfBirth = yearOfBirth_;
    assignmentsScore.assign(assignmentsScore_.begin(), assignmentsScore_.end());
    projectScore = projectScore_;
}


int Student::getId() {
    return id;
}


std::string Student::getName() {
    return name;
}

int Student::getYearOfBirth() {
    return yearOfBirth;
}

int Student::getAge(){
    return (current_year - this->yearOfBirth);    
}

const std::vector<double> &Student::getAssignmentsScore(){
    return assignmentsScore;
}

double Student::getProjectScore(){
    return projectScore;    
}

 int Student::getNumStudents(){
    return (numStudents-1);
}

    /**
     * @brief Prints the details of the student.
     *
     * @example This method prints the following details of a student.
     * Id = 10
     * Name = Rex Fernando
     * Age =  19
     * Assignments = [57, 90, 81, 96, 80, 95, 78]
     * Project = 98
     * Total = 90.2143
     * Grade = CR
     */

void Student::printDetails() {
        vector<double>::iterator it;
        std::cout<< "STUDENT DETAILS:" << endl ;
    std::cout<< "Id = " << getId() << endl ;
    std::cout<< "Name = " << getName() << endl;
    std::cout<< "Age = " << getAge() << endl;
    std::cout<< "Assignments = " << "[";
        std::vector<double> zz= getAssignmentsScore();
    for(auto it = zz.begin(); it!= zz.end() ;it++)
    {
            cout<< (*it) << "," << " ";
    }
        cout<< "]" << endl;

    std::cout<< "Project = " << getProjectScore() << endl;
    std::cout<< "Total = " << getTotal() << endl;
    std::cout<< "Grade = " << getGrade() << endl;
}


    /**
     * @brief Get the total score of a student.
     *
     * @return The total score of the student.
     */


//#endif //A3_STUDENT_HPP

processStudent.hpp

///

// File Name:      processStudent.hpp

//

// Author:         Gerald

// CS email:       gerald@cs.wisc.edu

//

// Description:    Methods to perform some processing on student objects.

//

// IMPORTANT NOTE: This file should NOT be modified.

///



#ifndef A3_PROCESSSTUDENT_HPP

#define A3_PROCESSSTUDENT_HPP



#include <fstream>

#include <functional>

#include <vector>



#include "GradStudent.hpp"

#include "UndergradStudent.hpp"



// The maximum number of assignments.

static const int max_assignments = 7;



// The delimiter character in students.txt input file.

static const char dlm = ',';



/**

 * @brief Read students' data from an input file and store them in 2 vectors

 *        based on the type of the student.

 *

 * @param inFile The input file with students' data.

 * @param gstudents Vector of graduate students.

 * @param ugstudents Vector of undergraduate students.

 */

void fillStudents(std::ifstream &inFile,

                  std::vector<GradStudent> &gstudents,

                  std::vector<UndergradStudent> &ugstudents);



/**

 * @brief Prints the details of a group of students.

 *

 * @param students A vector of student references to be printed.

 */

void printStudents(const std::vector<std::reference_wrapper<Student>> &students);



/**

 * @brief Computes the statistics like number of students, mean of total score, and

 *        the sorted list of students in descending order based on their total.

 *

 * @param students A vector of student references for which the statistics needs to computed.

 */

void computeStatistics(std::vector<std::reference_wrapper<Student>> &students);



#endif //A3_PROCESSSTUDENT_HPP

* processStudent.cpp*

///
// File Name:      processStudent.cpp
//
// Author:         1
// CS email:       <your CS email>
//
// Description:    Methods to perform some processing on student objects.
///

#include "processStudent.hpp"

#include <algorithm>
#include <cctype>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <typeinfo>
#include <vector>

using namespace std;





void fillStudents(std::ifstream &inFile,

                  std::vector<GradStudent> &gstudents,

                  std::vector<UndergradStudent> &ugstudents) {

    std::vector<std::string> words;
    std::string line;

    while(getline(inFile, line)) {

        words.clear();

        while (line.size()) {


            int i = line.find(",");

            if (i != std::string::npos) {

                words.push_back(line.substr(0, i));

                line = line.substr(i + 1);

            } else {

                words.push_back(line);

                line = "";

            }

        }


        std::string UorG;
//        char UorG;
        std::string name;
        std::string yearAsString;

        std::string p_gradeAsString;
        std::string other1;
        std::string other2;

        UorG = words[0];
        name = words[1];
        yearAsString = words[2];


        std::vector<std::string>::const_iterator first = words.begin() + 3;
        std::vector<std::string>::const_iterator last = words.begin() + 10;
        std::vector<std::string> a_gradeAsString(first, last);


        p_gradeAsString = words[10];
        other1 = words[11];
        other2 = words[12];
        int year = std::stoi(yearAsString);
        int p_grade = std::stoi(p_gradeAsString);

        std::vector<double> a_grade_all;
        for (int i = 0; i != a_gradeAsString.size(); ++i) {
            a_grade_all.push_back(stoi(a_gradeAsString[i]));
        }


        if (UorG == "U") {
            UndergradStudent u2(name, year, a_grade_all, p_grade, other1, other2);
            ugstudents.push_back(u2);
        }
        else {
            GradStudent g2(name, year, a_grade_all, p_grade, other1, other2);
            gstudents.push_back(g2);
        }
        // TODO: Implement this method.
    }

}

void printStudents(const std::vector<std::reference_wrapper<Student>> &studentRefs) {
    for (auto it = studentRefs.begin(); it != studentRefs.end(); ++it) {
        it->get().printDetails();
        std::cout << std::endl;
    }

    // TODO: Implement this method.

}

bool cmp(std::reference_wrapper<Student> a, std::reference_wrapper<Student> b )
{
    return a.get().getTotal() > b.get().getTotal();
}

void computeStatistics(std::vector<std::reference_wrapper<Student>> &students) {
//    std::cout << "This part is statistics" << endl << endl;
    // TODO: Implement this method.
    std::cout << "Number of students = " << students.size() << endl;
    double tot = 0;
    for (auto it = students.begin(); it != students.end(); ++it)
        tot += it->get().getTotal();
    std::cout << "The mean of the total score = " << tot/students.size() <<endl;
    std::cout << "The sorted list of students (id, name, total, grade) in descending order of total:" << endl;
    sort(students.begin(), students.end(), cmp);
    for(auto it = students.begin(); it != students.end(); ++it)
    {
        std::cout<< it->get().getId() << "," << " ";
        std::cout<< it->get().getName() << "," << " ";
        std::cout<< it->get().getTotal() << "," << " ";
        std::cout<< it->get().getGrade() << endl ;
    }

    std::cout << endl;


    // compute the # of students based on the type of students.

    // compute the mean of the total score.

    // sort and print the students based on their total.
}

main.cpp

///
// File Name:      main.cpp
//
// Author:         Gerald
// CS email:       gerald@cs.wisc.edu
//
// Description:    The main program for a3.
//
// IMPORTANT NOTE: This file should NOT be modified.
///

#include <iostream>
#include <fstream>
#include <functional>
#include <vector>

#include "GradStudent.hpp"
#include "processStudent.hpp"
#include "UndergradStudent.hpp"

/**
 * @brief The program execution begins here.
 * @param argc The number of command-line arguments.
 * @param argv The command line arguments.
 * @return 0 for normal program termination, -1 for abnormal termination.
 */
int main(int argc, char *argv[]) {

    // Check if the program is given command line arguments.
    if (argc != 2) {
        std::cerr << "USAGE: " << argv[0] << " <students.txt>" << std::endl;
        return 1;
    }

    // Open the students.txt input file.
    std::ifstream inFile(argv[1]);
    if (!inFile.is_open()) {
        std::cerr << "Input file " << argv[1] << " cannot be opened!" << std::endl;
        return 1;
    }

    std::vector<GradStudent> gstudents;
    std::vector<UndergradStudent> ugstudents;

    fillStudents(inFile, gstudents, ugstudents);

    std::cout << "The total number of students in the class = "
              << Student::getNumStudents() << std::endl << std::endl;

    // create a vector of references for undergraduate students.
    std::vector<std::reference_wrapper<Student>> ugstudentRefs;
    for (auto it = ugstudents.begin(); it != ugstudents.end(); ++it) {
        ugstudentRefs.push_back(*it);
    }

    std::cout << "UNDERGRADUATE STUDENT INFORMATION" << std::endl;
    std::cout << "---------------------------------" << std::endl;
    printStudents(ugstudentRefs);

    std::cout << "UNDERGRADUATE STUDENT STATISTICS" << std::endl;
    std::cout << "--------------------------------" << std::endl;
    computeStatistics(ugstudentRefs);

    // create a vector of references for graduate students.
    std::vector<std::reference_wrapper<Student>> gstudentRefs;
    for (auto it = gstudents.begin(); it != gstudents.end(); ++it) {
        gstudentRefs.push_back(*it);
    }

    std::cout << "GRADUATE STUDENT INFORMATION" << std::endl;
    std::cout << "----------------------------" << std::endl;
    printStudents(gstudentRefs);

    std::cout << "GRADUATE STUDENT STATISTICS" << std::endl;
    std::cout << "---------------------------" << std::endl;
    computeStatistics(gstudentRefs);

    return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值