gpt4 book ai didi

c++ - 我将如何清理这些If语句?

转载 作者:行者123 更新时间:2023-12-02 09:53:52 25 4
gpt4 key购买 nike

if (calculation == "help") {
cout << "add, subtract, multiplication, divide\nsquare root, sin, cos, power\n";
}
else if (calculation == "add") {
cout << num1 << " + " << num2 << " = " << num1 + num2 << endl;
}
else if (calculation == "subtract") {
cout << num1 << " - " << num2 << " = " << num1 - num2 << endl;
}
else if (calculation == "multiplication") {
cout << num1 << " x " << num2 << " = " << num1 * num2 << endl;
}
else if (calculation == "divide") {
cout << num1 << " / " << num2 << " = " << num1/ num2 << endl;
}
else if (calculation == "square root") {
cout << "The square root of the first number is " << sqrt(num1) << ". The square root of the second number is " << sqrt(num2) << "." << endl;
}
else if (calculation == "sin") {
cout << "The sine of the first number is " << sin(num1) << ". The sine of the second number is " << sin(num2) << "." << endl;
}
else if (calculation == "cos") {
cout << "The cosine of the first number is " << cos(num1) << ". The cosine of the second number is " << cos(num2) << "." << endl;
}
else if (calculation == "power") {
cout << num1 << " to the power of " << num2 << " = " << pow(num1, num2) << endl;
}

我有一个解决这些if语句的想法,例如创建 map 或字典。我不相信您也不能在C++中将字符串与switch语句一起使用。任何帮助深表感谢!

编辑:我能够使用 map 。
map<string, int> Choices = {
{ "help", 0 },
{ "add", 1 },
{ "subtract", 2 },
{ "multiply", 3 },
{ "divide", 4 },
{ "square root", 5 },
{ "sine", 6 },
{ "cosine", 7 },
{ "power", 8 }
};

it = Choices.find(choice);
i = it->second;

如果有更快的方法,请告诉我。感谢您的所有回复!

最佳答案

如果您匹配前两个字符并将其转换为数字,则可以创建一个开关。它不会很漂亮,但是会很快

编辑:如果字符串的第一部分是唯一的,我就如何使用“字符串”创建开关做了一个示例。如果需要,可以用两个以上的字符来完成,如果有重复,可以用大写字母进行切换。

仅在性能非常重要的情况下执行此操作,否则它将破坏其他解决方案的速度

#include <iostream>
#include <string>

int main()
{
// first some code to show how to calculate constant from string if the first part in string is unique
unsigned int uNumber = (unsigned int)'A';
std::cout << uNumber << std::endl;
uNumber <<= 8;
std::cout << uNumber << std::endl;
uNumber += (unsigned int)'A';
std::cout << uNumber << std::endl;

unsigned int uNumberAA = (unsigned int)*(uint16_t*)"AA";
std::cout << uNumberAA << std::endl;
uNumberAA = (unsigned int)*(uint16_t*)"AAAA";
std::cout << uNumberAA << std::endl;


// How to create a switch with strings

static constexpr unsigned int he_help = ((unsigned int)'h' << 8) + (unsigned int)'e'; // "help"
static constexpr unsigned int ad_add = ((unsigned int)'a' << 8) + (unsigned int)'d'; // "add"
static constexpr unsigned int su_subtract = ((unsigned int)'s' << 8) + (unsigned int)'u'; // "subtract"
static constexpr unsigned int mu_multiplication = ((unsigned int)'m' << 8) + (unsigned int)'u'; // "multiplication"

std::string calculation = "subtract";

uNumber = ((unsigned int)calculation[0] << 8) + (unsigned int)calculation[1];
switch(uNumber)
{
case he_help :
std::cout << "help" << std::endl;
break;
case ad_add :
std::cout << "add" << std::endl;
break;
case su_subtract :
std::cout << "subtract" << std::endl;
break;
case mu_multiplication :
std::cout << "multiplication" << std::endl;
break;
}
}

关于c++ - 我将如何清理这些If语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62034952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com