Learning Objectives of the Project
In this project, students will learn to work with files (streams), structures, arrays and arrays of structures, and definitely teamwork.
Group Work
Students are supposed to work in groups of 2 or 3 members. If you have any problem finding group members, please let the Professor know.
Description of the Project
In this project, you will write a software for the Campus Health Centre located at a university called YouOIT to maintain patients’ information.
The health centre maintains a master text file, called patients.txt that contains the names of all patients, their SIN number, patients’ doctor name, and doctor ID. The following figure shows an example of how the file looks like:
First name Last name SIN Doctor Name Doctor ID
Dave Ramdin 100 Mike 1
Lili Bega 101 Mike 1
Joe Biden 103 Helen 2
… ... ... ... ...
NOTE: The file does not contain the titles.
Task
Your task for this project is to develop a software that would allow a nurse to:
Add new patient
Remove patient
Search for a patient by SIN
Print all patients sorted by last name
Print all patients cared by same doctor. For example, Dr. Mike, with ID: 1 has two patients
Dave Ramdin 100
Lili Bega 101
Exit the program and save all the updates to the file
Your program should read the data file, and fill an array of structures (one structure for each patient), and perform all operations on the array of structures. When the user asks to exit the program, the master text file would be replaced with the content of the array. Further explanation of the project will be given in the tutorial on June 8, 2009.
+ - + - + - + - + - + - + - + - + - + - + - + - + - + - +
整个题目本来是有部分中文翻译的,但有好几处翻译不是很准确,容易让阅读的朋友产生误会,比如:
“Remove patient” 被翻译成了“删除病人的名字”,实际上应该是“删除病人”;
“Print all patients sorted by last name”被翻译成了“打印所有病人的姓”,而实际上应该是“按姓排序,打印出所有病人”等等。为避免misunderstanding起见,干脆就去掉了那些中文。除此目的外,也想让正在学校读书的DDMM们感受一下国外的题目是什么样子的,顺带练习一下e文。
这个题目的绝对难度并不大。但有很多细节需要给予系统性的考虑。尽管在“Learning Objectives of the Project”并未言明,另外一个更重要的目标就是,让大家在软件设计能力方面得到一次锻炼的机会。
下面是我给那个朋友提供的参考代码,用到了较多的STL,正在学习STL的朋友,也可以参考一下。代码在VS2005中编译通过,其中还有一些很小的细节需要完善。望各位硕学先进指正!
+ - + - + - + - + - + - + - + - + - + - + - + - + - + - +
#include <iostream>
#include <fstream>
#include <string>
#include <list>
using namespace std;
// the structure of patient
struct Patient
{
public:
// constructor
Patient();
// overloaded constructor
Patient(string first_name, string last_name, int sin, string doctor_name, int doctor_id);
// variable to hold the first name of a patient
string first_name;
// variable to hold the last name of a patient
string last_name;
// variable to hold the SIN of a patient
int sin;
// variable to hold the doctor's name
string doctor_name;
// variable to hold the doctor's id.
int doctor_id;
// friend member functions will be used for sorting and other purposes
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);
};
Patient::Patient()
{
}
Patient::Patient(string first_name, string last_name, int sin, string doctor_name, int doctor_id)
{
this->first_name = first_name;
this->last_name = last_name;
this->sin = sin;
this->doctor_name = doctor_name;
this->doctor_id = doctor_id;
}
bool operator<(const Patient& first, const Patient& second)
{
unsigned int i=0;
while((i<first.last_name.length()) && (i<second.last_name.length()))
{
if (tolower(first.last_name[i])<tolower(second.last_name[i]))
{
return true;
}
else if (tolower(first.last_name[i])>tolower(second.last_name[i]))
{
return false;
}
++i;
}
if (first.last_name.length()<second.last_name.length())
{
return true;
}
else
{
return false;
}
}
bool operator>(const Patient& first, const Patient& second)
{
unsigned int i=0;
while((i<first.last_name.length()) && (i<second.last_name.length()))
{
if (tolower(first.last_name[i]) > tolower(second.last_name[i]))
{
return true;
}
else if (tolower(first.last_name[i]) < tolower(second.last_name[i]))
{
return false;
}
++i;
}
if (first.last_name.length() > second.last_name.length())
{
return true;
}
else
{
return false;
}
}
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.doctor_name == p2.doctor_name) && (p1.doctor_id == p2.doctor_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.doctor_name == p2.doctor_name) && (p1.doctor_id == p2.doctor_id));
}
class PatientDatabase
{
public:
// constructor of class patientdatabase
PatientDatabase();
// load the data persisted in the data file before
bool loadDatabase(string dataFilename);
// save data to data file, returns how many patients stored in the data file.
int saveDatabase(string dataFilename);
// add a patient to the database
bool addPatient(Patient patient);
// another form of adding patient to the database, overloaded.
bool addPatient(string first_name,string last_name, int sin, string doctor_name, int doctor_id);
// remove a patient for database by his/her sin
bool removePatient(int sin);
// search a patient by sin
Patient* searchPatient(int sin);
// print all patients' last name
void printAllPatientsSortedByLastname();
// print all patients cared by the same doctor
void printAllPatientsCaredBy(int doctor_id);
private:
// the container of all the patients, namely database
list<Patient> database;
};
// constructor of class patientdatabase
PatientDatabase::PatientDatabase()
{
}
// load the data persisted in the data file before
bool PatientDatabase::loadDatabase(string dataFilename)
{
int location = 0;
char linestring[100];
string s;
string first_name;
string last_name;
int sin;
string doctor_name;
int doctor_id;
// try opening the database
ifstream fin(dataFilename.c_str());
if(!fin)
{
cout << "can not open the database..." << endl;
return false;
}
while(fin)
{
// read one line from patients.txt, and store it in linestring
fin.getline(linestring, 100);
s = linestring;
// if it's the last line which is '/n' only, break the loop
if(s.length() == 0) break;
string stemp;
// retrieve first name
location = s.find_first_of(",");
first_name = s.substr(0, location);
stemp = s.substr(location + 1);
s.empty();
// retrieve last name
location = stemp.find_first_of(",");
last_name = stemp.substr(0, location);
s = stemp.substr(location + 1);
stemp.empty();
// retrieve SIN
location = s.find_first_of(",");
sin = atoi(s.substr(0, location).c_str());
stemp = s.substr(location + 1);
s.empty();
// retrieve doctor's name
location = stemp.find_first_of(",");
doctor_name = stemp.substr(0, location);
s = stemp.substr(location + 1);
stemp.empty();
// retrieve doctor's ID
location = s.find_first_of(",");
doctor_id = atoi(s.substr(0, location).c_str());
stemp.empty();
s.empty();
// load a patient into the 'list<patient> database'
addPatient(first_name, last_name, sin, doctor_name, doctor_id);
}
fin.close();
return true;
}
// save data to data file, returns how many patients stored in the data file.
int PatientDatabase::saveDatabase(string dataFilename)
{
int i = 0;
string buffer;
char temp[4];
string tempstr;
// open a text file as database on disk
ofstream outfile(dataFilename.c_str());
list<Patient>::iterator it;
for(it = database.begin(); it != database.end(); ++it)
{
buffer.append(it->first_name + ",");
buffer.append(it->last_name + ",");
// we need to convert 'int' to 'string' before appended to 'buffer'
itoa(it->sin, temp, 10); // itoa causes a depreciated warning, don't care, it doesn't hurt!
tempstr = temp;
buffer.append(tempstr + ",");
buffer.append(it->doctor_name + ",");
// we need to convert 'int' to 'string' before appended to 'buffer'
itoa(it->doctor_id, temp, 10); // itoa causes a depreciated warning, don't care, it doesn't hurt!
tempstr = temp;
buffer.append(tempstr + ",/n");
outfile.write(buffer.c_str(), buffer.length());
buffer.clear();
i++;
}
return i;
}
// add a patient to the database
bool PatientDatabase::addPatient(Patient patient)
{
database.push_back(patient);
return true;
}
// another form of adding patient to the database, overloaded.
bool PatientDatabase::addPatient(string first_name,string last_name, int sin, string doctor_name, int doctor_id)
{
Patient p;
p.first_name = first_name;
p.last_name = last_name;
p.sin = sin;
p.doctor_name = doctor_name;
p.doctor_id = doctor_id;
database.push_back(p);
return true;
}
// remove a patient for database by his/her sin
bool PatientDatabase::removePatient(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;
}
// search a patient by sin
Patient* PatientDatabase::searchPatient(int sin)
{
list<Patient>::iterator it;
for(it = database.begin(); it != database.end(); it++)
{
if(it->sin == sin)
{
return (Patient*)(&(*it));
}
}
return NULL;
}
// print all patients sorted by last name
void PatientDatabase::printAllPatientsSortedByLastname()
{
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->doctor_name << '/t' << it->doctor_id << endl;
}
}
// print all patients cared by the same doctor
void PatientDatabase::printAllPatientsCaredBy(int doctor_id)
{
list<Patient>::iterator it;
for(it = database.begin(); it != database.end(); ++it)
{
if(it->doctor_id == doctor_id)
{
cout << '/t' << it->first_name << '/t' << it->last_name << '/t' << it->sin << '/t' << it->doctor_name << '/t' << it->doctor_id << endl;
}
else
{
continue;
}
}
}
int main(void)
{
short action;
string dataFilename = "patients.txt";
PatientDatabase* pdb = new PatientDatabase();
string first_name;
string last_name;
int sin;
string doctor_name;
int doctor_id;
int pid;
int did;
while(true)
{
system("cls");
cout << "/t0. Load database" << endl;
cout << "/t1. Input patient's data" << endl;
cout << "/t2. Search a patient by SIN" << endl;
cout << "/t3. Print all patients sorted by last name" << endl;
cout << "/t4. Print all patients cared by the same doctor" << endl;
cout << "/t5. Remove a patient" << endl;
cout << "/t6. Save database" << endl;
cout << "/t7. Quit" << endl;
cout << "/t---------------------------------------" << endl;
cout << "/tPlease enter your action:" << endl;
cout << "/t";
cin >> action;
if(action < 0 || action > 7)
{
cout << "/tSorry, you can only enter a number between 0 and 6, please return :)" << endl;
cout << "/t";
system("pause");
continue;
}
switch(action)
{
case 0:
if(pdb->loadDatabase(dataFilename))
{
cout << "/tDatabase was successfully loaded" << endl;
cout << "/t";
system("pause");
}
break;
case 1:
cout << "/tFirst Name: ";
cin >> first_name;
cout << "/tLast Name: ";
cin >> last_name;
cout << "/tSIN: ";
cin >> sin;
cout << "/tDoctor Name: ";
cin >> doctor_name;
cout << "/tDoctor ID: ";
cin >> doctor_id;
if(pdb->addPatient(first_name, last_name, sin, doctor_name, doctor_id))
{
cout << "/tPatient's data was successfully accepted." << endl;
}
cout << "/t";
system("pause");
break;
case 2:
Patient *pSearched;
cout << "/tPlease enter the patient's ID to be searched" << endl;
cout << '/t';
cin >> pid;
pSearched = pdb->searchPatient(pid);
if(pSearched != NULL)
{
cout << '/t' << pSearched->first_name << " " << pSearched->last_name << " was selected out." << endl;
}
cout << "/t";
system("pause");
break;
case 3:
cout << "/tAll patients sorted by lastname(z->a)" << endl;
pdb->printAllPatientsSortedByLastname();
cout << "/t";
system("pause");
break;
case 4:
cout << "/tPlease enter the doctor's ID:" << endl;
cout << '/t';
cin >> did;
pdb->printAllPatientsCaredBy(did);
cout << "/t";
system("pause");
break;
case 5:
cout << "/tPlease enter the patient's ID to be removed" << endl;
cout << '/t';
cin >> pid;
pdb->removePatient(pid);
cout << "/t";
system("pause");
break;
case 6:
cout << '/t' << pdb->saveDatabase(dataFilename) << " records were successfully saved to " << dataFilename.c_str() << endl;
cout << "/t";
system("pause");
break;
case 7:
exit(0);
}
}
return 0;
}
本文介绍了一个用于维护大学健康中心患者信息的软件项目。该系统使用结构体数组存储患者数据,并提供了添加、删除、搜索患者等功能。此外,系统还能按姓氏排序显示所有患者,并列出特定医生的所有患者。
4622

被折叠的 条评论
为什么被折叠?



