Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1.  
  2. #include<iostream>
  3.  
  4. using namespace std;
  5.  
  6. double ToCelsius(double farhrenheit);
  7. double ToKelvin(double fahrenheit);
  8.  
  9. int main()
  10. {
  11.     // Prompt the user and get their input.
  12.     cout << "Enter a temperature in Fahrenheit and it will be converted into Celsius and Kelvin." << endl;
  13.  
  14.     // TODO: Change this to an int to follow the specification to the T.
  15.     // It doesn't really make sense to only allow whole numbers to be entered,
  16.     // but whatever.
  17.     double fahrenheit = 0.0;
  18.     cin >> fahrenheit;
  19.  
  20.     // Call into our functions to perform the calculations.
  21.     double celsius = ToCelsius(fahrenheit);
  22.     double kelvin = ToKelvin(fahrenheit);
  23.  
  24.     // Print the results.
  25.     cout << fahrenheit << " is " << celsius << " in Celsius." << endl;
  26.     cout << fahrenheit << " is " << kelvin << " in Kelvin." << endl;
  27.     system("pause");
  28.  
  29.     return 0;
  30. }
  31.  
  32. double ToCelsius(double fahrenheit)
  33. {
  34.     // Hi, my name is *ms narrator voice starts* Paul Pham *ms narrator voice ends* and this is a micro-optimization.
  35.     // Division takes longer to perform than multiplication, so we'll only do the division one time
  36.     // then just use that calculated value.
  37.  
  38.     const double FIVE_OVER_NINE = 5.0 / 9.0;
  39.     return (fahrenheit - 32) * FIVE_OVER_NINE;
  40.  
  41.     // Use this one to remove the constant double...
  42.     // return (fahrenheit - 32) * (5.0 / 9.0);
  43. }
  44.  
  45. double ToKelvin(double fahrenheit)
  46. {
  47.     // It'll save us some typing - and reduce code duplication - to convert to celsius first.
  48.     // We could just accept the celsius as an input parameter, but that would be inconsistent
  49.     // with our ToCelsius function.
  50.     return ToCelsius(fahrenheit) + 273.15;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement