Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- double ToCelsius(double farhrenheit);
- double ToKelvin(double fahrenheit);
- int main()
- {
- // Prompt the user and get their input.
- cout << "Enter a temperature in Fahrenheit and it will be converted into Celsius and Kelvin." << endl;
- // TODO: Change this to an int to follow the specification to the T.
- // It doesn't really make sense to only allow whole numbers to be entered,
- // but whatever.
- double fahrenheit = 0.0;
- cin >> fahrenheit;
- // Call into our functions to perform the calculations.
- double celsius = ToCelsius(fahrenheit);
- double kelvin = ToKelvin(fahrenheit);
- // Print the results.
- cout << fahrenheit << " is " << celsius << " in Celsius." << endl;
- cout << fahrenheit << " is " << kelvin << " in Kelvin." << endl;
- system("pause");
- return 0;
- }
- double ToCelsius(double fahrenheit)
- {
- // Hi, my name is *ms narrator voice starts* Paul Pham *ms narrator voice ends* and this is a micro-optimization.
- // Division takes longer to perform than multiplication, so we'll only do the division one time
- // then just use that calculated value.
- const double FIVE_OVER_NINE = 5.0 / 9.0;
- return (fahrenheit - 32) * FIVE_OVER_NINE;
- // Use this one to remove the constant double...
- // return (fahrenheit - 32) * (5.0 / 9.0);
- }
- double ToKelvin(double fahrenheit)
- {
- // It'll save us some typing - and reduce code duplication - to convert to celsius first.
- // We could just accept the celsius as an input parameter, but that would be inconsistent
- // with our ToCelsius function.
- return ToCelsius(fahrenheit) + 273.15;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement