编写实现温度转换的函数

 

代码如下,如有帮助,请采纳一下,谢谢。

#include <iostream>
using namespace std;

int main()
{
	float dd;
	char cc;
	cout << "请输入温度和单位:";
	cin >> dd >> cc;
	if ('f' == cc || 'F' == cc)
	{
		cout << "转摄氏度为:"<< 5.0 * (dd - 32)/9.0 << endl;
	}else if ('c' == cc || 'C' == cc)
	{
		cout << "转华氏度为:"<< 9.0*dd/5+32 << endl;
	}else
		cout << "not valid" << endl;
	return 0;
}

 

#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<<"not valid!";
    }
}