2016-10-14 7 views
0

私は、病院の人を最初に入院した順番にソートしてリストを降順で並べ替えるメンバ関数を使用する必要があります。単にSorting()を追加するだけでもかまいません。私のprintHospitalPersonnel関数に結びついたDateのstruct内の "function"は、ソートメニューオプションが呼ばれたときに順序付きリストを出力しますか?以下は、私が持っているコードです、ありがとう!メンバー関数を実装する(ソート)

#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(' '); 
} 
+0

私が正しく理解していれば、あなたの 'HospitalPersonnel * 'ベクトルのエントリーを' admitDate'でソートしたいと思っています。だから、基本的にはベクトルをソートする必要があります。これを「Date」のメンバ関数にすることは、私には論理的に意味をなさない。しかし、あなたはそれを行うことができます。たぶんあなたの質問を明確にすることができます。あなたの問題は何ですか?メンバー関数を作成するには?ソートの実装方法「printHospitalPersonnel関数に結びついている」とはどういう意味ですか? – Jonas

+0

問題は、病院の人員を最新の入院患者から医師と看護師が最も古い患者に分類する新しいメンバー機能をソートして実装するためのメニューオプションを追加したいということです。私はこれをどのように追加するべきか、それを設定する方法について迷っています –

答えて

1

典型的なアプローチは、基本的には、operator<を実装islessthan機能を持っている、とソートを実行するためにテンプレートフリー機能を使用することです。割り当てによって許可されている場合、std::sortは既に書かれており、使用の準備ができています。そうでない場合は、<を使用して独自のソート関数を記述し、データを並べ替えます。 std::sortDate sの配列をソート

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

は上空に簡単です:

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

しかし...

あなたはDate秒をソートする必要はありません。 HospitalPersonnelをソートします。 HospitalPersonnelには、姓と名、IDと役割だけでなく、並べ替える2つのDateがあります。幸運にもstd :: sortではコンパレータ関数を指定してソート対象を定義することができます。独自のソートを記述する必要がある場合は、ソート関数を1つ記述して別の方法で呼び出すことができるため、このモデルに従うことをお勧めします。例えば

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); 

1回の警告で使用されていますstd::sort場所で提供されたリストをソートします。リストをソートしたくない場合は、リストのコピーを作成し、ソートして返します。

+0

私は2つの構造体をクラスに結合してから、bool compareAdmitDateをstruct Dateに追加し、compareLastNameをHopsitalPersonnel構造体に追加する必要がありますか?また、私は –

+0

std :: sort(allHospitalPersonnelPointer、 allHospitalPersonnelPointer + totalHospitalPersonnel、 compareAdmitDate)と呼ぶだけです。 –

+0

大変申し訳ありませんが、私は事故で入力しています。.....対応するステートメントを構造体に入れた後、ユーザーがSortingメニューオプションを入力した場合の処理​​とstd :: sortを宣言しますか? –

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); 
}; 
+0

型なしの 'sort'の宣言は禁止されています –

+0

2つの関数はクラスの一部である必要はなく、 'std :: sort'はソートを要求したメニュー項目がどこであっても呼び出されるべきです。 – user4581301