-1

I would like to convert a string equation such as 10+20 or even use of numerous operators such as 10+20*5 into an answer such as 110 from a string equation.

I have currently tried looping through each char of the equation string and sorted the numbers and operators, however, I'm not able to get any further than this. Help would be much appreciated!

int calculateFromString(string equation) {
  int numbers;
  string operators;

  // iterate through the equation string
  for (int i = 0; i < equation.length(); i++) {
    if (equation.charAt(i) >= '0' && equation.charAt(i) <= '9') {
      numbers += int(equation.charAt(i));
    }
    if (equation.charAt(i) == '+' || equation.charAt(i) == '-' || equation.charAt(i) == '*' || equation.charAt(i) == '/') {
      operators += equation.charAt(i);
    }
  }
  return numbers;
}

My intended result would be:

calculateFromString("10+20*5")
= 110

I was able to get the operators from the string and store them into the operators string using:

    if (equation.charAt(i) == '+' || equation.charAt(i) == '-' || equation.charAt(i) == '*' || equation.charAt(i) == '/') {
      operators += equation.charAt(i);
    }

However, I'm not sure how I can turn this data into an equation that can be calculated.

Cohen
  • 2,375
  • 3
  • 16
  • 35
  • 3
    You forgot to initialize numbers. Also `numbers + (equation[i] - '0');` does nothing useful. It calculates a value using the uninitialized numbers variable then throws away the result of the calculation. `operators + (equation[i]);` does nothing useful for a similar reason. – drescherjm Jun 02 '22 at 14:23
  • `String operators;` what is a `String` did you mean `std::string`? Remember that `c++` is a case sensitive language so `String` and `string` would be 2 different things. – drescherjm Jun 02 '22 at 14:27
  • Yes, I meant to use std::string – Cohen Jun 02 '22 at 14:29
  • @Cohen -- Just some cautionary words: Is this the extent of the arithmetic operations you will be using? For example, will there be parentheses, functions, etc. that can appear in the equation? If so, in all likelihood, you will have to start over from scratch to implement any further functionality. The issue is that you must know all the requirements up-front before writing an equation solver. That's the nature of the "equation solver" beast. – PaulMcKenzie Jun 02 '22 at 14:34
  • The way you're trying to do it , 10+30*20 would mean 40*20. If you have to take in account precedence of operators, you have to treat string as a syntax tree – Swift - Friday Pie Jun 02 '22 at 14:36
  • If you fixed `numbers + (equation[i] - '0');` to update numbers it would still be wrong in that it would only support 1 digit numbers. For example if the input was 34 it would add 4 to 3. And numbers would be 7 after the second iteration. That is certainly not what you want. – drescherjm Jun 02 '22 at 14:37
  • Thanks for letting me know, currently, I'm just trying to implement basic functionality, so without any arithmetic operations, but asking for help in order to get a basic foundation started. – Cohen Jun 02 '22 at 14:39
  • 1
    I believe you want the formula: `number = (number * 10) + (equation[i] - '0');` – Thomas Matthews Jun 02 '22 at 15:34

1 Answers1

-1

there is several sub problem in what you want to achieve

pseudo code:

for char in equation;
   if char == number
       do stuff
    else if char == operation
       do stuff

the number in the equation, will they only be integer ? that's a single problem its self.

lets assume its only integer...

To test if char is a number you can check if the ascii code is a number (numbers will be between the decimal value of 48 and 57)

Or you can try/catch std::stoi(char)... if no error occurs than its a number. (At some point you will need std::stoi anyway and try/catch error is a good practice to use)

there's a sub problem to that-> how to determine when the number ends ?

than you will have to create a sub routine to determine the math operation priorities, that won't be an easy task.

first use a std::list or std::vector to store string like that:

[number, operation, number, operation]

then i would use two std::map

one for the numbers and its index

the other one operations and its index

with the index you can determine what operation and numbers goes together