资源描述
停车厂管理系统数据结构与算法设计
队列代码:
#ifndef LinkedQueue_
#define LinkedQueue_
#include "node.h"
#include "xcept.h"
template<class T>
class LinkedQueue {
public:
LinkedQueue() {front = rear = 0,pos=0;}
~LinkedQueue();
LinkedQueue<T>& Add(T& x,T& y);
LinkedQueue<T>& Delete(T& x,T& y);
/********************************************/
int Position()
{
return pos;
} //得到车在队列中的位置
private:
Node<T> *front;
Node<T> *rear;
int pos;
/**************************************************/
};
template<class T>
LinkedQueue<T>::~LinkedQueue()
{// Queue destructor. Delete all nodes.
Node<T> *next;
while (front) {
next = front->link;
delete front;
front = next;
}
}
template<class T>
LinkedQueue<T>& LinkedQueue<T>::Add(T& x,T& y)
{
Node<T> *p = new Node<T>;
p->data=x;
p->time=y;
p->link=0;
pos++;
if (front)
{
rear->link = p;
}
else front = p;
rear = p;
return *this;
}
template<class T>
LinkedQueue<T>& LinkedQueue<T>::Delete(T& x,T& y)
{
x=front->data;
y=front->time;
pos--;
Node<T> *p = front;
front = front->link;
delete p;
return *this;
}
#endif
节点代码:
#ifndef Node_
#define Node_
template <class T> class LinkedQueue;
template <class T>
class Node {
friend LinkedQueue<T>;
public:
T data;
T time;
Node<T> *link;
};
#endif
栈的代码:
#ifndef Stack_
#define Stack_
#include "xcept.h"
template<class T>
class Stack {
// LIFO objects
public:
Stack(int MaxStackSize = 10);
~Stack() {delete [] stack;}
bool IsEmpty() const {return top == -1;}
bool IsFull() const {return top == MaxTop;}
T Top() const;
Stack<T>& Add(const T& x);
Stack<T>& Delete(T& x);
int top; // current top of stack
int MaxTop; // max value for top
T *stack; // element array
};
template<class T>
Stack<T>::Stack(int MaxStackSize)
{// Stack constructor.
MaxTop = MaxStackSize - 1;
stack = new T[MaxStackSize];
top = -1;
}
template<class T>
T Stack<T>::Top() const
{// Return top element.
if (IsEmpty()) throw OutOfBounds(); // Top fails
return stack[top];
}
template<class T>
Stack<T>& Stack<T>::Add(const T& x)
{// Add x to stack.
if (IsFull()) throw NoMem(); // add fails
stack[++top] = x;
return *this;
}
template<class T>
Stack<T>& Stack<T>::Delete(T& x)
{// Delete top element and put in x.
if (IsEmpty()) throw OutOfBounds(); // delete fails
x = stack[top--];
return *this;
}
#endif
// 停车厂管理.cpp : Defines the entry point for the console application.
主要代码:
#include "stdafx.h"
#include <iostream.h>
#include "lqueue.h"
#include "stack.h"
#include "node.h"
void main()
{
LinkedQueue<int> L; //便道停车队列
Stack<Node<int>>> S(2); //停车厂栈
Stack<Node<int>>> TemS(2); //临时栈
cout<<" --------------停车厂管理系统--------------------"<<endl;
cout<<"********************************************************************************"<<endl;
cout<<" ----------------温馨提示------------------------"<<endl;
cout<<"********************************************************************************"<<endl;
cout<<" --------------车辆到达请输入A-------------------"<<endl;
cout<<" --------------车辆离开请输入D-------------------"<<endl;
cout<<" --------------输入结束请输入E-------------------"<<endl;
cout<<"***********************停车厂收费标准为1美元/分钟*******************************"<<endl;
while(1)
{
char in;
int num;
int time;
cin>>in;
/**************************业务处理区*******************************/
switch(in)
{
case 'A':
Node<int> n;
cout<<"请输入车牌号和到达时间: ";
cin>>num;
cin>>time;
n.data=num;
n.time=time;
n.link=NULL;
if(S.top<1)
{
S.Add(n);
cout<<"车辆在停车厂内的位置为:第"<<S.top+1<<"号车位!"<<endl;
}
else
{
L.Add(n.data,n.time);
cout<<"车辆在便道上的停车位置为:第"<<L.Position()<<"号车位!"<<"(备注:此时不收费)"<<endl;
}
break;
case 'D':
Node<int> tem;
tem=S.Top();
cout<<"请输入车牌号和离开时间: ";
cin>>num>>time;
if(tem.data==num)
{
S.Delete(tem);
cout<<"车牌为"<<tem.data<<"的车"<<"应收费用为: "<<time-tem.time<<"美元!"<<endl;
if(L.Position()!=0)
{
L.Delete(tem.data,tem.time);
tem.time=time;
S.Add(tem);
cout<<"便道上车牌为"<<tem.data<<"的车"<<"进入停车厂第"<<S.top+1<<"号车位!"<<"(备注:此时开始收费)"<<endl;
}
}
else
{
while(S.top>=0)
{
S.Delete(tem);
if(tem.data==num)
{
cout<<"车牌为"<<tem.data<<"的车"<<"应收费用为: "<<time-tem.time<<"美元!"<<endl;
}
else
TemS.Add(tem);
}
}
//恢复停车厂
if(L.Position()!=0)
{
while(TemS.top>=0)
{
TemS.Delete(tem);
S.Add(tem);
}
//便道上的车进入停车厂
L.Delete(tem.data,tem.time);
tem.time=time;
S.Add(tem);
cout<<"便道上车牌为"<<tem.data<<"的车"<<"进入停车厂第"<<S.top+1<<"号车位!"<<"(备注:此时开始收费)"<<endl;
}
else
{
while(TemS.top>=0)
{
TemS.Delete(tem);
S.Add(tem);
}
}
break;
case 'E':
cout<<"谢谢您的使用!"<<endl;
return;
break;
}
}
}
展开阅读全文