C++课程设计:研究生初试录取程序

要求:

研究生考试课程为4门,其中数学、外语、政治为统一命题,而专业基础课则根据不同的专业由招生学校自行命题。国家对初试录取分数有总分要求(如某一年要求四门课总分应达到310分),另外还有对每门课的最低分数要求(如总分为100的试卷最低应达到40分,总分为150的试卷最低应达到65分)。

基本要求

(1)编程统计初试合格的人数,并按总分由高到低的顺序输出合格考生的信息。

(2)程序运行时从原始数据文件中读取以下信息:考生姓名,准考证号,报考专业,是否应届生,4门课程(政治、数学、外语、专业基础课)成绩;

(3)输入录取的总分要求,各课程的最低分数要求。

(4)输出过线考生的姓名、准考证号、报考专业、是否应届生、4门课程(政治、数学、外语、专业基础课)成绩及总分,并保存在另一个文件中。


实现:

满足了题目所有的要求,但是没有进行扩展。

student.h代码:

除了基本的set,get函数。额外添加了输入输出流的重载,以及检查有没有过线的GetisPassed函数。

#ifndef STUDENT_H
#define STUDENT_H

#include <string>
#include <iostream>
#include <iomanip>

using std::string;
using std::cin;
using std::setw;

class student
{
    friend std::istream& operator>> (std::istream &, student& );
    friend std::ostream& operator<< (std::ostream &, student& );

    public:
        student(){}
        student(const string& na,const string &id,const string& maj,const string& isF,
                const int& politics,const int& math,const int& english,const int& major);
                //
        void SetAll(const string& na,const string &id,const string& maj,const string& isF,
                const int& politics,const int& math,const int& english,const int& major);
        //
        string Getname() { return name; }
        void Setname(string val) { name = val; }

        string GetID() { return ID; }
        void SetID(string val) { ID = val; }

        string Getmajor() { return major; }
        void Setmajor(string val) { major = val; }

        string GetisFresh() { return isFresh; }
        void SetisFresh(string val) { isFresh = val; }
        //
        int GetscorePolitics() { return scorePolitics; }
        void SetscorePolitics(int val) { scorePolitics = val; }

        int GetscoreMath() { return scoreMath; }
        void SetscoreMath(int val) { scoreMath = val; }

        int GetscoreEnglish() { return scoreEnglish; }
        void SetscoreEnglish(int val) { scoreEnglish = val; }

        int GetscoreMajor() { return scoreMajor; }
        void SetscoreMajor(int val) { scoreMajor = val; }
        //
        int GetscoreTotal() { return scoreTotal; }
        void Settotal();
        //
        bool GetisPassed(const int& , const int& , const int&, const int& , const int& );

    private:
        string name;
        string ID;
        string major;
        string isFresh;
        int scorePolitics;
        int scoreMath;
        int scoreEnglish;
        int scoreMajor;
        int scoreTotal;
};

#endif // STUDENT_H

student.cpp代码:

构造函数直接读入8项主要信息,顺便计算总分。

#include "student.h"

student::student(const string& na,const string &id,
                 const string& maj,const string& isF,
                 const int& politics,const int& math,
                 const int& english,const int& major)
{
    SetAll(na, id ,maj ,isF,
           politics ,math ,english, major);
    Settotal();
}

void student::SetAll(const string& na,const string &id,
                     const string& maj,const string& isF,
                     const int& politics,const int& math,
                     const int& english,const int& major)
{
    Setname(na);
    SetID(id);
    Setmajor(maj);
    SetisFresh(isF);
    SetscorePolitics(politics);
    SetscoreMath(math);
    SetscoreEnglish(english);
    SetscoreMajor(major);
}

void student::Settotal()
{
    scoreTotal = scorePolitics
        + scoreMath
        + scoreEnglish
        + scoreMajor;
}

std::istream& operator >> (std::istream &input, student& s)
{
    input >> s.name >> s.ID >> s.major >> s.isFresh
          >> s.scorePolitics >> s.scoreMath
          >> s.scoreEnglish >> s.scoreMajor;
    s.Settotal();
    return input;
}

std::ostream& operator<< (std::ostream &output, student& s)
{
    output << setw(10) << s.name;
    output << setw(10) << s.ID;
    output << setw(10) << s.major;
    output << setw(10) << s.isFresh;
    output << setw(10) << s.scorePolitics;
    output << setw(10) << s.scoreMath;
    output << setw(10) << s.scoreEnglish;
    output << setw(10) << s.scoreMajor;
    output << setw(10) << s.scoreTotal << "\n";
    return output;
}

bool student::GetisPassed(const int& po, const int& ma,
                          const int& en, const int& maj,
                          const int& tot)
{
    if( this->GetscorePolitics() >= po
        && this->GetscoreEnglish() >= en
        && this->GetscoreMath() >= ma
        && this->GetscoreMajor() >= maj
        && this->GetscoreTotal() >= tot)
     {
         return true;
     }

     return false;
}

main部分:

message.txt 文件读入,vector存储,控制台输出并保存在out.txt 文件。

利用sort+lambada表达式排序,然后直接输出。

#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include "student.h"

using namespace std;

int main()
{
    vector<student> stu;
    student temp;
    ifstream inFile("D:\\message.txt");
    ofstream outFile("D:\\out.txt");

    if(!inFile || !outFile)
    {
        cerr << "file error!" << endl;
    }

    int po, en, ma, maj, tot;

    cout << "Please input the message of 4 objects score and total score\n";
    cin >> po >> en >> ma >> maj >> tot;

    while(inFile >> temp)
    {
        if(temp.GetisPassed(po, en, ma, maj, tot))
            stu.push_back(temp);
    }

    sort(stu.begin(), stu.end(), [](student& a, student& b)
            {return a.GetscoreTotal() > b.GetscoreTotal();});

    cout << "\nThe passed students are:\n\n";

    cout << setw(10) << "Name";
    cout << setw(10) << "ID";
    cout << setw(10) << "Major";
    cout << setw(10) << "isFresh";
    cout << setw(10) << "Politics";
    cout << setw(10) << "English";
    cout << setw(10) << "Math";
    cout << setw(10) << "Major";
    cout << setw(10) << "Total" << "\n";

    for(auto &x : stu)
    {
        cout << x;
        outFile << x;
    }

    return 0;
}

message.txt文件:

小明 001 物理 Y 100 100 100 90
小白 002 化学 Y 100 100 100 80
小红 003 计算机 Y 100 100 100 70
小黄 004 生物 Y 100 100 100 60
小蓝 005 电子 N 100 100 100 99

控制台读入:

70 70 70 70 380

out.txt文件:

      小蓝       005      电子         N       100       100       100        99       399
      小明       001      物理         Y       100       100       100        90       390
      小白       002      化学         Y       100       100       100        80       380

猜你喜欢

转载自blog.csdn.net/qq_40526226/article/details/86104190