# Stringy Sums

• 05-14-2006
bumfluff
Stringy Sums
Code:

`(6/2*(3+4))/6`
all stored in a string...would it then be possible me to calculate that sum in any way?
• 05-14-2006
Salem
Do a board search for "reverse polish"
• 05-14-2006
jafet
Or a Google™. There's also this nifty site called "Wikipedia", rings a bell?
• 05-14-2006
bumfluff
Yep...I looked at the stuff on Wikipedia asked my Dad as he is a fair good Maths person and I understand how you order it...but how do you get a program to output say 3+4-
• 05-14-2006
OnionKnight
There is no standard way. You can write our own math expression interpreter that works recursively on parentheses. You can also make compile your math expression into reverse polish notation format which allows you to deal with the expression easier since parentheses are unnecessary when using it. Executing a compiled expression is also faster than interpreting it which is nice when you want to execute it fast in rapid succession, like when you want to graph a function say "x^2 + 4*x + 12".
You can use the shunting yard algorithm to convert from infix to RPN.
http://en.wikipedia.org/wiki/Reverse...infix_notation
• 05-14-2006
bumfluff
No I mean how can I get the result of the sum I can think of ways in which I could convert to RPN but not to do the sum.
• 05-14-2006
OnionKnight
After converting it to RPN calculating the sum will be easy. You'll have a stack that contains both numbers and operators like this:
6 2 / 3 4 + * 6 /
Then you traverse the stack. If a number is found add that number to new stack which we can call the number stack. If an operator is found pop as many numbers needed for the operator, perform the operation and push the result back to the number stack. In this case all the operators are binary operators which means they take two operands so you pop two numbers from the stack.

In steps,
Stack: 6 2 / 3 4 + * 6 /
Num stack:

Stack: 2 / 3 4 + * 6 /
Num stack: 6

Stack: / 3 4 + * 6 /
Num stack: 2 6

Stack: 3 4 + * 6 /
Num stack: 3

Stack: 4 + * 6 /
Num stack: 3 3

Stack: + * 6 /
Num stack: 4 3 3

Stack: * 6 /
Num stack: 7 3

Stack: 6 /
Num stack: 21

Stack: /
Num stack: 21 6

Stack:
Num stack: 3.5

The result is now at the top of the number stack.
• 05-14-2006
bumfluff
I don't really get you there...I will have all of my stuff stored in a string.
• 05-14-2006
OnionKnight
If you converted it to RPN you wouldn't be having it in a string. Well you could but I don't see why.
If you don't really feel up to doing any RPN conversion you could just interpret the expression.
• 05-14-2006
bumfluff
Well what I have is theuser inputting their equation into a string and then it is converting it to RPN.
• 05-14-2006
OnionKnight
Show some code? I don't know how you're storing the converted RPN expression.
• 05-14-2006
bumfluff
Well I may be completely wrong but I haven't quite finished the code
• 05-14-2006
bumfluff
Code:

```#include <iostream> #include <string> #include <cstdlib> using namespace std; int main() {       string sum = "";       string output= "";       string stack = "";       cout << "Please type in your sum: ";       getline(cin, sum);       for (int i = 0; i < sum.length(); i++) {       char x = sum.at(i);       switch (x) {       case '1':           output += x;           break;       case '2':           output += x;           break;       case '3':           output += x;           break;       case '4':           output += x;           break;       case '5':           output += x;           break;       case '6':           output += x;           break;       case '7':           output += x;           break;       case '8':           output += x;           break;       case '9':           output += x;           break;       case '0':           output += x;           break;       case '+':           stack += x;           break;       case '-':           stack += x;           break;       case '*':           stack += x;           break;       case '/':           stack += x;           break;       default :           cout << "Error with input";           cin.ignore();           cin.get();           return 0;       }       }       output += stack;       cout << output << endl;       system("PAUSE");       return 0; }```
That is what I have done...I may be wrong in what I have done and I have yet to add parenthesis
• 05-14-2006
bumfluff
Code:

```case ')':           int z;           z = sum.find_last_of('(', 0);           int n;           n = ++z;           output += sum.substr(n);           sum.erase(z);           break;```
It seems to delete the wrong part of the string when I try that...why?

How can I get it to delete the bit on the end that I need to get rid of.
• 05-15-2006
twomers
Ok. If I was going to do this, I would do it this way:

Code:

```#include <iostream> #include <string> #include <cstdlib> using namespace std; int main() {         string sum = "";         cout<< "Please type in your sum: ";         cin >> sum;         int        x = 0;         double output = 0;         for (int i = 0; i < sum.length(); i++)         {                 x = sum.at(i);                 switch ( x )                 {                         case '1':                                 output += ( x - '0');                                 break;                         case '2':                                 output += ( x - '0');                                 break;                         case '3':                                 output += ( x - '0');                                 break;                         case '4':                                 output += ( x - '0');                                 break;                         case '5':                                 output += ( x - '0');                                 break;                         case '6':                                 output += ( x - '0');                                 break;                         case '7':                                 output += ( x - '0');                                 break;                         case '8':                                 output += ( x - '0');                                 break;                         case '9':                                 output += ( x - '0');                                 break;                         case '0':                                 output += ( x - '0');                                 break;                         case '+':                                 output += ( sum[i+1] - '0' );                                                                 i++;                                 break;                         case '*':                                 output *= ( sum[i+1] - '0' );                                                                 i++;                                 break;                         default :                                 cout << "Error with input";                                 cin.ignore();                                 cin.get();                                 return 0;                 }         }         cout << output << endl;         return 0; }```
This works, but it doesn't have any hierarchy of operators, if you know what I mean. It just works from the left hand side to the right hand side.

This is essentially your code, but the changes I made were that the output is an integer (it'd probably be better to have a float or something), and in all the operators, because the input is a string, I subtracted '0' from it ... ie:

Code:

```'1' = 31 (from memory, so it's prob wrong), in ASCII, '1' - '0' = 1 in ascii. int Var = '1' - '0'; cout<< Var;```
Output: 1

get me?