數據結構課程設計--立體停車場管理系統(改)

/* 
* Copyright (c)2016,煙臺大學計算機與控制工程學院 
* All rights reserved. 
* 文件名稱:lalala 
* 作    者:臧新曉 
* 完成日期:2016年12月30日 
* 版 本 號:v1.0 
* 問題描述:立體車庫管理系統
* 輸入描述:無 
* 程序輸出:測試數據 
*/  


 

//dingyi.h
#include <iostream>  
#include <string.h>  
#include <stdio.h>  
#include <stdlib.h>//standard library標準庫文件  
#include <time.h>//得到當前時間的頭文件  
#define FLOOR 2 //全局變量定義兩層樓  
#define PNUMBER 6//全局變量定義每層樓有六個車位  
using namespace std;  
struct Car//定義每輛車的結構體  
{  
    char license[8];//車牌號  
    int floor;//樓層  
    int pnumber;//對應樓層的車位  
    int ptime;//停車時間  
    int empty;//標誌位,有車爲1,無車爲0  
};  
struct Date//時間結構體  
{  
    char day[11];//日期  
    char shike[9];//時間  
    char weekday[10];//星期幾  
};  
typedef struct Snode//節點的結構體  
{  
    char license[8];//車牌號  
    Date date;//停車的時刻  
    Date dateleave;//車離開的的時刻  
    int floor;//樓層  
    int pnumber;//對應車位  
    int ptimecount;//本次停車總時間  
    float cost;//停車費  
    Snode *next;  
}CustList;  
struct User//管理員結構體  
{  
    char id[10];//賬戶  
    char name[10];//姓名  
    char password[10];//密碼  
  
};  
void Welcome();//歡迎界面  
void ShowStarA();//展示界面a  
void ShowStarB();//展示界面b  
void ShowStarC();//展示界面c  
int Enterpark(Car cars[FLOOR][PNUMBER],char *license,int &floor,int &pnumber);//存車函數  
void Savecars(Car cars[FLOOR][PNUMBER]);//保存車輛信息  
bool IsEmpty(Car cars[FLOOR][PNUMBER]);//取車時判斷車場是否爲空  
bool IsFull(Car cars[FLOOR][PNUMBER]);//存車時判斷車場是否爲空  
bool IsSame(Car cars[FLOOR][PNUMBER],char *license);//查看存的車牌號是否存在  
void Print(char license[],int floor,int pnumber,int ptime,float &cost);//輸出 收費後 個人 憑據  
void Leavepark(Car cars[FLOOR][PNUMBER],char license[],int &floor,int &pnumber,int &ptime);//取車函數  
int CarOutMenu(char license[],int &floor,int &pnumber,int &ptime,float &cost,char note[100]);//取車的菜單項  
void Customer(Car cars[FLOOR][PNUMBER],CustList *cl);//顧客界面  
void Manage(Car cars[FLOOR][PNUMBER],CustList *cl,CustList *clr);//後臺程序  
void GetDate(Date &date);//得到當前時間  
int UsertestAdd();//登陸界面或者註冊用戶  
void Load(Car cars[FLOOR][PNUMBER]);//加載初始化的四輛汽車  
void Loadfile(Car cars[FLOOR][PNUMBER],char license[8],int floor,int pnumber,int ptime,int empty);//加載每一個停車位的狀態  
void Showcars(Car cars[FLOOR][PNUMBER],CustList *cl);//展示現有汽車停車信息  
void Message(CustList *clr);//歷史停車信息  
void MessageIntoFile(char note[100],char license[8]);//保存信息至文件  
void CheckOneCar(CustList *clr,char *license);//查找某一車輛的停車信息  
void InitCustList(CustList *&cl);//初始化鏈表  
void InitCustList1(CustList *&cl,Car cars[FLOOR][PNUMBER]);//建立鏈表保存信息  
void InsertToFirst(CustList *cl,char *license,int floor,int pnumber,Date date);//頭插法,插入節點  
void InsertToLst(CustList *cl,char *license,int floor,int pnumber,int ptime,float cost,Date date,Date dateleave);//從文件中讀出時,尾差法,使得時間降序排列  
void SearchUpdate(CustList *&cl,char *license,float cost,int ptimecount,char note[],Date date);//修改當前的當天的鏈表 cl  
void SaveListTemp(CustList *cl);//將鏈表的信息保存到歷史文件today.dat中  
void SaveList(CustList *cl);//保存歷史信息至文件  
void DeleteAll(CustList *cl);//刪除鏈表  
void LoadList(CustList *clr);//加載歷史文件 


 

//main.cpp
#include "dingyi.h"
int main()  
{  
    system("color 71");  
    Welcome();  
    //初始化還沒有出庫的車  
    Car cars[FLOOR][PNUMBER];  
    int i,j;  
    for(i=0;i<FLOOR;i++)  
    {  
        for(j=0;j<PNUMBER;j++)  
        {  
            cars[i][j].floor =0;  
            cars[i][j].pnumber=0;  
            cars[i][j].ptime=0;  
            cars[i][j].empty=0;  
            strcpy(cars[i][j].license ,"0");  
        }  
    }  
  Load(cars);//加載最初的四輛車  
    //加載歷史文件  
    CustList *cl,*clr;//cl保存當天的修改信息,clr保存以前的歷史信息,當推出程序時,自動把當天信息cl修改的動態歷史保存到clr中  
    InitCustList1(cl,cars);//cl保存當天的修改信息  
    InitCustList(clr);//保留歷史文件list.txt的內容  
    while(1)  
    {  
        ShowStarB();  
        cout<<"1 客戶界面"<<endl;  
        cout<<"2 車管界面"<<endl;  
        cout<<"3 退出整個程序"<<endl;  
        cout<<"請選擇:";  
        cin>>i;  
        if(i<1||i>3)  
        {  
            cout<<"您的操作非法!!!"<<endl;  
            continue;  
  
        }  
        system ("cls");  
        switch(i)  
        {  
        case 1:  
            {  
                Customer(cars,cl);  
                SaveList(cl);  
                SaveListTemp(cl);  
            }  
            break;  
        case 2:  
            {  
                LoadList(clr);  
                Manage(cars,cl,clr);  
            }  
            break;  
        case 3:  
            {  
                SaveList(cl);  
                SaveListTemp(cl);  
            }  
            exit(-1);  
        }  
    }  
    return 0;  
}  


 

//jiemian.cpp
#include"dingyi.h"  
void Welcome()  
{  
    int i;  
    char choice;  
    for(i=1;i<=160;i++)  
        cout<<"\004";  
    cout<<endl;  
    cout<<"                              立體停車場管理系統                        "<<endl;  
    cout<<endl;  
    cout<<"                             製作人:Zang xinxiao               "<<endl;  
    cout<<endl;  
    cout<<"                                班級: 計156-2                          "<<endl;  
    cout<<endl;
	for(i=1;i<=160;i++)  
        cout<<"\003"; 
    cout<<"                             是否進入該系統(Y/N)?                            "<<endl;  
    cin>>choice;  

    while(1)  
    {  
        if(choice=='Y'||choice=='y')  
        {  
            for(i=0;i<100000000;i++);  
            system ("cls");//清屏  
            break;  
        }  
        else  
            if(choice=='N'||choice=='n')  
                exit(0);  
            else  
            {  
            cout<<"請做出正確的選擇!"<<endl;  
                cin>>choice;  
            }  
    }  
}  
void ShowStarA()  
{  
    cout<<"********************************************************************************"<<endl;  
    cout<<"                               歡迎進入管理員界面                             "<<endl;  
    cout<<endl;  
    cout<<"********************************************************************************"<<endl;  
}  
void ShowStarB()  
{  
    cout<<"********************************************************************************"<<endl;  
    cout<<"                               歡迎進入停車場系統                             "<<endl;  
    cout<<endl;  
    cout<<"********************************************************************************"<<endl;  
}  
void ShowStarC()  
{  
    cout<<"********************************************************************************"<<endl;  
    cout<<"                                   你好!顧客                                 "<<endl;  
    cout<<endl;  
    cout<<"********************************************************************************"<<endl;  
}  
/***************************************** 
*功能描述:用順序查找的方式進行某一輛車的查找 
*輸入參數:無 
*輸出參數:無 
*返回值:無 
*其他聲明:完成單獨查找某一輛車的歷史信息 
*****************************************/  
  
void CheckOneCar(CustList *clr,char *license)  
{  
    if(!license||!clr)  
        return;  
    CustList *p;  
    p=clr->next;  
    cout<<endl;  
    cout<<"\t\t--------停車歷史信息--------"<<endl;  
    cout<<endl;  
    cout.setf(ios::left);  
    cout.width(8);  
    cout<<"車牌";  
    cout.width(8);  
    cout<<"樓層號";  
    cout.width(8);  
    cout<<"車位號";  
    cout.width(10);  
    cout<<"停車時間";  
    cout.width(8);  
    cout<<"消費"<<endl;  
    while(p)  
    {  
        if(strcmp(p->license,license)==0)  
        {  
            cout<<endl;  
            cout<<"----------------------------------------------------------------------"<<endl;  
            cout<<"                                        進庫時間:"<<p->date.day<<" "<<p->date.shike<<" "<<p->date.weekday<<endl;  
            cout.setf(ios::left);  
            cout.width(8);  
            cout<<p->license;  
            cout.width(8);  
            cout<<p->floor;  
            cout.width(8);  
            cout<<p->pnumber;  
            cout.width(10);  
            cout<<p->ptimecount;  
            cout.width(8);  
            cout<<p->cost;  
            cout<<endl;  
            if(strcmp(p->date.day,p->dateleave.day)==0&&strcmp(p->date.shike,p->dateleave.shike)==0 &&strcmp(p->date.weekday,p->dateleave.weekday)==0 )  
                cout<<"                                        出庫時間: 未知"<<endl;  
            else  
                cout<<"                                        出庫時間:"<<p->dateleave.day<<" "<<p->dateleave.shike<<" "<<p->dateleave.weekday<<endl;  
            cout<<endl;  
        }  
        p=p->next ;  
    }  
}  
/***************************************** 
*功能描述:所有的歷史停車信息 
*輸入參數:無 
*輸出參數:無 
*返回值:無 
*其他聲明:無 
*****************************************/  
  
void Message(CustList *clr)  
{  
    cout<<endl;  
    cout<<"\t\t--------停車歷史信息--------"<<endl;  
    cout<<endl;  
    Snode *p=clr->next;  
    cout.setf(ios::left);  
    cout.width(8);  
    cout<<"車牌";  
    cout.width(8);  
    cout<<"樓層號";  
    cout.width(8);  
    cout<<"車位號";  
    cout.width(10);  
    cout<<"停車時間";  
    cout.width(8);  
    cout<<"消費"<<endl;  
    while(p)  
    {   cout<<"----------------------------------------------------------------------"<<endl;  
        cout<<"                                        進庫時間:"<<p->date.day<<" "<<p->date.shike<<" "<<p->date.weekday<<endl;  
        cout.setf(ios::left);  
        cout.width(8);  
        cout<<p->license;  
        cout.width(8);  
        cout<<p->floor;  
        cout.width(8);  
        cout<<p->pnumber;  
        cout.width(10);  
        cout<<p->ptimecount;  
        cout.width(8);  
        cout<<p->cost;  
        cout<<endl;  
        if(strcmp(p->date.day,p->dateleave.day)==0&&strcmp(p->date.shike,p->dateleave.shike)==0 &&strcmp(p->date.weekday,p->dateleave.weekday)==0 )  
            cout<<"                                        出庫時間: 未知"<<endl;  
        else  
        cout<<"                                        出庫時間:"<<p->dateleave.day<<" "<<p->dateleave.shike<<" "<<p->dateleave.weekday<<endl;  
        cout<<endl;  
        p=p->next;  
    }  
    cout<<endl;  
}  
//加載歷史文件  
void LoadList(CustList *clr)  
{  
    DeleteAll(clr);  
    FILE *fp=fopen("history.txt","rb");  
    if (fp==NULL)  
        return;  
    int n;  
    Snode p;  
    while(!feof(fp))  
    {  
        n=fread(&p,sizeof(p),1,fp);  
        if(n!=1)  
            break;  
        InsertToLst(clr,p.license,p.floor,p.pnumber,p.ptimecount,p.cost,p.date,p.dateleave );  
    }  
    fclose(fp);  
}  
//獲得尾巴以便於使時間降序排列輸出停車信息  
Snode* GetTail(CustList *cl)  
{  
    Snode *p=cl;  
    while(p->next)  
        p=p->next;  
    return p;  
}  
/***************************************** 
*功能描述:從文件中讀出時,尾插法,使得時間降序排列 
*輸入參數:無 
*輸出參數:無 
*返回值:無 
*其他聲明:輔助查取歷史信息功能的實現 
*****************************************/  
  
  
void InsertToLst(CustList *cl,char *license,int floor,int pnumber,int ptime,float cost,Date date,Date dateleave)  
{  
  
    Snode *s;  
    s=new Snode;  
    strcpy(s->license,license);  
    s->floor=floor;  
    s->pnumber=pnumber;  
    s->ptimecount=ptime;  
    s->cost=cost;  
    s->date =date;  
    s->dateleave =dateleave;  
    s->next=NULL;  
    Snode *q=GetTail(cl);  
    q->next=s;  
}  
//刪除鏈表  
void DeleteAll(CustList *cl)  
{  
Snode *p=cl->next;  
Snode *q=NULL;  
while(p)  
{  
    q=p;  
    p=p->next;  
    delete q;  
}  
cl->next=p;  
}  
//將鏈表的信息保存到歷史文件today.dat中  
void SaveListTemp(CustList *cl)  
{  
    FILE* fp = fopen("today.txt","ab");  
    if(fp==NULL)  
    {  
        FILE* fp = fopen("today.txt","wb");  
  
    }  
    Snode *p=cl->next;  
    Snode q;  
    while(p)  
    {  
        strcpy(q.license,p->license);  
        q.cost=p->cost;  
        q.floor=p->floor;  
        q.pnumber=p->pnumber;  
        q.date =p->date ;  
        q.dateleave =p->dateleave ;  
        q.ptimecount=p->ptimecount;  
        fwrite(&q,sizeof(q),1,fp);  
        p=p->next;  
    }  
    fclose(fp);  
}  
void SaveList(CustList *cl)//保存歷史信息至文件  
{  
    FILE* fp = fopen("history.txt","ab");  
    if(fp==NULL)  
    {  
        FILE* fp = fopen("history.txt","wb");  
    }  
    Snode *p=cl->next;  
    Snode q;  
    while(p)  
    {  
        strcpy(q.license,p->license);  
        q.cost=p->cost;  
        q.floor=p->floor;  
        q.pnumber=p->pnumber;  
        q.dateleave =p->dateleave;  
        q.date =p->date ;  
        q.ptimecount=p->ptimecount;  
        fwrite(&q,sizeof(q),1,fp);  
        p=p->next;  
  
    }  
    fclose(fp);  
  
}  
//初始化  
void InitCustList(CustList *&cl)  
{  
    cl=new CustList;  
    cl->next=NULL;  
    cl->floor =0;  
    cl->pnumber =0;  
    cl->ptimecount =0;  
    cl->cost =0;  
}  
/***************************************** 
*功能描述:頭插法,插入節點,以便於保存數據 
*輸入參數:無 
*輸出參數:無 
*返回值:無 
*其他聲明:只是將之前的信息進行復制,轉到鏈表的形式進行保存 
*****************************************/  
  
  
void InsertToFirst(CustList *cl,char *license,int floor,int pnumber,Date date)  
{  
    if(license==NULL)  
        return;  
    Snode *c=new Snode;  
    strcpy(c->license,license);  
    c->floor=floor;  
    c->pnumber=pnumber;  
    c->dateleave =date;  
    strcpy(c->date.day,date.day);  
    strcpy(c->date.shike,date.shike);  
    strcpy(c->date.weekday,date.weekday);  
    c->ptimecount =0;  
    c->cost =0;  
    c->next = cl->next;  
    cl->next = c ;  
}  
void MessageIntoFile(char note[100],char license[8])//保存信息至文件  
{  
    FILE* fp = fopen("message.txt","ab");  
    if(fp==NULL)  
    {  
        FILE* fp = fopen("message.txt","wb");  
  
    }  
    Date date1;  
    GetDate(date1);  
}  
/***************************************** 
*功能描述:存車 
*輸入參數:車牌號以及車位位置 
*輸出參數:存車成功 
*返回值:1 
*其他聲明:無 
*****************************************/  
  
