zoukankan      html  css  js  c++  java
  • 循环顺序队列模拟病人看病程序

      1 #include<iostream>
      2 #include<cstdlib>
      3 using namespace std;
      4 #define OK 1
      5 #define ERROR 0
      6 #define OVERFLOW -2
      7 #define MAXQSIZE 100
      8 typedef int QElemType;
      9 typedef int Status;
     10 typedef struct
     11 {
     12     QElemType *base;
     13     int front;//头指针
     14     int rear;//尾指针
     15 }SqQueue;
     16 
     17 Status InitQueue(SqQueue &Q){   //构造一个空队列
     18     Q.base=new QElemType[MAXQSIZE];
     19     if(!Q.base)exit(OVERFLOW);
     20     Q.front=Q.rear=0;
     21     return OK;
     22 }
     23 
     24 bool exist(SqQueue Q,int no){  //1.队列中是否有no病历号的病人
     25     bool findno=false;
     26     for(int i=Q.front;i!=Q.rear;i=(i+1)%MAXQSIZE){
     27         if(Q.base[i]==no){
     28             findno=true;
     29             break;
     30         }
     31     }
     32     return findno;
     33 }
     34 
     35 Status EnQueue(SqQueue &Q,int no){   //1.在队尾插入no病历号的病人
     36     if((Q.rear+1)%MAXQSIZE==Q.front) //如果队满,则返回0
     37         return ERROR;
     38     else{
     39         Q.base[Q.rear]=no;     //先插,后将尾指针后移一位
     40         Q.rear=(Q.rear+1)%MAXQSIZE;
     41     }
     42     return OK;  //插入成功
     43 }
     44 
     45 Status DeQueue(SqQueue &Q,int &no){  //2.就诊即出队,引用出队病号
     46     if(Q.front==Q.rear)return ERROR;  //如果队空,返回0
     47     no=Q.base[Q.front];
     48     Q.front=(Q.front+1)%MAXQSIZE;
     49     return OK;
     50 }
     51 
     52 void SeeDoctor()
     53 {
     54     int sel,no;
     55     bool flag=true;   //先标记为true
     56     SqQueue Q;
     57     InitQueue(Q);   //构造队列
     58     while(flag){
     59         cout<<"1:排队 2:就诊 3:查看排队 4.不再排队,余下依次就诊 5:下班  请选择:";
     60         cin>>sel;
     61         switch(sel){
     62         case 1:   //排队,查看是否用排队
     63             cout<<"  >>输入病历号:";
     64             while(true){
     65                 cin>>no;
     66                 if(exist(Q,no))
     67                     cout<<"  >>输入的病历号重复,重新输入:";
     68                 else
     69                     break;
     70             }
     71             if(EnQueue(Q,no))
     72                 cout<<"排队成功"<<endl<<endl;
     73             else
     74                 cout<<"当前排队人数已满,无法排队"<<endl<<endl;
     75             break;
     76         case 2:    //就诊
     77             if(DeQueue(Q,no))
     78                 cout<<"  >>病人"<<no<<"就诊."<<endl<<endl;
     79             else
     80                 cout<<"  >>没有排队的病人!"<<endl<<endl;
     81             break;
     82         case 3:    //查看排队
     83             if(Q.front==Q.rear)
     84                 cout<<"  >>没有排列的病人!"<<endl<<endl;
     85             else{
     86                 cout<<"  >>排队病人:";
     87                 for(int i=Q.front;i!=Q.rear;i=(i+1)%MAXQSIZE)
     88                     cout<<Q.base[i]<<' ';
     89                 cout<<endl;
     90             }
     91             break;
     92         case 4:    //不再排队,余下依次就诊
     93             if(Q.front==Q.rear)  //队空
     94                 cout<<"  >>没有排列的病人!"<<endl<<endl;
     95             else{                //队不空
     96                 cout<<"  >>病人按以下顺序就诊:";
     97                 for(int i=Q.front;i!=Q.rear;i=(i+1)%MAXQSIZE)
     98                     cout<<Q.base[i]<<' ';
     99                 cout<<endl;
    100             }
    101             delete []Q.base;    //释放Q.base指向的一整块内存
    102             Q.base=NULL;  //顺便赋值为NULL,防止出现野指针
    103             flag=false;
    104             break;
    105         case 5:   //下班
    106             if(Q.front!=Q.rear)
    107                 cout<<"  >>请排队的病人明天就医!"<<endl;
    108             else
    109                 cout<<"  >>没有排队的病人!"<<endl;
    110             delete []Q.base;    //释放Q.base指向的一整块内存
    111             Q.base=NULL;   //顺便赋值为NULL,防止野指针
    112             flag=false;
    113             break;
    114         }
    115     }
    116 }
    117 int main()
    118 {
    119     SeeDoctor();
    120     return 0;
    121 }
  • 相关阅读:
    Spring 注解注入—@Qualifier 注释
    Spring基于 @Autowired 和@Required区别与联系
    Spring基于注解@Required配置
    MySQL存储过程---变量的应用
    MySQL存储过程---基础
    MySQL中的变量
    MySQL内置函数-单行函数(流程控制函数)
    MySQL内置函数-版本、用户等函数
    MySQL内置函数-单行函数(字符函数)
    MySQL内置函数-单行函数(日期函数)
  • 原文地址:https://www.cnblogs.com/acgoto/p/8898191.html
Copyright © 2011-2022 走看看