将包含数学的字符串转换为整数的递归函数

问题描述 投票:0回答:1

[如果我有字符串4+6*7/2,我想要一个计算值的函数,在这种情况下为25。为此,我编写了一个递归函数,该函数逐个字符地分析字符串。

基本算法如下(或应该这样):如果我们现在分析的字符是正常密码,并且没有遇到运算符(+,-,*,/),但是我们将其存储在名为first_nr的字符串中,该字符串最终将成为该字符的左侧。运营商。直到遇到操作员之前,我们都会这样做。如果遇到运算符,我们将存储哪个运算符,以便我们以后知道要做什么。因为遇到了一个运算符,所以我们现在必须将以下数字添加到另一个名为second_nr的字符串中,该字符串最终将成为该运算符右侧的数字。 [直到这里我都实现了]当然,我们还需要考虑计算的顺序,因此我将首先忽略所有的优缺点,直到分析所有的时间和除数。

这样,例如,如果我有以下操作字符串:123+789+4。我的函数首先将123放在first_nr中,然后查看一个运算符并将+存储在operator中。因为现在operator != NULL会将以下数字添加为789second_str。结合这三个参数,在这种情况下,我们可以形成first_nr + operator + second_str。我的程序需要在遇到下一个运算符之前执行该程序,因此在这种情况下它将形成123+789并且重复递归。

我做了一些努力,但仍然有很多漏洞。现在,如果我能运行字符串912+4,我将很高兴。因此,请忽略加号之外的所有运算符,忽略计算顺序(第一次加和除,再加上加号和最小值),并忽略一个字符串中的多个运算符。

如果我可以运行最基本的字符串,那么我将改进算法,使其也可以处理更复杂的字符串。

我的努力:

12+5

问题是#include <iostream> #include <string> #include <algorithm> //Enumeration of all the possible //math operators enum Operator { PLUS, MIN, TIMES, DIVIDE, UNDEFINED }; /************************IGNORE********************/ Operator charToOperator(char c) { switch(c) { case '+': return Operator::PLUS; break; case '-': return Operator::MIN; break; case '*': return Operator::TIMES; break; case '/': return Operator::DIVIDE; break; default: return Operator::UNDEFINED; break; } } /***************************************************/ /* * Recursive function performing all the calculations from an action string. * For example, if the string actions has value "5+7" in the first recursive run * result should contain 12 after the last recursion. * * :param result: Double containing the calculated result after the last recursion * :param actions: Action string (what you type in your calculator; e.g: 5+5). We analyze the first character of this string each time and add it to first_nr, second_nr, or make it the operator. First character gets deleted after each recursion * :param first_nr: Empty at first recursion, number of left side of the operator. So in 55+77 this paramater will be "55". Gets resetted at the next operator * :param second_nr: Idem as first_nr but for the right side of the operator. * :param oper: Operation to calculate the first_nr and second_nr */ int calculate(double& result, std::string& actions, std::string& first_nr, std::string& second_nr, Operator& oper) { //Base-condition: //If action string is empty return if (actions == "") { return result; } //Get first character from action string char c = actions[0]; //If first character is an operator char operatorInChar[] = {'+', '-', '*', '/'}; if (std::find(std::begin(operatorInChar), std::end(operatorInChar), c) != std::end(operatorInChar)) { //If operator is not yet set in a previous recursion if (oper == NULL || oper == Operator::UNDEFINED) { oper = charToOperator(c); //If second_nr is not empty, we need to calculate the two numbers together if (second_nr != "") { //Update result result = std::stod(first_nr) + std::stod(second_nr); //For now we only implement plus //Calculation is done, so reset first_nr, second_nr and operator //for the next 'first_nr [operator] second_nr' block in the action string first_nr = ""; second_nr = ""; oper = Operator::UNDEFINED; } } } else { //If the character is not a operator but a number we append it to the correct nr //we add to first_nr if the operator is not yet set, if we already encountered an operator //we add to second_nr. //e.g: actions = "123+789" if (oper == NULL || oper == Operator::UNDEFINED) { first_nr += c; } else { second_nr += c; } } //Remove first character from action string because it's analysed in this recursion actions = actions.erase(0, 1); //Make recursive call return calculate(result, actions, first_nr, second_nr, oper); } int main() { //String we want to calculate std::string str = "5+7"; std::string str_copy_for_output = str; //Variables double result = 0; std::string first_nr = ""; std::string second_nr = ""; Operator oper = Operator::UNDEFINED; //Call function int calculation = calculate(result, str, first_nr, second_nr, oper); //Output std::cout << str_copy_for_output << " = " << calculation << std::endl; return 0; } 始终为0。将不胜感激!

c++ algorithm recursion
1个回答
0
投票

oper是未定义的(由于它不是指针,因此不能为NULL;编译器会警告您),first_nr为“”,second_nr为“”,动作为“ 5 + 7”。

    printf被执行。
  1. oper是未定义的,first_nr是“ 5”,second_nr是“”,action是“ +7”。
  2. first_nr += c;被执行。
  3. oper是PLUS,first_nr是“ 5”,second_nr是“”,动作是“ 7”。
  • 哇! [+]不在oper = charToOperator(c);中,因此actions将永远不会出现在actions[0]中,并且该分支也将不再执行。您可能需要在字符串末尾另外执行它。
  • 热门问题
    推荐问题
    最新问题