题目
- 使用 C++ 语言,实现下面这段 python 代码所实现的 温度转换 功能,
体会 python 语言的简洁高效。
C++ 代码实现温度转换
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
int main()
{
string t;
cout << "请输入带有符号的温度值(123C / 100C):";
cin >> t;
cout << "输入的带有符号的温度值为:" << t << endl;
if (t.find('c', 0) != -1 || t.find('C', 0) != -1)
{
double num = stod(t.substr(0, t.find('c', 0)));
cout << "华氏度:" << num * 9 / 5 + 32 << "F" << endl;
}
else if (t.find('f', 0) != -1 || t.find('F', 0) != -1)
{
double num = stod(t.substr(0, t.find('c', 0)));
cout << "摄氏度:" << (num - 32) * 5 / 9 << "C" << endl;
}
else
{
cout<<"输入有误!";
}
}