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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
double convert(string start, string end);
double priceCalc(int startHour, int startMinute, int endHour, int endMinute);
bool errorCheck(int startHour, int startMinute, int endHour, int endMinute);
int main()
{
char ch;
do
{
string callStart,callEnd;
cout<<"What time the call start? (hh:mm): ";
getline(cin, callStart);
cout<<"What time did the call end? (hh:mm): ";
getline(cin, callEnd);
cout<<convert(callStart,callEnd)<<" $" << endl;
cout << "Run again(y/n)? ";
cin >> ch; cin.ignore(250,'\n');
do
{
ch = toupper(ch);
}while( !(ch == 'Y' || ch == 'N'));
}while(ch == 'Y');
return 0;
}
double convert(string start, string end)
{
int startHour = 0, startMinute = 0, endHour = 0, endMinute = 0;
string::size_type startPos = start.find(":");
if(startPos != string::npos)
;
else
{
cout << "Only : is allowed" << endl;
return 0;
}
string::size_type endPos = end.find(":");
if(endPos != string::npos)
;
else
{
cout << "Only : is allowed" << endl;
return 0;
}
start.replace(start.find(":"), 1, " ");
end.replace(end.find(":"), 1, " ");
istringstream iss(start);
iss >> startHour >> startMinute;
iss.clear();
iss.str(end);
iss >> endHour >> endMinute;
if (errorCheck(startHour,startMinute,endHour,endMinute))
return 0;
return priceCalc(startHour,startMinute,endHour,endMinute);
}
bool errorCheck(int startHour, int startMinute, int endHour, int endMinute)
{
if (((startHour * 60) + startMinute) >= ((endHour * 60) + endMinute))
{
cout << "Time error ";
return true;
}
if (startHour>23||startHour<00||endHour>23||endHour<00||startMinute>59||startMinute<00||endMinute>59||endMinute<00)
{
cout << "Time error ";
return true;
}
else
return false;
}
double priceCalc(int startHour, int startMinute, int endHour, int endMinute)
{
int startinMiuntes = startHour * 60 + startMinute;
int endinMinutes = endHour * 60 + endMinute;
int totMin = 0;
int const pricePerMin=4;
double price;
const double VAT=1.25;
const double disconut=0.35;
const double discount30min=0.85;
totMin=endinMinutes-startinMiuntes;
if((startHour>=8 && startMinute>=0) && (endHour<=18 && endMinute<=30))
price=totMin*pricePerMin*VAT;
else
price=(pricePerMin*disconut)*totMin*VAT;
if(totMin>30)
{
price=price*discount30min;
}
return price;
}
|