int Enterpark(Car cars[FLOOR][PNUMBER],char *license,int &floor,int &pnumber)  
{  
    cout<<endl;  
    //輸入車牌號,判斷車牌號的有效性  
    cout<<"請輸入7標準位車牌號:";  
    char licen[8];  
    cin>>licen;  
    cout<<endl;  
    while((!licen)||(strlen(licen)!=7))  
    {  
        cout<<"輸入有誤! ";  
        cout<<"請輸入正確的7位車牌號:";  
        cin>>licen;  
    }  
    //查看是否有重複  
    while(1)  
    {  
        if(IsSame(cars,licen)==1)  
        {  
            cout<<"已經存在這輛車,請重新輸入7標準位車牌號:"<<endl;  
            cin>>licen;  
        }  
        else break;  
    }  
    int i,j;  
    if(IsFull(cars)==1)  
    {  
        cout<<"已經沒有空車位了,謝謝惠顧!正在跳轉到顧客界面。。。"<<endl;  
        return 0;  
    }  
    int sign=1;//sign 標誌第幾層有空位0表一,1表二  
    cout<<"您可以選擇的車位"<<endl;  
    cout<<"----------------------------"<<endl;  
    cout.setf(ios::left);  
    cout.width(8);  
    cout<<"樓層";  
    cout.width(8);  
    cout<<"車位號";  
    cout<<endl;  
    for(i=0;i<FLOOR;i++)  
    {  
        if(sign==0) break;  
        for(j=0;j<PNUMBER;j++)  
        {  
            if((cars[i][j].empty==0)&&(i==0))  
            {  
                sign=0;  
                cout.setf(ios::left);  
                cout.width(8);  
                cout<<i+1;  
                cout.width(8);  
                cout<<j+1<<endl;  
                if(j==PNUMBER-1) {break;}  
            }  
            else if((cars[i][j].empty==0)&&(i==1))  
            {  
                cout.setf(ios::left);  
                cout.width(8);  
                cout<<i+1;  
                cout.width(8);  
                cout<<j+1<<endl;  
            }  
        }  
    }  
    cout<<"----------------------------"<<endl;  
    int temf;  
    cout<<"請輸入車位號:";  
    cin>>temf;  
    while(1)  
    {  
        if((cars[sign][temf-1].empty==0)&&(temf>=1&&temf<=6))  
        {  
            //修改一個車位的信息  
            cars[sign][temf-1].empty=1;  
            floor=cars[sign][temf-1].floor=sign+1;  
            pnumber=cars[sign][temf-1].pnumber=temf;  
            strcpy(cars[sign][temf-1].license,licen);  
            strcpy(license,licen);  
            cars[sign][temf-1].ptime=0;  
            //修改整個停車場的信息  
            for(i=0;i<FLOOR;i++)  
            {  
                for(j=0;j<PNUMBER;j++)  
                {  
                    if(cars[i][j].empty==1)  
                        cars[i][j].ptime+=5;  
                }  
            }  
            break;  
        }  
        else {  
            cout<<"該車位已經存入車輛!請您重新選擇車位號:";  
            cin>>temf;  
        }  
    }  
    return 1;  
  
}  
/***************************************** 
*功能描述:取車 
*輸入參數:要取車輛的車牌號 
*輸出參數:可隨機選擇是否輸出數據 
*返回值:無 
*其他聲明:無 
*****************************************/  
  
