基本要求:
(1)病人信息包括:姓名、就诊编号、年龄、性别、职业、住址等。
(2)医生信息包括:工作编号、姓名、专业方向、所属科室、本院就职年数等。
(3)预约登记信息包括:病人、医生、预约时间、病情描述、备注等。
(4)完成以下功能:病人信息和医生信息的添加、修改、浏览、删除和查询。
(5)完成以下功能:预约登记;预约登记信息的浏览与查询。
实现:
共设计4个类:病人patient类,医生doctor类,预约信息books类,还有和用户交互的oper类
代码如下:
- main函数。定义三个vector存类,再定义4个类的对象。从文件里读入数据放入vector后,在while循环里实现与用户的交互。交互的函数利用oper来实现,简化了main里的代码。
main.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string>
#include "patient.h"
#include "doctor.h"
#include "books.h"
#include "oper.h"
using namespace std;
int main()
{
vector<patient> pat;
vector<doctor> doc;
vector<books> bok;
patient pa;
doctor dt;
books bk;
oper op;
fstream inFilep("D:\\patient.txt", ios::in|ios::out);
fstream inFiled("D:\\doctor.txt", ios::in|ios::out);
fstream inFileb("D:\\books.txt", ios::in|ios::out);
if(!inFileb || !inFiled || !inFilep)
{
cout << "cannot find file error!\n";
exit(0);
}
else
cout << "File open success!\n";
system("pause");
while(inFilep >> pa) pat.push_back(pa);
while(inFiled >> dt) doc.push_back(dt);
while(inFileb >> bk) bok.push_back(bk);
string n;
while(1)
{
system("cls");
op.start();
cin >> n;
if(n == "1") op.add(pat, doc, bok);
else if(n == "2") op.modify(pat, doc, bok);
else if(n == "3") op.dispaly(pat, doc, bok);
else if(n == "4") op.del(pat, doc, bok);
else if(n == "5") op.query(pat, doc, bok);
else if(n == "0") return 0;
else cout << "Wrong input!\n";
system("pause");
}
for(auto &x: pat) inFilep << x;
for(auto &x: doc) inFiled << x;
for(auto &x: bok) inFileb << x;
return 0;
}
- oper类。一个start界面+五个操作。五个操作函数的参数都是三种类的vector数组,并且通过引用达到直接修改数据的目的。pravite数据成员都是一些需要临时读入的变量。
oper.h
#ifndef OPER_H
#define OPER_H
#include <vector>
#include <iostream>
#include "patient.h"
#include "doctor.h"
#include "books.h"
using std::cin;
using std::cout;
using std::vector;
class oper
{
public:
oper();
void start();
void add(vector<patient> &vp, vector<doctor> &vd, vector<books> &vb);
void modify(vector<patient> &vp, vector<doctor> &vd, vector<books> &vb);
void dispaly(vector<patient> &vp, vector<doctor> &vd, vector<books> &vb);
void del(vector<patient> &vp, vector<doctor> &vd, vector<books> &vb);
void query(vector<patient> &vp, vector<doctor> &vd, vector<books> &vb);
protected:
private:
void hintMessage1();
void hintMessage2();
void hintMessage3();
string a,b;
string n;
patient paa;
doctor doo;
books bok;
};
#endif // OPER_H
oper.cpp
#include "oper.h"
oper::oper()
{
//ctor
}
void oper::start()
{
cout << "请输入要进行的操作:\n"
<< "1:添加\n"
<< "2:修改\n"
<< "3:浏览\n"
<< "4:删除\n"
<< "5:查询\n"
<< "0:退出\n";
}
void oper::hintMessage1()
{
cout << "医生信息请输入:姓名、工作编号、专业方向、本院就职年数、所属科室\n";
cout << "患者信息请输入:姓名、就诊编号、年龄、性别、职业、住址\n";
cout << "预约信息请输入:医生姓名、医生编号、病人姓名、就诊编号、预约时间、病情描述、备注\n";
}
void oper::hintMessage2()
{
cout << "医生信息请输入:姓名、工作编号\n";
cout << "患者信息请输入:姓名、就诊编号\n";
cout << "预约信息请输入:医生姓名、病人姓名\n";
}
void oper::hintMessage3()
{
cout << "\n请输入类型\n";
cout << "1,患者; 2,医生; 3,预约信息\n";
}
void oper::add(vector<patient> &vp, vector<doctor> &vd, vector<books> &vb)
{
hintMessage3();
cin >> n;
hintMessage1();
if(n == "1"){
cin >> paa;
vp.push_back(paa);
}
else if(n == "2"){
cin >> doo;
vd.push_back(doo);
}
else if(n == "3"){
cin >> bok;
vb.push_back(bok);
}
else
cout << "Worng input!\n";
}
void oper::modify(vector<patient> &vp, vector<doctor> &vd, vector<books> &vb)
{
hintMessage3();
cin >> n;
hintMessage2();
cin >> a >> b;
if(n == "1"){
for(auto &x: vp){
if(x.Getname() == a && x.Getid() == b){
cout << "Query Success!\n";
cout << "请输入想要修改的信息\n";
cin >> paa;
x = paa;
cout << "Modify Success!\n";
return;
}
}
}
else if(n == "2"){
for(auto &x: vd){
if(x.Getname() == a && x.Getid() == b){
cout << "Query Success!\n";
cout << "请输入想要修改的信息\n";
cin >> doo;
x = doo;
cout << "Modify Success!\n";
return;
}
}
}
else if(n == "3"){
for(auto &x: vb){
if(x.GetdocName() == a && x.GetpatName() == b){
cout << "Query Success!\n";
cout << "请输入想要修改的信息\n";
cin >> bok;
x = bok;
cout << "Modify Success!\n";
return;
}
}
}
else{
cout << "Worng input!\n";
return;
}
cout << "cannot find it!\n";
}
void oper::dispaly(vector<patient> &vp, vector<doctor> &vd, vector<books> &vb)
{
hintMessage3();
string n;
cin >> n;
if(n == "1")
for(auto &x: vp) cout << x;
else if(n == "2")
for(auto &x: vd) cout << x;
else if(n == "3")
for(auto &x: vb) cout << x;
else
cout << "Worng input!\n";
}
void oper::del(vector<patient> &vp, vector<doctor> &vd, vector<books> &vb)
{
hintMessage3();
cin >> n;
hintMessage2();
cin >> a >> b;
if(n == "1"){
for(auto it=vp.begin(); it!=vp.end(); it++){
if(it->Getname() == a && it->Getid() == b){
vp.erase(it);
return;
}
}
}
else if(n == "2"){
for(auto it=vd.begin(); it!=vd.end(); it++){
if(it->Getname() == a && it->Getid() == b){
vd.erase(it);
return;
}
}
}
else if(n == "3"){
for(auto it=vb.begin(); it!=vb.end(); it++){
if(it->GetdocName() == a && it->GetpatName() == b){
vb.erase(it);
return;
}
}
}
else{
cout << "Worng input!\n";
return;
}
cout << "cannot find it!\n";
}
void oper::query(vector<patient> &vp, vector<doctor> &vd, vector<books> &vb)
{
hintMessage3();
cin >> n;
hintMessage2();
cin >> a >> b;
if(n == "1"){
for(auto &x: vp){
if(x.Getname() == a && x.Getid() == b){
cout << x;
return;
}
}
}
else if(n == "2"){
for(auto &x: vd){
if(x.Getname() == a && x.Getid() == b){
cout << x;
return;
}
}
}
else if(n == "3"){
for(auto &x: vb){
if(x.GetdocName() == a && x.GetpatName() == b){
cout << x;
return;
}
}
}
else{
cout << "Worng input!\n";
return;
}
cout << "cannot find it!\n";
}
- books预约类。
books.h
#ifndef BOOKS_H
#define BOOKS_H
#include <string>
#include <iostream>
#include <iomanip>
using std::string;
using std::cin;
using std::setw;
class books
{
friend std::istream& operator>> (std::istream &, books& );
friend std::ostream& operator<< (std::ostream &, books& );
public:
books(){}
books(string docname, string docid,
string patname, string patid,
string time, string description, string others);
string GetdocName() { return docName; }
void SetdocName(string val) { docName = val; }
string GetdocID() { return docID; }
void SetdocID(string val) { docID = val; }
string GetpatName() { return patName; }
void SetpatName(string val) { patName = val; }
string GetpatID() { return patID; }
void SetpatID(string val) { patID = val; }
string GetbookTime() { return bookTime; }
void SetbookTime(string val) { bookTime = val; }
string Getdescription() { return description; }
void Setdescription(string val) { description = val; }
string Getothers() { return others; }
void Setothers(string val) { others = val; }
protected:
private:
string docName;
string docID;
string patName;
string patID;
string bookTime;
string description;
string others;
};
#endif // BOOKS_H
books.cpp
#include "books.h"
books::books(string docname, string docid,
string patname, string patid,
string time, string description,string others)
{
SetdocName(docname);
SetdocID(docid);
SetpatName(patname);
SetpatID(patid);
SetbookTime(time);
Setdescription(description);
Setothers(others);
}
std::istream& operator >> (std::istream &input, books& s)
{
input >> s.docName >> s.docID
>> s.patName >> s.patID
>> s.bookTime >> s.description >> s.others;
return input;
}
std::ostream& operator<< (std::ostream &output, books& s)
{
output << setw(10) << s.docName
<< setw(10) << s.docID
<< setw(10) << s.patName
<< setw(10) << s.patID
<< setw(10) << s.bookTime
<< setw(10) << s.description
<< setw(10) << s.others << "\n";
return output;
}
- doctor类。除了基本的setter和getter函数,只重载了两个流操作符。
doctor.h
#ifndef DOCTOR_H
#define DOCTOR_H
#include <string>
#include <iostream>
#include <iomanip>
using std::string;
using std::cin;
using std::setw;
class doctor
{
friend std::istream& operator>> (std::istream &, doctor& );
friend std::ostream& operator<< (std::ostream &, doctor& );
public:
doctor(){}
doctor(string , string );
string Getid() { return id; }
void Setid(string val) { id = val; }
string Getname() { return name; }
void Setname(string val) { name = val; }
string Getmajor() { return major; }
void Setmajor(string val) { major = val; }
int Getyears() { return years; }
void Setyears(int val) { years = val; }
string Getdepartment() { return department; }
void Setdepartment(string val) { department = val; }
protected:
private:
string name;
string id;
string major;
int years;
string department;
};
#endif // DOCTOR_H
doctor.cpp
#include "doctor.h"
doctor::doctor(string name, string id)
{
Setname(name);
Setid(id);
}
std::istream& operator >> (std::istream &input, doctor& s)
{
input >> s.name >> s.id
>> s.major >> s.years
>> s.department;
return input;
}
std::ostream& operator<< (std::ostream &output, doctor& s)
{
output << setw(10) << s.name;
output << setw(10) << s.id;
output << setw(10) << s.major;
output << setw(10) << s.years;
output << setw(10) << s.department << "\n";
return output;
}
- patient类。同样,只需要重载两个流操作符。
patient.h
#ifndef PATIENT_H
#define PATIENT_H
#include <string>
#include <iostream>
#include <iomanip>
using std::string;
using std::cin;
using std::setw;
class patient
{
friend std::istream& operator>> (std::istream &, patient& );
friend std::ostream& operator<< (std::ostream &, patient& );
public:
patient(){}
patient(string ,string );
string Getid() { return id; }
void Setid(string val) { id = val; }
string Getname() { return name; }
void Setname(string val) { name = val; }
int Getage() { return age; }
void Setage(int val) { age = val; }
string Getsex() { return sex; }
void Setsex(string val) { sex = val; }
string Getwork() { return work; }
void Setwork(string val) { work = val; }
string Getaddress() { return address; }
void Setaddress(string val) { address = val; }
//void display();
protected:
private:
int age;
string id;
string name;
string sex;
string work;
string address;
};
#endif // PATIENT_H
patient.cpp
#include "patient.h"
patient::patient(string name,string id)
{
Setname(name);
Setid(id);
}
std::istream& operator >> (std::istream &input, patient& s)
{
input >> s.age >> s.id >> s.name >> s.sex
>> s.work >> s.address;
return input;
}
std::ostream& operator<< (std::ostream &output, patient& s)
{
output << setw(10) << s.age;
output << setw(10) << s.id;
output << setw(10) << s.name;
output << setw(10) << s.sex;
output << setw(10) << s.work;
output << setw(10) << s.address << "\n";
return output;
}
总结一下:两个半小时的时间,完成了这份代码。中间其实有些地方不够完善,不过不太好修改。还有的地方虽然知道写的很low,但限于能力不知道有什么更好的实现方法。愿慢慢进步。