首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

停车场管理代码(帮忙加个注释!)解决方案

2012-04-09 
停车场管理代码(帮忙加个注释!)#include stdio.h#include conio.h#include stdlib.h#define SMAX 2/

停车场管理代码(帮忙加个注释!)
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define SMAX 2 /*车库容量*/
#define QMAX 10 /*每辆车每时刻的费用*/

typedef struct{  
  char ad;
  int num;
  int time;
} ElemType; /*元素类型*/  

typedef struct{
  ElemType *data;
  int top;
} SeqStack; /*顺序栈类型*/

typedef struct{
  ElemType *data;
  int front,rear;
} SeqQueue; /*顺序队类型*/

typedef struct node{
  ElemType data;
  struct node *link;
} NodeQueue; /*链队结点类型*/

typedef struct{
  NodeQueue *front,*rear;
} LinkQueue; /*链队类型*/

void InitStack(SeqStack *S) /*初始化栈*/  
{
  S->data=(ElemType*)malloc(SMAX*sizeof(ElemType));
  S->top=-1;
}

int SEmpty(SeqStack *S)  
{
  if(S->top==-1) return 1;
  else return 0;
}

int SFull(SeqStack *S)  
{
  if(S->top==SMAX-1) return 1;
  else return 0;
}

int Push(SeqStack *S,ElemType e) 
{
  if(S->top==SMAX-1) return 0;
  S->data[++S->top]=e;
  return 1;
}

int Pop(SeqStack *S,ElemType *e)  
{
  if(S->top==-1) return 0;
  *e=S->data[S->top--];
  return 1;
}

void InitQueue(SeqQueue *Q)
{
  Q->data=(ElemType*)malloc(QMAX*sizeof(ElemType));
  Q->front=Q->rear=0;
}

int QEmpty(SeqQueue *Q)
{
  if(Q->front==Q->rear) return 1;
  else return 0;
}

int EnQueue(SeqQueue *Q,ElemType e)
{
  if((Q->rear+1)%QMAX==Q->front) return 0;
  Q->data[Q->rear]=e;
  Q->rear=(Q->rear+1)%QMAX;
  return 1;
}

int DeQueue(SeqQueue *Q,ElemType *e)
{
  if(Q->front==Q->rear) return 0;
  *e=Q->data[Q->front];
  Q->front=(Q->front+1)%QMAX;
  return 1;
}

void InitLQueue(LinkQueue *Q) /*初始化便道*/
{
  Q->front=Q->rear=NULL;
}

int LQEmpty(LinkQueue *Q)
{
  if(Q->front==NULL) return 1;
  return 0;
}

int EnLQueue(LinkQueue *Q,ElemType e)
{
  NodeQueue *p;
  p=(NodeQueue*)malloc(sizeof(NodeQueue));
  if(p==NULL) return 0;
  p->data=e;
  p->link=NULL;
  if(Q->front==NULL)
  {
  Q->front=Q->rear=p;
  }
  else
  {
  Q->rear->link=p;
  Q->rear=p;
  }
  return 1;
}

int DeLQueue(LinkQueue *Q,ElemType *e)
{
  NodeQueue *p;
  if(Q->front==NULL) return 0;
  p=Q->front;
  Q->front=p->link;
  *e=p->data;
  free(p);
  return 1;
}

void main()
{
  SeqStack ST,temp;
  LinkQueue Q;
  NodeQueue *p,*q;
  ElemType x,y;

  InitStack(&ST);InitStack(&temp);
  InitLQueue(&Q);

  scanf("%c %d %d",&x.ad,&x.num,&x.time);
  while(x.ad=='A'||x.ad=='D')
  {
  printf("%c %d %d\n",x.ad,x.num,x.time);
  if(x.ad=='A') 
  {
  if(!SFull(&ST)) 
  {
  Push(&ST,x);
  printf("Arrive in the park:%d\n",ST.top);


  }
  else 
  {
  EnLQueue(&Q,x);
  printf("Arrive in the roadedge:\n");
  }
  }
  else
  {
  while(!SEmpty(&ST)&&ST.data[ST.top].num!=x.num)
  {
  Pop(&ST,&y);
  Push(&temp,y);
  }
  if(!SEmpty(&ST)) 
  {
  Pop(&ST,&y);
  printf("Stop time:%d\n",x.time-y.time);
  while(!SEmpty(&temp))
  {
  Pop(&temp,&y);
  Push(&ST,y);
  }
  }
  else
  {
  if(!LQEmpty(&Q))
  {
  p=Q.front;
  while(p->data.num!=x.num)
  {
  q=p;
  p=p->link;
  }
  if(p==Q.front)
  {
  Q.front=p->link;
  }
  else
  {
  q->link=p->link;
  }
  free(p);
  }
  }
  }
  scanf("%*c%c %d %d",&x.ad,&x.num,&x.time);
  }
}


[解决办法]

C/C++ code
int SEmpty(SeqStack *S)/*判断栈是否为空*/    {  if(S->top==-1) return 1;  else return 0;}int SFull(SeqStack *S)/*判断栈是否已满*/   {  if(S->top==SMAX-1) return 1;  else return 0;}int Push(SeqStack *S,ElemType e) /*进栈*/{  if(S->top==SMAX-1) return 0;  S->data[++S->top]=e;  return 1;}int Pop(SeqStack *S,ElemType *e) /*出栈*/{  if(S->top==-1) return 0;  *e=S->data[S->top--];  return 1;}void InitQueue(SeqQueue *Q)/*初始化队列*/{  Q->data=(ElemType*)malloc(QMAX*sizeof(ElemType));  Q->front=Q->rear=0;}int QEmpty(SeqQueue *Q)/*判断队列是否为空*/  {  if(Q->front==Q->rear) return 1;  else return 0;}int EnQueue(SeqQueue *Q,ElemType e)/*判断队列是否已满*/  {  if((Q->rear+1)%QMAX==Q->front) return 0;  Q->data[Q->rear]=e;  Q->rear=(Q->rear+1)%QMAX;  return 1;}int DeQueue(SeqQueue *Q,ElemType *e)/*删除队列中的元素*/  {  if(Q->front==Q->rear) return 0;  *e=Q->data[Q->front];  Q->front=(Q->front+1)%QMAX;  return 1;}void InitLQueue(LinkQueue *Q) /*初始化便道*/{  Q->front=Q->rear=NULL;}int LQEmpty(LinkQueue *Q)/*判断便道是否为空*/{  if(Q->front==NULL) return 1;  return 0;}int EnLQueue(LinkQueue *Q,ElemType e)/*增加便道中的元素*/{  NodeQueue *p;  p=(NodeQueue*)malloc(sizeof(NodeQueue));  if(p==NULL) return 0;  p->data=e;  p->link=NULL;  if(Q->front==NULL)  {  Q->front=Q->rear=p;  }  else  {  Q->rear->link=p;  Q->rear=p;  }  return 1;}int DeLQueue(LinkQueue *Q,ElemType *e)/*删除便道中的元素*/{  NodeQueue *p;  if(Q->front==NULL) return 0;  p=Q->front;  Q->front=p->link;  *e=p->data;  free(p);  return 1;}
[解决办法]
能看出来干啥就可以了

热点排行