天天看點

C[泊車管理系統]

//

//  main.c

//  泊車管理系統

//  Created by 丁小未 on 13-7-14.

//  Copyright (c) 2013年 dingxiaowei. All rights reserved.

//題目:泊車管理系統

//(1)管理人員根據密碼進入系統

//(2)管理車位資訊(車位編号,狀态)和每分鐘的收費率;

//(3)停車時錄入汽車停泊資訊(車牌号,車型,停泊位置,停泊開始時間);如果車位已滿要給出提示;

//(4)取車時,根據車牌取,如果沒有給出提示;需要根據車輛停泊時間自動計算費用并顯示在螢幕上。

#include <stdio.h>

#include <string.h>

#include <time.h>

#define MAX 20

struct StopedCar

{

    char carNum[20];

//車牌号

    char carshap[20];//車型

    int areaNum; //停泊位置号碼

    char stopedTime[30];

//開始停泊時間

    int h;  //記錄小時

    int m;  //記錄分鐘

};

int total;  //總記錄數

char password[20];

//密碼

struct StopedCar stopedcar[MAX];

int price=1.0;//預設泊車單價為1¥1M

/***************函數申明******************************************/

void Init();//初始化,檔案不存在,則建立,同時記錄下檔案中記錄的個數

int get_menu_choice(); //接受菜單選擇

void menu();//菜單相應

void Show_menu();//顯示菜單

FILE *file_operate(char *mode);

//檔案操作類型

void set_psw();//設定密碼

int psw_check();//密碼驗證

int Inputonecar(int i);//根據車位号增加一條記錄

void Inputfile(int i,FILE *fp);//将下标為i的記錄寫入檔案

void Input(FILE *fp);//向管理系統中增加記錄

int SetPrice();//設定停車單價(例如:¥1for1s)

void GetCarByAreaNum(FILE *fp);//根據車位号取車

/*****以下函數是為了簡化部分工作從上面的函數中劃分出來的功能**************/

void PrintTitle();//列印頭資訊

void ShowAllCarInfo(); //顯示所有停車資訊

void Showonecarinfo(int i); 

//顯示停車資訊

float countMoney(float danjia);//計算根據傳入的單價來計算泊車的費用

void Readfile(int i,FILE *fp);//從檔案中讀取一條記錄

int CheckNumber(int areaNum_temp);//檢查車位号是否為空,存在傳回序号,如果該車位有停車則傳回-1

char* GetTime();//擷取目前時間

int Cost(char *time1,int danjia);//根據單價和時間差來計算停車消費的價格

int Cost2(int h,int m,int danjia);

void GetCarByAreaNum2(FILE *fp);

int timeh();

int timem();

/***************函數的實作****************************************/

//擷取目前系統時間

char* GetTime()

    {

        time_t now;

        struct tm *timenow;

        time(&now);

        timenow = localtime(&now);

    char *p =

asctime(timenow);

    return p;

    }

}

//傳回目前小時

int timeh()

    int h;

    time_t   now;

    struct   tm     *timenow;

    time(&now);

    timenow   =   localtime(&now);

    char *p = asctime(timenow);

    h=timenow->tm_hour;

    return h;

//傳回目前分鐘

int timem()

    int m;

    m=timenow->tm_min;

    return m;

//接受菜單選擇

int get_menu_choice()

    int menu_ch;

    do {

        printf("輸入菜單選項:");

        scanf("%d",&menu_ch);

        if((menu_ch<0)||(menu_ch>9))

            printf("輸入錯誤!");

    } while ((menu_ch<0)||(menu_ch>9));

    return menu_ch;

//顯示菜單

void Show_menu()

    printf(">>>>>>>>>>>>>>>歡迎使用泊車管理系統<<<<<<<<<<<<<<<<\n");

    printf("************************************************\n");

    printf("*   1.新增停車資訊         |   2.顯示所有停車資訊   *\n");

    printf("*   3.按照車牌号取車       |   4.按照車位号取車     *\n");

    printf("*   5.按照車牌号查詢車資訊  |   6.按照車位号查詢車資訊*\n");

    printf("*   7.設定泊車單價         |   8.密碼設定         *\n");

    printf("*   9.主動清屏            |   0.退出             *\n");

//清屏,有點問題

//void clear()

//{

//    system("pause");

//    system("cls");

//}

//菜單響應

void menu()

    while (1) {

        Show_menu();

        switch (get_menu_choice()) {

            case 1://新增停車資訊

                Input(file_operate("ab")); 

//二進制追加寫入

                break;

            case 2://顯示所有泊車資訊

                ShowAllCarInfo();

            case 3://按照車牌好取車

            case 4://按照車位号取車

                GetCarByAreaNum2(file_operate("wb"));

            case 5://按照車牌号查詢車位資訊

            case 6:

//按照車位号查詢車位資訊

            case

7://設定停車價格(例如:¥1for1s)

                SetPrice();

             case 8://密碼設定

                set_psw();

            case 9://手動清屏

                //clear();

                printf("手動清屏有誤!");

            case 0://結束程式

                //system("cls");

                printf("*********************\n");

                printf("    

歡迎使用本系統     \n");

                printf("**********************\n");

                //exit(0);

            default:

        }

//列印頭資訊

void PrintTitle()

    printf("-------------------------------------------------------------\n");

    printf("  

車牌号          車型  

停泊車位号          停泊開始時間   \n");

//初始化,檔案不存在,則建立,同時記錄下檔案中的記錄數

void Init()

    int ii;

    FILE *fp;

    total=0;

    if((fp=fopen("stopedcar.txt","rb"))==NULL)

        fp=fopen("stopedcar.txt","ab");

        //clear();

        menu();

    else

        for(ii=0;feof(fp)==0;ii++)  

//feof檢查檔案是否結束

        {

            Readfile(ii,fp);

        total=ii-1;

    //total=ii-1;

    fclose(fp);

FILE *file_operate(char *mode)

    char choice;

    do

        fflush(stdin);//清空輸入緩存,以便不影響後面的輸入

        if((fp=fopen("stopedcar.txt",mode))==NULL)

            puts("Fail to open the file!");

            puts("Try again!(Y/N)?");

            scanf("%c",&choice);

        else

            fp=fopen("stopedcar.txt",mode);

    while(choice=='y'||choice=='Y');

    if(choice=='n'||choice=='N'){}

        //exit(1); //異常退出  exit(0);系統正常退出

    return(fp);

//從檔案中讀取一條記錄

void Readfile(int i,FILE *fp)

    fscanf(fp,"%20s",&stopedcar[i].carNum);

    fscanf(fp,"%20s",&stopedcar[i].carshap);

    fscanf(fp,"%5d",&stopedcar[i].areaNum);

    fscanf(fp,"%30s",&stopedcar[i].stopedTime);

//顯示列印一條泊車資訊

void Showonecarinfo(int i)

    printf("%10s   %10s   %1d  %30s \n",stopedcar[i].carNum,stopedcar[i].carshap,stopedcar[i].areaNum,stopedcar[i].stopedTime);

//顯示列印所有的泊車資訊

void ShowAllCarInfo()/*顯示列印所有的資訊 */

    int i;

    printf("有%d個記錄:\n",total);

    PrintTitle();

    for(i=0;i<total;i++)

        Showonecarinfo(i);

//檢查車位号是否為空,存在傳回序号,如果該車位沒有停車則傳回-1 

int CheckNumber(int areaNum_temp)

    int i,result=0;

        if(stopedcar[i].areaNum==areaNum_temp)

            result=1;

            break;

    if(result==1)

        return i;

        return -1;

//根據車位号增加一條記錄

int Inputonecar(int i)

    int areaNum;

        printf("輸入車位号:(如1)");

        scanf("%d",&areaNum);

        if(areaNum<0)

            printf("error!\n");

        if (areaNum>MAX) {

            printf("沒有大于%d的車位\n",MAX);

    while(areaNum<0);

    //該車位有停車

    if(CheckNumber(areaNum)>0)

        printf("該車位已經停了車!\n");

        return 0;

    //如果該車位沒有停車

        stopedcar[i].areaNum=areaNum; 

//将停車的車位号記錄為目前的車位号

        printf("Input

車牌号(less than 20 chars):");

        scanf("%s",stopedcar[i].carNum);

        printf("車型号(例如:baomaX6):");

        scanf("%s",&stopedcar[i].carshap);

        char *time=GetTime();//擷取目前系統時間

        stopedcar[i].h=timeh();//記錄小時

        stopedcar[i].m=timem();//記錄分鐘

        char *q=stopedcar[i].stopedTime;

        strcpy(q, time);//将目前時間指派給car.StopedTime

        PrintTitle();

        //Inputfile(i,fp);

        return 1;

//把下标為i 的記錄寫入檔案

void Inputfile(int i,FILE *fp) 

//向管理系統中增加記錄

void Input(FILE *fp)

    i=total;

    if(Inputonecar(i))

        total++;

        Inputfile(i,fp);

//設定泊車單價

int SetPrice()

    printf("請設定泊車單價:(例如:1¥for 1 m)\n");

    scanf("%d",&price);

    printf("您設定的單價是:%d¥1M\n",price);

    return price;

int Cost2(int h,int m,int danjia)

    int money;

    //擷取目前小時和分鐘

    int hnow=timeh();

    int mnow=timem();

    return money=((hnow-h)*60+(mnow-m))*danjia;

//根據單價和時間差來計算停車消費的價格

int Cost(char *time1,int danjia)

    char forein[2];

    char now[2];

    int minu;

    int sec;

    int t1,t2;

    for (int i=14; i<=15; i++,time1++) {

        forein[i-14]=time1[i];

    minu=(forein[0]-48)*10+(forein[1]-48);

    for (int i=17; i<=18; i++,time1++) {

        forein[i-17]=time1[i];

    sec=(forein[0]-48)*10+(forein[1]-48);

    t1=minu*60+sec;

    char *p=GetTime();

        forein[i-14]=p[i];

        forein[i-17]=p[i];

    t2=minu*60+sec;

    money=(t2-t1)*danjia;

    return money;

//根據車位号取車

void GetCarByAreaNum2(FILE *fp)

    int i,j,k,no_temp,choice2;

    printf("輸入要取車的車位号:(例如1)");

    scanf("%d",&no_temp);

    i=CheckNumber(no_temp);

    if(i>=0)

        Showonecarinfo(i);//根據車位号來查詢是第幾個

        printf("确認取車?(1.是/2.否)");

        scanf("%d",&choice2);

        if(choice2==1)

//            char *time1=stopedcar[i].stopedTime;

//            printf(time1);

            int h=stopedcar[i].h;

            int m=stopedcar[i].m;

            printf("您需要支付¥%d的停車費\n",Cost2(h,m,price));

            for (j=i; j<total; j++) {

                strcpy(stopedcar[j].carNum,

stopedcar[j+1].carNum);

                strcpy(stopedcar[j].carshap,

stopedcar[j+1].carshap);

            }

            total--;

            for (k=0; k<total; k++) {

                Inputfile(k, fp);

        printf("該車位上沒有停車\n");

void GetCarByAreaNum(FILE *fp)

            char *time1=stopedcar[i].stopedTime;

            printf(time1);

            printf("您需要支付¥%d的停車費\n",Cost(time1,price));

                strcpy(stopedcar[j].carNum, stopedcar[j+1].carNum);

                strcpy(stopedcar[j].carshap, stopedcar[j+1].carshap);

//設定密碼

void set_psw()

    char psw_set[20],psw_check[20],c;

    unsigned int i;

    {   printf("You must set password first!\n");

        printf("Enter password:(less than 12 numbers and key'.'for end)\n");

        for(i=0;(c=getchar())!='.';i++)

            //putchar('*');  

            psw_set[i]=c;

        psw_set[i]='\0';

        printf("-----------------\n");

        printf("conform password:");

            //putchar('*');

            psw_check[i]=c;

        psw_check[i]='\0';

        //printf("------------\n");

        if(strcmp(psw_set,psw_check)==0)

        {printf("Set password success!\n");

            strcpy(password,psw_set);

    while(strcmp(psw_set,psw_check)!=0);

    //clear();

    fp=fopen("password.txt","wb");

    fprintf(fp,"%s",password);

//密碼驗證

int psw_check()

    unsigned int i=1,j=1;

    char pword[20],c;

    if((fp=fopen("password.txt","rb"))==NULL)

        fp=fopen("password.txt","a");

        set_psw();

    fscanf(fp,"%s",password);

        printf("\nInput password:key'.'for end(%d/three times)",j);

        for(j=0;(c=getchar())!='.';j++)

            pword[j]=c;

        pword[j]='\0';

        i++;

    while(strcmp(pword,password)!=0&&i<=3);

    if(i<=3)

        printf("You have tryed for three times,fail to open the file!\n");

int main(int argc,

const char * argv[])

    if(psw_check())

        Init();

    return 0;

繼續閱讀