C++应用——停车场统计系统

一、问题分析

    创建一个系统用于停车场的车辆统计;车辆分为两种(小车和中大型车),每进来一辆车就需要记录它的车牌、停车时间还有类型。当汽车离开时,应检索车牌,打印出进来时录入的信息。并且系统还具备时刻记录停车场的剩余车位数量。

二、设计实现

    创建类CPark

    定义数据成员(string C_num;int C_type; string Intime,Outtime; int Times; double C_money; intS_seat,MB_seat;)

    成员函数(CPark(ints_seat=0,int mb_seat=0); void OccupyOne(); void Pullout(); void show();)

三、核心控制设计

    成员函数OccupyOne();Pullout();是重要环节。录入车辆需要建立文件,进行信息录入;车辆驶出的检索功能要求信息录入的格式很高。对文件进行检索操作就是操作字符串,使用了getline();函数大大简化操作。

四、编码与测试

    a、代码如下:

#include<iostream>
#include<string>
#include<fstream>
using namespace std;

class CPark{
    string C_num;
    int C_type;
    string Intime,Outtime;
    int Times;
    double C_money;
    int S_seat,MB_seat;
public:
    CPark(int s_seat=0,int mb_seat=0);
    void OccupyOne();
    void Pullout();
    void show();
};

CPark::CPark(int s_seat,int mb_seat){
        S_seat=s_seat;
        MB_seat=mb_seat;
    }

void CPark::OccupyOne(){
        int type=0;
        cout<<"Please input type(1 or 2):";
        cin>>type;
        switch (type){
        case 1:
            if(S_seat>0){
                S_seat--;
                C_type=type;
                cout<<"Input the car's information:"<<endl;
                cin>>C_num>>Intime;
            }
            else{
                type=0;
                cout<<"Don't have a seat!\n";
            }
           break;
        case 2:
            if(MB_seat>0){
                MB_seat--;
                C_type=type;
                cout<<"Input Twocar's information:"<<endl;
                cin>>C_num>>Intime;
            }
            else{
                type=0;
                cout<<"Don't have a seat!\n";
            }
           break;
        default:
            cout<<"Don't have the type!\n";
            break;
                    }
        if(type==1||type==2){
            ofstream f1("CarInfor.dat",ios::app);
            f1<<C_num<<" "<<C_type<<" "<<Intime<<endl;
            f1.close();
            cout<<"Information has been saved!\n";
            show();cout<<endl;
        }
    }

 void CPark::Pullout(){
    string c_num1,c_num2,c_num3="ab1234";
    cout<<"Input CarNumber :";
    cin>>c_num1;
    ifstream f2("CarInfor.dat",ios::in);
    while(getline(f2,c_num2)){
        for(int i=0;i<6;i++){
            c_num3[i]=c_num2[i];
        }
        if(c_num3==c_num1){
            cout<<"\nCarNumber: "<<c_num3<<"  Type: "<<c_num2[7]<<endl;
            cout<<"Come in time: \n"<<c_num2[9]<<c_num2[10]<<c_num2[11]<<c_num2[12];
            cout<<"-"<<c_num2[13]<<c_num2[14]<<"-"<<c_num2[15]<<c_num2[16]<<" ";
            cout<<c_num2[17]<<c_num2[18]<<":"<<c_num2[19]<<c_num2[20]<<endl;
            if(c_num2[7]=='1')S_seat++;
            else MB_seat++;
        }
    }
    f2.close();
    show();cout<<endl;
 }

 void CPark::show(){
    cout<<"     CarSeats!\n";
    cout<<"Type1: "<<S_seat<<"  Type2: "<<MB_seat<<endl;
 }

int main()
{
    CPark park1(200,100);
    int op1;
    cout<<"     (1)Come in a car\n     (2)Go out a car\n     (0)Exit!!!\nInput a operation:";
    cin>>op1;
    while(op1){
        switch(op1){
            case 1:
                park1.OccupyOne();
                break;
            case 2:
                park1.Pullout();
                break;
            default:
                cout<<"     Don't have the command!\n";
                break;
            }
        cout<<"Continue!!\n  (1)Come in a car  (2)Go out a car  (0)Exit!!!\nInput a operation:";
        cin>>op1;
    }

   return 0;
}

    b、测试结果:

五、总结

     因为本人的C++学习还比较浅显,所以停车场系统并没有全面深入地实现(例如,计时计费功能);仅以此博客初次体会面向对象程序设计的一般方法及流程。若各位前辈对此博客有意见或建议,请不吝指出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值