关于停车场管理的问题

为什么我的S.base->ID 变了呢?

 

#include<iostream>
#include<string>
#define STACK_INIT_SIZE 10  //存储空间初始分配量
#define STACKINCREMENT 100  //存储空间分配增量
using namespace std;

struct Car{
    string ID;
    string a_time; //到达时间
    string p_time;//停车时间
    string l_time;//离开时间 /取车时间
};
typedef struct {
    Car *base;
    Car *top;
    int stacksize;
}Stack;

typedef struct{          //栈模拟停车场
        Car *base;
        Car *top;
        int stacksize;
}SqStack;

typedef struct Node{  //队列模拟便道
      Car car;
      struct Node *next;
}  QNode,*QueuePtr;

typedef struct{
      QueuePtr front;
      QueuePtr rear;
} LinkQueue;

//*****************************************************************
void InitStack(SqStack &S) {
     S.base = (Car *) malloc(STACK_INIT_SIZE * sizeof(Car));
  if(!S.base)
   exit(0);
  S.top = S.base;
  S.stacksize = STACK_INIT_SIZE;

}
void InitStack(Stack &S) {
     S.base = (Car *) malloc(STACK_INIT_SIZE * sizeof(Car));
  if(!S.base)
   exit(0);
  S.top = S.base;
  S.stacksize = STACK_INIT_SIZE;

}

/*int GetTop(SqStack &S, Car &ch) {   //获取栈顶元素
 if(S.top == S.base) return 0;
// memcpy(S.top, &ch, sizeof(ch));
 memcpy(&ch, (S.top -1), sizeof(Car));
// ch = *(S.top – 1);
 return 1;
}  */
//*******************       错误
void Push(SqStack &S, Car ch) { // 元素入栈
 if(S.top – S.base >= S.stacksize) {
  S.base = (Car*)realloc(S.base,(S.stacksize + (STACKINCREMENT * sizeof(Car))));
  if(!S.base) exit(0);
  S.top = S.base + S.stacksize;
  S.stacksize += STACKINCREMENT;
 }
 memcpy(S.top ,&ch, sizeof(Car));
    S.top ++; 
 cout << S.base->ID << endl;
}  
//******************       
void Push_1(Stack &S, Car ch) {
 if(S.top – S.base >= S.stacksize) {
  S.base = (Car*)realloc(S.base,(S.stacksize + (STACKINCREMENT * sizeof(Car))));
  if(!S.base) exit(0);
  S.top = S.base + S.stacksize;
  S.stacksize += STACKINCREMENT;
 }
 
 memcpy(&ch, S.top, sizeof(Car)); 
 S.top ++;
}
int Pop(SqStack &S, Car &ch) {     
 if(S.top == S.base ) {
  return 0;
    }
 memcpy(&ch,(S.top-1) , sizeof(Car));
 S.top –;
 return 1;
}
int Pop_1(Stack &S, Car ch) { //取栈顶元素
 if(S.top == S.base ) {
  return 0;
 }
 –S.top;
 memcpy(&ch, S.top, sizeof(Car));
// ch = * –S.top;
 return 1;
}
bool IsEmpty(SqStack &S) {         //判断栈是否为空
 if(S.top == S.base)
 return true;               
 else return false;
}

bool IsOver(SqStack &S) {             //判断是否栈满
     if(S.top – S.base == S.stacksize)
     return true;
     else return false;
}
//***************************************************************************
//初始化队列
int InitQueue(LinkQueue &Q){/*进行初始化*/
   // 构造一个空队列Q
   Q.front = Q.rear =(QueuePtr)malloc(sizeof(QNode));
   if (!Q.front) exit (0);                  
                                            //存储分配失败
   Q.front->next = NULL;
   return 1;
}

int QueueEmpty(LinkQueue &Q){/*判断LinkQueue Q是否为空*/
 if(Q.front == Q.rear) return 1;
 else return 0;
}

int QueueLength(LinkQueue Q){/*LinkQueue Q的长度的大小*/
     return ((Q.rear)-(Q.front));
}

int DeQueue (LinkQueue &Q, Car &e) {// 若队列不空,则删除Q的队头元素
   if (Q.front == Q.rear)    return 0;
   QueuePtr p = Q.front->next;   e = p->car;
   Q.front->next = p->next;
   if (Q.rear == p)  Q.rear = Q.front;
   free (p);      return 1;
}

int EnQueue(LinkQueue &Q,Car &e){/*插入数据 e到LinkQueue Q中*/ // 插入元素e为Q的新的队尾元素
 
    QueuePtr p = (QueuePtr) malloc (sizeof (QNode));
    if (!p)  exit (0);   //存储分配失败
    p->car = e;  
    p->next = NULL;
    Q.rear->next = p;   
    Q.rear = p;
    return 1;

}
//**********************************************************************************

void output_Stack(SqStack &S) {
     Car ch;
     Car *Sq;
     Sq = S.top;
     cout << “停车场的车辆有:” << endl;
     while(S.base != Sq ) {
         memcpy(&ch, (Sq -1), sizeof(Car));
         cout <<“/t车牌号为:”<<  ch.ID << ” 的车辆,停入时间为:” << ch.p_time <<endl;
         Sq –;
     }
}
void parking(SqStack &S,LinkQueue &Q, Car &car) {  //停车的函数
     if( !IsOver(S)) {
         car.p_time = car.a_time;
         Push(S, car);
        
     }
     else
   EnQueue(Q, car);
}

void Test_parking(SqStack &S, LinkQueue &Q) {  //测试parking函数
     Car car;
     cout << “Please enter the car’s ID, ID = ” ;  cin >> car.ID;
     cout <<“please enter the arrive time, time = “; cin >> car.a_time;
     parking(S, Q, car);
  //  output_Stack(S);   
}
//*********************************************************************************
void getCar(SqStack &S, Stack &St, LinkQueue &Q, string ID) { //取车的函数
     Car ch;
     Car e;
    
     while(S.base != S.top) {
                 
         if((*S.base).ID != ID){
            Pop(S, ch);
            Push_1(St, ch);
            S.top –;
         }
        
         else{
              Pop(S, ch);
              cout << “please enter the leave time,time = “; cin >> ch.l_time;
              cout << “车牌号 ” << ch.ID << ” 的车辆, 停车时间为 ” << ch.p_time << ” 取车时间为 ” << ch.l_time << endl;  
              while(St.base != St.top) {
                    Pop_1(St, e);
                    Push(S, e); 
                    St.top –;   
              }
              break;
         }
     }
     if(!IsOver(S) && !QueueEmpty(Q)){
                   
        Car e1;
        DeQueue(Q, e1);
        string time;
        cin >> time;
        parking(S,Q, e1);    
              
     }
}
void Test_getCar(SqStack &S,  LinkQueue &Q, Stack &St) { //测试取车的函数
    
     string ID;
     cout << “please enter the ID ID = “; cin >> ID;
    
  //   string time;
 //    cout << “please enter the time, time = ” ; cin >> time;
    
     getCar(S, St,Q, ID);
//     output_Stack(S);
}
//***************************************************************
int main() {
    SqStack S;
    LinkQueue Q;
    Stack St;
   
    InitStack(S);
    InitQueue(Q);
    InitStack(St);
   
    while(1) {
        cout << “Please select……..” << endl;
        cout << “/tParking………….1” << endl;
        cout << “/tgetCar…………2” << endl;
        int n;
        cin >> n;
        if(n == 1)
             Test_parking(S,Q);
        if(n == 2)
             Test_getCar(S,Q,St);
    }
    system(“pause”);
    return 0;
}

    原文作者:停车场模拟问题
    原文地址: https://blog.csdn.net/bnswdnrz/article/details/5970390
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