我的老师已经分配了一个程序来使用if-else
语句和switch
语句,因此我们了解如何实现这两个语句。该程序要求我们提示用户分别以磅和米为单位输入他们的体重和身高。这是我的尝试:
没有开关
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
double height, weight, BMI, heightMeters, weightKilo;
const double KILOGRAMS_PER_POUND = 0.45359237;
const double METERS_PER_INCH = 0.0245;
cout << "Please enter your height (inches) and weight (pounds)" << endl;
cin >> height >> weight;
weightKilo = weight*KILOGRAMS_PER_POUND;
heightMeters = height*METERS_PER_INCH;
BMI = weightKilo / (heightMeters*heightMeters);
if (BMI < 18.5) {
cout << "You are underweight " << endl;
}
else if (BMI >= 18.5 && BMI < 25.0) {
cout << "You are normal" << endl;
}
else if (BMI >= 25.0 && BMI < 30.0) {
cout << "You are overweight" << endl;
}
else if (BMI >= 30.0 && BMI < 35) {
cout << "You are obese" << endl;
}
else {
cout << "You are gravely overweight" << endl;
}
}
使用开关
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
double height, weight, heightMeters, weightKilo;
int BMI, q;
const double KILOGRAMS_PER_POUND = 0.45359237;
const double METERS_PER_INCH = 0.0245;
cout << "Please enter your height (inches) and weight (pounds)" << endl;
cin >> height >> weight;
weightKilo = weight*KILOGRAMS_PER_POUND;
heightMeters = height*METERS_PER_INCH;
BMI = weightKilo / (heightMeters*heightMeters);
if (BMI < 18.5) {
q = 1;
}
else if (BMI >= 18.5 && BMI < 25.0) {
q = 2;
}
else if (BMI >= 25.0 && BMI < 30.0) {
q = 3;
}
else if (BMI >= 30.0 && BMI < 35) {
q = 4;
}
else {
q = 5;
}
switch (q) {
case 1: cout << "You are underweight" << endl; break;
case 2: cout << "You are a normal weight " << endl; break;
case 3: cout << "You are overweight" << endl; break;
case 4: cout << "You are obese" << endl; break;
case 5: cout << "You are gravely overweight" << endl; break;
}
}
这是我想到的方式,包括一个switch语句。有没有办法将第一个代码块实现为switch语句?
我几乎可以肯定无法使用范围或使用双打(18.5)。我给老师发了电子邮件,他们给了我一个答案
对你来说可能没有意义,但有时你将不得不编写一个没有意义的程序。我并不是说你没有合法的问题,但如果有人能弄清楚你可以。但是,也许它无法解决。那是挑战&#34;。
所以,我问:是否有一些方法只是为第一个代码块使用switch语句,或者是我在代码中使用switch语句的最佳方法,即使它在没必要吗?
答案 0 :(得分:108)
与C ++一样,支持标准库算法。在这种情况下,您需要进行范围查找。对于有序的边界序列,这很容易:
double const boundaries[] = { 18.5, 25, 30, 35 };
switch (upper_bound(begin(boundaries), end(boundaries), BMI) - boundaries) {
case 0: cout << "You are underweight " << endl; break;
case 1: cout << "You are normal" << endl; break;
case 2: cout << "You are overweight" << endl; break;
case 3: cout << "You are obese" << endl; break;
case 4: cout << "You are gravely overweight" << endl; break;
};
实际上,我建议你
switch
(请参阅下面的 BONUS 部分)using namespace std
(请参阅Why is "using namespace std" considered bad practice?)查看实时演示on Coliru
#include <iostream>
#include <algorithm>
const char* bmi_classification(double bmi) {
static double const boundaries[] = { 18.5, 25, 30, 35 };
double const* lookup = std::upper_bound(std::begin(boundaries), std::end(boundaries), bmi);
switch (lookup - std::begin(boundaries)) {
case 0: return "underweight";
case 1: return "normal";
case 2: return "overweight";
case 3: return "obese";
case 4: return "gravely overweight";
}
throw std::logic_error("bmi_classification");
}
int main() {
for (double BMI : { 0.0, 18.4999, 18.5, 24.0, 25.0, 29.0, 30.0, 34.0, 35.0, 999999.0 }) {
std::cout << "BMI: " << BMI << " You are " << bmi_classification(BMI) << "\n";
}
}
打印
BMI: 0 You are underweight
BMI: 18.4999 You are underweight
BMI: 18.5 You are normal
BMI: 24 You are normal
BMI: 25 You are overweight
BMI: 29 You are overweight
BMI: 30 You are obese
BMI: 34 You are obese
BMI: 35 You are gravely overweight
BMI: 999999 You are gravely overweight
<强>奖金强>
您可以更优雅,而无需使用switch
:
<强> Live On Coliru 强>
const char* bmi_classification(double bmi) {
constexpr int N = 5;
static constexpr std::array<char const*, N> classifications {
{ "underweight", "normal", "overweight", "obese", "gravely overweight" }};
static constexpr std::array<double, N-1> ubounds {
{ 18.5, 25, 30, 35 }};
auto lookup = std::upper_bound(std::begin(ubounds), std::end(ubounds), bmi);
return classifications.at(lookup - std::begin(ubounds));
}
答案 1 :(得分:20)
除非你有一个绝对可怕的编译器扩展,否则你不能在C ++的范围内switch
。
但如果您创建std::vector
BMI范围,则可以优雅地使用开关:
std::vector<double> v = {18.5, 25.0 /*etc*/}
然后使用std::lower_bound
和std::distance
来获取给定BMI在上述范围内的位置。 此是您switch
的数量。
然后,您可以进一步进一步定义std::vector<std::string>
输出消息。那么你既不需要switch
也不需要if
块!所有选择逻辑都委托给std::lower_bound
。
我故意没有给你完整的代码:我相信这些提示就足够了。
答案 2 :(得分:5)
您不能在开关内使用双。文档说:
switch ( expression )
case constant-expression : statement
[default : statement]
表达式必须是整数类型或类类型 其中有一个明确的转换为整数类型。积分 促销按照积分促销中的描述进行。
旁注:
有些编译器(如Clang 3.5.1)允许case x ... y
作为C ++语言的扩展。但这也是一个完整的数据类型。像
switch(x){
case 0:
cout << "Test1";
break;
case 0 ... 9:
cout << "Test2";
break;
答案 3 :(得分:3)
C ++中的开关只允许您检查整数和字符的值。
BMI是双重类型,因此无法在交换机中检查其值。
在使用开关的解决方案中,您还应将变量BMI声明为double。如果将其声明为整数,则所有十进制结果将被转换为整数,并且您将丢失小数位。
答案 4 :(得分:2)
您可以从数组/向量动态计算案例标签,而不是硬编码if / else表达式:
//#include "stdafx.h"
#include <iostream>
using namespace std;
inline int seg(double d){ //calculate segment for a BMI of d
constexpr double segs[] = { 18.5, 25, 30, 35 };
constexpr int n = sizeof(segs)/sizeof(double);
int r; for(r=0; r<n; r++)
if(d<segs[r]) return r;
return r;
}
int main()
{
double height, weight, heightMeters, weightKilo;
int BMI, q;
const double KILOGRAMS_PER_POUND = 0.45359237;
const double METERS_PER_INCH = 0.0245;
cout << "Please enter your height (inches) and weight (pounds)" << endl;
cin >> height >> weight;
weightKilo = weight*KILOGRAMS_PER_POUND;
heightMeters = height*METERS_PER_INCH;
BMI = weightKilo / (heightMeters*heightMeters);
switch (seg(BMI)) {
case 0: cout << "You are underweight" << endl; break;
case 1: cout << "You are a normal weight " << endl; break;
case 2: cout << "You are overweight" << endl; break;
case 3: cout << "You are obese" << endl; break;
case 4: cout << "You are gravely overweight" << endl; break;
}
}
(如果你真的想要,你甚至可以制作seg函数constexpr
。
答案 5 :(得分:1)
您可以执行以下操作:
switch ((round)BMI)
{
case 1: case 2: case 3: .... case 15: case 16: case 17: cout<< "You are underweight " << endl; break;
case 18: ... case 24: cout << "You are normal" << endl; break;
case 25: ... case 29: cout << "You are overweight" << endl; break;
case 30: ... case 34: cout << "You are obese" << endl; break;
default: cout << "You are gravely overweight" << endl;
}
我也忍不住注意到这一点,因为您使用的是if-else
,您可以避免else-if
语句中的第一个条件,如:
if (BMI < 18.5)
{
cout << "You are underweight " << endl;
}
else if (BMI < 25.0)
{
cout << "You are normal" << endl;
}
else if (BMI < 30.0)
{
cout << "You are overweight" << endl;
}
else if(BMI < 35)
{
cout << "You are obese" << endl;
}
else
{
cout << "You are gravely overweight" << endl;
}
除此之外,你的两个实现都很好看。