大虾们帮忙找找问题不知道哪出错了,应该是出在主控程序里面,怎么改呢?
程序代码:
#include "stdafx.h" #include<string> #include<iostream.h> #include<assert.h> #include "SeqStack.h" #include "SeqQueue.h" using namespace std; struct car{ std::string num; //定义字符串 int time; }; int main(int argc, char* argv[]) { int iput,num,time; car car1,car2,car3; cout<<"************************欢迎进入停车场管理系统***************************"<<endl; cout<<"***本系统的收费规则是在停车场内的收费为每小时20元,在通道内停车收费0元***"<<endl; cout<<"停车场的停车泊位数为5个(通道泊位为5个):"<<endl; SeqStack<struct car> carpart1; SeqStack<struct car> carpart2;//备用栈,用来存放出栈的元素 SeqQueue<struct car> tongdao; SeqQueue<struct car> tongdao2;//备用队列,用来存放出队元素 iput=0; while(iput!=5) { do{ cout<<"[1] 开 车 进 入 "<<endl; cout<<"[2] 开 车 离 去 "<<endl; cout<<"[3] 输出停车场中所有汽车的车牌号 "<<endl; cout<<"[4] 输出候车道上的所有汽车的车牌号"<<endl; cout<<"[5] 退出该停车系统 "<<endl; cin>>iput; }while(iput>5 || iput<1); switch(iput){ case 1: cout<<"请输入入库车牌号"<<endl; cin>>num; car1.num=num; cout<<"请输入入库时间"<<endl; cin>>time; car1.time=time; carpart1.Push(car1); if(carpart1.IsFull()==true){ tongdao.EnQueue(car1); } if(tongdao.IsFull()==true){ cout<<"停车场与通道都已经满了"<<endl; } continue; case 2: cout<<"请输入离开的车牌号"<<endl; cin>>num; car2.num=num; cout<<"请输入出库时间"<<endl; cin>>time; car2.time=time; carpart1.Pop(car3); carpart2.Push(car3); while(car3.num!=car2.num){ carpart1.Pop(car3); carpart2.Push(car3); } if(carpart1.Pop(car3)==false){ cout<<"输入的车牌号不存在"<<endl; } else {cout<<"收费为"<<((car2.time-car3.time)*20)<<endl;} while(carpatr2.IsEmpty()!=true){ carpart2.Pop(car3); carpart1.Push(car3); } continue; case 3: while(carpart1.IsEmpty()!=true){ carpart1.Pop(car3); carpart2.Push(car3); cout<<"停车场内车牌为"<<car3.num<<endl; } while(carpatr2.IsEmpty()!=true){ carpart2.Pop(car3); carpart1.Push(car3); } if(carpart1.IsEmpty()==true){ cout<<"停车场内没有车"<<endl; } continue; case 4: while(tongdao.IsEmpty()!=true){ tongdao.EnQueue(car3); tongdao2.DeQueue(car3); cout<<"通道内车牌为"<<car3.num<<endl; } while(tongdao2.IsEmpty()!=true){ tongdao2.DeQueue(car3); tongdao1.EnQueue(car3); } if(tongdao1.IsEmpty()==true){ cout<<"通道内没有车"<<endl; } continue; case 5: cout<<"欢迎您再次光临!"<<endl; system("PAUSE"); } } } #include <iostream.h> #include <process.h> #include<iostream.h> #include "Queue.h" #include"SeqStack.h" template <class KT> class SeqQueue:public Queue<KT>{ public: SeqQueue(int sz=5); ~SeqQueue(){delete[] elements;} bool EnQueue(const KT &x);//队列不满,则将x进队,否则溢出 bool DeQueue(KT &x); bool getFront(KT &x); void makeEmpty(){front==rear;} bool IsEmpty()const{return(front==rear)?true:false;} bool IsFull()const{return ((rear+1)%maxSize==front)?true:false;} int getSize()const{return (rear-front+maxSize)%maxSize;} friend ostream & operator<<(ostream &os,SeqQueue<KT>&Q); protected: int rear,front; KT *elements; int maxSize; }; template<class KT> SeqQueue<KT>::SeqQueue(int sz):front(0),rear(0),maxSize(sz){ elements=new KT[maxSize]; assert(elements!=NULL); }; template<class KT> bool SeqQueue<KT>::EnQueue(const T & x){ if(IsFull() == true)return false; elements[rear]=x; rear=(rear+1)%maxSize; return true; }; template<class KT> bool SeqQueue<KT>::DeQueue(KT &x){ if(IsEmpty()==true)return false; x=elements[front]; front=(front+1)%maxSize; return true; }; template<class KT> bool SeqQueue<KT>::getFront(KT & x)const{ if(IsEmpty()==true)return false; x=elements[front]; return true; }; template <class KT> ostream& operator<<(ostream& os,SeqQueue<KT>& Q){ os<<"front="<<Q.front<<",rear="<<Q.rear<<endl; for(int i=front;i!=rear;i=(i+1)%maxSize;) os<<i<<":"<<Q.elements[i]<<endl; return os; };const int maxSize=5; enum bl2{nfalse,ntrue}; template <class KT> class Queue { public: Queue(){}; //构造函数 ~Queue(){}; //析构函数 virtual void EnQueue(const KT&x)=0;//新元素x进入队列 virtual bool DeQueue(KT &x)=0;//队头元素出队列 virtual bool getFront(KT &x)=0;//读取对头元素的值 virtual bool IsEmpty()const=0;//判空 virtual bool IsFull()const=0;//判满 virtual int getSize()const=0;//求队列元素的个数 };
程序代码:
#include <assert.h> #include <iostream.h> #include "stack.h" #include <process.h> //const int stackIncreament = 20; template <class T> class SeqStack:stack<T>{ public: SeqStack(int sz=5); ~SeqStack() {delete []elements;} void Push(const T& x); bool Pop(T& x); bool getTop(T& x); bool IsEmpty() {return (top == -1) ? true : false;} bool IsFull(){return (top == maxSize-1) ? true : false;} int getSize(){return top+1;} void MakeEmpty() {top = -1;} private: T *elements; int top; int maxSize; /*void overflowProcess();*/ }; template <class T> SeqStack<T>::SeqStack(int sz):top(-1), maxSize (sz) { elements = new T[maxSize]; assert(elements != NULL); }; /*template <class T> void SeqStack<T>::overflowProcess() { T *newArray = new T[maxSize + stackIncreament]; if (newArray = NULL) {cerr << "存储分配失败!" << endl; exit(1);} for (int i = 0; i <= top; i++) newArray[i] = elements[i]; maxSize = maxSize + stackIncreament; delete []elements; elements = newArray; }; */ template <class T> void SeqStack<T>::Push(const T& x) { if (IsFull()== true ) overflowProcess(); elements[++top] = x; }; template <class T> bool SeqStack<T>::Pop(T& x) { if (IsEmpty() == true) return false; x = elements[top--]; return true; }; template <class T> bool SeqStack<T>::getTop(T& x) { if (IsEmpty() == true) return false; x=elements[top]; return true; };const int maxSize2 = 5; enum bl{mfalse,mtrue}; template <class T> class Stack { //栈的类定义 public: Stack(){}; //构造函数 virtual void Push(const T& x); //新元素x进栈 virtual bool Pop(T& x); //栈顶元素出栈, 由x返回 virtual bool getTop(T& x); //读取栈顶元素, 由x返回 virtual bool IsEmpty(); //判断栈空否 virtual bool IsFull(); //判断栈满否 virtual int getSize(); //计算栈中元素个数 };