void Leavepark(Car cars[FLOOR][PNUMBER],char license[],int &floor,int &pnumber,int &ptime)  
{  
    //判斷車位是否已空  
    if(IsEmpty(cars)==1)  
    {  
        cout<<"停車場已沒有車停放!請確定您是否停車。"<<endl;  
        return;  
    }  
    //查找車位  
    int sign=1;//標誌位,車庫有該車爲1,沒有賦值爲零,初始值爲0  
    int i,j;  
    while(sign)  
    {  
        for(i=0;i<FLOOR;i++)  
        {  
            for(j=0;j<PNUMBER;j++)  
            {  
                if(strcmp(cars[i][j].license,license)==0)  
                {  
                    sign=0;  
                    floor=i;  
                    pnumber=j;  
                    ptime=cars[i][j].ptime;  
                }  
            }  
        }  
        if(sign)  
        {  
            cout<<"您剛纔輸入的車牌號不存在!"<<endl;  
            cout<<"請重新輸入:";  
            cin>>license;  
        }  
    }  
    if(sign==0)  
    cout<<"車已找到,請稍等。。。"<<endl;  
    cout<<endl;  
    floor+=1;  
    pnumber+=1;  
    for(i=0;i<100000000;i++);
    cars[floor-1][pnumber-1].floor =0;  
    cars[floor-1][pnumber-1].pnumber =0;  
    cars[floor-1][pnumber-1].ptime=0;  
    cars[floor-1][pnumber-1].empty=0;  
    strcpy(cars[floor-1][pnumber-1].license," ");  
}  
//取車的菜單項  
int CarOutMenu(char license[],int &floor,int &pnumber,int &ptime,float &cost,char note[100])  
{  
    cout<<"1 輸出憑據"<<endl;  
    cout<<"2 直接返回主頁"<<endl;  
        int p=0;//標誌,若已經輸出過憑據,則置爲1  
  
    int j=0;  
    int i=0;  
    //計算費用  
    cost=ptime*float (0.2);  
    while(1)  
    {  
        cout<<"請選擇:";  
        int i;  
        cin>>i;  
        if(i<1||i>3)  
        {  
            cout<<"沒有此項操作!!!"<<endl;  
            continue;  
        }  
        switch(i)  
        {  
        case 1:  
            {  
                p=1;  
                cout<<endl;  
                cout<<"\t\t--------本輛汽車停車信息--------"<<endl;  
                cout<<endl;  
                Print(license,floor,pnumber,ptime,cost);  
                break;  
            }  
        }  
        if(i==1) continue;  
        else  
        {  
            j=1;  
            break;  
        }  
    }  
return j;  
}  
//得到當前時間  
void GetDate(Date &date)  
{  
    time_t t = time( 0 );  
    char str[31];  
    strftime(str, sizeof(str), "%Y/%m/%d %X %A",localtime(&t) );  
    int sign=1,j=0,k=0,l=0;  
    for(int i=0;i<31;i++)  
        if(sign==1)  
        {  
            if(str[i]==' ') {sign=2;continue;}  
            else {  
                date.day[j]=str[i];  
                j++;  
            }  
        }  
    else if(sign==2)  
        {  
            if(str[i]==' ') {sign=3;continue;}  
            else {  
                date.shike[k]=str[i];  
                k++;  
            }  
        }  
    else if(sign==3)  
        {  
            if(str[i]=='\0') break;  
            else {  
                date.weekday[l]=str[i];  
                l++;  
            }  
        }  
    date.day[j]='\0';  
    date.shike[k]='\0';  
    date.weekday[l]='\0';  
}  
//判斷車場是否爲空  
bool IsEmpty(Car cars[FLOOR][PNUMBER])  
{  
int i,j;  
for(i=0;i<FLOOR;i++)  
    for(j=0;j<PNUMBER;j++)  
        if(cars[i][j].empty==1) return false;  
return true;  
}  
//判斷車場是否爲滿  
bool IsFull(Car cars[FLOOR][PNUMBER])  
{  
int i,j;  
for(i=0;i<FLOOR;i++)  
    for(j=0;j<PNUMBER;j++)  
        if(cars[i][j].empty==0) return false;  
return true;  
}  
//查看存的車牌號是否存在  
bool IsSame(Car cars[FLOOR][PNUMBER],char *license)  
{  
int i,j;  
for(i=0;i<FLOOR;i++)  
    for(j=0;j<PNUMBER;j++)  
        if(strcmp(cars[i][j].license,license)==0) return true;  
return false;  
}  
//逐條添加記錄到car.dat中  
void Savecars(Car cars[FLOOR][PNUMBER])  
{  
    FILE* fp = fopen("car.txt","wb");  
    if(fp==NULL)  
    {  
        cout<<"Can not open this file"<<endl;  
        return;  
    }  
    Car car;  
    int i=0;  
    while(i<FLOOR)  
    {  
        int j=0;  
        while(j<PNUMBER)  
        {  
            if(cars[i][j].empty ==1)  
            {  
                car.floor =cars[i][j].floor ;  
                strcpy(car.license ,cars[i][j].license );  
                car.pnumber =cars[i][j].pnumber ;  
                car.ptime =cars[i][j].ptime ;  
                car.empty=cars[i][j].empty ;  
                fwrite(&car,sizeof(car),1,fp);  
            }  
            j++;  
        }  
        i++;  
    }  
    fclose(fp);  
}  
  
