// 程序仅在VS7.0下通过。
/*
1. 引入类求解下列桌球城计费问题:
某桌球城营业时间为9:00到23:00,
每张桌子收费标准为9:00到18:00或21:00到23:00为0.45元/分钟、
18:00到21:00为0.60元/分钟。
请编写一个C++程序,输入顾客占用一张桌子的起、止时间(精确到分钟),
输出计费结果。
*/
#include <iostream>
#include <map>
using namespace std;
const double default_price = 0; // 设定价格的默认值
typedef map < int ,double> intMap;
class fee{
public:
inline void addprice (int,int,double);
void init_price();
double customer_pay(int,int);
private:
intMap _price;
};
// 增加收费标准
inline void fee::addprice (int time_s, int time_e, double price)
{
for ( ; time_s < time_e ; time_s++ )
_price[time_s] = price;
}
// 初始化收费标准
void fee::init_price()
{
for ( int i=0; i<= 23 ; i++ )
if (_price[i]==0)
_price[i] = default_price;
}
// 客户需要支付款额
double fee::customer_pay(int time_begin, int time_end)
{
double _customer_pay = 0;
int begin = time_begin/100 ;
int end = time_end/100;
// 减去多加的时间
_customer_pay -= (time_begin - begin*100) * _price[begin];
for ( ; begin < end ; begin++ )
_customer_pay += _price[begin]*60;
// 加上少加的时间
_customer_pay += (time_end - end*100 ) * _price[end];
return _customer_pay;
}
void main()
{
fee business;
// 9:00到18:00为0.45元/分钟
business.addprice (9,18,0.45);
// 21:00到23:00为0.45元/分钟
business.addprice (21,23,0.45);
// 18:00到21:00为0.60元/分钟
business.addprice (18,21,0.6);
business.init_price();
//以下测试数据
// 9点10分 到 12点30分 ,应该是90元
cout << business.customer_pay (910,1230) << endl;
// 20点30分 到 21点40分,应该是36元
cout << business.customer_pay (2030,2140) << endl;
// 16点30分 到 22点30分,应该是189元
cout << business.customer_pay (1630,2230) << endl;
// 20点分50 到 22点59分,应该是59.55元
cout << business.customer_pay (2050,2259) << endl;
// 9点 到 23点,应该是405元
cout << business.customer_pay (900,2300) << endl;
}
// 清翔兔
// 05/11/02