Jump to content



Κελσιου σε Φαρεναι και το αναποδο (σε C++)


Dr.Paneas

Recommended Posts

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


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

#include <stdlib.h>
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <limits>

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 << "\t\t" << fahr << endl;
}
}
else
{
for(i=start; i<=end; i+=step)
{
celsius=fahrenheitToCelsius(i);
cout << setprecision(2) << setw(5) << i << "\t\t" << 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\n2. Fahrenheit to Celsius" << endl
<< "Your Choice:";

cin >> choice;

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

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



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

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

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\t\tFahrenheit" << endl;
makeTable(lowBound, maxBound, step,1);
}
}
else
{
cout << "Select a number" << endl
<< "1. Specific conversion\n2. Print table list" << endl
<< "Your Choice:";
cin >> choice2;



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

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


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\tCelsius" << endl;
makeTable(lowBound, maxBound, step,2);
}

}
return 0;
}


return (EXIT_SUCCESS);
}

ΥΣ:Το blackslash13 ειναι το καινουριο μου nickname:p

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Δημιουργία...

Important Information

Ο ιστότοπος theLab.gr χρησιμοποιεί cookies για να διασφαλίσει την καλύτερη εμπειρία σας κατά την περιήγηση. Μπορείτε να προσαρμόσετε τις ρυθμίσεις των cookies σας , διαφορετικά θα υποθέσουμε ότι είστε εντάξει για να συνεχίσετε.