慕衍> 2022-06-08 00:24 采纳率: 87.5%
浏览 39

用的是vs2019,为什么会出现这么多奇奇怪怪的问题以及如何解决啊

img






```c
//Stack.h
#ifndef _STACK_H_
#define _STACK_H_
#include"ParkSystem.h"
typedef struct {
    busType* top;
    busType* base;
}Stack;
#define MAXSIZE 15
void InitStack(Stack* s);
void Pop(Stack* s, busType* e);
void Push(Stack* s, busType* e);
void GetTop(Stack* s,busType* e);
int EmptyStack(Stack s);
#endif

//Stack.c
#include<stdio.h>
#include<stdlib.h>
#include"Stack.h"
void InitStack(Stack* s) {
    s->base = (busType*)malloc(MAXSIZE * sizeof(busType));
    s->top = s->base;
}
void Push(Stack* s, busType* e) {
    if ((s->top - s->base) == MAXSIZE) {
        return;
    }
    *s->top = *e;
    s->top++;
}
void Pop(Stack* s, busType* e) {
    if (s->base == s->top) {
        return;
    }
    s->top--;
    *e = *(s->top);
}
void GetTop(Stack *s,busType *e) {
    s->top--;
    *e = *(s->top);
}
int EmptyStack(Stack s) {
    if (s.base == s.top) {
        return 0;
    }
    return 1;
}

//Queue.h
#ifndef _QUEUE_H_
#define _QUEUE_H_
#include"ParkSystem.h"
typedef struct {
    busType* base;
    int front;
    int rear;
}Queue;
#define MAXSIZE 15
void InitQueue(Queue* Q);
void EnQueue(Queue* Q, busType* e);
void DeQueue(Queue* Q, busType* e);
void Top(Queue* Q, busType* e);
int Empty(Queue Q);
#endif

//Queue.c
#include<stdio.h>
#include<stdlib.h>
#include"Queue.h"
void InitQueue(Queue* Q) {
    Q->base = (busType*)malloc(MAXSIZE * sizeof(busType));
    Q->front = 0;
    Q->rear = 0;
}
void EnQueue(Queue* Q, busType *e) {
    if ((Q->rear + 1) % MAXSIZE == Q->front) {
        return;
    }
    Q->base[Q->rear] = *e;
    Q->rear = (Q->rear + 1) % MAXSIZE;
}
void DeQueue(Queue* Q, busType* e) {
    if (Q->front == Q->rear) {
        return;
    }
    *e = Q->base[Q->front];
    Q->front=(Q->front + 1) % MAXSIZE;
}
void Top(Queue* Q, busType* e) {
    *e = Q->base[Q->front];
}
int Empty(Queue Q) {
    if (Q.front == Q.rear) {
        return 0;
    }
    return 1;
}

//ParkSystem.h
#ifndef _PARKSYSTEM_H_
#define _PARKSYSTEM_H_
#include"Stack.h"
#include"Queue.h"
typedef struct{
    int nType;
    int busNo;
    int occurTime;
    int status;
}busType;
void busArrive(Stack* s, Queue* q, busType* x);
void busLeave(Stack* s, Queue* q, busType x);
#endif

//ParkSystem.c
#include<stdio.h>
#include"ParkSystem.h"
void busArrive(Stack* s, Queue* q, busType* x) {
    if ((s->top - s->base) == 7 && (q->rear - q->front + MAXSIZE) % MAXSIZE == 4) {
        printf("停车场已满!\n");
        return;
    }
    //两种情况,停车场里未满(便道可满可不满),停车场里已满便道未满
    if (s->top - s->base < 7) {
        x->status = 1;
        Push(s, x);
    }
    else {
        x->status = 2;
        EnQueue(q, x);
    }
}
void busLeave(Stack* s, Queue* q, busType x) {
    Stack temp;
    InitStack(&temp);
    busType i, j;
    if (x.status == 1) {
        GetTop(s,&i);
        while (i.busNo != x.busNo) {
            Pop(s, &j);
            Push(&temp, &j);
            GetTop(s, &i);
        }
        Pop(s, &j);
        printf("车号:%d#,到达时间:%d,离开时间:%d,普,收费:%d\n", x.busNo, j.occurTime, x.occurTime, (x.occurTime - j.occurTime) * 7);
        while (EmptyStack(temp)) {
            Pop(&temp, &j);
            Push(s, &j);
        }
    }
    else {
        busType i, j;
        Top(q, &i);
        while (i.busNo != x.busNo) {
            DeQueue(q, &j);
            EnQueue(q, &j);
            Top(q, &i);
        }
        DeQueue(q, &j);
        printf("车号:%d#,到达时间:%d,离开时间:%d,普,收费:%d\n", x.busNo, j.occurTime, x.occurTime, (x.occurTime - j.occurTime) * 5);
    }
}

//main.c
#include<stdio.h>
#include"ParkSystem.h"
int main()
{
    Stack S;
    InitStack(&S);
    Queue Q;
    InitQueue(&Q);
    busType i;
    scanf("%d %d %d ", &i.busNo, &i.nType, &i.occurTime);
    busArrive(&S, &Q, &i);
    scanf("%d %d %d ", &i.busNo, &i.nType, &i.occurTime);
    busLeave(&S, &Q, i);
    return 0;
}



img

  • 写回答

3条回答 默认 最新

  • 关注

    出现这种问题的原因是因为头文件重复包含,即头文件A包含了B,头文件B又包含了A,导致编译器在加载头文件时陷入死循环。

    解决办法:修改头文件包含,如果两个文件代码确实紧密关联,建议写到一个文件中。

    评论

报告相同问题?

问题事件

  • 创建了问题 6月8日