面向对象程序设计

一、实验目的

(1)要求学生能够熟练掌握C++语言的基本知识和技能。 

2)基本掌握面向对象程序设计的基本思路和方法。 

3)能够利用所学的基本知识和技能,解决简单的面向对象程序设计问题设计

二、实验要求

     (1)  以栈模拟停车场,以队列模拟车场外的便道,按照从终端读入的输入数据序列进行模拟管理

     (2)每一组输入数据包括三个数据项:汽车“到达”或“离去”信息、汽车牌照号码及到达或离去的时刻

     (3)对每一组输入数据进行操作后的输出数据为:若是车辆到达,则输出汽车在停车场内或便道上的停车位置;若是车离去

    (4)则输出汽车在停车场内停留的时间和应交纳的费用

三、主要代码

#includ<stdio.h>

void main(){

InitStack(S);

    InitQueue(Q);

printf("                                                               ");

printf("                                                               ");

printf("                    欢迎进入停车场系统 ");

printf("            ********************************************** ");

printf("                   1  新车到来     ");

printf("                   2  车离开       ");

printf("                   3  查看车场信息   ");

printf("                   4  查看便道信息   ");

printf("                   #  退出         ");

printf("                计科高职13-3:齐工合伙人         ");

printf("            ********************************************** ");

char cc;

cc=' ';

while(cc!='#'){

printf("请选择操作: ");

 cc=getchar();

        getchar();

switch (cc){

 case'1': Car_Come(S,Q);break;

     case'2': Car_leave(S,Q);break;

 case'3': seeStack(S);break;

 case'4': seeQueue(Q);break; 

 case'#': break;

        default:{printf("选择不正确!请重新选择! ");

 continue;

 }

}

}

}

void seeStack(SqStack S){

if(StackEmpty(S)==-1){

printf("站内没有车!  ");

return;

}

else{

while(StackEmpty(S)!=-1){

      Pop(S,tempCar);

  printf("%d 位置:%-10s  进站时间:%d ",S.top,tempCar.carLicense,tempCar.time);

}

}

printf("-------------------------------------------  ");

}

void seeQueue(LinkQueue Q){

   if(QueueEmpty(Q)==-1){

printf("便道内没有车!  ");

return;

}

else{

       InitQueue(tempQ);

printf("便道内车辆:  ");

while(QueueEmpty(Q)!=-1){

       DeQueue(Q,tempCar);

   printf(" %s  ",tempCar.carLicense);

EnQueue(tempQ,tempCar.carLicense);

}

//重新进队列

while(QueueEmpty(tempQ)!=-1){

       DeQueue(tempQ,tempCar);

   EnQueue(Q,tempCar.carLicense);

}

}

   printf("-------------------------------------------  ");

void Car_Come(SqStack &S,LinkQueue &Q){

printf("新车到来: ");

printf("输入车牌号:");

scanf("%s",&tempCar.carLicense);

if(StackFull(S)==-1){

EnQueue(Q,tempCar.carLicense);

printf("此车辆已进入便道! ");

}

else{

  printf("输入车进入停车场时间:");

  scanf("%d",&tempCar.time);

  getchar();

  Push(S,tempCar.carLicense,tempCar.time);

  printf("在停车场中的位置: %d ",(S.top-1));

}

printf("----------------------------");

}

void Car_leave(SqStack &S,LinkQueue &Q)

{

    int d,come_time=0;

char car_license[15];

    printf("车辆离开车站:");

printf("请输入要离开车辆的位置:");

scanf("%d",&d);

if(StackEmpty(S)==-1){

return;

}

else{

if(d<S.top){

InitStack(tempS);

 while(S.top!=(d+1)){

     Pop(S,tempCar);

 Push(tempS,tempCar.carLicense,tempCar.time);

   }

       Pop(S,tempCar);

   come_time=tempCar.time;

   printf("输入车离开时间:");

   scanf("%d",&tempCar.time);

   printf("%s 已经离开! ",tempCar.carLicense);

   printf("离开时间:%d ",tempCar.time);

       printf("费用:%d  ",unit_price*(tempCar.time-come_time)); 

   //重新进栈

        while(StackEmpty(tempS)!=-1){

     Pop(tempS,tempCar);

 Push(S, tempCar.carLicense,tempCar.time);         

   }

if(QueueEmpty(Q)==-1){

return;

    }

else{

      DeQueue(Q,tempCar);

  printf("%s 进站 ",tempCar.carLicense);

      printf("输入车进站时间:");

      scanf("%d",&tempCar.time);

  Push(S,tempCar.carLicense,tempCar.time);

  printf("在停车场中的位置: %d ",(S.top-1));

}

}

printf("----------------------------");

}

void InitStack(SqStack &S){

S.elem=(CarType *)malloc((N+1)*sizeof(CarType));

if(S.elem==NULL)

return;

S.top=0;

}

int StackEmpty(SqStack S){

if(S.top==0)

return -1;

}

int StackFull(SqStack S){

 

if(S.top==N)

return -1;

}

void GetTop(SqStack S,CarType &e){

if(S.top==0)

return;

e=S.elem[S.top-1];

}

void Push(SqStack &S,char *ch,int time){ 

if(S.top<N+1){

strcpy(S.elem[S.top].carLicense,ch);

S.elem[S.top].time=time; 

    S.top++;

}

}

void Pop(SqStack &S,CarType &eCar){

if(S.top==0)

return;

--S.top;

strcpy(eCar.carLicense,S.elem[S.top].carLicense);

eCar.time=S.elem[S.top].time;

}

void InitQueue(LinkQueue &Q){

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

if(Q.front==NULL)

return;

Q.front->next=NULL;

}li

int QueueEmpty(LinkQueue Q){

if(Q.front==Q.rear)

return -1;

}

void EnQueue(LinkQueue &Q,char *ch){

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

if(p==NULL)

return;

strcpy(p->data.carLicense,ch);

p->next=NULL;

Q.rear->next=p;

Q.rear=p;

}

void DeQueue(LinkQueue &Q,CarType &eCar){

if(Q.front==Q.rear)

return;

p=Q.front->next;

strcpy(eCar.carLicense,p->data.carLicense);

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

if(Q.rear==p)

Q.rear=Q.front; 

free(p);

}

四、实验总结

      通过这次课程设计对程序知识进行了一次全面的综合训练,加深了对程序设计的基本概念和基本知识的理解,同时也锻炼了自己分析问题解决问题的能力。

原文地址:https://www.cnblogs.com/zhangrui128/p/4575813.html