16,548
社区成员




void PatientDatebase::SortPatientByLastname()
{
database.sort(greater<Patient>());
list<Patient>::iterator it;
for(it = database.begin(); it != database.end(); ++it)
{
cout << '\t' << it->first_name << '\t' << it->last_name << '\t' << it->SIN << '\t' << it->doc_name << '\t' << it->ID << endl;
}
}
void main(string outfile)
{
PatientDatebase pData;
cout<<"sort patients by last name: "<<endl;
pData.SortPatientByLastname();
}
inline void _Container_base12::_Orphan_all()
{ // orphan all iterators
#if _ITERATOR_DEBUG_LEVEL == 2
if (_Myproxy != 0)
{ // proxy allocated, drain it
_Lockit _Lock(_LOCK_DEBUG);
for (_Iterator_base12 **_Pnext = &_Myproxy->_Myfirstiter;
*_Pnext != 0; *_Pnext = (*_Pnext)->_Mynextiter)//在这行出错
(*_Pnext)->_Myproxy = 0;
_Myproxy->_Myfirstiter = 0;
}
#endif /* _ITERATOR_DEBUG_LEVEL == 2 */
}
#include<string>
#include <list>
using namespace std;
struct Patient
{
public:
Patient();
Patient(string first_name,string last_name,int SIN,string doc_name,int ID);
string first_name,last_name,doc_name;
int SIN,ID;
friend bool operator>(const Patient& p1,const Patient& p2);
friend bool operator<(const Patient& p1,const Patient& p2);
friend bool operator==(const Patient& p1,const Patient& p2);
friend bool operator!=(const Patient& p1,const Patient& p2);
};
class PatientDatebase
{
public:
PatientDatebase();
bool LoadDatabase();
bool SaveDatebase(string filenaem);
bool AddPatient(Patient patient);
bool AddPatient(string first_name,string last_name,int SIN,string doc_name,int ID);
bool DeletePatient(int sin);
void SortPatientByLastname();
void PrintSameID(int ID);
Patient* SearchPatient(int sin);
private:
list<Patient> database;
};
// Patient.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Patient.h"
#include <fstream>
#include <iostream>
#include <Windows.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
Patient::Patient()
{
}
Patient::Patient(string first_name,string last_name,int SIN,string doc_name,int I)
{
first_name=first_name;
last_name=last_name;
SIN=SIN;
doc_name=doc_name;
ID=ID;
}
bool operator<(const Patient& p1,const Patient& p2)
{
int i=0;
while((i<p1.last_name.length())&(i<p2.last_name.length()))
{
if(p1.last_name[i]<p2.last_name[i])
{
return true;
}
if(p1.last_name[i]>p2.last_name[i])
{
return false;
}
++i;
}
}
bool operator>(const Patient& p1,const Patient& p2)
{
int i=0;
while((i<p1.last_name.length())&(i<p2.last_name.length()))
{
if(p1.last_name[i]<p2.last_name[i])
{
return false;
}
if(p1.last_name[i]>p2.last_name[i])
{
return true;
}
++i;
}
}
bool operator==(const Patient& p1,const Patient& p2)
{
return ((p1.last_name == p2.last_name) && (p1.first_name == p2.first_name) && (p1.SIN == p2.SIN) && (p1.doc_name == p2.doc_name) && (p1.ID == p2.ID));
}
bool operator!=(const Patient& p1,const Patient& p2)
{
return !((p1.last_name == p2.last_name) && (p1.first_name == p2.first_name) && (p1.SIN == p2.SIN) && (p1.doc_name == p2.doc_name) && (p1.ID == p2.ID));
}
PatientDatebase::PatientDatebase()
{
}
bool PatientDatebase::LoadDatabase()
{
const int MAX_LINE_BUF=25;
char lineBuf[MAX_LINE_BUF];
char firstname[MAX_LINE_BUF],lastname[MAX_LINE_BUF],doc_name[MAX_LINE_BUF];
int sin,id,iRet;
ifstream iDatabase;
iDatabase.open("C:/Users/lrp/Desktop/patient.txt");
Patient patient;
if (!iDatabase.is_open() )
{
cout<<"open error"<<endl;
return false;
}
while( iDatabase.getline(lineBuf, MAX_LINE_BUF, '\n') )
{
if(lineBuf[0]==' ')
{
continue;
}
iRet = sscanf(lineBuf,"%s %s %d %s %d",firstname,lastname,&sin,doc_name,&id);
if(iRet!=5)
{
continue;
}
patient.first_name=firstname;
patient.last_name=lastname;
patient.SIN=sin;
patient.doc_name=doc_name;
patient.ID=id;
database.push_back(patient);
}
list<Patient>::iterator it = database.begin();
for(it = database.begin(); it != database.end(); ++it)
{
cout << '\t' << it->first_name << '\t' << it->last_name << '\t' << it->SIN << '\t' << it->doc_name << '\t' << it->ID << endl;
}
return true;
}
bool PatientDatebase::SaveDatebase(string filename)
{
string buf,tempstr;
char temp[4];
list<Patient>::iterator it;
ofstream outfile(filename.c_str());
for(it=database.begin();it!=database.end();++it)
{
buf.append(it->first_name+"\t");
buf.append(it->last_name+"\t");
itoa(it->SIN,temp,10);
tempstr=temp;
buf.append(tempstr+"\t");
buf.append(it->doc_name+"\t");
itoa(it->ID,temp,10);
tempstr=temp;
buf.append(tempstr+"\n");
outfile.write(buf.c_str(), buf.length());
buf.clear();
}
return true;
}
bool PatientDatebase::AddPatient(Patient patient)
{
database.push_back(patient);
return true;
}
bool PatientDatebase::AddPatient(string first_name,string last_name,int SIN,string doc_name,int ID)
{
Patient p;
p.first_name=first_name;
p.last_name=last_name;
p.SIN=SIN;
p.doc_name=doc_name;
p.ID=ID;
database.push_back(p);
return true;
}
bool PatientDatebase::DeletePatient(int sin)
{
list<Patient>::iterator it = database.begin();
while(it!=database.end())
{
if(it->SIN == sin)
{
// be careful here, database.erase(it) returns ++it.
// it's very easy to fall into the trap of infinite loop
it = database.erase(it);
//return true;
}
else
{
++it;
}
}
return true;
}
void PatientDatebase::SortPatientByLastname()
{
database.sort(greater<Patient>());
list<Patient>::iterator it;
for(it = database.begin(); it != database.end(); ++it)
{
cout << '\t' << it->first_name << '\t' << it->last_name << '\t' << it->SIN << '\t' << it->doc_name << '\t' << it->ID << endl;
}
}
void PatientDatebase::PrintSameID(int ID)
{
list<Patient>::iterator it;
for(it = database.begin(); it != database.end(); ++it)
{
if(it->ID ==ID)
{
cout << '\t' << it->first_name << '\t' << it->last_name << '\t' << it->SIN << '\t' << it->doc_name << '\t' << it->ID << endl;
}
else
{
continue;
}
}
}
Patient* PatientDatebase::SearchPatient(int sin){
list<Patient>::iterator it = database.begin();
while(it!=database.end())
{
if(it->SIN == sin)
{
cout << '\t' << it->first_name << '\t' << it->last_name << '\t' << it->SIN << '\t' << it->doc_name << '\t' << it->ID << endl;
}
++it;
}
return NULL;
}
void main(string outfile)
{
cout<<"please input the filename"<<endl;
// cin>>infile;
// infile="patient.txt";
PatientDatebase pData;
pData.LoadDatabase();
cout<<"please input patient's SIN"<<endl;
int sin;
cin>>sin;
pData.SearchPatient(sin);
cout<<"delete ? Y/N"<<endl;
string s;
cin>>s;
if(s=="Y")
{
cout<<"deleting"<<endl;
Sleep(3000);
pData.DeletePatient(sin);
}
cout<<"please input the outfile name"<<endl;
string out,location;
location="C:/Users/lrp/Desktop/";
cin>>out;
outfile=location.append(out);
pData.SaveDatebase(outfile);
int ID;
cout<<"please input doctor's ID"<<endl;
cin>>ID;
cout<<"Patients cared by doctor "<<ID<<":"<<endl;
pData.PrintSameID(ID);
cout<<"sort patients by last name: "<<endl;
pData.SortPatientByLastname();
}