C语言
主页 > 软件编程 > C语言 >

C语言实现简易停车场管理系统的代码

2022-03-15 | 秩名 | 点击:

问题描述:

设停车场内只有一个可停放n辆汽车的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按车辆到达时间的先后顺序,依次由北向南排列(大门在最南端,最先到达的第一辆车停放在车场的最北端),若车场内已停满n辆汽车,则后来的汽车只能在门外的便道上等候,一旦有车开走,则排在便道上的第一辆车即可开入;当停车场内某辆车要离开时,在它之后开入的车辆必须先退出车场为它让路,待该辆车开出大门外,其它车辆再按原次序进入车场,每辆停放在车场的车在它离开停车场时必须按它停留的时间长短交纳费用。

由题得,此系统要实现的功能为:

 (1)设计停车场内的结构。(由题分析为一个栈,因为只有一个门所以其就像数据结构中学到的栈,但是因为既要出栈又要入栈,此处我就将这个栈简化为了一个链表)

(2)当车库满时,在车库外等待的结构。(就像排队一样,先来先进后来后进,只能从一端进,另一端出)。

(3)车辆的结构,一辆车要有什么信息?首先要有这辆车的车牌号,可以用一个字符数组来存储(因为车牌不一定全是数字,还可能有汉字,英文字母等),题中要求要用要计算收费,就要知道驶入时间和驶出时间(怎样获取在下面会说到)。

算法描述:

1、刚开始定义结构类型,如车的类型,车库里的类型,车库外等待的类型。

2、声明所要用到的函数:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

 void menu(Stack *cheku,SequenQueue* paidui);//开始菜单

  

//就是展示出菜单的函数

//队列的相关操作

  

SequenQueue* InitQueue();//申请一个空队

int EmptyQueue(SequenQueue* Q);//判断队空

int FullQueue(SequenQueue* Q);//判断队满

int EnQueue(SequenQueue* Q, ElemType *e);//入队

int DeQueue(SequenQueue* Q, ElemType *e); //出队

Stack* build();//建链表

int fullstack(Stack *cheku);//判断链表满

void tingche(Stack *cheku,SequenQueue* paidui);//停车的函数

void likai(Stack *cheku,SequenQueue* paidui);//离开的函数

void chakan(Stack *cheku,SequenQueue* paidui);//查看车库停车情况的函数 

3、一些可能不理解的说明的说明。

(1). 获取时间的函数,写头文件 #include <time.h>,定义一个 time_t类型的变量starttime,starttime=time(NULL);就是获取1970年1月1日到当前的秒数,定义一个字符数组 tmp2[100],strftime(tmp2,sizeof(tmp2),"%Y-%m-%d  %H:%M:%S",localtime(&q->a.starttime));用这一句就能把当前时间的年月日时分秒存进字符数组中,puts(tmp2),就得到了当前的时间。

(2). 代码中用到了Sleep函数,这个函数的功能为延时,要用到一个头文件,#include <time.h>,此函数是与(3)中的配合使用。

(3).(2)中提到的就是system("cls"),他的作用就是清屏,(2)的作用就是把结果让用户看到。故两者配合,就能得到奇效。

程序代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <windows.h>

#include <string.h>

#define TRUE 1

#define FALSE 0

#define MAXSIZE 1024

static int maxsize;

typedef char ElemType;

typedef struct

{

    char  num[10];//车牌

    time_t starttime,endtime;//进入推出时间 

}car;   //车 

typedef struct Stack

{

    int top;

    car a;

    struct Stack *next;

     

}Stack;

typedef struct

{

    char a[10];

}dat;

typedef struct 

{//队列结构定义

   dat      data[MAXSIZE];

   int      front;

   int      rear;

 }SequenQueue;

void menu(Stack *cheku,SequenQueue* paidui);//开始菜单  

SequenQueue* InitQueue();//申请一个空队 

int EmptyQueue(SequenQueue* Q);//判断队空 

int FullQueue(SequenQueue* Q);//判断队满 

int EnQueue(SequenQueue* Q, ElemType *e);//入队 

int DeQueue(SequenQueue* Q, ElemType *e); //出队

Stack* build();//建链表 

void tingche(Stack *cheku,SequenQueue* paidui);

int fullstack(Stack *cheku);

void likai(Stack *cheku,SequenQueue* paidui);

void chakan(Stack *cheku,SequenQueue* paidui);

 int main()

{

    Stack *cheku=build();

    SequenQueue* paidui=InitQueue();

    printf("请输入车库最大容量:");

    scanf("%d",&maxsize); 

    system("cls");

    menu(cheku,paidui);

    return 0;

}

void menu(Stack *cheku,SequenQueue* paidui)

{

    printf("**********        欢迎来停车 !        **********\n");

    printf("**********        请选择一项          **********\n");

    printf("**********         1 : park.          **********\n");

    printf("**********         2 : leave.         **********\n");

    printf("**********         3 : view.          **********\n");

    printf("**********         4 : exit.          **********\n");

    int option;

    scanf("%d",&option);

    system("cls");

    switch(option)

    {

        case 1:

        {

                tingche(cheku,paidui);

                menu(cheku,paidui);

                break;

        }

        case 2:

        {

                likai(cheku,paidui);

                menu(cheku,paidui);

                break;

        }

        case 3:

        {

            chakan(cheku, paidui);

           menu(cheku,paidui);

            break;

        }

        case 4:

        {

            printf("**********     欢迎再次使用,谢谢!   **********\n");

            break;

        }

        default:{

            printf("**********      请输入正确的指令!    **********\n");

            Sleep(1000);

            menu(cheku,paidui);

            system("cls");

            break;

        }

    }

}

int fullstack(Stack *cheku)

{

    if(cheku->top<maxsize-1)

        return 1;

    else

        return 0;

}

SequenQueue* InitQueue()

{

    SequenQueue* Q = NULL;

    Q = (SequenQueue*)malloc(sizeof(SequenQueue));

    Q->front = Q->rear = 0;

    return Q;

}

int DeQueue(SequenQueue* Q, ElemType *e)

{

    if(EmptyQueue(Q))

        return FALSE;

    else

    {

        strcpy(e,Q->data[Q->front].a);

        Q->front=(Q->front+1)%MAXSIZE;

        return TRUE;

    } 

}

int EnQueue(SequenQueue* Q, ElemType *e)

{

    if(FullQueue(Q))

        {

            printf("等待的车辆太多,请下次再来");

            return FALSE;

        }

    strcpy(Q->data[Q->front].a,e);

    Q->rear = (Q->rear+1)%MAXSIZE;

    return TRUE;

}

int FullQueue(SequenQueue* Q)

{

    if((Q->rear+1)%MAXSIZE==Q->front)

    {

        return TRUE;

    }

    else

    {

        return FALSE;

    }

}

int EmptyQueue(SequenQueue* Q)

{

    if(Q->front == Q->rear)

        return TRUE;

    else

        return FALSE;

}

Stack* build(Stack *cheku,SequenQueue* paidui)

{

    Stack* a;

    a=(Stack*)malloc(sizeof(Stack));

    a->top=-1;

    return a;

}

void tingche(Stack *cheku,SequenQueue* paidui)

{

    Stack *p;

    p=(Stack *)malloc(sizeof(Stack));

    printf("请输入车牌号\n");

    fflush(stdin);

    gets(p->a.num);

    if(fullstack(cheku))

    {

        p->next=cheku->next;

        cheku->next=p;

        p->a.starttime=time(NULL);

        cheku->top++;

        printf("停车成功\n");

        Sleep(1000);

        system("cls");

    }

    else

    {

        printf("车库已满请在门口等待\n");

        EnQueue(paidui,p->a.num);

        Sleep(1000);

        system("cls");

    } 

}

void likai(Stack *cheku,SequenQueue* paidui)

{

    char m[10];

    Stack *p,*q;

    char e[10];

    int n=0;

    p=cheku;

    if(cheku->top==-1)

    {

        printf("车库为空\n");

        Sleep(1000);

        system("cls"); 

    }

    else

    {

    printf("请输入离开的车牌:\n");

    fflush(stdin);

    gets(m);

    while(p->next!='\0')

    {    double money; 

        if(strcmp(p->next->a.num,m)==0)

        {

            q=p->next;

            p->next=q->next;

            q->a.endtime=time(NULL);

            money=(q->a.endtime-q->a.starttime)*0.00139;

            char tmp1[100],tmp2[100];

            strftime(tmp1,sizeof(tmp1),"%Y-%m-%d  %H:%M:%S",localtime(&q->a.endtime));

            strftime(tmp2,sizeof(tmp2),"%Y-%m-%d  %H:%M:%S",localtime(&q->a.starttime));

            printf("停车时间:%s\n",tmp2);

            printf("离开时间:%s\n",tmp1);

            printf("共停%ds\n",q->a.endtime-q->a.starttime); 

            printf("收费%.5lf元(一小时五元)\n",money); 

            Sleep(3000);

            free(q);

            system("cls");

            cheku->top--;

            n++;

            if(EmptyQueue(paidui)==0)

            {

                DeQueue(paidui,e);

                Stack *d=(Stack *)malloc(sizeof(Stack));

                strcpy(d->a.num,e);

                d->a.starttime=time(NULL);

                d->next=cheku->next;

                cheku->next=d;

                cheku->top++;

                printf("已将等待的第一辆车进入停车场");

                Sleep(1000);

                system("cls"); 

            } 

            break;

        }

        p=p->next;

    }

    if(n==0)

    {

        printf("未找到该车辆信息请重试");

        Sleep(1000);

        system("cls");

    }

}     

}

void chakan(Stack *cheku,SequenQueue* paidui)

{

    if(cheku->top==maxsize-1)

    {

        printf("车库已满,共有%d的车辆在等候",(paidui->rear-paidui->front+MAXSIZE)%MAXSIZE);

        Sleep(1000);

        system("cls");

    }

    else

    {

        printf("车库还有%d个空位",maxsize-cheku->top-1);

        Sleep(1000);

        system("cls");

    }

}

代码可能不是最佳,如有错误,敬请指正。

原文链接:https://blog.csdn.net/m0_55627515/article/details/116301536
相关文章
最新更新