#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define FALSE -1
#define TRUE 1
#define MAX_QUEUE_SIZE 200
#define MAX_STACK_SIZE 8
int price = 2; //收费每秒单价
int earn = 0; //当日收入总和
//时间结构体
typedef struct {
int hour;
int min;
int sec;
}Time;
//车结构体且并入时间结构体
typedef struct {
char ID[8];
Time iTime;
Time oTime;
}Car;
//栈结构体
typedef struct {
Car base[MAX_STACK_SIZE];
int top;
int length;
int maxLength;
}Stack;
//列队结构体
typedef struct {
Car base[MAX_QUEUE_SIZE];
int front;
int rear;
int length;
int maxLength;
}DeQueue;
//自动获取车牌结构体
void getID(Car* P){
int num = rand()%10000+10000; //产生车辆的数字部分,且保证位数是五
char locations[25][5] = {"@A","@B","@C","@D","@E","@F","@G","@H",
"@J","@K","@L","@M","@N","@O","@P","@Q","@R","@S",
"@T","@U","@V","@W","@X","@Y","@Z"};
sprintf(P->ID,"%s%d",locations[rand()%25],num); //sprintf函数四个参数分别对应
}
//栈的初始化
Stack *IniSTK() {
Stack *STK = malloc(sizeof (Car)*MAX_STACK_SIZE);
for (int i = 0;i < MAX_STACK_SIZE; i++)
STK->base[i] = *(Car*)malloc(sizeof (Car));
STK-> top = -1;
STK-> maxLength = 8;
return STK;
}
//列队初始化
DeQueue *IniDQ() {
DeQueue *DQ = malloc(sizeof(DeQueue)*MAX_QUEUE_SIZE);
for (int i = 0;i < MAX_QUEUE_SIZE; i++)
DQ->base[i] = *(Car *)malloc(sizeof (Car));
DQ->front = DQ->rear = DQ->length = 0;
DQ->maxLength = 8;
return DQ;
}
//时间填充函数,用于减少代码重复量
struct tm* creatTime() {
time_t now;
struct tm *structTime;
time(&now);
structTime = localtime(&now);
return structTime;
}
//获得进入时间
void getInTime(Car *car) {
struct tm *structTime =creatTime();
car-> iTime.hour = structTime-> tm_hour;
car-> iTime.min = structTime-> tm_min;
car-> iTime.sec = structTime-> tm_sec;
}
//获得离开时间
void getOutTime(Car *car) {
struct tm *structTime = creatTime();
car->oTime.hour = structTime->tm_hour;
car->oTime.min =
如果在使用void关键字时出现了错误,具体情况需要看错误提示的内容。下面列举一些可能的解决方案:
void functionName() {
// 函数体
}
void functionName(void) {
// 函数体
}
void *p;
int *q = (int *)p;
q++; // 可以对q进行指针运算
总结来说,遇到void关键字出现错误时需要检查函数定义、函数参数定义和指针类型转换等问题。根据具体情况进行排查和解决。
malloc返回的指针类型是void,需要强制转换
malloc 那里改成
Stack * STK = (Stack*) malloc (sizeof(Stack));
DeQueue* DQ = (DeQueue*) malloc(sizeof(DeQueue));
修改如下,改动处见注释,供参考:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define FALSE -1
#define TRUE 1
#define MAX_QUEUE_SIZE 200
#define MAX_STACK_SIZE 8
int price = 2; //收费每秒单价
int earn = 0; //当日收入总和
//时间结构体
typedef struct {
int hour;
int min;
int sec;
}Time;
//车结构体且并入时间结构体
typedef struct {
char ID[8];
Time iTime;
Time oTime;
}Car;
//栈结构体
typedef struct {
Car base[MAX_STACK_SIZE];
int top;
int length;
int maxLength;
}Stack;
//列队结构体
typedef struct {
Car base[MAX_QUEUE_SIZE];
int front;
int rear;
int length;
int maxLength;
}DeQueue;
//自动获取车牌结构体
void getID(Car* P) {
int num = rand() % 10000 + 10000; //产生车辆的数字部分,且保证位数是五
char locations[25][5] = { "@A","@B","@C","@D","@E","@F","@G","@H",
"@J","@K","@L","@M","@N","@O","@P","@Q","@R","@S",
"@T","@U","@V","@W","@X","@Y","@Z" };
sprintf(P->ID, "%s%d", locations[rand() % 25], num); //sprintf函数四个参数分别对应
}
//栈的初始化
Stack* IniSTK() {
Stack* STK = (Stack*)malloc(sizeof(Car) * MAX_STACK_SIZE); // 修改
//Stack* STK = malloc(sizeof(Car) * MAX_STACK_SIZE); // 修改
//for (int i = 0; i < MAX_STACK_SIZE; i++) // 修改
//STK->base[i] = *(Car*)malloc(sizeof(Car)); // 修改
memset(STK->base, 0, sizeof(STK->base)); // 修改
STK->top = -1;
STK->maxLength = 8;
return STK;
}
//列队初始化
DeQueue* IniDQ() {
DeQueue* DQ = (DeQueue*)malloc(sizeof(DeQueue) * MAX_QUEUE_SIZE); // 修改
//DeQueue* DQ = malloc(sizeof(DeQueue) * MAX_QUEUE_SIZE); // 修改
//for (int i = 0; i < MAX_QUEUE_SIZE; i++) // 修改
// DQ->base[i] = *(Car*)malloc(sizeof(Car)); // 修改
memset(DQ->base, 0, sizeof(DQ->base)); // 修改
DQ->front = DQ->rear = DQ->length = 0;
DQ->maxLength = 8;
return DQ;
}
//时间填充函数,用于减少代码重复量
struct tm* creatTime() {
time_t now;
struct tm* structTime;
time(&now);
structTime = localtime(&now);
return structTime;
}
//获得进入时间
void getInTime(Car* car) {
struct tm* structTime = creatTime();
car->iTime.hour = structTime->tm_hour;
car->iTime.min = structTime->tm_min;
car->iTime.sec = structTime->tm_sec;
}
//获得离开时间
void getOutTime(Car* car) {
struct tm* structTime = creatTime();
car->oTime.hour = structTime->tm_hour;
car->oTime.min = structTime->tm_min;
car->oTime.sec = structTime->tm_sec;
}