C語言源碼實現(xiàn)停車場管理系統(tǒng)
本文實例為大家分享了C語言停車場管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
題目要求:

剛開始在Codeblocks下用C語言寫的,但是用指針傳遞參數(shù)的時候總是出問題。后來就用C++,但是調(diào)用了C的輸入輸出和文件操作的頭文件,所以代碼都是C的
main.cpp
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <windows.h>
#include <ctime>
#include <cstring>
#include <conio.h>
#define N 100
using namespace std;
typedef struct
{
char num[8];//車牌號
long int time_in;
int pos;//車輛的狀態(tài),0表示停在便道中,1表示停在停車場
} vehicle; //定義車輛類型
typedef struct
{
vehicle veh[N];
int top;
} SqStack; //用棧表示停車場
typedef struct LNode
{
vehicle veh;
struct LNode *next;
} LinkList; //用單鏈表表示便道
void Load(FILE *,SqStack *,LinkList *);
void ShowMenu(int );
int MakeChoice(int ,int );
void Parking(SqStack *,LinkList *);
void Back(SqStack *);
void EnterPkl(SqStack *,LinkList *);
void LeavePath(LinkList *);
void View(SqStack *,LinkList *);
void Write_and_Quit(FILE *,SqStack *,LinkList *);
int main()
{
SqStack *pkl;
LinkList *path;
FILE *fp;
pkl=(SqStack *)malloc(sizeof(SqStack));
path=(LinkList *)malloc(sizeof(LinkList));
fp=fopen("Parking_lot.txt","r+");
if(fp==NULL)
{
printf("數(shù)據(jù)加載失敗!按任意鍵退出程序");
getch();
return 0;
}
Load(fp,pkl,path);
while(1)
{
system("cls");
ShowMenu(pkl->top);
switch(MakeChoice(1,6))
{
case 1:
system("cls");
Parking(pkl,path);
break;
case 2:
system("cls");
Back(pkl);
break;
case 3:
system("cls");
EnterPkl(pkl,path);
break;
case 4:
system("cls");
LeavePath(path);
break;
case 5:
system("cls");
View(pkl,path);
break;
default:
system("cls");
Write_and_Quit(fp,pkl,path);
return 0;
}
}
return 0;
}
function.cpp
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <windows.h>
#include <ctime>
#include <cstring>
#include <conio.h>
#define N 100
using namespace std;
typedef struct
{
char num[8];//車牌號
long int time_in;
int pos;//車輛的狀態(tài),0表示停在便道中,1表示停在停車場
} vehicle; //定義車輛類型
typedef struct
{
vehicle veh[N];
int top;
} SqStack; //用棧表示停車場
typedef struct LNode
{
vehicle veh;
struct LNode *next;
} LinkList; //用單鏈表表示便道
void Load(FILE * fp,SqStack * pkl,LinkList * path)
{
pkl->top=-1;
path->next=NULL;
LinkList *p;
char num[8];
long int time_in;
int pos;
while(fscanf(fp,"%s %ld %d\n",num,&time_in,&pos)!=EOF)
{
if(pos==0)//該車輛在便道中
{
//尾插法建立單鏈表
p=(LinkList *)malloc(sizeof(LinkList));
strcpy(p->veh.num,num);
p->veh.time_in=time_in;
p->veh.pos=pos;
path->next=p;
path=p;
}
else//該車輛在停車場中
{
++pkl->top;
strcpy(pkl->veh[pkl->top].num,num);
pkl->veh[pkl->top].time_in=time_in;
pkl->veh[pkl->top].pos=pos;
}
}
path->next=NULL;
}
void ShowMenu(int n)
{
printf("********一個簡單的停車場管理系統(tǒng)********\n");
if(n+1==N)
printf("***************停車場已滿***************\n");
else
printf("**********當(dāng)前停車場共有%03d輛車**********\n",n+1);
printf("********說明:停車場每小時收費5元********\n");
printf("****************1.停車******************\n");
printf("****************2.取車******************\n");
printf("*********3.便道車輛進入停車場***********\n");
printf("**************4.離開便道****************\n");
printf("**************5.查看車輛****************\n");
printf("****************6.退出******************\n");
}
int MakeChoice(int m,int n)
{
int judge;
printf("請輸入%d~%d\n",m,n);
scanf("%d",&judge);
while(judge<m||judge>n)//確保輸入的是1~n
{
printf("輸入不合法,請輸入%d~%d\n",m,n);
fflush(stdin);//如果不加這句,輸入一些字母會導(dǎo)致函數(shù)無限循環(huán)
scanf("%d",&judge);
}
return judge;
}
void Parking(SqStack *pkl,LinkList *path)
{
LinkList *r;
printf("請輸入車牌號:");
if(pkl->top<N-1)
{
fflush(stdin);
scanf("%8s",pkl->veh[++pkl->top].num);
time(&(pkl->veh[pkl->top].time_in));
pkl->veh[pkl->top].pos=1;
printf("您的車輛已停至%2d號車位\n",pkl->top);
}
else
{
fflush(stdin);
r=(LinkList *)malloc(sizeof(LinkList));
scanf("%8s",r->veh.num);
printf("停車場已滿,您要暫時停放在便道中嗎?\n");
printf("1.確定 2.取消\n");
if(MakeChoice(1,2)==1)
{
while(path->next!=NULL)
path=path->next;
r->veh.time_in=0;
r->veh.pos=0;
path->next=r;
r->next=NULL;
printf("您的車輛已停放到便道中\(zhòng)n");
}
else
free(r);
}
printf("按任意鍵返回主菜單");
getch();
return;
}
void Back(SqStack *pkl)
{
int n,i=0;
long int time_out;
double hours;
vehicle t_pkl[N];
printf("請輸入您的車輛所在的車位(目前還有個小問題,前面的車走了之后當(dāng)前車位會-1):");
n=MakeChoice(0,pkl->top);
printf("%2d上的車輛車牌號為%s,您確定要取走該車輛嗎?\n",n,pkl->veh[n].num);
printf("1.確定 2.取消\n");
if(MakeChoice(1,2)==1)
{
time(&time_out);
hours=(time_out-pkl->veh[n].time_in)/3600.0;
printf("本次停車共計%lf小時,收費%lf元,請按任意鍵確認(rèn)支付\n",hours,hours*5);
getch();
for(i=0; pkl->top>=n; --pkl->top,++i) //把第n輛到第pkl->top輛車移到t_pkl
t_pkl[i]=pkl->veh[pkl->top];
//此時pkl->top指向第n-1輛車
for(i-=2; i>=0; --i) //把第n+1輛到第pkl->top輛車移回pkl
pkl->veh[++pkl->top]=t_pkl[i];
printf("支付成功!\n");
printf("取車成功,按任意鍵返回主菜單");
getch();
return;
}
else
{
printf("按任意鍵返回主菜單");
getch();
return;
}
}
void EnterPkl(SqStack *pkl,LinkList *path)
{
if(pkl->top==N-1)
printf("停車場已滿!");
else
{
printf("您確定將便道中第一輛車(車牌號:%8s)停入停車場嗎?\n",path->next->veh.num);
printf("1.確定 2.取消\n");
if(MakeChoice(1,2)==1)
{
pkl->veh[++pkl->top]=path->next->veh;
time(&pkl->veh[pkl->top].time_in);
path->next=path->next->next;
printf("已停入停車場\n");
}
}
printf("按任意鍵返回主菜單");
getch();
return;
}
void LeavePath(LinkList *path)
{
int i=0,n;
LinkList *q;
printf("請輸入要離開便道的車輛的位序:");
scanf("%d",&n);
while(i<n&&path!=NULL)
{
++i;
q=path;//保存當(dāng)前節(jié)點的前一個節(jié)點,如果找到的位置在鏈表最后,需要將前一個節(jié)點的指針域置為NULL
path=path->next;
}
if(path!=NULL)
{
printf("您確定便道中第%03d輛車(車牌號:%8s)離開便道嗎?\n",n,path->veh.num);
printf("1.確定 2.取消\n");
if(MakeChoice(1,2)==1)
{
if(path->next!=NULL)//確定離開并且不是便道中最后一輛車
{
q=path->next;
path->next=q->next;
free(q);
printf("第%03d輛車已離開便道\n",n);
}
else//確定離開并且是便道中最后一輛車
{
printf("第%03d輛車已離開便道\n",n);
q->next=NULL;
free(path);
}
}
}
else
printf("沒有找到第%03d輛車\n",n);
printf("按任意鍵返回主菜單");
getch();
return;
}
void View(SqStack *pkl,LinkList *path)
{
int i;
long int time_out;
double hours;
time(&time_out);
printf("停車場共有%03d輛車:\n",pkl->top+1);
for(i=0; i<=pkl->top; ++i)
{
hours=(time_out-pkl->veh[i].time_in)/3600.0;
printf("車位:%2d 車牌號:%8s 停車時長:%lf 應(yīng)繳費用:%lf\n",i,pkl->veh[i].num,hours,hours*5);
}
printf("便道車輛:\n");
if(path->next==NULL)
printf("無\n");
while(path->next!=NULL)
{
path=path->next;
printf("車牌號:%s\n",path->veh.num);
}
printf("按任意鍵返回主菜單");
getch();
return;
}
void Write_and_Quit(FILE *fp,SqStack *pkl,LinkList *path)
{
rewind(fp);
LinkList *pre=path,*p=path->next;
for(; pkl->top>-1; --pkl->top)
fprintf(fp,"%s %ld %d\n",pkl->veh[pkl->top].num,pkl->veh[pkl->top].time_in,pkl->veh[pkl->top].pos);
while(p!=NULL)
{
free(pre);
fprintf(fp,"%s %ld %d\n",p->veh.num,p->veh.time_in,p->veh.pos);
pre=p;
p=pre->next;
}
free(pre);
free(pkl);
fclose(fp);
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對數(shù)怎么表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-10C#中split用法實例總結(jié)
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10使用C語言求解撲克牌的順子及n個骰子


