c++的高速公收费源码
原文地址:爱游巴士
程序功能:
在某高速公路出口收费处,对三种类型的车辆计费,大型车每公里0.5元,中型车每公里0.4元,小型车每公里0.3元,来车验条,根据行驶公里数即得应收款的金额。在交班时要统计出总数。
这里简单的整理了一个ExpressManager类,具体代码如下:
ExpressManager.h
1 #ifndef __EXPRESSMANAGER_H__ 2 #define __EXPRESSMANAGER_H__ 3 4 #include <memory> 5 6 enum e_CarType 7 { 8 E_CARTYPE_S, 9 E_CARTYPE_M, 10 E_CARTYPE_B, 11 E_CARTYPE_SIZE, 12 }; 13 14 struct s_Car 15 { 16 float passCharge; 17 float trip ; 18 enum e_CarType type; 19 20 }; 21 22 static float CHARGE_DEF[E_CARTYPE_SIZE]={0.3F,0.4F,0.5F}; 23 const int NAME_LEN=16; 24 25 class ExpressManager 26 { 27 28 public: 29 30 explicit ExpressManager(const char* name="Express"):_passCharge(0.0f),_tempCharge(0.0f) 31 { 32 memcpy( _name,name,NAME_LEN ); 33 } 34 35 ~ExpressManager(void){} 36 37 // pass the express manager 38 // @ car the pass car 39 // @ passCharge the charge for passing 40 float Pass( const struct s_Car* car,float passCharge,float& rCharge ); 41 42 // show the charge for passing 43 // @ car the pass car 44 float ShowCharge( const struct s_Car* car ) const ; 45 46 47 float ShowTotal( ) const 48 { 49 printf("\t Total is:%0.2f\n",_passCharge); 50 return _passCharge; 51 } 52 53 void PassCar(); 54 55 void PassTest(); 56 57 private: 58 59 // caculate the charge for passing 60 // @ car the parss car 61 // @ return !0 ,succss to get the value; -1 ,bad data 62 inline float Caculate( const struct s_Car* car ) const 63 { 64 return car->trip*CHARGE_DEF[car->type]; 65 } 66 67 float _passCharge; 68 float _tempCharge; 69 char _name[NAME_LEN]; 70 }; 71 72 #endif
ExpressManager.cpp
#include "stdafx.h" #include "ExpressManager.h" // pass the express manager // @ car the pass car // @ passCharge the charge for passing float ExpressManager::Pass( const struct s_Car* car,float passCharge,float& rCharge ) { if( car !=NULL ) { float need = Caculate( car ); _tempCharge+=passCharge; if( _tempCharge >= need ) { _passCharge += need; rCharge = _tempCharge - need; _tempCharge = 0; return true; } else { printf( "Fail to pass ! less %0.2f\n",need-_tempCharge ); return false; } } else { printf("Fail to pass Null data!\n"); } return false; } // show the charge for passing // @ car the pass car float ExpressManager::ShowCharge( const struct s_Car* car ) const { if( car != NULL ) { return Caculate( car ); } else { printf("Fail to show Null data!\n"); return 0.0f; } } void ExpressManager::PassCar() { struct s_Car temp={0}; float pCharge=0.0f; float rCharge=0.0f; printf("plz,input car info! e. type trip\n"); if( scanf_s("%d %f",&temp.type,&temp.trip )!= EOF ) { temp.passCharge = ShowCharge( &temp ); printf("plz pay %0.2f for passing\n",temp.passCharge ); while(true) { if( scanf_s( "%f",&pCharge ) != EOF ) { if( Pass( &temp,pCharge,rCharge ) ) { if( rCharge==0.0f) { printf("congratulation! see you next time!\n"); } else { printf("congratulation! payback you %0.2f see you next time!\n",rCharge); } break; } } } } } void ExpressManager::PassTest() { int cmd = 0; while(true) { printf("welcome to %s\n",_name); printf("\t'p' - pass the car!\n"); printf("\t's' - show the total charge!\n"); printf("\t'q' - Exit!\n"); fflush(stdin); if( (cmd = getchar()) !=EOF ) { if( cmd == 'q' ) break; else if( cmd == 's' ) { ShowTotal(); } else if( cmd == 'p' ) { PassCar(); } } } }
测试代码
1 #include "ExpressManager.h" 2 #include <stdio.h> 3 4 int main(int argc, char* argv[]) 5 { 6 ExpressManager *Express1=new ExpressManager("G2023"); 7 8 Express1->PassTest(); 9 10 return 0; 11 }