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;
? ? 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(">>>>>>>>>>>>>>>歡迎使用泊車管理系統<<<<<<<<<<<<<<<<\n");
? ? printf("************************************************\n");
? ? printf("* ? 1.新增停車信息 ? ? ? ? | ? 2.顯示所有停車信息 ? *\n");
? ? printf("* ? 3.按照車牌號取車 ? ? ? | ? 4.按照車位號取車 ? ? *\n");
? ? printf("* ? 5.按照車牌號查詢車信息? | ? 6.按照車位號查詢車信息*\n");
? ? printf("* ? 7.設置泊車單價 ? ? ? ? | ? 8.密碼設置 ? ? ? ? *\n");
? ? printf("* ? 9.主動清屏? ? ? ? ? ? | ? 0.退出 ? ? ? ? ? ? *\n");
? ? printf("************************************************\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();
? ? ? ? ? ? ? ? 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("*********************\n");
? ? ? ? ? ? ? ? printf(" ? ? 歡迎使用本系統 ? ? \n");
? ? ? ? ? ? ? ? printf("**********************\n");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? //exit(0);
? ? ? ? ? ? default:
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? }
}
//打印頭信息
void PrintTitle()
{
? ? printf("-------------------------------------------------------------\n");
? ? 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;
? ? 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 \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;
? ? 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!\n");
? ? ? ? if (areaNum>MAX) {
? ? ? ? ? ? printf("沒有大于%d的車位\n",MAX);
? ? ? ? }
? ? }
? ? while(areaNum<0);
? ? //該車位有停車
? ? if(CheckNumber(areaNum)>0)
? ? {
? ? ? ? printf("該車位已經停了車!\n");
? ? ? ? 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("請設置泊車單價:(例如: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)
{
? ? 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的停車費\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);
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? else
? ? ? ? printf("該車位上沒有停車\n");
}
//根據車位號取車
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的停車費\n",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("該車位上沒有停車\n");
}
//設置密碼
void set_psw()
{
? ? char psw_set[20],psw_check[20],c;
? ? unsigned int i;
? ? FILE *fp;
? ? do
? ? { ? 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:");
? ? ? ? for(i=0;(c=getchar())!='.';i++)
? ? ? ? {
? ? ? ? ? ? //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);
? ? ? ? }
? ? ? ? else
? ? ? ? ? ? printf("error!\n");
? ? }
? ? 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("\nInput password:key'.'for end(%d/three times)",j);
? ? ? ? for(j=0;(c=getchar())!='.';j++)
? ? ? ? {
? ? ? ? ? ? //putchar('*');
? ? ? ? ? ? pword[j]=c;
? ? ? ? }
? ? ? ? pword[j]='\0';
? ? ? ? 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!\n");
? ? ? ? return 0;
? ? }
?? ?
}
int main(int argc, const char * argv[])
{
?? ?
? ? if(psw_check())
? ? {
? ? ? ? Init();
? ? ? ? //clear();
? ? ? ? menu();
? ? }
? ? return 0;
}
轉載于:https://blog.51cto.com/dingxiaowei/1366853
總結
- 上一篇: windows 远程登录用户管理
- 下一篇: get path