Пример #1
0
string Temperature::ToString(char unit) const {
		stringstream ss;
		if(unit == 'F' || unit == 'f') {
		ss << GetTempAsFahrenheit() << " " << "Fahrenheit";
		} else if (unit == 'C' || unit == 'c') {
		ss << GetTempAsCelsius() << " " << "Celsius";
		} else if (unit == 'K' || unit == 'k') { 
		ss << setprecision(2) << fixed << GetTempAsKelvin() << " " << "Kelvin";
	    } else {
	    	ss << "Invalid Unit";
	    }
	
		return ss.str();
}
Пример #2
0
/*
 * Get a string representation of the temperature.
 * The string will be formatted as:
 * "TEMP UNIT"
 * where TEMP is the temperature to 2 decimal places and UNIT is either
 * "Kelvin", "Celsius", or "Fahrenheit".
 * The conversion to perform is denoted by the parameter.
 * If the unit given through the argument is invalid, set the string to:
 * "Invalid Unit"
 * @uses stringstream
 * @param char unit - The conversion to perform, either 'K', 'C' or 'F',
 *                    defaults to 'K' and is case-insensitive
 * @return string - A string representation of the temperature or invalid if
 *                  the provided unit is not recognized
 */
string Temperature::ToString(char unit) const
{
    stringstream fin;
    if(toupper(unit)=='K')
    {
        fin<<std::fixed<<std::setprecision(2)<<GetTempAsKelvin()<<" Kelvin";
        return fin.str();
    }
    if (toupper(unit)=='C')
    {
        fin<<GetTempAsCelsius()<<" Celsius";
        return fin.str();
    }
    if(toupper(unit)=='F')
    {
        fin<<GetTempAsFahrenheit()<<" Fahrenheit";
        return fin.str();
    }
    else
    {
        return ("Invalid Unit");
    }
}