Stringy Sums

This is a discussion on Stringy Sums within the C++ Programming forums, part of the General Programming Boards category; Say I had: Code: (6/2*(3+4))/6 all stored in a string...would it then be possible me to calculate that sum in ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    545

    Stringy Sums

    Say I had:
    Code:
    (6/2*(3+4))/6
    all stored in a string...would it then be possible me to calculate that sum in any way?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Do a board search for "reverse polish"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Or a Google™. There's also this nifty site called "Wikipedia", rings a bell?
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    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-

  5. #5
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    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

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    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.

  7. #7
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    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.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    I don't really get you there...I will have all of my stuff stored in a string.

  9. #9
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    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.

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Well what I have is theuser inputting their equation into a string and then it is converting it to RPN.

  11. #11
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Show some code? I don't know how you're storing the converted RPN expression.

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Well I may be completely wrong but I haven't quite finished the code

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    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

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    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.

  15. #15
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    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?
    Last edited by twomers; 05-15-2006 at 01:59 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays & Sums
    By mrlooneytoon in forum C Programming
    Replies: 9
    Last Post: 04-05-2007, 06:35 PM
  2. Sums...
    By karb0noxyde in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2004, 05:39 PM
  3. Calculating Sums
    By faxtoaster in forum C Programming
    Replies: 3
    Last Post: 07-10-2003, 08:23 PM
  4. Sums of an array ??
    By RedRattler in forum C Programming
    Replies: 2
    Last Post: 04-04-2003, 07:23 PM
  5. C++ problem w/ sums
    By Kaoru in forum C++ Programming
    Replies: 5
    Last Post: 09-16-2002, 01:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21