1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
#include <iostream>
#include <iomanip>
using namespace std;
//prototypes
int GetConversionMethod(int method);
int GetTemperatureRange(int range);
void DisplayConversions(double temp, double temp2, int method, double interval);
double FahrenheitToCelcius(double temp);
double CelciusToFahrenheit(double temp);
void main()
{
char again;
double temp, temp2, interval=1;
//set up decimal points
cout.setf(ios::fixed, ios::floatfield);
cout.setf(ios::showpoint);
cout<<setprecision(2);
cout<<setw(50)<<"Temperature Conversion"<<endl<<endl;
cout<<"This program will convert celcius to fahrenheit.\n";
cout<<"You can calculate single temperature of a range. Follow the instructions.\n\n";
do
{
int method=0, range=0;
method = GetConversionMethod(method);
range = GetTemperatureRange(range);
if (range==1)
{
cout<<"Enter the temperature: ";
cin>>temp;
temp2=temp;
}//end if
if (range==2)
{
cout<<"Enter the first temperature: ";
cin>>temp;
cout<<"Enter the second temperature: ";
cin>>temp2;
cout<<"Enter the interval you wish Temperatures to increase by: ";
cin>>interval;
}//end if
DisplayConversions(temp, temp2, method, interval);
cout<<endl<<"Do you want to preform another calculation? <y or n>";
cin>>again;
cout<<endl<<endl;
}while(again=='y' || again=='Y');//end do-while
}//end main
int GetConversionMethod(int method)
/*
the function asks the user if they would like to convert Celcius to Fahrenheit
or Fahrenheit to Celcius.
It will repeat until the user enters 1 or 2.
Pre: none
Post: will return method to main
*/
{
while(method !=1 && method !=2)
{
cout<<"Do you wish to convert 1. Celcius to Fahrenheit or 2. Fahrenheit to Celcius: ";
cin>>method;
}//end while
return method;
}//end GetConversionMethod
int GetTemperatureRange(int range)
/*
this function asks if they would like to convert a single value or a range.
It will repeat untill they enter 1 or 2.
Pre: none
Post: Will return if they want a range or not back to main
*/
{
while (range !=1 && range !=2)
{
cout<<"Do you want to convert 1. A single value or 2. A range of values: ";
cin>>range;
}//end while
return range;
}//end GetTemperatureRange
void DisplayConversions(double temp, double temp2, int method, double interval)
/*
This function displays the conversions in a table.
It will also switch the two temperatures so that temp is less than temp2.
It also calculates the temperatures using a while loop, displaying
them each line.
Pre:the temp, temp2 method and interval all need to be entered.
Post: Upon completion, the program will display the converted temperatures.
*/
{
double temp3;
if (method==1)
cout<<" Celcius Fahrenheit"<<endl;
if (method==2)
cout<<" Fahrenheit celcius"<<endl;
if (temp2<=temp)
{
double a;
a=temp;
temp=temp2;
temp2=a;
}//end if
while (temp<=temp2)
{
if (method==1)
{
temp3=CelciusToFahrenheit(temp);
}//end if
if(method ==2)
{
temp3=FahrenheitToCelcius(temp);
}//end if
cout<<setw(9)<<temp<<setw(14)<<temp3<<endl;
}//end while
}//end DisplayConversions
double FahrenheitToCelcius(double temp)
/*
This function converst fahrenheit to celcius.
Pre:temp needs to be entered
Post: will return the converted temperatur back to display conversions
*/
{
double temp3;
temp3=(temp-32)*5/9;
return temp3;
}//end Celcius to Fahrenheit
double CelciustoFahrenheit(double temp)
/*
This function converts Celcius to Fahrenheit.
Pre: temp needs to be entered
Post: will return the converted temperature back to DisplayConversions
*/
{
double temp3;
temp3=temp*9/5+32;
return temp3;
}//end CelciusToFahrenheit
|