[C++]第四次作业:引入类求解下列桌球城计费问题

// 程序仅在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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值