如何操作 C++ 中的函数根据条件输出不同的结果?
我想知道如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您熟悉布尔逻辑运算符
and
(&&) 和or
(||) 吗?另请务必在边界条件(-20、-30、-40)下进行测试,以确保获得所需的结果!编辑:: 从评论中回答你的问题:
Are you familiar with boolean logic operators
and
(&&) andor
(||)? Also be sure to test at the boundary conditions (-20, -30, -40) to make sure you're getting the results you want!EDIT:: To answer your question from the comments: