我的程序有点意想不到的问题
sjyf 2005-10-21 03:20:10 做的模拟停车场管理系统,遇到了这样的问题:便道我用队列来表示的,它用来在停车场满的时侯来停放车辆,但是我不知道怎么回事它仅能存放一个元素,不知道问题出在哪里,请大虾们帮一帮!
其中s1表示停车场(用栈表示),s2(用栈表示)用来存放当s1出栈时的元素不是栈顶元素时的元素.
//////////////////////////stocar.h///////////////////////////
#ifndef stackcar
#define stackcar
class car {
public:
car():intNum(0),intTime(0){};
void set(int x, int y);
int getNum();
int getTime();
car(car &s);
private:
int intNum, intTime;
};
#endif
/////////////////////////////////////stosta.h///////////////////////////////////
#include "stocar.h"
#ifndef stostack
#define stostack
class stack {
public:
stack(int maxsize);
void push(car &);
car pop();
int empty();
void printtop();
int isfull();
car gettop();
int getsize();
private:
int size;
car *stastr;
int top;
int maxSize;
};
#endif
////////////////////////////////////stoque.h//////////////////////////////////////////////
#include "stocar.h"
#ifndef stoqueue
#define stoqueue
class qNode {
public:
qNode(car s, qNode *l);
qNode(){};
void modnext(qNode *p);
car getdata();
qNode* getnext();
private:
car data;
qNode *next;
};
class queue {
public:
queue();
void enQue(qNode &item);
car deQue();
int Qisempty();
private:
qNode *front, *rear;
};
#endif
/////////////////////////////////////stocar.cpp////////////////////////////////////
#include "stocar.h"
void car::set(int x, int y)
{
intNum = x;
intTime = y;
}
int car::getNum()
{
return intNum;
}
int car::getTime()
{
return intTime;
}
car::car(car &s)
{
intNum = s.getNum();
intTime = s.getTime();
}
//////////////////////////////////////////stosta.cpp/////////////////////////////////////
#include "stosta.h"
#include <iostream.h>
stack::stack(int maxsize)
{
stastr = new car[maxsize];
if(stastr){
top = -1;
size = 0;
maxSize = maxsize;
}
}
void stack::push( car &item)
{
stastr[++ top] = item;
size ++;
}
car stack::pop()
{
car s;
s = stastr[top --];
size --;
return s;
}
int stack::empty()
{
if (top == -1)
return 1;
else
return 0;
}
void stack::printtop(void)
{
cout << "车牌号为: "<< stastr[top].getNum() << "\40的汽车在: " << stastr[top].getTime() << \
"\40时进入了停车场!" << endl;
}
int stack::isfull()
{
if(top == maxSize-1){
cout << "停车场已满!,该车辆将在便道上等待!" << endl;
return 1;
}
else
return 0;
}
car stack::gettop()
{
car s(stastr[top]);
return s;
}
int stack::getsize()
{
return size;
}
//////////////////////////////stoque.cpp/////////////////////////////////////////
#include "stocar.h"
#include "stoque.h"
#include <iostream.h>
qNode::qNode(car s, qNode *l = NULL)
{
data.set(s.getNum(), s.getTime());
next = NULL;
}
queue::queue()
{
front = NULL;
rear = NULL;
}
void queue::enQue(qNode &s)
{
qNode *p; //这里请仔细看一下,队列只能存储一个接点
p = new qNode; //我不明白是为什么
p -> modnext(NULL);
if(front == NULL){
rear = p;
front = rear;
}
else{
rear -> modnext(p);
rear = p;
}
}
void qNode::modnext(qNode *p)
{
next = p;
}
int queue::Qisempty()
{
if(front == NULL)
return 1;
else
return 0;
}
car queue::deQue()
{
car d(front -> getdata());
front = front -> getnext();
return d;
}
qNode *qNode::getnext()
{
return next;
}
car qNode::getdata()
{
car d(data);
return d;
}
/////////////////////////////////car.cpp////////////////////////////////////////////////
#include "stocar.h"
#include "stosta.h"
#include <iostream.h>
#include "stoque.h"
#include <process.h>
char choice();
int main(void)
{
int mSize;
cout << "请你设置停车场的大小: ";
cin >> mSize;
double rate;
cout << "每辆车单位时间内要收的钱: " ;
cin >> rate;
stack s1(mSize); //创建栈s1(停车场)
stack s2(mSize - 1); //创建栈s2用来暂时存放由s1出来而不离开的车辆
queue Q; //创建队列Q(便道)当s1满时,车辆在Q中等待
while(1){
char ch;
ch = choice();
//车辆到达
if(ch == 'a' || ch == 'A'){
int n, t;
cout << "请你输入你的车牌号: ";
cin >> n;
cout << "请输入现在时间: ";
cin >> t;
car c;
c.set(n, t);
if(!s1.isfull()){ //栈不满入栈,满则入队
s1.push(c);
s1.printtop();
}
else{
qNode q(c, NULL);
Q.enQue(q);
}
}
//车辆离开
if(ch == 'D' || ch == 'd'){
int n, t;
cout << "请输入要离开的车牌号: ";
cin >> n;
cout << "请输入现在的时间: ";
cin >> t;
if(s1.empty()){ //停车场空输出相应信息
cout << "停车场是空的!" << endl;
continue;
}
while(s1.gettop().getNum() != n && s1.getsize() != 0)
s2.push(s1.pop()); //非空,且出栈元素不是栈顶元素,s2暂存它们
if(s1.gettop().getNum() == n){
int sub = t - s1.pop().getTime(); //出栈计算停留时间
double sum = sub * rate;
cout << "这辆车的主人需要交纳: " << sum << "\40元钱" << endl;
while(!s2.empty()) //s2中元素回到s1
s1.push(s2.pop());
if(Q.Qisempty()){ //便道非空则把元素进入s1
cout << "对空!";
continue;
}
if(!Q.Qisempty())
s1.push(Q.deQue());
}
if(s1.getsize() == 0){ //输入车辆不在s1内
cout << "没有你要的信息!" << endl;
while(!s2.empty())
s1.push(s2.pop());
}
}
if(ch == 'e' || ch == 'E')
exit(1);
}
return 0;
}
char choice()
{
char c;
cout << "把车停到停车场按A;";
cout << "离开停车场按D;";
cout << "退出系统按E.";
cin >> c;
return c;
}