您的位置:首页 > 理论基础 > 数据结构算法

数据结构——停车场代码

2012-08-29 12:00 274 查看
初学,写的有点困难,改了一遍又一遍,终于改好了,VC2010编译通过~~代码没有优化过。

#include <iostream>

#include <stdio.h>

using namespace std;

const int STACK_INIT_SIZE = 2;

const int TRUE = 1;

const int FALSE = 0;

typedef struct Car //定义汽车类型

{

int num;

int time;

}Car;

typedef struct SqStack{

Car *base;

Car *top;

}SqStack;

void InitStack(SqStack &S)

{

S.base = (Car *)malloc(STACK_INIT_SIZE * sizeof(Car));

S.top = S.base;

}

int IsEmpty(SqStack &S)

{

return(S.top == S.base?TRUE:FALSE);

}

int IsFull(SqStack &S)

{

return(S.top - S.base >= STACK_INIT_SIZE?TRUE:FALSE);

}

void Push(SqStack &S,int n,int t)

{

S.top->num = n;

S.top->time = t;

S.top++;

}

void Pop(SqStack &S,Car &car)

{

S.top--;

car = * S.top;

}

typedef struct QNode {

Car data;

struct QNode *next;

}QNode;

typedef struct LinkQueue {

QNode *front;

QNode *rear;

}LinkQueue;

void InitQueue(LinkQueue &Q)

{

Q.front = Q.rear = (QNode *)malloc(sizeof(QNode));

if(!Q.front) exit (-1);

Q.front->next = NULL;

}

void EnQueue(LinkQueue &Q,int n,int t)

{

QNode *p = (QNode *)malloc(sizeof(QNode));

if(!p) exit (-1);

p->data.num = n;

p->data.time = t;

p->next = NULL;

Q.rear->next = p;

Q.rear = p;

}

void DeQueue(LinkQueue &Q,Car &car)

{

QNode *p = (QNode *)malloc(sizeof(QNode));

if (Q.front != Q.rear)

{

p=Q.front->next;

car = p->data;

Q.front->next=p->next;

if(Q.rear==p) Q.rear=Q.front;

free(p);

}

}

int IsInStack(SqStack &S,int n)

{

Car *p=S.top;

while(p!=S.base)

{

p--;

if(p->num == n) break;

}

if(p->num == n) return 1;

else return 0;

}

int main()

{

SqStack S,R;

LinkQueue Q;

InitStack(S);

InitStack(R);

InitQueue(Q);

int j=0,k=0; // 停车场计数器,便道计数器

int time;

while(1)

{

cout << "请输入车辆信息:";

char State;

int n,t;

cin >> State >> n >> t;

if(State == 'A') //车辆进入

{

if(IsFull(S)) // 先判断车库是否满了

{

EnQueue(Q,n,t); // 放入便道里

k++; // 便道里的位置计数器

cout << "在便道中位置:" << k << endl;

}

else

{

Push(S,n,t); // 放入车库中

j++; // 车库的位置计数器

cout << "在车库中位置:" << j << endl;

}

}

else if(State == 'D') //车辆离开

{

if(IsEmpty(S))

{

cout << "车库为空!" << endl;

continue;

}

if(IsInStack(S,n)) //是否在车库中

{

Car *f=S.top;

f--;

while(n != f->num) f--;

if(t <= f->time)

{

cout << "输入时间错误!" << endl;

continue;

}

Car car;

Car *p=S.top;

p--; //辅助查找指针

while(n != p->num) //查找要出去的车辆

{

Car car1;

Pop(S,car1);

Push(R,car1.num,car1.time);

p--;

}

Pop(S,car);

j--;

time=t-car.time;

cout << n << "号车停了" << time << "分钟,需要收费" << endl;

while(!IsEmpty(R))

{

Car car2;

Pop(R,car2);

Push(S,car2.num,car2.time);

}

if(Q.rear!=Q.front)

{

Car car1;

DeQueue(Q,car1);

Push(S,car1.num,t);

j++;

k--;

}

}

else //在便道中

{

QNode *r=Q.front;

while(r->next->data.num != n)

{

r=r->next;

}

if(t <= r->next->data.time)

{

cout << "输入时间错误!" << endl;

continue;

}

Car car;

int a=Q.front->next->data.num;

r=r->next;

while(Q.front->next!=r)

{

DeQueue(Q,car);

EnQueue(Q,car.num,car.time);

}

DeQueue(Q,car);

k--;

time = t - car.time;

cout << n << "号车停了" << time << "分钟,不需要收费" << endl;

while(Q.front != Q.rear &&Q.front->next->data.num !=a && n!=a)

{

DeQueue(Q,car);

EnQueue(Q,car.num,car.time);

}

}

}

else break;

}

system("pause");

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: