数据结构停车场问题,出停车场时有问题。


include

include

include

using namespace std;
class Car
{
public:
int licence;
int time;
};
class Parknode//停车场节点
{
public:
Car car;
Parknode next;
Parknode(int l = 0, int t = 0)
{
car.licence = l;
car.time = t;
next = NULL;
}
void setpnode(int l, int t)
{
car.licence = l;
car.time = t;
}
};
class Parkstack//停车场栈
{
public:
int size;//停车位个数
int address;//停车场位置
Parknode *top;
Parknode *p;
Parkstack()
{
top = new Parknode();
top->next = NULL;
}
void Push(int l, int t)
{
p = new Parknode(l, t);
p->next = top->next;
top->next = p;
}
Car Pop()
{
Car p_retvalue;
if (top->next != NULL)
{
p = top->next;
p_retvalue = p->car;
top->next = p->next;
delete p;
return p_retvalue;
}
}
int Gettop()
{
if (top->next != NULL)
{
return top->next->car.licence;
}
}
};
class Waitnode//便道节点
{
public:
Car car;
Waitnode *next;
Waitnode(int l = 0, int t = 0)
{
car.licence = l;
car.time = t;
next = NULL;
}
};
class Waitqueue//等候队列
{
public:
Waitnode *front, *rear;
Waitnode *p;
int address;
Waitqueue()
{
rear = front = new Waitnode();
}
void Enter(int l, int t)
{
rear->next = new Waitnode(l, t);
rear = rear->next;
}
Car leave()
{
if (!isempty())
{
p = front->next;
Car w_retvalue = p->car;
front->next = p->next;
delete p;
if (front->next == NULL)
rear = front;
return w_retvalue;
}
else
{
cout << "便道空!" << endl;
}
}
Car Front()//取便道头一辆车
{
if (!isempty())
{
return front->next->car;
}
}
bool isempty()
{
return front->next == NULL;
}
};
int main()
{
char status[2];
int price;//每小时停车费用
int money;//总停车费
int stime;//进入停车场时间
int etime;//离开停车场时间
Parkstack park;//停车场
Parkstack temp;//临时停车场
Waitqueue wait;//便道
park.address = 1;//停车场位置
wait.address = 1;//便道位置
cin >> park.size >> price;
//第一辆车进入停车场
cin >> status;
while (strcmp(status, "E") != 0)
{
Car car;
cin >> car.licence >> car.time;
if (strcmp(status, "A") == 0)//进入停车场
{
if (park.size == 0)
{
cout << "停车场已满" << endl;
wait.Enter(car.licence, car.time);
cout << "汽车" << car.licence << "停靠在便道的" << wait.address << "位置" << endl;
wait.address++;
}
else
{
park.Push(car.licence, car.time);
cout << "汽车" << car.licence << "停靠在停车场" << park.address << "号位置" << endl;
park.address++;
park.size--;
stime = car.time;
}
}
else if (strcmp(status, "D") == 0)//;离开停车场
{
while (park.top->next != NULL)
{
if (park.Gettop() == car.licence)
{
park.Pop();
etime = car.time;
money = price
(etime - stime);
cout << "汽车" << car.licence << " 停车" << etime - stime << "小时,缴纳停车费" << money << "元" << endl;
park.size++;
park.address--;
while (temp.top->next != NULL)
{
temp.Pop();
park.Push(temp.Pop().licence, temp.Pop().time);
}
if (!wait.isempty())
{
wait.leave();
wait.address--;
park.Push(wait.leave().licence, wait.leave().time);
cout << "汽车" << car.licence << "停靠在停车场" << park.address << "号位置" << endl;
park.address++;
park.size--;
stime = car.time;
}
break;
}
else
{
park.Pop();
temp.Push(park.Pop().licence, park.Pop().time);
}
if (park.top->next == NULL)
{
cout << "汽车" << car.licence << "不在停车场" << endl;
}
}
}//else if
else
{
cout << "请重新输入A或D" << endl;
}
cin >> status;
}//while
system("pause");
}

C++ 数据结构和算法

Mr.朋友卡 9 years, 8 months ago

Your Answer