Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import <iostream>
- #import <cmath>
- #define COLUMN_SEPARATOR "t| "
- #define MAX_TEMP 500
- #define MIN_TEMP -500
- inline bool between(double x, double max, double min) {
- return max >= x && min <= x;
- }
- void getInput(double &lower, double &upper, double &step) {
- double temp1, temp2;
- std::cout << "Please enter consecutively the upper and lower limits, both between " << MIN_TEMP << " and " << MAX_TEMP << "." << std::endl;
- std::cin >> temp1;
- std::cin >> temp2;
- while (!between(temp1, MAX_TEMP, MIN_TEMP) || !between(temp2, MAX_TEMP, MIN_TEMP)) {
- std::cout << "At least one of the temperatures is out of bounds. Please reenter:" << std::endl;
- std::cin >> temp1;
- std::cin >> temp2;
- }
- upper = std::max(temp1, temp2);
- lower = std::min(temp1, temp2);
- std::cout << "Please enter a positive stepsize, smaller than the difference between the limits." << std::endl;
- std::cin >> step;
- while (step < 0 || step > upper - lower) {
- std::cout << "The stepsize is out of bounds. Please reenter:" << std::endl;
- std::cin >> step;
- }
- }
- double toFahrenheit(double celsius) {
- return celsius*(9/5) + 32;
- }
- void printTable(double start, double end, double step) {
- std::cout << "Celsius" << COLUMN_SEPARATOR << "Fahrenheit" << std::endl;
- std::cout << "=======" << COLUMN_SEPARATOR << "==========" << std::endl;
- for (double i = start; i < end; i += step) {
- std::cout << i << COLUMN_SEPARATOR << toFahrenheit(i) << std::endl;
- }
- }
- int main() {
- double start, end, step;
- getInput(start, end, step);
- printTable(start, end, step);
- return 0;
- }
- 192:Challenges 11684$ ./a.out Please enter consecutively the upper and lower limits, both between -500 and 500.
- 3.692
- 65.937
- Please enter a positive stepsize, smaller than the difference between the upper and lower limit.
- 5.3729
- Celsius | Fahrenheit
- ======= | ==========
- 3.692 | 35.692
- 9.0649 | 41.0649
- 14.4378 | 46.4378
- 19.8107 | 51.8107
- 25.1836 | 57.1836
- 30.5565 | 62.5565
- 35.9294 | 67.9294
- 41.3023 | 73.3023
- 46.6752 | 78.6752
- 52.0481 | 84.0481
- 57.421 | 89.421
- 62.7939 | 94.7939
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement