实现成员函数(排序)

时间:2016-10-14 18:28:45

标签: c++ member-functions

我需要使用成员函数(我相信在我的Date结构中)按照最早被录取的顺序对医院人员进行排序,然后在列表中排序。我可以简单地添加一个Sorting();我的Date结构中的“function”绑定到我的printHospitalPersonnel函数,并在调用排序菜单选项时打印出有序列表?下面是我的代码,谢谢!

#include <iostream>
#include <iomanip> 
#include <fstream> 
#include <string.h> 
#include <cstdlib>
using namespace std;

/*
Structure to store the date
*/
struct Date
{
    int month;
    int day;
    int year;
};

/*
Structure to store HospitalPersonnels data
*/
struct HospitalPersonnel
{
    string firstname;
    string lastname;
    string ID;
    string role;

    string dutyDay0;
    string dutyDay1;
    string dutyDay2;

    //for patients
    char exitFlag;
    Date admitDate;
    Date exitDate;
};

HospitalPersonnel* readHospitalPersonnelFromFile(int &totalHospitalPersonnels);
void menu(HospitalPersonnel *hospitalPersonnel, int totalHospitalPersonnel);
void printHorizontalLine( int width, char border_char );
void printHospitalPersonnel(HospitalPersonnel *hospitalPersonnel, int totalHospitalPersonnel, string roleFlag, char patientExitFlag);
string getFullNameForDay(string dayAbbreviation);

/*
* Entry point
*/
int main()
{
    HospitalPersonnel *allHospitalPersonnel;
    int totalHospitalPersonnel = 0;
    allHospitalPersonnel = readHospitalPersonnelFromFile(totalHospitalPersonnel);

    if( allHospitalPersonnel == NULL )
    {
        cout << "allHospitalPersonnel is NULL" << endl;
        return 0;
    }

    printHospitalPersonnel(allHospitalPersonnel, totalHospitalPersonnel , "", ' ');
    menu(allHospitalPersonnel, totalHospitalPersonnel);

    return 0;
}



/*
* Responsible for reading HospitalPersonnel records from hospitalPersonnel.txt into HospitalPersonnel array of structs
*
*
* @param totalHospitalPersonnel: reference variable which post execution, contains size of HospitalPersonnel
* @param return: pointer pointing to the array of structs containing HospitalPersonnel data
*/
HospitalPersonnel* readHospitalPersonnelFromFile(int &totalHospitalPersonnel)
{
    char delimiter;
    HospitalPersonnel *allHospitalPersonnelPointer;
    //input stream for HospitalPersonnels data
    ifstream allHospitalPersonnelInFile;

    //open HospitalPersonnels file
    allHospitalPersonnelInFile.open("hospitalPersonnel.txt");

    //error handling in case file does not exist - start
    if( !allHospitalPersonnelInFile )
    {
        cout << "Error opening hospitalPersonnel.txt" << endl;
        return NULL;
    }
    //error handling in case file does not exist - end
    cout << "Success opening hospitalPersonnel.txt" << endl;
    allHospitalPersonnelInFile >> totalHospitalPersonnel;
    allHospitalPersonnelPointer = new HospitalPersonnel[totalHospitalPersonnel];

    cout << "totalHospitalPersonnels: " << totalHospitalPersonnel << endl;

    for(int i = 0; i < totalHospitalPersonnel; i++)
    {
        allHospitalPersonnelInFile >> allHospitalPersonnelPointer[i].firstname;
        allHospitalPersonnelInFile >> allHospitalPersonnelPointer[i].lastname;
        allHospitalPersonnelInFile >> allHospitalPersonnelPointer[i].ID;
        allHospitalPersonnelInFile >> allHospitalPersonnelPointer[i].role;

        if( allHospitalPersonnelPointer[i].role != "PT" )
        {
            // this is a Doctor or a Nurse Practitioner
            allHospitalPersonnelInFile >> allHospitalPersonnelPointer[i].dutyDay0;
            allHospitalPersonnelInFile >> allHospitalPersonnelPointer[i].dutyDay1;
            allHospitalPersonnelInFile >> allHospitalPersonnelPointer[i].dutyDay2;

            allHospitalPersonnelPointer[i].dutyDay0 = getFullNameForDay(allHospitalPersonnelPointer[i].dutyDay0);
            allHospitalPersonnelPointer[i].dutyDay1 = getFullNameForDay(allHospitalPersonnelPointer[i].dutyDay1);
            allHospitalPersonnelPointer[i].dutyDay2 = getFullNameForDay(allHospitalPersonnelPointer[i].dutyDay2);
        }
        else
        {
            // this is a Patient
            allHospitalPersonnelInFile >> allHospitalPersonnelPointer[i].exitFlag;

            // admit date
            allHospitalPersonnelInFile >> allHospitalPersonnelPointer[i].admitDate.month;
            allHospitalPersonnelInFile >> delimiter;
            allHospitalPersonnelInFile >> allHospitalPersonnelPointer[i].admitDate.day;
            allHospitalPersonnelInFile >> delimiter;
            allHospitalPersonnelInFile >> allHospitalPersonnelPointer[i].admitDate.year;

            if( allHospitalPersonnelPointer[i].exitFlag == 'Y' )
            {
                // this Patient has exit the hospital
                allHospitalPersonnelInFile >> allHospitalPersonnelPointer[i].exitDate.month;
                allHospitalPersonnelInFile >> delimiter;
                allHospitalPersonnelInFile >> allHospitalPersonnelPointer[i].exitDate.day;
                allHospitalPersonnelInFile >> delimiter;
                allHospitalPersonnelInFile >> allHospitalPersonnelPointer[i].exitDate.year;
            }
        }
    }

    allHospitalPersonnelInFile.close();

    return allHospitalPersonnelPointer;
}

/*
* Responsible for printing menu and handling user selection
*
*
* @param hospitalPersonnel: pointer pointing to the array of structs containing Hospital Personnel data
* @param totalHospitalPersonnel: size of hospitalPersonnel
*/

void menu(HospitalPersonnel *hospitalPersonnel, int totalHospitalPersonnel)
{
    int input;
    while( true )
    {
        cin >> input;
        switch( input )
        {
            case 0:
                // Press 0 to print all Hospital Personnel
                printHospitalPersonnel(hospitalPersonnel, totalHospitalPersonnel, "", ' ');
                break;
            case 1:
                // Press 1 to print only Patients
                printHospitalPersonnel(hospitalPersonnel, totalHospitalPersonnel, "PT", ' ');
                break;
            case 2:
                // Press 2 to print only Doctors
                printHospitalPersonnel(hospitalPersonnel, totalHospitalPersonnel, "MD", ' ');
                break;
            case 3:
                // Press 3 to print only Nurse Practitioners
                printHospitalPersonnel(hospitalPersonnel, totalHospitalPersonnel, "NP", ' ');
                break;
            case 4:
                // Press 4 to print only Admitted Patients
                printHospitalPersonnel(hospitalPersonnel, totalHospitalPersonnel, "", 'N');
                break;
            case 5:
                // Press 5 to print only non-Admitted Patients
                printHospitalPersonnel(hospitalPersonnel, totalHospitalPersonnel, "", 'Y');
                break;
            case 6:
                // Press 6 to exit
                exit(0);
        }

    }
}

/*
* Responsible for converting the initial for a day to the full name
*
*
* @param dayAbbreviation: short name for day
* @param return: long name for day
*/
string getFullNameForDay(string dayAbbreviation)
{
    if( dayAbbreviation == "M" )
        return "Mondays";
    else if( dayAbbreviation == "T" )
        return "Tuesdays";
    else if( dayAbbreviation == "W" )
        return "Wednesdays";
    else if( dayAbbreviation == "TH" )
        return "Thursdays";
    else if( dayAbbreviation == "F" )
        return "Fridays";
    else if( dayAbbreviation == "SA" )
        return "Saturdays";
    else if( dayAbbreviation == "SU" )
        return "Sundays";

    return "";
}

