停车场

  1. #include <stdlib.h>  
  2. #include <string.h>  
  3. #include <time.h>  
  4.   
  5. #define MAX 5//停车场的容量  
  6. #define PRICE 2.0//停车收费价格:元/小时  
  7.   
  8. typedef struct car  
  9. {  
  10.     char name[10];//车牌号  
  11.     struct tm intime;//入库时间  
  12.     struct tm outtime;//出库时间  
  13.     int expense;//停车费用  
  14.     int length;//停车时间  
  15.     int position;//车位  
  16. }CAR;//车位信息  
  17. typedef struct stack  
  18. {  
  19.     int top;  
  20.     CAR num[MAX];  
  21. }Stack;//栈  
  22. struct carnode  
  23. {  
  24.     CAR data;  
  25.     struct carnode*next;  
  26. };//过道车的结点  
  27. typedef struct carnode Carnode;  
  28. typedef struct carnode* Link;  
  29. typedef struct queue  
  30. {  
  31.     Carnode*head;  
  32.     Carnode*rear;  
  33. }Queue;//队列  
  34.   
  35. int initstack(Stack **s)//初始化栈  
  36. {  
  37.     (*s) = (Stack*)malloc(sizeof(Stack));  
  38.     if(*s == NULL)  
  39.     {  
  40.         printf("分配内存出错!n");  
  41.         exit(-1);  
  42.     }  
  43.     (*s)->top = -1;  
  44.     return 0;  
  45. }  
  46. int stackisempty(Stack **s)//判断栈是否为空  
  47. {  
  48.     if((*s)->top == -1)  
  49.     {  
  50.         return 1;  
  51.     }  
  52.     else  
  53.     {  
  54.         return 0;  
  55.     }  
  56. }  
  57. int stackisfull(Stack **s)//判断栈是否为满  
  58. {  
  59.     if((*s)->top == MAX-1)  
  60.     {  
  61.         return 1;  
  62.     }  
  63.     else  
  64.     {  
  65.         return 0;  
  66.     }  
  67. }  
  68. int stackpush(Stack **s,CAR car)//入栈  
  69. {  
  70.     if(!stackisfull(s))  
  71.     {  
  72.         (*s)->top++;  
  73.         (*s)->num[(*s)->top] = car;  
  74.     }  
  75. }  
  76. CAR stackpop(Stack **s)//出栈  
  77. {  
  78.     CAR car;  
  79.     if(!stackisempty(s))  
  80.     {  
  81.         car = (*s)->num[(*s)->top];  
  82.         (*s)->top--;  
  83.     }  
  84.     return car;  
  85. }  
  86. int initqueue(Queue **q)//初始化队列  
  87. {  
  88.     (*q) = (Queue*)malloc(sizeof(Queue));  
  89.     if(*q == NULL)  
  90.     {  
  91.         printf("内存分配出错!\n");  
  92.         exit(-1);  
  93.     }  
  94.     (*q)->head = NULL;  
  95.     (*q)->rear = NULL;  
  96.     return 0;  
  97. }  
  98. int queueisempty(Queue **q)//判断队列是否为空  
  99. {  
  100.     if((*q)->head == NULL && (*q)->rear == NULL)  
  101.     {  
  102.         return 1;  
  103.     }  
  104.     else  
  105.     {  
  106.         return 0;  
  107.     }  
  108. }  
  109. int queuepush(Queue **q,CAR car)//入队列  
  110. {  
  111.     Link tmp = NULL;  
  112.     tmp = (Link)malloc(sizeof(Carnode));  
  113.     if(tmp == NULL)  
  114.     {  
  115.         printf("内存分配失败!\n");  
  116.         exit(-1);  
  117.     }  
  118.     tmp->data = car;  
  119.   
  120.     tmp->next = (*q)->head;  
  121.     (*q)->head = tmp;  
  122.       
  123.     return 0;  
  124. }  
  125. CAR queuepop(Queue **q)//出队列  
  126. {  
  127.     CAR car;  
  128.     Link tmp = NULL;  
  129.     Link p = NULL;  
  130.     tmp = (Link)malloc(sizeof(Carnode));  
  131.     if(tmp == NULL)  
  132.     {  
  133.         printf("内存分配失败!\n");  
  134.         exit(-1);  
  135.     }  
  136.     tmp = (*q)->head;  
  137.     if(!queueisempty(q))  
  138.     {  
  139.         if(tmp == (*q)->rear)  
  140.         {  
  141.             car = tmp->data;  
  142.             free(tmp);  
  143.             (*q)->head = NULL;  
  144.             (*q)->rear = NULL;  
  145.             return car;  
  146.         }  
  147.         while(tmp->next != (*q)->rear)  
  148.         {  
  149.             p = tmp;  
  150.             tmp = tmp->next;  
  151.         }  
  152.         if((*q)->rear == NULL)  
  153.         {  
  154.             if(tmp == (*q)->head)  
  155.             {  
  156.                 car = ((*q)->head)->data;  
  157.                 free((*q)->head);  
  158.                 (*q)->head = NULL;  
  159.             }  
  160.             else  
  161.             {  
  162.                 car = tmp->data;  
  163.                 free(tmp);  
  164.                 (*q)->rear = p;  
  165.                 p->next = NULL;  
  166.             }  
  167.         }  
  168.         else  
  169.         {  
  170.             car = ((*q)->rear)->data;  
  171.             free((*q)->rear);  
  172.             (*q)->rear = tmp;  
  173.             tmp->next = NULL;  
  174.         }  
  175.     }  
  176.       
  177.     return car;  
  178. }  
  179. int printfdata(struct tm gm)//显示时间  
  180. {  
  181.     printf("%d/%d %2d:%2d:%2d\n",gm.tm_mon,gm.tm_mday,gm.tm_hour+8,gm.tm_min,gm.tm_sec);  
  182.     return 0;  
  183. }  
  184. int showstack(Stack **s)//查看车库车辆  
  185. {  
  186.     int i;  
  187.     printf("****************************************\n");  
  188.     printf("车位使用情况\n");  
  189.     if(stackisempty(s))  
  190.     {  
  191.         printf("停车场已没有车辆!\n");  
  192.     }  
  193.     else  
  194.     {  
  195.         printf("位置\t车牌号\t进站时间\n");  
  196.         for(i = 0; i <= (*s)->top;i++)  
  197.         {  
  198.             printf("%d\t",(*s)->num[i].position);  
  199.             printf("%s\t",(*s)->num[i].name);  
  200.             printfdata((*s)->num[i].intime);  
  201.         }  
  202.         printf("\t\t\t共%d辆车!",(*s)->top+1);  
  203.         if((*s)->top == MAX-1)  
  204.         {  
  205.             printf("(已满)\n");  
  206.         }  
  207.         else  
  208.         {  
  209.             printf("还可以停放%d辆车\n",MAX - 1 - (*s)->top);  
  210.         }  
  211.     }  
  212.     printf("*******************************************\n");  
  213.     return 0;  
  214. }  
  215. int showqueue(Queue **q)//查看过道车辆  
  216. {  
  217.     printf("****************************************\n");  
  218.     if(!queueisempty(q))  
  219.     {  
  220.         Carnode *p = NULL;  
  221.         p = (Link)malloc(sizeof(Carnode));  
  222.         if(p == NULL)  
  223.         {  
  224.             printf("内存分配出错!\n");  
  225.             exit(-1);  
  226.         }  
  227.         p = (*q)->head;  
  228.         printf("过道使用情况!\n");  
  229.         printf("车牌号\t进入时间\n");  
  230.         while(p != NULL)  
  231.         {  
  232.             printf("%s\t",p->data.name);  
  233.             printfdata(p->data.intime);  
  234.             p = p->next;  
  235.         }  
  236.     }  
  237.     else  
  238.     {  
  239.         printf("过道上没有车在等待\n");  
  240.     }  
  241.     printf("****************************************\n");  
  242.     return 0;  
  243. }  
  244. int showall(Stack **s,Queue **q)//查看整个停车场车辆  
  245. {  
  246.     showstack(s);  
  247.     showqueue(q);  
  248.     menu(s,q);  
  249.     return 0;  
  250. }  
  251. int reach(Stack **s,Queue **q)//入库  
  252. {  
  253.     CAR car;  
  254.     struct tm *gm;  
  255.     time_t lt;  
  256.     time(<);  
  257.     gm = gmtime(<);  
  258.   
  259.     car.intime = *gm;  
  260.     printf("输入车牌号:");  
  261.     scanf("%s",car.name);  
  262.   
  263.     if(!stackisfull(s) && queueisempty(q))//栈未满,入栈  
  264.     {  
  265.         car.position = ((*s)->top) + 2;  
  266.         stackpush(s,car);  
  267.         showstack(s);  
  268.     }  
  269.     else if(stackisfull(s) || !queueisempty(q))//栈满,入队列  
  270.     {  
  271.         printf("提示:车位满,只有先停放在过道上!\n");  
  272.         car.position = MAX;  
  273.         queuepush(q,car);  
  274.         showall(s,q);  
  275.     }  
  276.     menu(s,q);  
  277.     return 0;  
  278. }  
  279. int printfrate(CAR *car)//打印帐单  
  280. {  
  281.     printf("****************************************\n");  
  282.     printf("帐单\n");  
  283.     printf("车牌号:%s\n",car->name);  
  284.     printf("停车位置:%d\n",car->position);  
  285.     printf("进入时间:");  
  286.     printfdata(car->intime);  
  287.     //printf("\n");  
  288.     printf("离开时间:");  
  289.     printfdata(car->outtime);  
  290.     //printf("\n");  
  291.     printf("停车时间(秒):%d\n",car->length);  
  292.     printf("费用(元):%d\n",car->expense);  
  293.     //printf("\n\n");  
  294.     printf("****************************************\n");  
  295.     return 0;  
  296. }  
  297. int leave_s(Stack **s,Queue **q)//离开车库  
  298. {  
  299.     struct tm *gm;  
  300.     time_t lt;  
  301.       
  302.     Stack *p = NULL;  
  303.     initstack(&p);  
  304.     //char nowtime[10];  
  305.     CAR car;  
  306.     int i;  
  307.     int pos;  
  308.   
  309.     if(stackisempty(s))  
  310.     {  
  311.         printf("所有车位是空的,没有车辆需要离开!\n");  
  312.     }  
  313.     else  
  314.     {  
  315.         printf("现在车位使用情况!\n");  
  316.         showstack(s);  
  317.         printf("哪个车位的车辆需要离开:");  
  318.         scanf("%d",&pos);  
  319.         if(pos > 0 && pos <= (*s)->top+1)  
  320.         {  
  321.             for(i = (*s)->top+1; i > pos;i--)  
  322.             {  
  323.                 car = stackpop(s);  
  324.                 car.position = car.position - 1;  
  325.                 stackpush(&p,car);  
  326.             }  
  327.             car = stackpop(s);  
  328.             time(<);  
  329.             gm = gmtime(<);  
  330.             car.outtime = (*gm);//出栈时间  
  331.             car.length = mktime(&car.outtime)-mktime(&car.intime);  
  332.             car.expense = (car.length/3600+1)*PRICE;  
  333.             printfrate(&car);  
  334.             while(!stackisempty(&p))  
  335.             {  
  336.                 car = stackpop(&p);  
  337.                 stackpush(s,car);  
  338.             }  
  339.             while(!stackisfull(s) && !queueisempty(q))  
  340.             {  
  341.                 car = queuepop(q);  
  342.                 time(<);  
  343.                 gm = gmtime(<);  
  344.                 car.intime = (*gm);  
  345.                 stackpush(s,car);  
  346.             }  
  347.         }  
  348.         else  
  349.         {  
  350.             printf("输入车位错误,那个车位没有车!\n");  
  351.         }  
  352.     }  
  353.     menu(s,q);  
  354.     return 0;  
  355. }  
  356. int leave_q(Stack **s,Queue **q)  
  357. {  
  358.     char name[10];  
  359.     Carnode *tmp = NULL;  
  360.     Carnode *p = NULL;  
  361.     CAR car;  
  362.   
  363.     /*tmp = (Link)malloc(sizeof(Carnode)); 
  364.     if(tmp == NULL) 
  365.     { 
  366.         printf("内存分配失败!\n"); 
  367.         exit(-1); 
  368.     }*/  
  369.   
  370.     tmp = (*q)->head;  
  371.   
  372.     if(queueisempty(q))  
  373.     {  
  374.         printf("过道上是空的,没有车辆需要离开!\n");  
  375.     }  
  376.     else  
  377.     {  
  378.         showqueue(q);  
  379.         printf("过道上哪个车需要离开,输入车牌号:");  
  380.         scanf("%s",name);  
  381.         while(tmp != NULL)  
  382.         {  
  383.             if(strcmp(name,(tmp->data).name) == 0)  
  384.             {  
  385.                 break;  
  386.             }  
  387.             p = tmp;  
  388.             tmp = tmp->next;  
  389.         }  
  390.         if(tmp == NULL)  
  391.         {  
  392.             printf("过道没有这辆车!\n");  
  393.         }  
  394.         else  
  395.         {  
  396.             if((*q)->rear == NULL)  
  397.             {  
  398.                 if(tmp->next == NULL)  
  399.                 {  
  400.                     if(tmp == (*q)->head)  
  401.                     {  
  402.                         car = tmp->data;  
  403.                         free(tmp);  
  404.                         (*q)->head = NULL;  
  405.                     }  
  406.                     else  
  407.                     {  
  408.                         car = tmp->data;  
  409.                         p->next = NULL;  
  410.                     }  
  411.                 }  
  412.                 else  
  413.                 {  
  414.                     if(tmp == (*q)->head)  
  415.                     {  
  416.                         car = tmp->data;  
  417.                         (*q)->head = tmp->next;  
  418.                         free(tmp);  
  419.                     }  
  420.                     else  
  421.                     {  
  422.                         car = tmp->data;  
  423.                         p->next = tmp->next;  
  424.                         free(tmp);  
  425.                     }  
  426.                 }  
  427.             }  
  428.             else  
  429.             {  
  430.                 if(tmp == (*q)->head)  
  431.                 {  
  432.                     if(tmp == (*q)->rear)  
  433.                     {  
  434.                         car = tmp->data;  
  435.                         free(tmp);  
  436.                         (*q)->head = NULL;  
  437.                         (*q)->rear = NULL;  
  438.                     }  
  439.                     else  
  440.                     {  
  441.                         car = tmp->data;  
  442.                         (*q)->head = tmp->next;  
  443.                         free(tmp);  
  444.                     }  
  445.                 }  
  446.                 else  
  447.                 {  
  448.                     if(tmp == (*q)->rear)  
  449.                     {  
  450.                         car = tmp->data;  
  451.                         (*q)->rear = p;  
  452.                         p->next = NULL;  
  453.                         free(tmp);  
  454.                     }  
  455.                     else  
  456.                     {  
  457.                         car = tmp->data;  
  458.                         p->next = tmp->next;  
  459.                         free(tmp);  
  460.                     }  
  461.                 }  
  462.             }  
  463.             printf("************************\n");  
  464.             printf("车牌号:%s\n",car.name);  
  465.             printf("进入过道时间:");  
  466.             printfdata(car.intime);  
  467.             printf("不收费,欢迎再次使用!\n");  
  468.             printf("************************\n");  
  469.         }  
  470.     }  
  471.     return 0;  
  472. }  
  473. int leave(Stack **s,Queue **q)  
  474. {  
  475.     char ch;  
  476.   
  477.     while(1)  
  478.     {  
  479.         printf("输入你的车位置(1)车库(2)过道:");  
  480.         scanf(" %c",&ch);  
  481.         if(ch == '1' || ch == '2')  
  482.         {  
  483.             break;  
  484.         }  
  485.         else  
  486.         {  
  487.             printf("输入序号有误!\n");  
  488.         }  
  489.     }  
  490.     switch(ch)  
  491.     {  
  492.         case '1':leave_s(s,q);  
  493.                  break;  
  494.         case '2':leave_q(s,q);  
  495.                  break;  
  496.     }  
  497.     menu(s,q);  
  498.     return 0;  
  499. }  
  500. int quit(Stack **s,Queue **q)//退出系统  
  501. {  
  502.     printf("欢迎你的下次使用!\n");  
  503.     exit(0);  
  504.     return 0;  
  505. }  
  506. int menu(Stack **s,Queue **q)//菜单     
  507. {  
  508.     char ch;  
  509.   
  510.     printf("******欢迎使用停车场系统******\n");  
  511.     printf("        (1)驶入停车场         \n");  
  512.     printf("        (2)离开停车场         \n");  
  513.     printf("        (3)查看停车场信息     \n");  
  514.     printf("        (4)退出系统           \n");  
  515.     printf("提示:本停车场共%d个车位,停满后的车辆停在过道上!\n",MAX);  
  516.     printf("收费标准,停在停车场的车辆:%.2f元/小时,停在过道上的车辆不收费!\n",PRICE);  
  517.     while(1)  
  518.     {  
  519.         printf("输入你的选择(1-4):");  
  520.         scanf(" %c",&ch);  
  521.         if(ch >= '1' && ch <= '4')  
  522.         {  
  523.             break;  
  524.         }  
  525.         else  
  526.         {  
  527.             printf("输入序号有误!\n");  
  528.         }  
  529.     }  
  530.     switch(ch)  
  531.     {  
  532.         case '1': reach(s,q);  
  533.                   break;  
  534.         case '2': leave(s,q);  
  535.                   break;  
  536.         case '3': showall(s,q);  
  537.                   break;  
  538.         case '4': quit(s,q);  
  539.                   break;  
  540.     }  
  541.     return 0;  
  542. }  
  543. int main()  
  544. {  
  545.     Stack *s = NULL;  
  546.     initstack(&s);  
  547.     Queue *q = NULL;  
  548.     initqueue(&q);  
  549.     menu(&s,&q);  
  550.     return 0;  
  551. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值