这个还需要修改的内容如何修改昂,改了好几次还是不行,总是报错
/*
实验题目一:类的设计与定义。
实验要求:点类Point的设计与定义。Point 成员x,y。
成员函数:构造, set,get,移动,比较等。测试Point类
*/
#include<iostream>
#include<string>
using namespace std;
class Point{
public:
Point (){} //默认构造函数
Point(double xx,double yy):x(xx),y(yy){} //构造函数
void move(double xx,double yy){x=x+xx;y+=yy;} //坐标移动xx,yy
void setX(double xx){x=xx;} //设置坐标x
void setY(double yy){y=yy;} //设置坐标y
double getX(){return x;} //取得坐标x
double getY(){return y;} //取得坐标y
void setPoint(double xx,double yy){x=xx;y=yy;} //设置坐标x,y
void inputPoint();//从键盘输入坐标x,y
bool operator==(Point p); //运算符==重载,比较坐标点是否相等
void showPoint(){cout<<" 点坐标:("<<x<<","<<y<<")";}
private:
double x,y; //坐标x,y
};
void Point::inputPoint() //从键盘输入坐标x,y
{ cout<<"输入点坐标x,y:"<<endl;
cin>>x>>y;
}
bool Point::operator==(Point p)//运算符重载,比较坐标点是否相等
{ if(x==p.x&&y==p.y) return true;
else return false;
}
//int main(){
// Point p1(1,1),p2,p3;
// p2.setPoint(2,2);
// p3.inputPoint();
// p1.showPoint();
// p2.showPoint();
// p3.showPoint();
// p2.setX(1);
// p2.setY(1);
// p2.showPoint();
// if(p1==p2)
// {cout<<"两个点坐标相等"<<endl;}
// else
// {cout<<"两个点坐标不相等"<<endl;}
// return 0;
//}
/*实验题目二:类的组合
实验要求: Circle组合Point,声明基类Circle,半径radius(double),
圆心点center(Point).成员函数:构造函数,set,get,area,addRdius等。测试Circle类
*/
class Circle
{
public:
Circle(){}//默认构造函数
Circle(double x,double y,double r):center(x,y),radius(r){}//构造函数
void setRadius(double r){radius=r;}//半径赋值
double getRadius(){return radius;}//取得半径
void setCenter(Point p){center =p;}//圆心坐标赋值
Point getCenter(){return center;}//取得圆心坐标点
void setCircle(Point p,double r ){center=p;radius=r;}//圆数据赋值
void inputCircle();//从键盘输入圆心坐标,圆半径
void inputCircle(Point p);//已知圆心坐标点,从键盘输入,圆半径
void addRadius(double r){radius=radius+r;} //半径增量rr
double area(){return 3.14*radius*radius;}//求圆面积
void showCircle();//输出圆的圆心坐标和半径
private:
double radius;//圆半径
Point center;//圆心坐标,类的组合
};
void Circle::inputCircle()//从键盘输入圆心坐标,圆半径
{ center.inputPoint();
cout<<"输入半径"<<endl;
cin>>radius;
}
void Circle::inputCircle(Point p)//已知圆心坐标点,从键盘输入圆半径
{ center=p;
cout<<"输入半径"<<endl;
cin>>radius;
}
void Circle::showCircle()//输出圆的圆心坐标和半径
{ center.showPoint();
cout<<" 半径:"<<radius;
}
//int main(){
// Circle c1(1,1,2),c2,c3;
// c1.showCircle();
// Point p1(2,2);
// c2.setCircle(p1,3);
// c2.showCircle();
// c3.inputCircle();
// c3.showCircle();
// cout<<"area="<<c1.area()<<endl;
//return 0;
//}
/*实验题目三:派生类的应用—继承派生
实验要求:从Circle类派生出派生类Cylinder,新增数据:序号no(string),名字name(string),高height(double),
成员函数:构造函数,get,set,addHeight,area,volumn等。测试派生类Cylinder
*/
class Cylinder :public Circle //圆柱类公有继承圆类
{
public:
Cylinder(){}//默认构造函数
Cylinder(double x,double y,double r,string num,string nam,double h)
:Circle(x,y,r),no(num),name(nam),height(h){}//构造函数
void setNo(string num){no=num;}//编号赋值
void setName(string nam){name=nam;}//名字赋值
void setHeight(double h){height=h;}//高赋值
string getNo(){return no;}//得到编号
string getName(){return name;}//取得名字
double getHeight(){return height;}//取得高
void Cylinder::setCylinder(Circle c,string num,string nam,double h);//圆柱赋值
void Cylinder::setCylinder(Point cent,double r,string num,string nam,double h);//圆柱赋值
void inputCylinder();//从键盘读入一个圆柱
void inputCylinder(Point p);//已知圆心坐标点,从键盘读入一个圆柱其他信息
void addHeight(double hh){height+=hh;}//高增量hh
double area(){return(2*Circle::area()+2*3.14*getRadius()*height);}//求圆柱面积
double volumn(){return(Circle::area()*height);}//求圆柱体积
void showCylinder();// 输出一个圆柱信息
bool operator==(Cylinder cy);//比较,只比较圆心坐标
bool operator==(Point p);//比较,只比较圆心坐标
private:
string no;
string name;
double height;//圆柱高
};
void Cylinder::setCylinder(Circle c,string num,string nam,double h)//圆柱赋值
{//数据:继承自基类 点,半径,新增的编号,名字,高
setCenter(c.getCenter());//基类 点赋值
setRadius(c.getRadius()); //基类 半径赋值
no=num; //编号赋值
name=nam;//名字赋值
height=h;//高赋值
}
void Cylinder::inputCylinder()//从键盘读入一个圆柱
{//数据:继承自基类 点,半径,新增的高
inputCircle();
cout<<"输入圆柱的编号,名字和高"<<endl;
cin>>no>>name>>height;
}
void Cylinder::inputCylinder(Point p)//已知圆心坐标点,输入一个圆柱其他信息
{//数据:继承自基类 点,半径,新增的高
inputCircle(p);
cout<<"输入圆柱的编号,名字和高"<<endl;
cin>>no>>name>>height;
}
void Cylinder::setCylinder(Point cent,double r,string num,string nam,double h)//圆柱赋值
{//数据:继承自基类 点,半径,新增的编号,名字,高
setCenter(cent);//基类 点赋值
setRadius(r); //基类 半径赋值
no=num; //编号赋值
name=nam;//名字赋值
height=h;//高赋值
}
void Cylinder::showCylinder()// 输出一个圆柱信息
{
showCircle(); //输出基类 点,半径
cout<<" 编号: "<<no<<" 名字: "<<name<<" 高:"<<height<<endl;
}
bool Cylinder::operator==(Cylinder cy1)//比较,只比较圆心坐标
{
return (this->getCenter()==cy1.getCenter());
}
bool Cylinder::operator==(Point p)//比较,只比较圆心坐标
{
return(this->getCenter()==p);
}
//int main(){
// Cylinder cy1(1,1,2,"1001","南师001",3),cy2(2,1,4,"1003","南师003",5),cy3;
// cy1.showCylinder();
// Circle c1(2,2,3);
// cy2.setCylinder(c1,"1002","南师002",4);
// cy2.showCylinder();
// cy3.inputCylinder();
// cy3.showCylinder();
// cout<<"area="<<cy1.area()<<endl;
// //输出圆柱的圆心点坐标的x值
// cout<<" 坐标点x: ";
// cout<<cy1.getCenter().getX()<<endl;
// //举例cy1找到他负责的学生x,
// //学生处cy1老师,打电话找辅导员center ,辅导员找到x
// //修改圆柱的圆心点坐标
// cy1.setCenter(Point(2,0));
// cy1.showCylinder();
// return 0;
//}
/*
//总结:组合与继承实现了代码复用。
1.除了构造和析构函数,在组合类Circle中,可以调用数据成员类Point的函数。
在派生类Cylinder中,可以调用基类Circle的函数
2.做到各司其职,每个类负责对其数据成员的赋值、输出等操作。
如Cylinder中:数据有继承自基类Circle的圆心点和半径,新增的高。
调用基类Circle中的函数对点和半径处理,然后对高处理。
如:setCylinder(Circle c,double hh); //圆柱赋值
inputCylinder();//键盘输入对圆柱赋值
showCylinder(); //输出圆柱信息
*/
/*
实验题目四:派生类的应用—继承派生与组合
实验要求:在实验三的基础上增加点类Point作为Circle的成员,组合。
Cylinder继承了Circle
增加管理类Manager,对一组圆柱进行增删改查操作,数据文件操作
*/
#include<fstream>
class Manager{
public:
Manager(){length=0;}
void inputFromFile(string filename);//从文本磁盘文件filename读入数据到cylinder数组
void outputFile(string filename); //将数据cylinder数组写入文本磁盘文件filename
void inputFromBinaryFile(string filename);//从二进制磁盘文件filename读入数据到cylinder数组
void outputBinaryFile(string filename); //将数据cylinder数组写入二进制磁盘文件
void traveralCylinder(); //遍历所以圆柱对象
void traveralCylinderIndex(int idx);//遍历下标为idx的圆柱对象
void printArea(); //输出圆柱面积
void printVolumn(); //输出圆柱体积
void insertCylinder(Cylinder cy ); //插入一个圆柱对象
bool searchCylinder( Cylinder cy); //查找一个圆柱对象cy,找到返回true,否则返回false
bool searchCylinder( Point p); //查找一个圆心点,找到返回true,否则返回false
int searchCylinderIndex( Cylinder cy); //查找一个圆柱对象cy,找到返回下标,否则返回-1
int searchCylinderIndex( Point p); //查找一个圆柱的圆心点坐标,找到返回下标,否则返回-1
void modifyCylinder( Cylinder cy);//修改cy的半径
void deleteCylinder( Cylinder cy);//删除圆柱cy
private :
Cylinder cylinder[100];
int length; //实际圆柱个数
};
void Manager::inputFromFile(string filename)
{
double xx,yy,rr,hh;
string num,nam;
ifstream infile(filename,ios::in); //1.打开
//定义输入文件流对象,以输入方式打开磁盘文件f1.dat
if(!infile) //2.判断
{ cerr<<"open error!"<<endl;
exit(1);
}
int i=0;
while(!infile.eof()) //3.读出
{
infile>>xx>>yy>>rr>>num>>nam>>hh;//从磁盘文件读数
cylinder[i].setCylinder(Point(xx,yy),rr,num,nam,hh);
i++;
}//while
length=i;
infile.close(); //4.关闭
}
void Manager::outputFile(string filename){
ofstream outfile(filename,ios::out); //1打开
//定义输出文件流对象,以输出方式打开磁盘文件f1.dat
if(!outfile) //2判断
{ cerr<<"open error!"<<endl;
exit(1);
}
double xx,yy,rr,hh;
string num,nam;
for(int i=0;i<length;i++){
xx=cylinder[i].getCenter().getX();
yy=cylinder[i].getCenter().getY();
rr=cylinder[i].getRadius();
num=cylinder[i].getNo();
nam=cylinder[i].getName();
hh=cylinder[i].getHeight();
outfile<<xx<<" "<<yy<<" "<<rr<<" "<<num<<" "<<nam<<" "<<hh<<endl; //写文件
}
outfile.close();//4关闭
}
void Manager::inputFromBinaryFile(string filename)
{
ifstream infile(filename,ios::binary); //1.打开
//定义输入文件流对象,以输入方式打开磁盘文件filename
if(!infile) //2.判断
{ cerr<<"open error!"<<endl;
exit(1);
}
int i=0;
while(!infile.eof()) //3.读出
{
infile.read((char*)&cylinder[i],sizeof(cylinder[i]));//从磁盘文件读数
i++;
}
length=i-1; //多读入1个元素
cout<<"inputFromBinaryFile length="<<length<<endl;
infile.close(); //4.关闭
}
void Manager::outputBinaryFile(string filename){
ofstream outfile(filename,ios::binary); //1打开
//定义输出文件流对象,以输出方式打开磁盘文件f1.dat
if(!outfile) //2判断
{ cerr<<"open error!"<<endl;
exit(1);
}
Cylinder cy;
for(int i=0;i<length;i++){
outfile.write((char*)&cylinder[i],sizeof(cylinder[i])); //写文件
}
cout<<"outputBinaryFile length="<<length<<endl;
outfile.close();//4关闭
}
void Manager::traveralCylinder() //遍历所有圆柱对象
{
int i;
for(i=0;i<length;i++)
{
cout<<"序号: "<<i+1<<": ";
cylinder[i].showCylinder();
}
}
void Manager::traveralCylinderIndex(int idx)//遍历下标为idx的圆柱对象
{
cout<<"序号: "<<idx+1<<": ";
cylinder[idx].showCylinder();
}
void Manager::printArea()
{
int i;
for(i=0;i<length;i++)
{
cout<<"序号: "<<i+1<<" area: "<<cylinder[i].area()<<endl;
}
}
void printArea(Circle *ptrc)
{ cout<<ptrc->area()<<endl;}
void Manager::insertCylinder(Cylinder cy ) //插入一个圆柱对象到尾部
{
cylinder[length]=cy;
length++; //圆柱个数增1;
}
bool Manager::searchCylinder( Cylinder cy) //查找一个圆柱对象cy
{
int i;
for(i=0;i<length;i++)
{
if(cylinder[i]==cy)
return true;
}
return false;
}
bool Manager::searchCylinder( Point p) //查找一个圆心点,查找成功返回下标,否则返回-1
{
int i;
for(i=0;i<length;i++)
{
if(cylinder[i]==p)
return true;
}
return false;
}
int Manager::searchCylinderIndex( Cylinder cy) //查找一个圆柱对象cy,找到返回下标,否则返回-1
{
int i;
for(i=0;i<length;i++)
{
if(cylinder[i]==cy)
return i;
}
return -1;
}
int Manager::searchCylinderIndex( Point p) //查找一个圆柱圆心点坐标,找到返回下标,否则返回-1
{
int i;
for(i=0;i<length;i++)
{
if(cylinder[i].getCenter()==p)
return i;
}
return -1;
}
void Manager::modifyCylinder( Cylinder cy)//修改cy的半径增加2倍
{
int idx=searchCylinderIndex(cy);
if(idx==-1)
{cout<<"没有这个圆柱"<<endl;}
else
{
double rr=cylinder[idx].getRadius();
cylinder[idx].setRadius(2*rr);
}
}
void Manager::deleteCylinder( Cylinder cy)//删除cy
{
int idx=searchCylinderIndex(cy); //找到cy所在位序
if(idx==-1) // 没找到
{cout<<"没有这个圆柱"<<endl;}
else //找到,则删除,后面元素前移
{
for(int i=idx+1;i<length;i++)
{cylinder[i-1]=cylinder[i];} //前移元素
length--; //个数减1
}
}
void menu()
{
cout<<endl<<endl;
cout<<"*----------操作菜单----------*"<<endl;
cout<<"* *"<<endl;
cout<<"* 1.从磁盘文件中读取数据 *"<<endl;
cout<<"* 2.数据存入磁盘文件中 *"<<endl;
cout<<"* 3.遍历数据 "<<" 4.查找数据 *"<<endl;
cout<<"* 5.插入数据 "<<" 6.修改数据 *"<<endl;
cout<<"* 7.删除数据 "<<" 8.退出操作 *"<<endl;
cout<<"* *"<<endl;
cout<<"*----------------------------*"<<endl;
}
int main()
{
Cylinder cy;
Manager manager;
int choose;
bool result;
int idx;
Point p;
while(1){
menu();
cout<<" 请选择你的操作(输入数字): ";
cin>>choose;
switch(choose){
case 1: manager.inputFromFile("data.txt"); break;
case 2: manager.outputFile("out.txt"); break;
//case 1: manager.inputFromBinaryFile("sourceBinary.dat"); break;
//case 2: manager.outputBinaryFile("destBinary.dat"); break;
case 3: manager.traveralCylinder(); break;
case 4: cout<<"4.查找数据 "<<endl;
p.inputPoint();
idx=manager.searchCylinderIndex(p);
if(idx!=-1)
{cout<<"查找成功"<<endl;
manager.traveralCylinderIndex(idx);
}
else
{cout<<"查找不成功"<<endl;}
break;
case 5:
//从键盘读入一个圆柱信息,不存在则插入
p.inputPoint();
result=manager.searchCylinder(p);
if(!result)//如果不存在,则插入
{ cy.inputCylinder(p);
manager.insertCylinder(cy);
}
else
{cout<<"在该坐标位置已经有圆柱,不能插入。"<<endl;}
;break;
case 6:
cy.inputCylinder(); //从键盘读入一个圆柱
manager.modifyCylinder(cy); //存在则修改
break;
case 7:
cout<<"7.删除数据 "<<endl;
cy.inputCylinder(); //从键盘读入一个圆柱
manager.deleteCylinder(cy); //找到则删除
break;
case 8: exit(0);
}//switch
}//while
return 0;
}
/*V2版本新增函数
int Manager::searchCylinderIndex( Point p); //查找一个圆柱的圆心点坐标,找到返回下标,否则返回-1
void Manager::traveralCylinderIndex(int idx)//遍历下标为idx的圆柱对象
void Circle::inputCircle(Point p);//已知圆心坐标点,从键盘输入,圆半径
void Cylinder::inputCylinder(Point p);//已知圆心坐标点,从键盘读入一个圆柱其他信息
*/
/*
需要整改内容:
1.插入、删除、修改时需要判断是否存在此对象,
只需输入比较关键字就可。请修改完善代码
2.修改时,最好输入要修改的数据。请修改完善代码
3.删除时,需要保持数组的连续性。请修改完善代码
4.成员名命名规范
5.函数中形参 使用 const &
6.功能不全。求面积,体积,请修改完善代码
*/
/*
int main()
{
int choose;
while(1){
menu();
cout<<" 请选择你的操作(输入数字): ";
cin>>choose;
switch(choose){
case 1: cout<<"1.从磁盘文件中读取数据 "<<endl; break;
case 2: cout<<"2.数据存入磁盘文件中 "<<endl; break;
case 3: cout<<"3.遍历数据 "<<endl; break;
case 4: cout<<"4.查找数据 "<<endl;break;
case 5: cout<<"5.插入数据 "<<endl;break;
case 6: cout<<"6.修改数据 "<<endl; break;
case 7: cout<<"7.删除数据 "<<endl; break;
case 8: exit(0);
}//switch
}//while
return 0;
}
*/
医院就诊系统一共实现四个功能:1排队,2就诊,3查询排队患者信息,4退出系统
1、 排队,排队的病历号从0开始,每执行依次排队,病历号+1,进链队
2、 就诊,依次出链队,先进先出,同时随机显示诊断结果
3、 查询排队人数,不仅显示出人数,也显示出排队患者的病历号
4、 退出系统,同时清空链队。
具体代码的解释都写在注释里了
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
typedef int ElemType;
struct LNode{
ElemType data;//值域
LNode* next;//链接指针域
};
struct LinkQueue{
LNode* front;//队首指针
LNode* rear;//队尾指针
};
//1.初始化队列
void InitQueue(LinkQueue& HQ){
HQ.front=HQ.rear=NULL;
}
//7.计算链队长度
int LenthQueue(LinkQueue& HQ){
LNode* p;
int Lenth=0;
p=HQ.front->next;
while(p!=NULL){
Lenth++;
p=p->next;
}
return Lenth;
}
//2.向链队中插入一个元素
ElemType EnQueue(LinkQueue& HQ,ElemType item){
LNode* newptr=new LNode;//得到一个新结点
newptr->data=item;
newptr->next=NULL;//新结点指针域置空
if(HQ.rear==NULL)
HQ.front=HQ.rear=newptr;//若链队为空的话,则新结点既是队首又是队尾
else//若链队非空,则新结点被链接到队尾,并且修改队尾指针
HQ.rear=HQ.rear->next=newptr;
}
//3.从链队中删除一个元素
ElemType OutQueue(LinkQueue& HQ,ElemType &e){
//判空
if(HQ.front==NULL){
cerr<<"没有患者在排队!"<<endl;
}
/* LNode* p=HQ.front;//暂存队首指针以便回收队首结点
HQ.front=p->next;//修改队首指针,使其指向下一个结点
e = p->data;
if(HQ.front==NULL)
HQ.rear=NULL;//若删除之后链队为空,则使队尾指针为空
delete p;*/
LNode *p;
p = HQ.front->next;
e = p->data;
HQ.front->next = p->next;
if (HQ.rear == p)
HQ.rear = HQ.front;
free(p);
}
/*//4.读取队首元素
ElemType PeekQueue(LinkQueue& HQ){
if(HQ.front==NULL){
cerr<<" 链队队首无元素!"<<endl;
exit(1);
}
return HQ.front->data;
}
*/
//5.检查链队是否为空
bool EmptyQueue(LinkQueue& HQ){
return HQ.rear==NULL;
}
//6.清除链队中的所有元素,使其成为一个空队
void ClearQueue(LinkQueue& HQ){
LNode* p=HQ.front;//队首指针赋给p
while(p!=NULL){/*依次删除链队里的每一个结点,循环结束后,队首指针已经为空!*/
HQ.front=HQ.front->next;
delete p;
p=HQ.front;
}
HQ.rear=NULL;
}
//疾病类型的输出,运用随机
void Type_Sickness(int type){
switch(type){
case 1:
cout<<" 您得的是感冒,吃点药吧!"<<endl;
break;
case 2:
cout<<" 您得的是严重感冒,打针吧!"<<endl;
break;
case 3:
cout<<" 您发烧37.8度,挂瓶吧!"<<endl;
break;
case 4:
cout<<" 您是劳累过度了,注意休息即可!"<<endl;
break;
case 5:
cout<<" 您的病需要进一步诊断,请转去大医院!"<<endl;
break;
case 6:
cout<<" 您什么病也没有,恭喜您!"<<endl;
break;
}
}
//查询
void Search(LinkQueue& HQ) {
LNode *p;
//判空
if(HQ.front==NULL){
cerr<<"当前无排队患者!"<<endl;
}
else{
cout<<"当前排队患者:"<<endl;
p=HQ.front->next;
while(p!=NULL){
cout<<"病历号为"<<p->data<<"的患者"<<endl;
p=p->next;
}
}
}
main(){
LinkQueue HQ;
InitQueue(HQ);
int num;//排队病历号
//int numm;//当前就诊患者病历号
int n;
cout<<"**医院就诊系统**"<<endl<<endl;
cout<<" 1.排队"<<endl;
cout<<" 2.就诊"<<endl;
cout<<" 3.查询"<<endl;
cout<<" 4.下班"<<endl;
while(1){
cout<<"请选择服务:"<<endl;
cin>>n;
if(n==1){//排队
EnQueue(HQ,num);
cout<<"患者病历号为:"<<num<<endl;
cout<<"在您前面有"<<LenthQueue(HQ)<<"位患者"<<endl;
num++;
}
if(n==2){//就诊
OutQueue(HQ,num);
cout<<"当前就诊患者病历号为:"<<num<<endl;
cout<<"诊断结果为:";
Type_Sickness(rand()%6);// rand()%6产生的值为0-5,rand()函数产生的数在0到32767之间
}
if(n==3){//查询
Search(HQ) ;
cout<<endl;
}
if(n==4){//退出
ClearQueue(HQ);
cout<<"您已经成功退出系统!"<<endl;
break;
}
}
system("pause");
return 0;
}