/*
* Responsible for printing the HospitalPersonnel array of structs
*
*
* @param hospitalPersonnel: pointer pointing to the array of structs containing HospitalPersonnel data
* @param totalHospitalPersonnel: size of hospitalPersonnel
*/
void printHospitalPersonnel(HospitalPersonnel *hospitalPersonnel, int totalHospitalPersonnel, string roleFlag = "", char patientExitFlag = ' ')
{
    if( hospitalPersonnel == NULL || totalHospitalPersonnel < 1 )
    {
        return;
    }

    cout << endl;
    printHorizontalLine(85, '*');
    printHorizontalLine(85, '*');
    for(int i = 0; i < totalHospitalPersonnel; i++)
    {

        if( roleFlag.length() != 0 )
        {
            if( roleFlag != hospitalPersonnel[i].role )
            {
                // skip roles which do not match roleFlag
                continue;
            }
        }

        if( patientExitFlag != ' ' )
        {
            if( patientExitFlag != hospitalPersonnel[i].exitFlag )
            {
                continue;
            }
        }

        // filter - end

        cout.clear();
        cout.fill(' ');

        cout
        << left
        << setw(3)
        << i
        << left << setw(10)
        << hospitalPersonnel[i].firstname
        << left << setw(10)
        << hospitalPersonnel[i].lastname

        << left << setw(15)
        << hospitalPersonnel[i].ID
        << left << setw(10)
        << hospitalPersonnel[i].role;


        if( hospitalPersonnel[i].role != "PT" )
        {
            // this is a Medical staff
            cout
            << hospitalPersonnel[i].dutyDay0
            << "-"
            << hospitalPersonnel[i].dutyDay1
            << "-"
            << hospitalPersonnel[i].dutyDay2;
        }
        else
        {
            cout << hospitalPersonnel[i].exitFlag << "\t";
            cout
            << right <<setw(2) << setfill('0')
            << hospitalPersonnel[i].admitDate.month
            << ":"
            << right <<setw(2) << setfill('0')
            << hospitalPersonnel[i].admitDate.day
            << ":"
            << right <<setw(2) << setfill('0')
            << hospitalPersonnel[i].admitDate.year
            << "\t";

            if( hospitalPersonnel[i].exitFlag == 'Y' )
            {
                cout
                << right <<setw(2) << setfill('0')
                << hospitalPersonnel[i].exitDate.month
                << ":"
                << right <<setw(2) << setfill('0')
                << hospitalPersonnel[i].exitDate.day
                << ":"
                << right <<setw(2) << setfill('0')
                << hospitalPersonnel[i].exitDate.year;
            }
        }

        cout << endl;
    }
    printHorizontalLine(85, '*');
    printHorizontalLine(85, '*');
    cout << endl;
    cout << "Press 0 to print all Hospital Personnel" <<endl;
    cout << "Press 1 to print only Patients" << endl;
    cout << "Press 2 to print only Doctors" << endl;
    cout << "Press 3 to print only Nurse Practitioners" << endl;
    cout << "Press 4 to print only Admitted Patients" << endl;
    cout << "Press 5 to print only non-Admitted Patients" << endl;
    cout << "Press 6 to exit: ";
}


/*
* Responsible for printing a horizontal line which consists of border_char characters
*
*
* @param width: count of border_char
* @param border_char: width made out of characters
*/
void printHorizontalLine( int width, char border_char )
{
    cout.fill( border_char );
    cout << setw( width ) << border_char << "\n";
    cout.fill(' ');
}

2 个答案:

答案 0 :(得分:1)

典型的方法是实现operator<,基本上有一个islessthan函数,并使用模板化的自由函数来执行排序。如果作业允许,std::sort已经编写好并可供您使用。如果没有使用<编写自己的排序函数来订购数据。

bool operator< (const Date& lhs, const Date& rhs)
{ 
    // code goes here
}

使用Datestd::sort s的数组进行排序非常简单:

std::sort(dates, dates+number_of_dates);

...然而

您不想对Date进行排序。您想要对HospitalPersonnel进行排序。 HospitalPersonnel有两个Date可以排序,还有名字和姓氏,ID和角色。幸运的是,std :: sort允许您指定比较器函数来定义要排序的内容。如果你必须编写自己的排序,我建议遵循这个模型,因为它允许你编写一个排序函数并以不同的方式调用它。

例如:

bool compareAdmitDate(const HospitalPersonnel &rhs, const HospitalPersonnel &lhs)
{
    return rhs.admitDate < lhs.admitDate;
}

bool compareLastName(const HospitalPersonnel &rhs, const HospitalPersonnel &lhs)
{
    return rhs.lastname< lhs.lastname;
}

这些与

一起使用
std::sort(allHospitalPersonnelPointer, 
          allHospitalPersonnelPointer + totalHospitalPersonnel,
          compareAdmitDate);

一个警告:std::sort将对提供的列表进行排序。如果您不希望列表排序,请复制列表,然后排序并返回副本。

答案 1 :(得分:0)

这样的事情? @ user4581301

struct HospitalPersonnel
{
    string firstname;
    string lastname;
    string ID;
    string role;

    string dutyDay0;
    string dutyDay1;
    string dutyDay2;

    //for patients
    char exitFlag;
    Date admitDate;
    Date exitDate;

    bool compareAdmitDate(const HospitalPersonnel &rhs, const HospitalPersonnel &lhs)
    {
        return rhs.admitDate < lhs.admitDate;
    }
    bool compareLastName(const HospitalPersonnel &rhs, const HospitalPersonnel &lhs)
    {
        return rhs.lastname < lhs.lastname;
    }
    std::sort(HospitalPersonnel, HospitalPersonnel + totalHospitalPersonnel, compAdmitDate);
};