这个停车场 使用栈作为停车场,队列作为栈,另有一栈作为临时停车道负责从停车场中暂时退出的车
运行时出现大量错误,例如循环没有进行,不过本人c语言不精 找不到错误 求改正
附: 本人对结构体不是太了解,求高手解答
运行时出现大量错误,例如循环没有进行,不过本人c语言不精 找不到错误 求改正
附: 本人对结构体不是太了解,求高手解答
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <stdlib.h>
#include <time.h>
#define FALSE -1
#define TRUE 1
#define Stack_Size 2
#define Money 2
#define PARK 4
#define SHORCUT 5
typedef struct time
{
int hour;
int min;
}Time;
typedef struct CarNode
{
char CarNumber[10];
Time reach;
Time leave;
}StackElementType, QueueElementType;
typedef struct ZHAN
{
StackElementType *CarNode[Stack_Size];
int top;
}SeqStack;
typedef struct Node
{
CarNode *data;
struct Node *next;
}LinkQueueNode;
typedef struct ZIZE
{
LinkQueueNode *front;
LinkQueueNode *rear;
}LinkQueue;
void Pop(SeqStack *S, StackElementType *x);
void Push(SeqStack *S, CarNode *x);
void EnterQueue(LinkQueue *Q, QueueElementType *x);
void PRINT(CarNode *p);
void InitStack(SeqStack *S)
{
S->top = -1;
}
int InitQueue(LinkQueue *Q)
{
Q->front = (LinkQueueNode *)malloc(sizeof(LinkQueueNode));
if (Q->front != NULL)
{
Q->rear = Q->front;
Q->front->next = NULL;
return(TRUE);
}
else
return(FALSE);
}
int getInTo(SeqStack *S, LinkQueue *Q)
{
if (S->top != Stack_Size - 1)
{
printf("请输入车辆牌照和进入时间\n");
CarNode *p;
p = (CarNode *)malloc(sizeof(CarNode));
scanf_s("%s %d:%d", &p->CarNumber, &p->reach.hour, &p->reach.min);
Push(S, p);
return PARK;
}
else
{
printf("停车场已满,请入便道\n");
printf("请输入车辆牌照和进入时间\n");
LinkQueueNode *t;
t = (LinkQueueNode *)malloc(sizeof(LinkQueueNode));
scanf_s("%s %d:%d", &t->data->CarNumber, &t->data->reach.hour, &t->data->reach.min);
Q->rear->next = t;
Q->rear = t;
return SHORCUT;
}
}
void Push(SeqStack *S, CarNode *x) //入栈
{
S->top++;
S->CarNode[S->top] = x;
}
void Pop(SeqStack *S, StackElementType *x) //出栈
{
x = S->CarNode[S->top];
S->top--;
}
void EnterQueue(LinkQueue *Q, QueueElementType *x)
{
LinkQueueNode * NewNode;
NewNode = (LinkQueueNode *)malloc(sizeof(LinkQueueNode));
if (NewNode != NULL)
{
NewNode->data = x;
NewNode->next = NULL;
Q->rear->next = NewNode;
Q->rear = NewNode;
}
}
void Printf(LinkQueue *Q, SeqStack *S)
{
printf("首先输出停车场信息:\n");
printf("位置\t\t车牌号\t\t\t\t进入时间\n");
SeqStack *s;
s = S;
for (; s->top <= Stack_Size - 1; s->top++)
{
printf("%d\t\t%s\t\t\t\t%d:%d\n", s->top + 1, s->CarNode[s->top]->CarNumber, s->CarNode[s->top]->reach.hour, s->CarNode[s->top]->reach.min);
}
printf("输出便道信息:\n");
LinkQueueNode *q;
q = Q->front->next;
if (Q->front != Q->rear) /*判断通道上能否有车*/
{
printf("\n等待车辆的号码为:");
while (q != NULL)
{
puts(q->data->CarNumber);
q = q->next;
}
}
else printf("\n便道里没有车.\n");
}
void Leave(LinkQueue *Q, SeqStack *Enter, SeqStack *Temp)
{
CarNode *t;
LinkQueueNode *q;
if (Enter->top>0)
{
printf("请输入车在车场的位置/1--%d/:", Enter->top);
int room;
StackElementType x;
scanf_s("%d", &room);
while (1)
{
if (Enter->top>room)
{
Pop(Enter, &x);
Push(Temp, &x);
}
else
break;
}
Pop(Enter, &x);
PRINT(&x);
while (1)
{
if (Temp->top != 0)
{
Pop(Temp, &x);
Push(Enter, &x);
}
else
break;
}
/*判断通道上能否有车及车站能否已满*/
if ((Q->front != Q->rear) && Enter->top <= Stack_Size - 1) /*便道的车辆进入车场*/
{
q = Q->front->next;
t = q->data;
Enter->top++;
printf("\n便道的%s号车进入车场第%d位置.", t->CarNumber, Enter->top + 1);
printf("\n请输入现在的时间/**:**/:");
scanf_s("%d:%d", &(t->reach.hour), &(t->reach.min));
Q->front->next = q->next;
if (q == Q->rear)
Q->rear = Q->front;
Enter->CarNode[Enter->top] = t;
free(q);
}
else
printf("\n便道里没有车.\n");
}
else
printf("车站里没车\n");
}
void PRINT(CarNode *p) /*打印出站车的信息*/
{
int A1, A2, B1, B2;
printf("\n请输入离开的时间:/**:**/");
scanf_s("%d:%d", &(p->leave.hour), &(p->leave.min));
printf("\n离开车辆的车牌号为:");
puts(p->CarNumber);
printf("\n其到达时间为: %d:%d\n", p->reach.hour, p->reach.min);
printf("离开时间为: %d:%d\n", p->leave.hour, p->leave.min);
A1 = p->reach.hour;
A2 = p->reach.min;
B1 = p->leave.hour;
B2 = p->leave.min;
printf("\n应交费用为: %2d元\n", ((B1 - A1) * 60 + (B2 - A2))*Money);
free(p);
}
int main(void)
{
SeqStack Enter, Temp;
LinkQueue Wait;
InitStack(&Enter);
InitStack(&Temp);
InitQueue(&Wait);
int ch;
InitStack(&Enter); /*初始化车站*/
InitStack(&Temp); /*初始化让路的临时栈*/
InitQueue(&Wait); /*初始化通道*/
bool a = true;
while (a)
{
printf(" 1. 车辆到达\n");
printf(" 2. 车辆离开\n");
printf(" 3. 列表显示\n");
printf(" 4. 退出系统\n");
scanf_s("%d", &ch);
switch (ch)
{
case 1:
{
int a = getInTo(&Enter, &Wait);
if (a == PARK)
printf("车已存入停车场\n");
else
printf("车已入便道\n");
}break; /*车辆到达*/
case 2:Leave(&Wait, &Enter, &Temp); break; /*车辆离开*/
case 3:Printf(&Wait, &Enter); break; /*列表打印信息*/
case 4:a = false; break; /*退出主程序*/
default: break;
}
}
return 0;
}
解决方案
40
都是指针的不规范操作造成,例如越界,未初始化,对NULL指针操作等等
10
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。
被幽默会死人吗?
被幽默会死人吗?
