Advertisement
Guest User

Untitled

a guest
Jul 29th, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. #import <iostream>
  2. #import <cmath>
  3.  
  4. #define COLUMN_SEPARATOR "t| "
  5. #define MAX_TEMP 500
  6. #define MIN_TEMP -500
  7.  
  8. inline bool between(double x, double max, double min) {
  9. return max >= x && min <= x;
  10. }
  11.  
  12. void getInput(double &lower, double &upper, double &step) {
  13. double temp1, temp2;
  14. std::cout << "Please enter consecutively the upper and lower limits, both between " << MIN_TEMP << " and " << MAX_TEMP << "." << std::endl;
  15. std::cin >> temp1;
  16. std::cin >> temp2;
  17. while (!between(temp1, MAX_TEMP, MIN_TEMP) || !between(temp2, MAX_TEMP, MIN_TEMP)) {
  18. std::cout << "At least one of the temperatures is out of bounds. Please reenter:" << std::endl;
  19. std::cin >> temp1;
  20. std::cin >> temp2;
  21. }
  22. upper = std::max(temp1, temp2);
  23. lower = std::min(temp1, temp2);
  24. std::cout << "Please enter a positive stepsize, smaller than the difference between the limits." << std::endl;
  25. std::cin >> step;
  26. while (step < 0 || step > upper - lower) {
  27. std::cout << "The stepsize is out of bounds. Please reenter:" << std::endl;
  28. std::cin >> step;
  29. }
  30. }
  31.  
  32. double toFahrenheit(double celsius) {
  33. return celsius*(9/5) + 32;
  34. }
  35.  
  36. void printTable(double start, double end, double step) {
  37. std::cout << "Celsius" << COLUMN_SEPARATOR << "Fahrenheit" << std::endl;
  38. std::cout << "=======" << COLUMN_SEPARATOR << "==========" << std::endl;
  39. for (double i = start; i < end; i += step) {
  40. std::cout << i << COLUMN_SEPARATOR << toFahrenheit(i) << std::endl;
  41. }
  42. }
  43.  
  44. int main() {
  45. double start, end, step;
  46. getInput(start, end, step);
  47. printTable(start, end, step);
  48. return 0;
  49. }
  50.  
  51. 192:Challenges 11684$ ./a.out Please enter consecutively the upper and lower limits, both between -500 and 500.
  52. 3.692
  53. 65.937
  54. Please enter a positive stepsize, smaller than the difference between the upper and lower limit.
  55. 5.3729
  56. Celsius | Fahrenheit
  57. ======= | ==========
  58. 3.692 | 35.692
  59. 9.0649 | 41.0649
  60. 14.4378 | 46.4378
  61. 19.8107 | 51.8107
  62. 25.1836 | 57.1836
  63. 30.5565 | 62.5565
  64. 35.9294 | 67.9294
  65. 41.3023 | 73.3023
  66. 46.6752 | 78.6752
  67. 52.0481 | 84.0481
  68. 57.421 | 89.421
  69. 62.7939 | 94.7939
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement