怎么用C语言实现停车场管理系统-创新互联
本篇内容介绍了“怎么用C语言实现停车场管理系统”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
创新互联是一家专业提供横县企业网站建设,专注与成都网站设计、做网站、成都外贸网站建设公司、成都h5网站建设、小程序制作等业务。10年已为横县众多企业、政府机构等服务。创新互联专业网站设计公司优惠进行中。问题描述:停车场是一个能放n辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放。若车场满了,车要停在门外的便道上等候,一旦有车走,则便道上第一辆车进入。当停车场中的车离开时,由于通道窄,在它后面呢的车要先退出,待它走后再依次进入。汽车离开时按停放时间收费。
基本功能要求:
(1)建立三个数据结构分别是:停放队列、让路栈、等候队列。 (2)输入数据模拟管理过程,数据(入或出,车号)。
停车管理系统是C语言中队列和栈比较简单的应用,需要注意的是停车队列、等候队列、让路栈结构体的构建。在写代码时,出队列入栈和出栈入队列时,指针容易出错而造成段错误,应当注意。我所写的代码如下:
//定义结构体
#include#include #define F 0#define T 1#define MAX 3typedef struct Node //数据{ int number; int time;}Node;typedef struct QueueNode //队列结点{ struct Node infom; struct QueueNode * next;}*QueueNode;typedef struct LinkQueue //链队列{ struct QueueNode * front; struct QueueNode * rear;}LinkQueue;typedef struct stack //栈结点{ struct Node data; struct stack *next;}*StackNode;typedef struct LinkStack //链栈{ StackNode top; int count;}LinkStack;
//函数实现
void menu(LinkQueue *wait,LinkQueue *park,LinkStack *giveway,int num,int t);//菜单int init(LinkQueue *wait,LinkQueue *park,LinkStack *giveway);//初始化int linklength(LinkQueue q);//查看长度int enqueue(LinkQueue *q,int num,int t);//入队列int dequeue(LinkQueue *q,int *num,int *t);//出队列void park1(LinkQueue *wait,LinkQueue *park);//停车函数int push(LinkStack *s,int num,int t);//入栈int pop(LinkStack *s,int *num,int *t);//出栈void leave2(LinkQueue *wait,LinkQueue *park,LinkStack *giveway,int num,int t);//离开函数void view3(LinkQueue wait,LinkQueue park);//查看停车场状态int main(){ LinkQueue wait; LinkQueue park; LinkStack giveway; int num = 0; int t = 0; init(&wait,&park,&giveway); menu(&wait,&park,&giveway,num,t); return 0;}int init(LinkQueue *wait,LinkQueue *park,LinkStack *giveway){ QueueNode newnode1 = (QueueNode)malloc(sizeof(struct QueueNode)); if(NULL == newnode1) { return F; } newnode1->next = NULL; wait->front = newnode1; wait->rear = newnode1; QueueNode newnode2 = (QueueNode)malloc(sizeof(struct QueueNode)); if(NULL == newnode2) { return F; } newnode2->next = NULL; park->front = newnode2; park->rear = newnode2; giveway->top = NULL; giveway->count = 0;}void menu(LinkQueue *wait,LinkQueue *park,LinkStack *giveway,int num,int t){ printf("**********Welcome to our Car Parking !**********\n"); printf("********** Please choose function **********\n"); printf("********** 1 : park. **********\n"); printf("********** 2 : leave. **********\n"); printf("********** 3 : view. **********\n"); printf("********** 4 : exit. **********\n"); int option; scanf("%d",&option); switch(option) { case 1:{ park1(wait,park); printf("停车完成!\n"); menu(wait,park,giveway,num,t); break; } case 2:{ leave2(wait,park,giveway,num,t); menu(wait,park,giveway,num,t); break; } case 3:{ view3(*wait,*park); menu(wait,park,giveway,num,t); break; } case 4:{ printf("********** 欢迎再次使用,谢谢! **********\n"); break; } default:{ printf("********** 请输入正确的指令! **********\n"); menu(wait,park,giveway,num,t); break; } }}int linklength(LinkQueue q){ int i = 0; while(q.front != q.rear) { i++; q.front = q.front->next; } return i;}int enqueue(LinkQueue *q,int num,int t){ QueueNode newnode = (QueueNode)malloc(sizeof(struct QueueNode)); if(NULL == newnode) { return F; } newnode->infom.number = num; newnode->infom.time = t; newnode->next = NULL; q->rear->next = newnode; q->rear = newnode; return T;}int dequeue(LinkQueue *q,int *num,int *t){ if(q->front == q->rear) { printf("the queue is empty!\n"); return F; } *num = q->front->next->infom.number; *t = q->front->next->infom.time; QueueNode temp = q->front->next; q->front->next = temp->next; if(temp->next == NULL) { q->rear = q->front; } free(temp); return T;}void park1(LinkQueue *wait,LinkQueue *park){ printf("请输入车号和停车时间\n"); int num,t; scanf("%d,%d",&num,&t); if(linklength(*park) >= MAX) { printf("停车场已满,进入等待区!\n"); enqueue(wait,num,t); } else { enqueue(park,num,t); }}int push(LinkStack *s,int num,int t){ StackNode newnode = (StackNode)malloc(sizeof(struct stack)); if(NULL == newnode) { return F; } newnode->data.number = num; newnode->data.time = t; newnode->next = s->top; s->top = newnode; s->count++; return T;}int pop(LinkStack *s,int *num,int *t){ if(0 == s->count) { printf("the stack is empty !\n"); return F; } *num = s->top->data.number; *t = s->top->data.time; StackNode temp = s->top; s->top = s->top->next; free(temp); s->count--; return T;}void leave2(LinkQueue *wait,LinkQueue *park,LinkStack *giveway,int num,int t){ printf("请输入要离开车的车号\n"); int leavenumber; scanf("%d",&leavenumber); int i = 0; QueueNode head = park->front; while(head != park->rear) { if(head->next->infom.number != leavenumber) { head = head->next; i++; } else break; } int j = 0; if(i <= MAX-1) { while(j != i) { dequeue(park,&num,&t); push(giveway,num,t); j++; } dequeue(park,&num,&t); } else { printf("查无此车!\n"); } while(giveway->top != NULL) { pop(giveway,&num,&t); enqueue(park,num,t); } if(linklength(*wait) != 0) { dequeue(wait,&num,&t); enqueue(park,num,t); }}void view3(LinkQueue wait,LinkQueue park){ printf("******************** 目前停车场状况 ********************\n"); printf("停车场共%d个车位,当前停车场共有%d量车,等待区共有%d量车\n", MAX,linklength(park),linklength(wait)); printf("**************************************************************\n"); printf("车 号:"); QueueNode head1 = park.front; QueueNode head2 = park.front; while(head1 != park.rear) { printf("%d ",head1->next->infom.number); head1 = head1->next; } printf("\n"); printf("停车时间:"); while(head2 != park.rear) { printf("%d ",head2->next->infom.time); head2 = head2->next; } printf("\n");}
“怎么用C语言实现停车场管理系统”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!
当前题目:怎么用C语言实现停车场管理系统-创新互联
转载来于:http://ybzwz.com/article/ccioph.html