1

I have been trying to calculte the variables 'calculate' that is being assigned with the input of 22+3*9/8 . I tried alot but all of my efforts are in vain kindly let me know how can i solve this issue to get output result from

char calculate[10]; 
calculate = "22+3*9/8"; 
cout<< calculate;
Tim
  • 14,999
  • 1
  • 45
  • 68
  • 2
    C++ does not have a standard "eval" mechanism; you would need to find a library to do it, or write an expression parser and evaluator yourself. – geekosaur May 11 '12 at 19:17
  • 3
    possible duplicate of [Convert string to mathematical evaluation](http://stackoverflow.com/questions/9439295/convert-string-to-mathematical-evaluation) – Cody Gray - on strike May 11 '12 at 19:17
  • 1
    The code you've posted doesn't help us to understand what you're having trouble with. We can see that you're assigning `calculate` incorrectly, but there's no attempt to show us how you're evaluating the expression. [Stack Overflow is not your personal research assistant](http://meta.stackexchange.com/a/128553/142865), so please show us what you've tried, and then we can help. – John Dibling May 11 '12 at 19:20
  • Are you asking about C#, C++, or C++-CLI? Identifying the correct programming language might limit the possible answers. – Robᵩ May 11 '12 at 19:22
  • @user1371693 "*c++ and c++-cli*". Which one? Or do you want separate answers for each? – Robᵩ May 11 '12 at 19:30
  • @JerryCoffin: [status-completed](http://meta.stackexchange.com/questions/15650/ban-lmgtfy-let-me-google-that-for-you-links/34456#34456) – John Dibling May 11 '12 at 19:32
  • yeah any one can be very helpful.... – user1371693 May 11 '12 at 19:33
  • 1
    @JohnDibling: Wasn't aware of that. For the people who thought it was a good idea: http://dilbert.com/strips/comic/1989-11-25/ – Jerry Coffin May 11 '12 at 19:41

2 Answers2

3

you need to convert your infix statement to postfix then it is much easier to evaluate your expression using a stack. see e.g. (there are many ways to do this)

after you convert your expression to postfix

22+3*9/8 -> 22 3 9 * 8 / +

you can use a stack to evaluate the expression

when a number, then push on the stack, when an operator, take the two topmost operands on stack and calculate then push result on stack:

expr  stack (top ... bottom)
22    22
3     3 22
9     9 3 22
*     27 22
8     8 27 22
/     3 22        // here u could start using floating numbers instead 3.375 instead of 3
+     25
AndersK
  • 35,813
  • 6
  • 60
  • 86
1

You could use the new Roslyn API in C# and then call it from C++ API.

Then you could use the following example:

 using Roslyn.Compilers;
 using Roslyn.Scripting;
 using Roslyn.Scripting.CSharp;


 public int Eval(string expr) {
       var engine = new ScriptEngine();
       return (int)engine.Execute(expr);
 }

Warning though, you should probably check that the expression does not contain illegal characters or other kind of thing as this would probably be a security bug.

Yet Another Geek
  • 4,251
  • 1
  • 28
  • 40