Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Laboration 4, Assignment_2.cpp
- // Fabian Tjernström (fatj1700) 2018-12-09
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <cmath>
- #include <sstream>
- using namespace std;
- int main() {
- const double tax = 4;
- const double vat = 0.25;
- const double disc1 = 0.35;
- const double disc2 = 0.85;
- const double hStart = 8;
- const double hEnd = 18.5;
- double priceTotal, mTotal, mFullTax=0, mNoTax;
- double h1, m1, h2, m2, h1m1, h2m2;
- bool repeat = true;
- //Input
- while(repeat == true) {
- string input1, input2;
- cout << "Enter call start time [hh:mm]: ";
- cin >> input1;
- int pos1 = input1.find(":");
- if (pos1 < 1 || pos1 > 2) {
- cout << "Invalid Input!" << endl;
- return 0;
- }
- input1.replace(pos1, 1, " ");
- istringstream iss(input1);
- iss >> h1 >> m1;
- iss.clear();
- cout << "Enter call end time [hh:mm]: ";
- cin >> input2;
- int pos2 = input2.find(":");
- if (pos2 < 1 || pos2 > 2) {
- cout << "Invalid Input!" << endl;
- return 0;
- }
- input2.replace(pos2, 1, " ");
- iss.str(input2);
- iss >> h2 >> m2;
- iss.clear();
- if (m1 > 60 || m2 > 60 || m1 < 0 || m2 < 0) {
- cout << "Invalid Input!" << endl;
- return 0;
- }
- if (h1 > 24 || h2 > 24 || h1 < 0 || h2 < 0) {
- cout << "Invalid Input!" << endl;
- return 0;
- }
- //Calculate
- h1m1 = (h1 + (m1 / 60));
- h1m1 = (ceil(h1m1 * 100) / 100);
- h2m2 = (h2 + (m2 / 60));
- h2m2 = (ceil(h2m2 * 100) / 100);
- for (double i = h1m1; i <= h2m2; i += 0.01) {
- if (i > hStart && i < hEnd) {
- mFullTax++;
- }
- }
- mFullTax = ((mFullTax * 60) / 100);
- mTotal = (((h2 - h1) * 60) + (m2 - m1));
- mNoTax = (mTotal - mFullTax);
- priceTotal = ((mFullTax * tax) + ((mNoTax * tax) * disc1));
- if (mTotal > 30) {
- priceTotal *= disc2;
- }
- priceTotal = (priceTotal + (priceTotal * vat));
- //Output
- cout << "Start: ";
- cout << h1 << ":" << m1 << endl;
- cout << "End: ";
- cout << h2 << ":" << m2 << endl;
- cout << "Cost: ";
- cout << fixed << setprecision(2) << priceTotal << " kr" << endl;
- cout << endl;
- //Repeat Program
- cout << "Repeat Program? [y/n]: ";
- char answer;
- cin >> answer;
- repeat = answer == 'y';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement