1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
#include<iostream>
#include<iomanip> // setw(), setprecition()
#include<string>
#include<sstream> //istringstream
using namespace std;
//==========================================================================
//Function prototypes
//void timeCheck(int &hour, char &ch, int &min);
//bool corectTime(int &startHour, int &startMins, int &stopHour, int &stopMins);
double callCost(int &startHour, int &startMins, int &stopHour, int &stopMins);
int main()
{
//Tell user what this program is all about
cout << endl;
cout << " Calculate your call cost" << endl;
cout << " ------------------------" << endl;
//Get start and stop times.
string startTime;
cout << " Enter your stop and start times" << endl << endl;
cout << " Start time hh:mm or h:mm: ";
getline(cin, startTime);
//Extract the start time from the string
int hour1, mins1;
char ch1;
istringstream startTimeString(startTime);
startTimeString >> hour1 >> ch1 >> mins1;
cin.clear(); // clear the stream
cout << " Stop time hh:mm or h:mm: ";
string stopTime;
getline(cin, stopTime);
//Extract the stop time from the string
int hour2, mins2;
char ch2;
istringstream stopTimeString(stopTime);
stopTimeString >> hour2 >> ch2 >> mins2;
cin.clear(); // clear the stream
cout << fixed << setprecision(2);
cout << " Your call cost is: " << callCost(hour1, mins1, hour2, mins2) << endl;
return 0;
}
// fuction definitions
double callCost(int &startHour, int &startMins, int &stopHour, int &stopMins)
{
// i convert all times in mins by multiplying the hours by 60 and adding the minutes
int startTimeInMinutes = (startHour * 60) + startMins;
int stopTimeInMinutes = (stopHour * 60) + stopMins;
int callTime = stopTimeInMinutes - startTimeInMinutes;
const int pricePerMins = 4;
const double vat = 0.25; // VAT on total price
const double unconfortableDiscount = 0.65; // discount applied for calls that
// fall between 18:30 and 08:00
const double longCallDiscount = 0.15; // discount if call time > 30 mins
double normalPrice = callTime * pricePerMins;
double normalPricePlusVat = normalPrice * (1 + vat);
double lowPrice = callTime * (pricePerMins *(1-unconfortableDiscount)); // 65% discount applied for
// calls before 08:00 and after 18:30
double lowPricePlusVat = lowPrice * (1 + vat);
// Based on hour of the day, costs are different
// as mensioned about, all times are converted to mins to get accurate intervals.
// eg 18:30 = (18*60 + 30) = 1110 minutes
double price;
if (callTime <= 30) // apply nor mal rate
{
if (startTimeInMinutes >= 480 && stopTimeInMinutes <= 1110) // i.e 08:00 to 18:30
price = normalPricePlusVat; // normalPrice + normalPrice * 0.25;
if (startTimeInMinutes < 480 && stopTimeInMinutes > 1110) // before 08:00 and after 18:30
price = lowPricePlusVat;
}
if (callTime > 30) // Apply 15% discount to total price
{
if (startTimeInMinutes >= 480 && stopTimeInMinutes <= 1110)
price = normalPricePlusVat*0.85; // 15% of total price discount applied
// price - 0.15*price = price*0.85
if (startTimeInMinutes < 480 && stopTimeInMinutes > 1110)
price = lowPricePlusVat*0.85;
}
return price;
}
|