C++ 实现 摄氏度 华氏度 温度转换(尽量少的代码)

题目


  • 使用 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<<"输入有误!";
    }
}


在这里插入图片描述

发布了147 篇原创文章 · 获赞 2353 · 访问量 51万+

猜你喜欢

转载自blog.csdn.net/qq_43901693/article/details/104955812
今日推荐