1

I have equations like (7+((8%2)(7%3))). I want to solve this using bod-mas: first brackets should be solved after it divide, multiple, addition and subtraction.

I have the program in which the user creates a formula using salary heads like basic pay, etc. After that I find their value like the equation above and now I want to solve it.

I visited http://www.codeproject.com; they gave a solution like:

string equationString = "(7+((8%2)(7%3)))";
string explicitMultiply = equation String.Replace(")(", ")*(");

But it doesn't work. Why?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Rajbir Singh
  • 37
  • 1
  • 5
  • Maybe you mean [how to parse math expression](http://stackoverflow.com/questions/355062/is-there-a-string-math-evaluator-in-net) (assuming [bodmas](http://en.wikipedia.org/wiki/BODMAS) means order of operations)? – Alexei Levenkov Oct 08 '13 at 05:36
  • 2
    The OP is really asking about implicit multiplication. This is an interesting question. The silent downvotes and the inept editing are unfortunate, but typical of SO. – Philip Sheard Oct 08 '13 at 05:45
  • You're making a whole lot of assumptions about what's being asked, especially considering that there's a thread implying `%` is intended to be a "percentage of" operator. – Preston Guillot Oct 08 '13 at 06:04
  • The CodeProject project that is (badly) linked to seems to try to solve the implicit multiplication problem. There's nothing to indicate that the OP is after the same thing. – Avner Shahar-Kashtan Oct 08 '13 at 07:08

4 Answers4

2

C# has it operator precedence,

((8%2)(7%3)) //I tired but it is giving me an error Method Name expected

This is the result

(8 % 2) //return 0
(7 +0 ) // returns 7

Finally

(7 + ((8 % 2)*(7 % 3)))  //return 7

Updates:

Your code is completely a string which will not be executed to return integer

string equationString = "(7+((8%2)(7%3)))";
 string explicitMultiply = equationString.Replace(")(", ")*(");
 Console.WriteLine(explicitMultiply); 

returns (7 + ((8 % 2)*(7 % 3))) as a string

Incase if you want to do an equation conversion like NCalc or FLEE

Also take a look at BODMAS principle in .NET

Community
  • 1
  • 1
Praveen
  • 55,303
  • 33
  • 133
  • 164
  • but i want first the percentage of 8 that is 0.16 that multiple with 7%3 that will 0.21 and the answer will 7.0336 – Rajbir Singh Oct 08 '13 at 05:45
  • @RajbirSingh `(8 % 2)` returns 0 not 0.16. check this http://msdn.microsoft.com/en-us/library/0w4e0fzs.aspx – Praveen Oct 08 '13 at 05:48
  • @RajbirSingh The `%` operator in most programming languages is *modulus* (remainder after division), not *percentage*. If you want it to read `8%2` as *8 percent of 2* then you have a lot of work ahead of you. – Corey Oct 08 '13 at 05:51
  • i know sir it returns the modules of 8%2 that is 0 but i want it must use like percentage – Rajbir Singh Oct 08 '13 at 05:56
  • yes exotically my problem to read it not as modules read it as percentage and the brackets to solve first – Rajbir Singh Oct 08 '13 at 05:59
  • @RajbirSingh What do you mean by to do as percentage operation? % in c# is modulus operator. I'm not sure `8*100/10` – Praveen Oct 08 '13 at 06:31
0

You are really asking about implicit multiplication. The solution that you quote will not work for cases such as "7(3+4)". It will only work when there is a bracket on both sides of the implicit multiplication.

Implicit multiplication is difficult to implement in practice. The rule is saying that if parsing fails because an operator is missing, that a multiplication sign should be added.

The quoted solution is on the right lines, but incomplete.

Philip Sheard
  • 5,789
  • 5
  • 27
  • 42
  • i want the logic to solve first equitation between the brackets (7+((8*2)*(7*3))) that first 8*2 solve then 7*3 then both multiple like 16*21=336 and then 7 add – Rajbir Singh Oct 08 '13 at 06:40
  • In that case you have two problems: parsing an algebraic expression, and implementing implicit multiplication. You need to start by finding an innermost bracket, and then proceeding recursively. An innermost bracket is spanned by a pair of bracket symbols that do not contain any other bracket symbols. In the example that you give, (8*2) is an innermost bracket. – Philip Sheard Oct 08 '13 at 07:38
  • This is not difficult. You just need to find the maximum depth to which any expression is nested. – Philip Sheard Oct 08 '13 at 14:46
0

If you want to evaluate that string, then you can use http://ncalc.codeplex.com/ which is a open source framework.

And also you can evaluate it using the method defined at

Evaluating string "3*(4+2)" yield int 18

Which is like :

    string equationString = "(7+((8%2)(7%3)))";
    string explicitMultiply = equationString.Replace(")(", ")*(");
    double f = Evaluate(explicitMultiply);

    static double Evaluate(string expression)
    {
        var loDataTable = new DataTable();
        var loDataColumn = new DataColumn("Eval", typeof(double), expression);
        loDataTable.Columns.Add(loDataColumn);
        loDataTable.Rows.Add(0);
        return (double)(loDataTable.Rows[0]["Eval"]);
    }

I hope it will help you. :)

Community
  • 1
  • 1
Hitesh
  • 3,508
  • 1
  • 18
  • 24
0

Looks like you need a two step process: first, you need to go through your string expression and replace '%' with '/ 100 *' and then you can you use Alexei's how to parse math expressions.

Community
  • 1
  • 1
Danexxtone
  • 773
  • 4
  • 11
  • thank you sir it works. but when i operate this 3(4*2) it shows syntax error and how can i check the user input expression is in correct format – Rajbir Singh Oct 09 '13 at 05:23
  • if i replace ( with '*(' then if i have equation like 3((482/100)*2) create problem please tell me if anyone have solution of it – Rajbir Singh Oct 09 '13 at 08:31
  • Could you edit your original question and show us the code you have so far? More than likely, it's just a matter of tweaking the parsing of the string. – Danexxtone Oct 09 '13 at 12:45