123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- /*
- Lab 1 - TDDE18 - Selection and repetition
- Last modification : 2020-09-03
- */
-
- #include <iostream>
- #include <iomanip>
-
- /*
- 1-1 --> done
-
- Try to avoid comments on the same line as code
- --> done
-
- Statements should have it's own block declared underneath
- it so its easier to read.
- Ex if()
- {
- --> done
-
- use std:setw() and std::setfill(-) instead of a for loop to write
- --> done
-
- Use the give information to calculate the steps and then use a for loop
- instead of while() and adding the stride.
- Adding floats will eventually end up in errors because a float isn't precise
- which you avoid by using doubles, but worth noting.
- By calculating the amount of steps its guaranteed no errors will occur
- --> done: change the final while loop to a for loop
- */
-
- using namespace std;
-
- int main()
- {
- double first_price{};
- double last_price{};
- double stride{};
- double tax{};
- bool is_ok{false};
-
- // I/O begin
- cout << "INPUT PART" << endl;
- cout << "==========" << endl;
-
- // For each value, we check the consistency of the given value
- while (!is_ok)
- {
- cout << "Enter first price: ";
- cin >> first_price;
-
- if (first_price >= 0)
- {
- is_ok = true;
- }
- else
- {
- cout << "ERROR: First price must be at least 0 (zero) SEK" << endl;
- }
- }
-
- is_ok = false;
- while (!is_ok) {
- cout << "Enter last price: ";
- cin >> last_price;
-
- if (last_price >= first_price)
- {
- is_ok = true;
- }
- else
- {
- cout << "ERROR: The last price must be greater than or equal to the first price ("
- << first_price << " SEK)" << endl;
- }
- }
-
- is_ok = false;
- while (!is_ok) {
- cout << "Enter stride : ";
- cin >> stride;
-
- if (stride >= 0.01)
- {
- is_ok = true;
- }
- else
- {
- cout << "ERROR: Stride must be at least 0.01" << endl;
- }
- }
-
- is_ok = false;
- while (!is_ok) {
- cout << "Enter tax percent: ";
- cin >> tax;
-
- if (0 <= tax && tax <= 100)
- {
- is_ok = true;
- }
- else
- {
- cout << "ERROR: The tax value cannot be negative or greater than 100%" << endl;
- }
- }
- cout << endl;
-
- // Percent correction
- tax = tax * 0.01;
-
- cout << "TAX TABLE" << endl;
- cout << "=========" << endl;
- cout << setw(12) << "Price" << setw(12) << "Tax" << setw(20) << "Price with tax" << endl;
- cout << setfill('-') << setw(45) << " " << setfill(' ') << endl;
-
- // We calculate the necessary number of iteration
- int number_of_interation = static_cast <int> (last_price - first_price)/stride;
-
- double price = first_price;
- for (int i = 0; i <= number_of_interation; i++)
- {
- cout << fixed << setprecision(2) << setw(12)
- << price << setw(12)
- << price*tax << setw(20)
- << price + price*tax << endl;
- price += stride;
- }
-
- return 0;
- }
|