停車場管理系統

yongjunli發表於2007-03-17
做完了課程設計,用C語言寫的。不過挑了個容易點的,停車場管理系統。不過寫得好繁瑣啊,好耐無用C語言了。請大家指教,也幫幫急需的朋友們。呵呵···
終於可以放假啦,嘿嘿·····
/*******************************停車場管理器*************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <conio.h>
/********************************************************************************/
#define MAXSTACKSIZE 2 /*車庫容量*/
#define price 0.1 /*每車每分鐘費用*/
typedef struct time{
int hour;
int min;
}Time; /*時間結點*/
typedef struct {
char num[10];
Time reach;
Time leave;
}CarNode; /*車輛資訊結點*/
typedef struct {
CarNode *base;
CarNode *top;
int stacksize;
}SqStackCar; /*模擬車站*/
typedef struct car{
CarNode *data;
struct car *next;
}QueueNode;
typedef struct {
QueueNode *front;
QueueNode *rear;
}LinkQueueCar; /*模擬通道*/
int QueueEmpty(LinkQueueCar Q)     /*便道判空函式*/
{
  if(Q.front==Q.rear) return 1;
  else return 0;
}
/********************************************************************************/
void InitStack(SqStackCar *s) /*初始化棧*/
{
  s->base=(CarNode *)malloc(MAXSTACKSIZE*sizeof(CarNode));
  if(!s->base) exit(0);/*分配失敗*/
  s->top=s->base;
  s->stacksize=MAXSTACKSIZE;
}
int Push(SqStackCar *s,CarNode *e)  /*進站函式*/
{
  if(s->top-s->base>=s->stacksize) return 0;
  else *s->top++=*e;
  return 1;
}
int Pop(SqStackCar *s,CarNode *e)  /*出站函式*/
{
  if(s->top==s->base) return 0;
  *e=*--s->top;
  return 1;
}
int StackEmpty(SqStackCar s)       /*判空函式*/
{
  if(s.base==s.top) return 1;
  else return 0;
}
int InitQueue(LinkQueueCar *Q) /*初始化便道*/
{
  Q->front=Q->rear=(QueueNode *)malloc(sizeof(QueueNode));
  if(!Q->front) exit(0);
  Q->front->next=NULL;
  return 1;
}
/**************************************************************/
int EnQueue(LinkQueueCar *Q,CarNode *e)  /*便道插入函式*/
{
  QueueNode *p;
  p=(QueueNode *)malloc(sizeof(QueueNode));
  if(!p) exit(0);
  p->data=e;
  p->next=NULL;
  Q->rear->next=p;
  Q->rear=p;
  return 1;
}
int DeQueue(LinkQueueCar *Q,CarNode *e)   /*便道刪除函式*/
{
  QueueNode *p;
  if(Q->front==Q->rear) return 0;
  p=Q->front->next;
  e=p->data;
  Q->front->next=p->next;
  if(Q->rear==p) Q->rear=Q->front;
  free(p);
  return 1;
}
/********************************************************************************/
int Arrive(SqStackCar *In,LinkQueueCar *Wait)  /*車輛到達函式*/
{
  CarNode *i;
  QueueNode *w;
  i=(CarNode *)malloc(sizeof(CarNode));
  flushall();
  printf("Input the car number:");
  gets(i->num);
  if(In->top-In->base<MAXSTACKSIZE)  /*車場未滿,車輛進棧*/
  {
    printf("/nThe time the car arrive(00:00): ");
    scanf("%d:%d",&i->reach.hour,&i->reach.min);
    Push(In,i);
    printf("/nCar in success!!");
    sleep(1);
    return 1;
  }
  else   /*停車場已滿,車進便道*/
  {
    w=(QueueNode *)malloc(sizeof(QueueNode));
    w->data=i;
    w->next=NULL;
    Wait->rear->next=w;
    Wait->rear=w;
    printf("The PART is full,car must wait in the road!");
    sleep(1);
    return 1;
  }
  return 0;
}
/********************************************************************************/
int Departure(SqStackCar *In,SqStackCar *temp,LinkQueueCar *Wait)   /*車輛離開函式*/
{
   int flag=0,a1,a2,b1,b2, money;
   CarNode *p,*t;
   QueueNode *q;
   p=(CarNode *)malloc(sizeof(CarNode));
   flushall();
   printf("Input the out car number: ");
   gets(p->num);
   while(!StackEmpty(*In))
   {
   t=(CarNode *)malloc(sizeof(CarNode));
   Pop(In,t);
   if(strcmp(p->num,t->num)==0)   /*比較車場中有無這輛車,有即出站*/
   {
   printf("Input the time the car out(00:00):");
   scanf("%d:%d",&p->leave.hour,&p->leave.min);
   printf("The ");
   printf("%s",p->num);
   printf(" Car out the part!");
   a1= p->leave.hour;
   a2= t->reach.hour;
   b1= p->leave.min;
   b2=  t->reach.min;
   money = ((a1-a2+24)%24*60+(b1-b2+60)%60)*price;    /*計算車輛需要的費用*/
   printf("/nThe time the car arrive: %d:%d",t->reach.hour,t->reach.min);
   printf("/nThe time the car leave: %d:%d",p->leave.hour,p->leave.min);
   printf("/nNeed: %d yuan",money);
   flag=1;
   getch();
   free(t);
   break;
   }
   else
     Push(temp,t);
   } /*while*/
   if(!flag)
     {
     printf("No this car!!");
     getch();
     }
   while(!StackEmpty(*temp))
   {
     Pop(temp,p);
     Push(In,p);
   }
   free(p);
   if(flag&&Wait->front!=Wait->rear)    /*車站中有空位,便道有車,車入站*/
   {
     q=(QueueNode *)malloc(sizeof(QueueNode));
     q=Wait->front->next;
     t=q->data;
     if(q!=NULL)
     {
     Push(In,t);
     printf("/nThe ");
     printf("%s",t->num);
     printf(" car in part!");
     printf("/nInput the time the car arrive(00:00): ");
     scanf("%d:%d",&t->reach.hour,&t->leave.min);
     }
     Wait->front->next=q->next;
     if(q==Wait->rear) Wait->rear=Wait->front;
     free(q);
   }
   return 1;
}
/********************************************************************************/
void Print(SqStackCar *In,SqStackCar *Temp,LinkQueueCar *Wait)    /*列印函式*/
{
  int c=0;
  int count=1;
  CarNode *p,*t;
  QueueNode *q;
  q=(QueueNode *)malloc(sizeof(QueueNode));
  p=(CarNode *)malloc(sizeof(CarNode));
  t=(CarNode *)malloc(sizeof(CarNode));
  while(1&&c!='3')
  {
    clrscr();
    gotoxy(1,10);
    printf("1. Print the road!");
    gotoxy(1,11);
    printf("2. Print the part!");
    gotoxy(1,12);
    printf("3. return.");
    do{
      printf("/nInput your choice:");
      c = getche();
      printf("/n");
    }while(c!='1'&&c!='2'&&c!='3');
if(c=='2')     /*列印停車場*/
{
     printf("The car in the part!/n");
     count=1;
     while(!StackEmpty(*In))
     {
       Pop(In,t);
       Push(Temp,t);
     }
     while(!StackEmpty(*Temp))
     {
       Pop(Temp,t);
       printf("The ");
       printf("%d",count);
       printf(" car number is:  ");
       count++;
       puts(t->num);
       Push(In,t);
     }
     printf("Press any key to continue...");
     getch();
}
if(c=='1')    /*列印便道*/
  {
     printf("The car in the road!/n");
     count=1;
     q=Wait->front->next;
     if(Wait->front!=Wait->rear)  /**/
     {
     while(q!=NULL)
     {
       p=q->data;
       printf("The ");
       printf("%d",count);
       printf(" Car number is:  ");
       puts(p->num);
       q=q->next;
       count++;
     }
     }
     else printf("/nNo car in the road.");
     printf("Press any key to continue...");
     getch();
  }
 }
}
/***************************主程式***********************************************/
int Arrive(SqStackCar *In,LinkQueueCar *Wait);
int Departure(SqStackCar *In,SqStackCar *temp,LinkQueueCar *Wait);
void Print(SqStackCar *In,SqStackCar *temp,LinkQueueCar *Wait);
void initialization();
char readcommand();
void interpret(char cmd);
main()
{
   char cmd;
   SqStackCar part,temp;
   LinkQueueCar road;
   InitStack(&part);
   InitStack(&temp);
   InitQueue(&road);
   printf("Press any key to continue......");
   getch();
   while(1)
     {
   initialization();   /*初始化介面*/
   cmd = readcommand();  /*讀取停車場狀況*/
   clrscr();
   switch(cmd)
  {
    case 'a': Arrive(&part,&road); break;
    case 'A': Arrive(&part,&road); break;
    case 'd': Departure(&part,&temp,&road);   break;
    case 'D': Departure(&part,&temp,&road);   break;
    case 'p': Print(&part,&temp,&road);  break;
    case 'P': Print(&part,&temp,&road);  break;
    case 'e': printf("Press any to continue...");getch();exit(0);             break;
    case 'E': printf("Press any to continue...");getch();exit(0);             break;
    default : printf("ERROR!");            break;
  }
     }
}
/********************************************************************************/
void initialization()   /*初始函式*/
{
     int i;
     clrscr();
     gotoxy(0,0);
     for(i=1;i<=240;i++)
       printf("/1");
     gotoxy(15,8);
       printf("THIS IS A CAR PART MANAGE SYSYTEM!");
     gotoxy(15,12);
       printf("NAME:    LIYONGJUN.");
     gotoxy(15,13);
       printf("NUM:     3104006893.");
     gotoxy(15,14);
       printf("GRADE:   2004.");
     gotoxy(15,15);
       printf("CLASS:   COMPUTER SCIENCE AND TECHNOLOGY 10");
     gotoxy(1,20);
       printf("/n********************************************************************************");
       printf("1. Car Arrive--A  2. Car Departure--D  3. Print Car--P  4.Exit--E");
       printf("/n********************************************************************************");
       printf("Input C,D,P,E choose!!/n");
}
char readcommand()      /*選擇函式*/
{
    char cmd;
  do{
      printf("Input your choice:");
      cmd = getche();
      printf("/n");
    }while((cmd!='a')&&(cmd!='A')&&(cmd!='d')&&(cmd!='D')&&(cmd!='p')&&(cmd!='P')&&(cmd!='E')&&(cmd!='e'));
    return cmd;
}
 

相關文章