c_cpp 将华氏温度转换为摄氏温度或Vise-Versa

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 将华氏温度转换为摄氏温度或Vise-Versa相关的知识,希望对你有一定的参考价值。

#include <iostream>
#include <cctype>

int main()
{
    int temp;
    char unit;

    std::cout << "Please enter a temperature: ";
    std::cin >> temp;
    std::cout << "To convert to Celsius press 'c' or Fahrenheit press 'f': ";
    std::cin >> unit;

    if ( isupper(unit) )
    {
        unit = tolower(unit);
    }

    if (unit == 'c')
    {
        temp = (temp - 32)  / 9.0 * 5.0;
        std::cout << temp << " degrees Celsius\n";
    }
    else if (unit == 'f')
    {
        temp = 9.0 / 5.0 * temp + 32;
        std::cout << temp << " degrees Fahrenheit\n";
    }
    else
    {
        std::cout << "Sorry that unit of measure isn't recognized.\n";
    }

    return 0;
}

以上是关于c_cpp 将华氏温度转换为摄氏温度或Vise-Versa的主要内容,如果未能解决你的问题,请参考以下文章