如何操作 C++ 中的函数根据条件输出不同的结果?

发布于 2024-12-13 04:20:53 字数 1151 浏览 1 评论 0原文

我想知道如何使用 void 函数根据条件输出结果。我正在尝试创建一个 Windchill 计算器。

我可以添加什么来使下面的程序输出低于 4.8kph 速度的气温? 我该怎么做才能使 print_result 无效以打印各种语句 - 例如 -20 至 -30 Windchill (WC) 的“穿 3 层”或 -30 至 -40 的“穿 5 层”?感谢那些可以提供帮助的人!

#include <iostream>
#include <cmath>
using namespace std;

bool is_cold(double V)
{
    bool is_windy;
    if (V <= 4.8)
    {
        is_windy = false;
    }
    else
        is_windy = true;
    return (is_windy);
}
int windchill_index(double T, double V)
{
    int WC;
    WC = 13.12 + 0.6215*T - 11.37*pow(V,0.16) + 0.3965*T*pow(V, 0.16);
    return (WC);
}
void print_result(double WC)
{
    cout << "From the input for tempearature and wind speed, the wind chill is: "<< WC << endl;
}

int main()
{
    double WC = 0, T = 0, V = 0;
    bool is_windy = false;
    if (!is_windy)
        {
            cout << "Please enter the air temperature in Celsius followed by the windpseed in kph: " << endl;
            cin >> T;
            cin >> V;

            is_windy = is_cold(V);
        }
    WC = windchill_index(T, V);

    print_result (WC);
    return 0;
}

I would like to know how to use the void function to output results based on condition. I am trying to create a windchill calculator.

What can I add to make the program below output the air temperature below speeds of 4.8kph?
What can I do to void print_result to print various statements - like "wear 3 layers" for -20 to -30 windchill (WC) or "wear 5 layers" for -30 to -40? Thanks to those who can help!

#include <iostream>
#include <cmath>
using namespace std;

bool is_cold(double V)
{
    bool is_windy;
    if (V <= 4.8)
    {
        is_windy = false;
    }
    else
        is_windy = true;
    return (is_windy);
}
int windchill_index(double T, double V)
{
    int WC;
    WC = 13.12 + 0.6215*T - 11.37*pow(V,0.16) + 0.3965*T*pow(V, 0.16);
    return (WC);
}
void print_result(double WC)
{
    cout << "From the input for tempearature and wind speed, the wind chill is: "<< WC << endl;
}

int main()
{
    double WC = 0, T = 0, V = 0;
    bool is_windy = false;
    if (!is_windy)
        {
            cout << "Please enter the air temperature in Celsius followed by the windpseed in kph: " << endl;
            cin >> T;
            cin >> V;

            is_windy = is_cold(V);
        }
    WC = windchill_index(T, V);

    print_result (WC);
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Saygoodbye 2024-12-20 04:20:54

您熟悉布尔逻辑运算符 and (&&) 和 or (||) 吗?另请务必在边界条件(-20、-30、-40)下进行测试,以确保获得所需的结果!

void print_result(double WC)
{
    cout << "From the input for tempearature and wind speed, the wind chill is: "<< WC << endl;
    if (WC <= -20 && WC > -30) {
       cout << "Wear 3 layers!" << endl;   
    } else if (WC <= -30 && WC > -40) {
       cout << "Wear 5 layers!" << endl;
    } else {
       cout << "Wear ALL the layers!" << endl;
    }
}

编辑:: 从评论中回答你的问题:

int main()
{
    double WC = 0, T = 0, V = 0;
    bool is_windy = false;
    if (!is_windy)
        {
            cout << "Please enter the air temperature in Celsius followed by the windpseed in kph: " << endl;
            cin >> T;
            cin >> V;

            is_windy = is_cold(V);
        }
    //If its windy we want to calculate the windchill index, otherwise just use the ambient temp
    if (is_windy) {
       WC = windchill_index(T, V);
    } else {
       WC = T
    }

    print_result (WC);
    return 0;
}

Are you familiar with boolean logic operators and (&&) and or (||)? Also be sure to test at the boundary conditions (-20, -30, -40) to make sure you're getting the results you want!

void print_result(double WC)
{
    cout << "From the input for tempearature and wind speed, the wind chill is: "<< WC << endl;
    if (WC <= -20 && WC > -30) {
       cout << "Wear 3 layers!" << endl;   
    } else if (WC <= -30 && WC > -40) {
       cout << "Wear 5 layers!" << endl;
    } else {
       cout << "Wear ALL the layers!" << endl;
    }
}

EDIT:: To answer your question from the comments:

int main()
{
    double WC = 0, T = 0, V = 0;
    bool is_windy = false;
    if (!is_windy)
        {
            cout << "Please enter the air temperature in Celsius followed by the windpseed in kph: " << endl;
            cin >> T;
            cin >> V;

            is_windy = is_cold(V);
        }
    //If its windy we want to calculate the windchill index, otherwise just use the ambient temp
    if (is_windy) {
       WC = windchill_index(T, V);
    } else {
       WC = T
    }

    print_result (WC);
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文