Απο κελσίου σε φαρενάι και το ανάποδο C++

WiludrakeGR | Κυρ, 01/13/2008 - 23:27 | 1'

Παιδια ειχα μια εργασια για την σχολη οποτε το εψαξα λιγο και νομιζω εφτιαξα κατι καλο. Για δειτε:


//
// File: temp.cc
// Author: blackslash13
//
// Created on 29 Νοέμβριος 2007, 11:06 μμ
//

#include
#include
#include
#include
#include
#include

bool inputDouble(std::string prompt, double &val)
{
std::string input;
for( ;; )
{
std::stringstream sstr;
std::cout << prompt;
if (!std::getline(std::cin, input))
return false; // Failed to read input.
sstr << input;
if (sstr >> val) {
return true;
}
else
{
std::cout << 'Error, not a valid number' << std::endl;
}
}
}

double fahrenheitToCelsius(double fahrenheit)
{
double celsius;

celsius = (fahrenheit-32)/1.8;
return celsius;
}

double celsiusToFahrenheit(double celsius)
{
double fahrenheit;

fahrenheit = 1.8 * celsius + 32 ;
return fahrenheit;
}

void makeTable(double start, double end, double step, double choice)
{
using namespace std;
cout.setf(ios::fixed);
double i(0),fahr,celsius;

if(choice==1)
{
for(i=start; i<=end; i+=step)
{
fahr=celsiusToFahrenheit(i);
cout << setprecision(2) << setw(5) << i << ' ' << fahr << endl;
}
}
else
{
for(i=start; i<=end; i+=step)
{
celsius=fahrenheitToCelsius(i);
cout << setprecision(2) << setw(5) << i << ' ' << celsius << endl;
}

}

}

//
//
//
int main(int argc, char** argv) {

{
using namespace std;

double choice,choice2;
double degree,lowBound,maxBound,step;

cout << 'Select a number' << endl
<< '1. Celsius to Fahrenheit 2. Fahrenheit to Celsius' << endl
<< 'Your Choice:';

cin >> choice;

while(!cin.good() || choice<1 || choice>2)
{
cin.clear();
cin.ignore(numeric_limits::max(),' ');
cout << 'Not valid selection.... Your Choice:';
cin >> choice;
}

if(choice == 1)
{
cout << 'Select a number' << endl
<< '1. Specific conversion 2. Print table list' << endl
<< 'Your Choice:';
cin >> choice2;

while(!cin.good() || choice2<1 || choice2>2)
{
cin.clear();
cin.ignore(numeric_limits::max(),' ');
cout << 'Not valid selection.... Your Choice:';
cin >> choice2;
}

/* Thrash the garbage from input strem */
cin.clear();
cin.ignore(1,' ');

if(choice2==1)
{

while(!inputDouble('Enter Celsius temperature degree:', degree) || degree<=-316 || degree>=316)
{
cout << 'Not valid selection....';
}

cout << degree << ' Celsius = ' << celsiusToFahrenheit(degree) << ' Fahrenheit.' << endl;
}
else
{
while(!inputDouble('Enter Celsius lower bound degree:',lowBound) || lowBound<=-316 || lowBound>=316)
{
cout << 'Not valid selection....';
}

while(!inputDouble('Enter Celsius maximum bound degree:',maxBound) || maxBound<=-316 || maxBound>=316)
{
cout << 'Not valid selection....';
}

while(!inputDouble('Enter increment value for the list:',step) || step<=0)
{
cout << 'Not valid selection....';
}

cout << 'Celsius Fahrenheit' << endl;
makeTable(lowBound, maxBound, step,1);
}
}
else
{
cout << 'Select a number' << endl
<< '1. Specific conversion 2. Print table list' << endl
<< 'Your Choice:';
cin >> choice2;

while(!cin.good() || choice2<1 || choice2>2)
{
cin.clear();
cin.ignore(numeric_limits::max(),' ');
cout << 'Not valid selection.... Your Choice:';
cin >> choice2;
}

/* Thrash the garbage from input strem */
cin.clear();
cin.ignore(1,' ');

if(choice2==1)
{

while(!inputDouble('Enter Fahrenheit temperature degree:', degree) || degree<=-316 || degree>=316)
{
cout << 'Not valid selection....';
}

cout << degree << ' Fahrenheit = ' << fahrenheitToCelsius(degree) << ' Celsius.' << endl;
}
else
{
while(!inputDouble('Enter Fahrenheit lower bound degree:',lowBound) || lowBound<=-316 || lowBound>=316)
{
cout << 'Not valid selection....';
}

while(!inputDouble('Enter Fahrenheit maximum bound degree:',maxBound) || maxBound<=-316 || maxBound>=316)
{
cout << 'Not valid selection....';
}

while(!inputDouble('Enter increment value for the list:',step) || step<=0)
{
cout << 'Not valid selection....';
}

cout << 'Fahrenheit Celsius' << endl;
makeTable(lowBound, maxBound, step,2);
}

}
return 0;
}

return (EXIT_SUCCESS);
}

Δώσε αστέρια!

MO: (ψήφοι: 0)