如何正确解析子字符串,以便它们对我的新手计算器有效?
How do I properly parse substrings so that they can be effective for my newbie calculator?
如果可能的话请 ELI5,因为我只写了几天代码,这是我的第一个程序!下面是我的脚本的一部分,它应该解释某人输入的一行输入(例如“5+5”或其他内容)。
我还有其他格式不同的操作想稍后添加,这就是为什么我使用字符串而不是开关函数或其他东西的原因。
无论如何..这行不通:(下面是我的逻辑过程,也许有人可以指出我哪里搞砸了?:)
提前致谢!
if (fork.find("+" && "-" && "x" && "/"))
{
size_t pos = fork.find("+" && "-" && "x" && "/"); // Defines a position at the operator symbol
string afterfork = fork.substr(pos + 1); // Cuts a substring at the operator symbol + 1
size_t beforepos = fork.find_first_of(fork); // Defines a position at the beginning of the string
string beforefork = fork.substr(beforepos); // cuts a substring at the begninning of the string
string atfork = fork.substr(pos); // cuts a substring that only has one char (the operator +, -, x, etc)
int x = stoi(beforefork.c_str()); // converts the first substring to an integer
int y = stoi(afterfork.c_str()); // converts the third substring to an integer
string operation = atfork; // converts the middle substring that only has one char to a different name.
return input(x, operation, y); // will send this information to the input function (which will do the math for the calculator).
}
要在字符串中搜索字符列表之一,如果没有找到任何内容,您可以使用 find_first_of
. This function returns npos
。
const size_t operatorPos = input.find_first_of("+-*/");
if (operatorPos == std::string::npos) {
std::cout << "Couldn't find an operator!\n";
return;
}
要将字符串拆分为两个子字符串,可以使用substr
. To get a character at a position, use operator[]
。
const std::string left = input.substr(0, operatorPos);
const std::string right = input.substr(operatorPos + 1);
const char operation = input[operatorPos];
要将字符串转换为整数,嗯,有很多选择。当无法将字符串转换为整数时,我将使用 std::stoi
for this answer. This function throws an exception that we need to catch。
int leftInt;
try {
leftInt = std::stoi(left);
} catch (...) {
std::cout << '"' << left << "\" is not a valid integer!\n";
return;
}
int rightInt;
try {
rightInt = std::stoi(right);
} catch (...) {
std::cout << '"' << right << "\" is not a valid integer!\n";
return;
}
如果异常真的令人困惑(我花了很长时间才弄明白异常!)那么您可以尝试另一个函数。我最喜欢的(也是 IMO 最好的)是 std::from_chars
。另一种选择是不捕获异常。
const int leftInt = std::stoi(left);
const int rightInt = std::stoi(right);
在那种情况下,您将不会收到像 "five" is not a valid integer!
这样的友好错误消息。你会得到类似的东西:
libc++abi.dylib: terminating with uncaught exception of type std::invalid_argument: stoi: no conversion
Abort trap: 6
试试 运行 std::stoi("five")
自己看看吧!
请不要使用 using namespace std;
。 Just don't!
如果可能的话请 ELI5,因为我只写了几天代码,这是我的第一个程序!下面是我的脚本的一部分,它应该解释某人输入的一行输入(例如“5+5”或其他内容)。
我还有其他格式不同的操作想稍后添加,这就是为什么我使用字符串而不是开关函数或其他东西的原因。
无论如何..这行不通:(下面是我的逻辑过程,也许有人可以指出我哪里搞砸了?:)
提前致谢!
if (fork.find("+" && "-" && "x" && "/"))
{
size_t pos = fork.find("+" && "-" && "x" && "/"); // Defines a position at the operator symbol
string afterfork = fork.substr(pos + 1); // Cuts a substring at the operator symbol + 1
size_t beforepos = fork.find_first_of(fork); // Defines a position at the beginning of the string
string beforefork = fork.substr(beforepos); // cuts a substring at the begninning of the string
string atfork = fork.substr(pos); // cuts a substring that only has one char (the operator +, -, x, etc)
int x = stoi(beforefork.c_str()); // converts the first substring to an integer
int y = stoi(afterfork.c_str()); // converts the third substring to an integer
string operation = atfork; // converts the middle substring that only has one char to a different name.
return input(x, operation, y); // will send this information to the input function (which will do the math for the calculator).
}
要在字符串中搜索字符列表之一,如果没有找到任何内容,您可以使用 find_first_of
. This function returns npos
。
const size_t operatorPos = input.find_first_of("+-*/");
if (operatorPos == std::string::npos) {
std::cout << "Couldn't find an operator!\n";
return;
}
要将字符串拆分为两个子字符串,可以使用substr
. To get a character at a position, use operator[]
。
const std::string left = input.substr(0, operatorPos);
const std::string right = input.substr(operatorPos + 1);
const char operation = input[operatorPos];
要将字符串转换为整数,嗯,有很多选择。当无法将字符串转换为整数时,我将使用 std::stoi
for this answer. This function throws an exception that we need to catch。
int leftInt;
try {
leftInt = std::stoi(left);
} catch (...) {
std::cout << '"' << left << "\" is not a valid integer!\n";
return;
}
int rightInt;
try {
rightInt = std::stoi(right);
} catch (...) {
std::cout << '"' << right << "\" is not a valid integer!\n";
return;
}
如果异常真的令人困惑(我花了很长时间才弄明白异常!)那么您可以尝试另一个函数。我最喜欢的(也是 IMO 最好的)是 std::from_chars
。另一种选择是不捕获异常。
const int leftInt = std::stoi(left);
const int rightInt = std::stoi(right);
在那种情况下,您将不会收到像 "five" is not a valid integer!
这样的友好错误消息。你会得到类似的东西:
libc++abi.dylib: terminating with uncaught exception of type std::invalid_argument: stoi: no conversion
Abort trap: 6
试试 运行 std::stoi("five")
自己看看吧!
请不要使用 using namespace std;
。 Just don't!