Created
March 18, 2021 07:10
-
-
Save jatinsharrma/1888160b9b231f95675f90df6891f0ca to your computer and use it in GitHub Desktop.
c ++ | Temperature conversion | Celsius, Kelvin, Fahrenheit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
//---------------Function Declaration---------------- | |
void error(std::string); // returns error | |
bool valid_temp(double); // check for valid temprature in Kelvin | Below 0K is invalid | |
double ctok(double); // convert Celsius to Kelvin | |
double ktoc(double); // convert Kelvin to Celsius | |
double ftoc(double); // convert Fahrenheit to Celsius | |
double ctof(double); // convert Celsius to Fahrenhiet | |
double convertK(double, const char); // convert Kelvin to any | |
double convertC(double, const char); // convert Celsius to any | |
double convertF(double, const char); // convert Fahrenheit to any | |
double convert(double, const char, const char); // convert any to any | |
void getInput(double&, char& , char& ); // take input from user | |
// for dealing with error | |
class Invalid{}; // for invalid temprature | |
class Wrong{}; // for wrong format | |
int main() | |
{ | |
while(true){ | |
try{ | |
double temp = 0; | |
char ch1; | |
char ch2 ; | |
getInput(temp,ch1,ch2); | |
double converted = convert(temp,ch1,ch2); | |
std::cout << temp << ch1 << " = " << converted << ch2 <<std::endl; | |
} | |
catch(Invalid&){ | |
std::cout << "Invalid temperature !" <<std::endl; | |
} | |
catch(Wrong&){ | |
std::cout<< "Wrong Format !" <<std::endl; | |
} | |
catch(...){ | |
std::cout << "Somthing went wrong" <<std::endl; | |
} | |
} | |
} | |
// ----------------FUnction Definations---------------- | |
// returns error | |
void error(std::string s) | |
{ | |
throw std::runtime_error(s); | |
} | |
// check for valid temprature in Kelvin | Below 0K is invalid | |
bool valid_temp(double k) | |
{ | |
if(k >= 0) return true; | |
return false; | |
} | |
// convert Celsius to Kelvin | |
double ctok(double c) | |
{ | |
double k = c + 273.15; | |
if(!valid_temp(k)) throw Invalid(); | |
return k; | |
} | |
// convert Kelvin to Celsius | |
double ktoc(double k) | |
{ | |
double c = k - 273.15; | |
if(!valid_temp(k)) throw Invalid(); | |
return c; | |
} | |
// convert Fahrenheit to Celsius | |
double ftoc(double f) | |
{ | |
double c = (f - 32) * (5.0/9); | |
if(!valid_temp(ctok(c))) throw Invalid(); | |
return c; | |
} | |
// convert Celsius to Fahrenhiet | |
double ctof(double c) | |
{ | |
double f = c * (9.0/5) + 32; | |
if(!valid_temp(ctok(c))) throw Invalid(); | |
return f; | |
} | |
// convert Kelvin to any | |
double convertK(double t, const char ch) | |
{ | |
switch(ch){ | |
case 'C' : case 'c' : | |
return ktoc(t); | |
case 'F' : case 'f' : | |
return ctof(ktoc(t)); | |
} | |
throw Wrong(); | |
} | |
// convert Celsius to any | |
double convertC(double t, const char ch) | |
{ | |
switch(ch){ | |
case 'F' : case 'f' : | |
return ctof(t); | |
case 'K' : case 'k' : | |
return ctok(t); | |
} | |
throw Wrong(); | |
} | |
// convert Fahrenheit to any | |
double convertF(double t, char ch) | |
{ | |
switch(ch){ | |
case 'C' : case 'c' : | |
return ftoc(t); | |
case 'K' : case 'k' : | |
return ctok(ftoc(t)); | |
} | |
throw Wrong(); | |
} | |
// convert any to any | |
double convert(double t, const char ch1, const char ch2) | |
{ | |
if (ch1 == ch2) return t; | |
switch(ch1){ | |
case 'K': case 'k' : | |
return convertK(t,ch2); | |
case 'C': case 'c' : | |
return convertC(t,ch2); | |
case 'F': case 'f' : | |
return convertF(t,ch2); | |
} | |
throw Wrong(); | |
} | |
// take input from user | |
void getInput(double& t, char& ch1, char& ch2) | |
{ | |
std::cout << "Enter temprature : "; | |
std::cin >> t >> ch1; | |
std::cout << "[Kelvin:K , Celsius:C , Fahrenheirt:F]" <<std::endl; | |
std ::cout << "Enter output unit :"; | |
std::cin >> ch2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment