zoukankan      html  css  js  c++  java
  • 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;//默认泊车单价为11M


    /***************函数申明******************************************/

    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;

        time_t   now;

        struct   tm     *timenow;

        time(&now);

        timenow   =   localtime(&now);

        char *p = asctime(timenow);

        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(">>>>>>>>>>>>>>>欢迎使用泊车管理系统<<<<<<<<<<<<<<<< ");

        printf("************************************************ ");

        printf("*   1.新增停车信息         |   2.显示所有停车信息   * ");

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

        printf("*   5.按照车牌号查询车信息  |   6.按照车位号查询车信息* ");

        printf("*   7.设置泊车单价         |   8.密码设置         * ");

        printf("*   9.主动清屏            |   0.退出             * ");

        printf("************************************************ ");

    }

    //清屏,有点问题

    //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();

                    break;

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

                    break;

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

                    GetCarByAreaNum2(file_operate("wb"));

                    break;

                case 5://按照车牌号查询车位信息

                    break;

                case 6: //按照车位号查询车位信息

                    break;

                case 7://设置停车价格(例如:1for1s

                    SetPrice();

                    break;

                 case 8://密码设置

                    set_psw();

                    break;

                case 9://手动清屏

                    //clear();

                    printf("手动清屏有误!");

                    break;

                case 0://结束程序

                    //system("cls");

                    printf("********************* ");

                    printf("     欢迎使用本系统     ");

                    printf("********************** ");

                    break;

                    //exit(0);

                default:

                    break;

            }

        }

    }

    //打印头信息

    void PrintTitle()

    {

        printf("------------------------------------------------------------- ");

        printf("   车牌号          车型   停泊车位号          停泊开始时间   ");

        printf("------------------------------------------------------------- ");

    }



    //初始化,文件不存在,则建立,同时记录下文件中的记录数

    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;

        FILE *fp;

        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 ",stopedcar[i].carNum,stopedcar[i].carshap,stopedcar[i].areaNum,stopedcar[i].stopedTime);

    }

    //显示打印所有的泊车信息

    void ShowAllCarInfo()/*显示打印所有的信息 */

    {

        int i;

        printf("%d个记录: ",total);

        PrintTitle();

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

            Showonecarinfo(i);

    }

    //检查车位号是否为空,存在返回序号,如果该车位没有停车则返回-1 

    int CheckNumber(int areaNum_temp)

    {

        int i,result=0;

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

        {

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

            {

                result=1;

                break;

            }

        }

        if(result==1)

            return i;

        else

            return -1;

    }

    //根据车位号增加一条记录

    int Inputonecar(int i)

    {


        int areaNum;

        do

        {

            printf("输入车位号:(1)");

            scanf("%d",&areaNum);

            if(areaNum<0)

                printf("error! ");

            if (areaNum>MAX) {

                printf("没有大于%d的车位 ",MAX);

            }

        }

        while(areaNum<0);

        //该车位有停车

        if(CheckNumber(areaNum)>0)

        {

            printf("该车位已经停了车! ");

            return 0;

        }

        //如果该车位没有停车

        else

        {

            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();

            Showonecarinfo(i);

            //Inputfile(i,fp);

            return 1;

        }

    }

    //把下标为i 的记录写入文件

    void Inputfile(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 Input(FILE *fp)

    {

        int i;

        i=total;

        if(Inputonecar(i))

        {

            total++;

            Inputfile(i,fp);

        }

        fclose(fp);

    }


    //设置泊车单价

    int SetPrice()

    {

        printf("请设置泊车单价:(例如:1for 1 m ");

        scanf("%d",&price);

        printf("您设置的单价是:%d1M ",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)

    {

        int money;

        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();

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

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

        }

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

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

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

        }

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

        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)

        {

            PrintTitle();

            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的停车费 ",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);

                }

            }

        }

        else

            printf("该车位上没有停车 ");

    }

    //根据车位号取车

    void GetCarByAreaNum(FILE *fp)

    {

        int i,j,k,no_temp,choice2;

        printf("输入要取车的车位号:(例如1)");

        scanf("%d",&no_temp);

        i=CheckNumber(no_temp);

        if(i>=0)

        {

            PrintTitle();

            Showonecarinfo(i);//根据车位号来查询是第几个

            printf("确认取车?(1./2.否)");

            scanf("%d",&choice2);

            if(choice2==1)

            {

                char *time1=stopedcar[i].stopedTime;

                printf(time1);

                printf("您需要支付¥%d的停车费 ",Cost(time1,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);

                }

            }

        }

        else

            printf("该车位上没有停车 ");

    }


    //设置密码

    void set_psw()

    {

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

        unsigned int i;

        FILE *fp;

        do

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

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

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

            {

                //putchar('*');  

                psw_set[i]=c;

            }

            psw_set[i]='';

            printf("----------------- ");

            printf("conform password:");

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

            {

                //putchar('*');

                psw_check[i]=c;

            }

            psw_check[i]='';

            //printf("------------ ");

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

            {printf("Set password success! ");

                strcpy(password,psw_set);

            }

            else

                printf("error! ");

        }

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

        //clear();

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

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

        fclose(fp);

    }

    //密码验证

    int psw_check()

    {

        unsigned int i=1,j=1;

        FILE *fp;

        char pword[20],c;

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

        {

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

            set_psw();

        }

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

        fclose(fp);

        do

        {

            printf(" Input passwordkey'.'for end(%d/three times)",j);

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

            {

                //putchar('*');

                pword[j]=c;

            }

            pword[j]='';

            i++;

        }

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

        if(i<=3)

            return 1;

        else

        {

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

            return 0;

        }

        

    }



    int main(int argc, const char * argv[])

    {

        

        if(psw_check())

        {

            Init();

            //clear();

            menu();

        }

        return 0;

    }


  • 相关阅读:
    ubuntu下解决无法解析或打开软件包列表或状态文件的问题
    linux 解除文件root权限限制
    查看linux设备信息的命令
    R系安装rpm包
    重启窗口管理器
    内存泄漏如何定位?
    双屏显示,HDMI可以正常显示,lvds不显示
    避免linux下log在/var/log/messages 中重复输出的办法
    debian编包成功后,想要修改的文件的内容没有变化
    linux terminal 显示不全 将log内容打印出来
  • 原文地址:https://www.cnblogs.com/java20130723/p/3211445.html
Copyright © 2011-2022 走看看