我的程序有点意想不到的问题

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;
}








...全文
121 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sjyf 2005-10-24
  • 打赏
  • 举报
回复
我对qNode(car s, qNode *l = NULL)作了如下的修改: 但是队列存储的仍然是一个结点.

qNode::qNode(car s, qNode *l = NULL)
{
new qNode;
this -> data.set(s.getNum(), s.getTime());
this -> next = NULL;
}

我觉得问题出在 void queue::enQue(qNode &s)上

void queue::enQue(qNode &s)
{

qNode *p;
p = &s;
if(front == NULL){
rear = &s;
front = &s;
}
else{ //这里似乎rear一直等于front,整理了好久也
rear -> modnext(p); //不知道症结在何处,
rear = p;
}
}

希望高手们工作之余帮我看看,解决一下.
1vs2 2005-10-21
  • 打赏
  • 举报
回复
haha
zhangsong1238 2005-10-21
  • 打赏
  • 举报
回复
void queue::enQue(qNode &s)//这里的S引用没有用上,你每次调用enQue()时都是一 个新的点。
{
qNode *p; //这里请仔细看一下,队列只能存储一个接点
p = new qNode();//p=new qNode(s) 当然qNode要定义这样一个构造函数
p -> modnext(NULL);
if(front == NULL){
rear = p;
front = rear;
}
else{
rear -> modnext(p);
rear = p;
}

}
不知道我这样改对不对的,因为手上没有编译器所以也没有去验证的
v41dugu 2005-10-21
  • 打赏
  • 举报
回复
恩~~~顶。。。好长。。。
xiaocai0001 2005-10-21
  • 打赏
  • 举报
回复
这么长?

帮顶一个吧~

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