Sharing of my labs carried out during the TDDE18 course at Linköping University
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

select_and_rep.cc 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Lab 1 - TDDE18 - Selection and repetition
  3. Last modification : 2020-09-03
  4. */
  5. #include <iostream>
  6. #include <iomanip>
  7. /*
  8. 1-1 --> done
  9. Try to avoid comments on the same line as code
  10. --> done
  11. Statements should have it's own block declared underneath
  12. it so its easier to read.
  13. Ex if()
  14. {
  15. --> done
  16. use std:setw() and std::setfill(-) instead of a for loop to write
  17. --> done
  18. Use the give information to calculate the steps and then use a for loop
  19. instead of while() and adding the stride.
  20. Adding floats will eventually end up in errors because a float isn't precise
  21. which you avoid by using doubles, but worth noting.
  22. By calculating the amount of steps its guaranteed no errors will occur
  23. --> done: change the final while loop to a for loop
  24. */
  25. using namespace std;
  26. int main()
  27. {
  28. double first_price{};
  29. double last_price{};
  30. double stride{};
  31. double tax{};
  32. bool is_ok{false};
  33. // I/O begin
  34. cout << "INPUT PART" << endl;
  35. cout << "==========" << endl;
  36. // For each value, we check the consistency of the given value
  37. while (!is_ok)
  38. {
  39. cout << "Enter first price: ";
  40. cin >> first_price;
  41. if (first_price >= 0)
  42. {
  43. is_ok = true;
  44. }
  45. else
  46. {
  47. cout << "ERROR: First price must be at least 0 (zero) SEK" << endl;
  48. }
  49. }
  50. is_ok = false;
  51. while (!is_ok) {
  52. cout << "Enter last price: ";
  53. cin >> last_price;
  54. if (last_price >= first_price)
  55. {
  56. is_ok = true;
  57. }
  58. else
  59. {
  60. cout << "ERROR: The last price must be greater than or equal to the first price ("
  61. << first_price << " SEK)" << endl;
  62. }
  63. }
  64. is_ok = false;
  65. while (!is_ok) {
  66. cout << "Enter stride : ";
  67. cin >> stride;
  68. if (stride >= 0.01)
  69. {
  70. is_ok = true;
  71. }
  72. else
  73. {
  74. cout << "ERROR: Stride must be at least 0.01" << endl;
  75. }
  76. }
  77. is_ok = false;
  78. while (!is_ok) {
  79. cout << "Enter tax percent: ";
  80. cin >> tax;
  81. if (0 <= tax && tax <= 100)
  82. {
  83. is_ok = true;
  84. }
  85. else
  86. {
  87. cout << "ERROR: The tax value cannot be negative or greater than 100%" << endl;
  88. }
  89. }
  90. cout << endl;
  91. // Percent correction
  92. tax = tax * 0.01;
  93. cout << "TAX TABLE" << endl;
  94. cout << "=========" << endl;
  95. cout << setw(12) << "Price" << setw(12) << "Tax" << setw(20) << "Price with tax" << endl;
  96. cout << setfill('-') << setw(45) << " " << setfill(' ') << endl;
  97. // We calculate the necessary number of iteration
  98. int number_of_interation = static_cast <int> (last_price - first_price)/stride;
  99. double price = first_price;
  100. for (int i = 0; i <= number_of_interation; i++)
  101. {
  102. cout << fixed << setprecision(2) << setw(12)
  103. << price << setw(12)
  104. << price*tax << setw(20)
  105. << price + price*tax << endl;
  106. price += stride;
  107. }
  108. return 0;
  109. }