Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having a doubt. I searched a lot but unable to find the exact output I have a int number[4] array. say for example

number[0] = 5;
number[1] = 7;
number[2] = 8;
number[3] = 9;

Now the scenario is that I have to multiply/divide/add/subtract these numbers to some desired sum.

number[0] operator1 number[1] operator2 number[2] operator3 number[3]

These operators are to be selected by the user or can be random and these operators can be at any place. Like operators can be *, + etc

Now is there a way to store the arithmetic operators in an array so that we can make directly make a regular expression like this:

number[0] op[0] number[1] op[1] number[2] op[2] number[3]

considering op is an array of operators:

In this way the compiler will handle the BODMAS itself and we don't have to worry about that

OR

Is there a way to convert a string like this "6*4-4/5" to an expression in c++ such that I can directly get the result in an integer variable?
Posted
Updated 5-Jun-14 18:39pm
v2
Comments
[no name] 6-Jun-14 1:22am    
You have to create a parser for that.

Not sure where you are going with this but

char Number[8] = "6*4-4/5";

IS LITERALLY THE ANSWER .... THERE IS NOTHING TO DO

YOU ASKED A QUESTION YOU ACTUALLY GAVE THE ANSWER TOO.

Look at Number under the debugger and you will see this
C#
Number[0] = '6';
Number[1] = '*';
Number[2] = '4';
Number[3] = '-';
Number[4] = '4';
Number[5] = '/';
Number[6] = '5';
Number[7] = 0;


All you need to do is make a string evaluate function ... you know like
C#
bool evaluate(const char *expression, int &result)
{
...
}


Take that string and strip it and evaluate it character by character. I will even give you a hint if the number is bigger than 1 character you have to do the number bit as two stages find start and find end.

If you google it there will be thousands of things on evaluating a string sequence because it is part of any computer programming coarse.

I suspect this is homework which is why I wont fill the function in for you.
 
Share this answer
 
v4
No, the compiler will definitely not handle anything that can only be evaluated at runtime!

1. You need a type to represent the operators, there is no built-in type for that.
2. Then you need a parser that can distinguish the numbers from the operators and read them from the string, one by one.
3. Then you need to parse/interpret the resulting expression. If you don't wish to rely on existing frameworks for that purpose (see solution 1), then brace yourself for some extensive work, building an expression tree.
4. Work your way through the interpreted expression tree to execute each part of the expression, step by step.

Note: it's much easier to interpret and evaluate expressions in RPN[^]. Maybe you'd like to try your hand on that problem first.
 
Share this answer
 
v2
Use Boost.Spirit[^] or Ultragram[^]. But I don't know how your experience level is to use them.
 
Share this answer
 
You may either
  • Hand craft a (very simple indeed) mathematical expression parser (you may find many example just Googling[^] for).

or
  • Embed in your application a scripting language that will evaluate the string for you (for instance, embedding Lua[^] is pretty straightforward).


Additionally, as already suggested, you may also use a tool (however for such simple expressions, something like Boost.Spirit is, in my opinion, overkill).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900