//加載初始化的四輛汽車  
void Load(Car cars[FLOOR][PNUMBER])  
{  
    FILE* fp = fopen("car1.txt","rb");  
    if(fp==NULL)  
    {  
        cout<<"不能自動加載 can not open car1.txt"<<endl;  
        return;  
    }  
    Car car;  
    int n;  
    while(!feof(fp))  
    {  
        n = fread(&car,sizeof(car),1,fp);  
        if(n!=1)  
            break;  
        Loadfile(cars,car.license ,car.floor,car.pnumber ,car.ptime ,car.empty );  
    }  
    fclose(fp);  
}  
//加載每一個停車位的狀態  
void Loadfile(Car cars[FLOOR][PNUMBER],char license[8],int floor,int pnumber,int ptime,int empty )  
{  
    if(!license)  
        return;  
    if(floor>FLOOR||pnumber>PNUMBER) return;  
    strcpy(cars[floor-1][pnumber-1].license,license);  
    cars[floor-1][pnumber-1].floor =floor;  
    cars[floor-1][pnumber-1].pnumber =pnumber;  
    cars[floor-1][pnumber-1].ptime =ptime;  
    cars[floor-1][pnumber-1].empty =empty;  
}  
//輸出    收費後 個人 憑據  
void Print(char license[],int floor,int pnumber,int ptime,float &cost)  
{  
    cout.setf(ios::left);  
    cout.width(8);  
    cout<<"車牌";  
    cout.width(8);  
    cout<<"樓層";  
    cout.width(8);  
    cout<<"車位號";  
    cout.width(8);  
    cout<<"停車時間";  
    cout.width(8);  
    cout<<"消費"<<endl;  
    cout.setf(ios::left);  
    cout.width(10);  
    cout<<license;  
    cout.width(8);  
    cout<<floor;  
    cout.width(8);  
    cout<<pnumber;  
    cout.width(8);  
    cout<<ptime;  
    cout.width(8);  
    cout<<cost<<endl;  
}  
void Showcars(Car cars[FLOOR][PNUMBER])//展示現有汽車停車信息  
{  
    cout<<endl;  
    cout<<"\t\t--------現有汽車停車信息--------"<<endl;  
    cout<<endl;  
    int i,j;  
    cout.setf(ios::left);  
    cout.width(8);  
    cout<<"車牌";  
    cout.width(8);  
    cout<<"樓層";  
    cout.width(8);  
    cout<<"車位號";  
    cout.width(8);  
    cout<<"停車時間"<<endl;  
    for(i=0;i<FLOOR;i++)  
    {  
        for(j=0;j<PNUMBER;j++)  
        {  
            if(cars[i][j].empty==1)  
            {  
                cout.setf(ios::left);  
                cout.width(8);  
                cout<<cars[i][j].license;  
                cout.width(8);  
                cout<<cars[i][j].floor;  
                cout.width(8);  
                cout<<cars[i][j].pnumber;  
                cout.width(8);  
                cout<<cars[i][j].ptime<<endl;  
            }  
        }  
}  
}  
/***************************************** 
*功能描述:登陸界面或者註冊用戶 
*輸入參數:賬戶以及密碼 
*輸出參數:進入管理系統 
*返回值:0和1 
*其他聲明:無 
*****************************************/  
int UsertestAdd()  
{  
    //simple登陸驗證和註冊用戶功能  
    User UserArr[10]={"123","lalala","123"};//默認用戶  
    int count=0;//統計輸入用戶名和密碼錯誤次數  
    int i=0;//遍歷變量或找到用戶的ID  
    int k=1;//表示已存在的用戶數  
    while(1)  
    {  
        int n=0;//標識是否匹配,若匹配,則退出外循環  
        cout<<"請輸入您的管理賬號"<<endl;  
        char id[10];  
        cin>>id;  
        cout<<"請輸入密碼"<<endl;  
        char password[10];  
        cin>>password;  
        for(i=0;i<10;i++)  
        {  
            if(!strcmp(UserArr[i].id,id)&&!strcmp(UserArr[i].password,password))  
            {  
                n=1;  
                break;  
            }  
        }  
        if(n)  
        {  
            break;  
        }  
        i=0;  
        count++;  
        cout<<"密碼或賬號錯誤,";  
        if(count==2||k==2)  
        {  
            cout<<"您今天已累計輸錯"<<count<<" 次-";  
            cout<<"您是否要註冊用戶?(否則您將自動退出系統)"<<endl;  
            cout<<endl;  
            cout<<"輸入數字1表示同意註冊,其他則表示不同意條款"<<endl;  
            int tip=0;  
            cin>>tip;  
            if(tip==1)  
            {cout<<"輸入註冊密碼(知道此密碼,纔有權註冊管理員):";  
            char str[6];  
            cin>>str;  
            if(strcmp(str,"666666")==0)  
            {  
                if(k==2)  
                {  
                    cout<<"很遺憾,系統管理員用戶總數達到上限,無法註冊,若需註冊,請聯繫管理員"<<endl;  
                    return 0;  
                }  
                else{  
                    cout<<"請輸入要註冊賬號"<<endl;  
                    char id[10];  
                    cin>>id;  
                    strcpy(UserArr[k+1].id ,id);  
                    cout<<"請輸入您的姓名"<<endl;  
                    char name[10];  
                    cin>>name;  
                    strcpy(UserArr[k+1].name ,name);  
                    cout<<"請輸入您的密碼"<<endl;  
                    char password[10];  
                    cin>>password;  
                    strcpy(UserArr[k+1].password ,password);  
                    system ("cls");  
                    cout<<"註冊成功,系統正在爲您跳轉到登陸界面"<<endl;  
                    int j=0;  
                    while(j<1000000000){j++;}  
                    cout<<endl;  
                    ++k;  
                        ShowStarA();  
                }  
            }else return 0;  
            }  
        }  
    }  
    cout<<endl;  
    cout<<"*******************************************"<<endl;  
    cout<<"歡迎 "<<UserArr[i].name  <<" 進入停車場後臺管理系統"<<endl;  
    cout<<"*******************************************"<<endl;  
    cout<<endl;  
    return 1;  
}  
//顧客界面  
void Customer(Car cars[FLOOR][PNUMBER],CustList *cl)  
{  
    ShowStarC();  
    int j;  
    int sign;  
    while(1)  
    {  
        cout<<"1 存車"<<endl;  
        cout<<"2 取車"<<endl;  
        cout<<"3 返回主頁"<<endl;  
        cout<<"請選擇:";  
        int i;  
  
        char a;  
        cin>>i;  
        if(i<1||i>3)  
        {  
            cout<<"您的操作非法!!!"<<endl;  
            continue;  
        }  
        if(i==1||i==2)  
        {  
            switch(i)  
            {  
            case 1:  
                {  
                    system ("cls");  
                    ShowStarC();  
                    cout<<"   \t歡迎停車!"<<endl;  
                    char license[8];  
                    int floor,pnumber;  
                    sign=Enterpark(cars,license,floor,pnumber); //車輛入庫  
                    Date date;  
                    GetDate(date);  
                    InsertToFirst(cl,license,floor,pnumber,date); //修改當天的鏈表  
                    Savecars(cars);//修改當前的當天的car.txt文件  
                    if(sign==1)  
                    {  
                        cout<<"退出?(Y:是,N:否)"<<endl;  
                        cout<<"請輸入:";  
                        cin>>a;  
                    }  
                }  
            break;  
            case 2:  
                {  
                    system ("cls");  
                    ShowStarC();  
                    cout<<"\t歡迎取車!"<<endl;  
                    cout<<endl;  
                    int floor,pnumber;  
                    int ptime=0;  
                    float cost;  
                    cout<<"請輸入您的車牌號:";  
                    char licen[8];  
                    cin>>licen;  
                    cout<<endl;  
                    Leavepark(cars,licen,floor,pnumber,ptime);  
                        char note[100]="0";  
                    j=CarOutMenu(licen,floor,pnumber,ptime,cost,note);
					
					Date date;  
                    GetDate(date);
                    SearchUpdate(cl,licen,cost,ptime,note,date);  
                    
					Savecars(cars);  
                    if(j==1) return;  
                    break;  
                }  
  
            }  
        }  
        else  
        {  
            system ("cls");  
        break;  
        }  
        if(a=='Y')  
        {  
            system ("cls");  
            break;  
        }  
        if(sign==0)  
        {  
            for(i=0;i<1000000000;i++);  
            system ("cls");  
            break;  
        }  
    }  
}  
//修改當前的當天的鏈表 cl  
void SearchUpdate(CustList *&cl,char *license,float cost,int ptimecount,char note[],Date date)  
{  
    Snode *p;  
    p=cl->next;  
    while(p)  
    {  
        if(strcmp(p->license,license)==0)  
        {  
            p->cost=cost;  
            p->ptimecount=ptimecount;  
            if(strcmp(note,"0")!=0)  
                MessageIntoFile(note,license);  
			p->dateleave=date;
			strcpy(p->dateleave.day,date.day);  
			strcpy(p->dateleave.shike,date.shike);  
			strcpy(p->dateleave.weekday,date.weekday);
            cout<<p->dateleave.day;
            return;  
        }  
    p=p->next;  
    }  
}  
//後臺程序  
void Manage(Car cars[FLOOR][PNUMBER],CustList *cl,CustList *clr)  
{  
    ShowStarA();  
    int sign =UsertestAdd();  
    if(sign==0)  
        return;  
    else  
    {  
    while(1)  
    {  
        cout<<endl;  
        cout<<"1 顯示當前的停車場的車輛信息"<<endl;  
        cout<<"2 顯示當前時刻所有歷史停車信息"<<endl;  
        cout<<"3 查找某一車輛的歷史停車信息"<<endl;  
        cout<<"0 退出管理返回主頁面"<<endl;  
        cout<<"請選擇:";  
        int i;  
        char a;  
        cin>>i;  
        if(i<0||i>6)  
        {  
            cout<<"您的操作非法!!!"<<endl;  
            continue;  
        }  
        if(i>=1&&i<7)  
        {  
            switch(i)  
            {  
                break;  
            case 1:  
                {  
                    system ("cls");  
                    ShowStarA();  
                    Showcars(cars);  
                    break;  
                }  
            case 2:  
                {  
                    system ("cls");  
                    ShowStarA();  
                     Message(clr);  
                    break;  
                }  
            case 3:  
                {  
                    system ("cls");  
                    ShowStarA();  
                    cout<<"請您輸入車牌號:";  
                    char license[8];  
                    cin>>license;  
                    CheckOneCar(clr,license);  
                    break;  
                }  
            }  
            if(a=='Y'||a=='y')  
            {  
                system ("cls");  
                break;  
            }  
        }  
        else  
        {  
            SaveListTemp(cl);  
            SaveList(cl);  
            system ("cls");  
            break;  
        }  
    }  
    }  
}  
/***************************************** 
*功能描述:建立頭結點爲保存文件做基礎 
*輸入參數:無 
*輸出參數:無 
*返回值:無 
*其他聲明: 
*****************************************/  
  
void InitCustList1(CustList *&cl,Car cars[FLOOR][PNUMBER])  
{  
    Date date1;  
  
    //建立頭結點  
    cl=new CustList;  
    cl->next=NULL;  
    cl->floor =0;  
    cl->pnumber =0;  
    cl->ptimecount =0;  
    cl->cost =0;  
    int i,j;  
    for(i=0;i<FLOOR;i++)  
    {  
        for(j=0;j<PNUMBER;j++)  
        {  
            if(cars[i][j].empty==1)  
            {  
                GetDate(date1);  
                InsertToFirst(cl,cars[i][j].license ,cars[i][j].floor ,cars[i][j].pnumber,date1);  
            }  
        }  
    }  
}  


 

 

運行結果:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章