Advertisement
fabbe680

4.2

Jan 25th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. // Laboration 4, Assignment_2.cpp
  2. // Fabian Tjernström (fatj1700) 2018-12-09
  3.  
  4. #include <iostream>
  5. #include <iomanip>
  6. #include <string>
  7. #include <cmath>
  8. #include <sstream>
  9.  
  10. using namespace std;
  11.  
  12. int main() {
  13.  
  14. const double tax = 4;
  15. const double vat = 0.25;
  16. const double disc1 = 0.35;
  17. const double disc2 = 0.85;
  18. const double hStart = 8;
  19. const double hEnd = 18.5;
  20. double priceTotal, mTotal, mFullTax=0, mNoTax;
  21. double h1, m1, h2, m2, h1m1, h2m2;
  22. bool repeat = true;
  23.  
  24. //Input
  25.  
  26. while(repeat == true) {
  27.  
  28. string input1, input2;
  29.  
  30. cout << "Enter call start time [hh:mm]: ";
  31. cin >> input1;
  32.  
  33. int pos1 = input1.find(":");
  34.  
  35. if (pos1 < 1 || pos1 > 2) {
  36. cout << "Invalid Input!" << endl;
  37. return 0;
  38. }
  39.  
  40. input1.replace(pos1, 1, " ");
  41.  
  42. istringstream iss(input1);
  43. iss >> h1 >> m1;
  44. iss.clear();
  45.  
  46. cout << "Enter call end time [hh:mm]: ";
  47. cin >> input2;
  48.  
  49. int pos2 = input2.find(":");
  50.  
  51. if (pos2 < 1 || pos2 > 2) {
  52. cout << "Invalid Input!" << endl;
  53. return 0;
  54. }
  55.  
  56. input2.replace(pos2, 1, " ");
  57.  
  58. iss.str(input2);
  59. iss >> h2 >> m2;
  60. iss.clear();
  61.  
  62. if (m1 > 60 || m2 > 60 || m1 < 0 || m2 < 0) {
  63. cout << "Invalid Input!" << endl;
  64. return 0;
  65. }
  66.  
  67. if (h1 > 24 || h2 > 24 || h1 < 0 || h2 < 0) {
  68. cout << "Invalid Input!" << endl;
  69. return 0;
  70. }
  71.  
  72. //Calculate
  73.  
  74. h1m1 = (h1 + (m1 / 60));
  75. h1m1 = (ceil(h1m1 * 100) / 100);
  76. h2m2 = (h2 + (m2 / 60));
  77. h2m2 = (ceil(h2m2 * 100) / 100);
  78.  
  79. for (double i = h1m1; i <= h2m2; i += 0.01) {
  80. if (i > hStart && i < hEnd) {
  81. mFullTax++;
  82. }
  83. }
  84.  
  85. mFullTax = ((mFullTax * 60) / 100);
  86. mTotal = (((h2 - h1) * 60) + (m2 - m1));
  87. mNoTax = (mTotal - mFullTax);
  88.  
  89. priceTotal = ((mFullTax * tax) + ((mNoTax * tax) * disc1));
  90.  
  91. if (mTotal > 30) {
  92. priceTotal *= disc2;
  93. }
  94.  
  95. priceTotal = (priceTotal + (priceTotal * vat));
  96.  
  97. //Output
  98.  
  99. cout << "Start: ";
  100. cout << h1 << ":" << m1 << endl;
  101. cout << "End: ";
  102. cout << h2 << ":" << m2 << endl;
  103. cout << "Cost: ";
  104. cout << fixed << setprecision(2) << priceTotal << " kr" << endl;
  105. cout << endl;
  106.  
  107. //Repeat Program
  108.  
  109. cout << "Repeat Program? [y/n]: ";
  110. char answer;
  111. cin >> answer;
  112. repeat = answer == 'y';
  113. }
  114.  
  115. return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement