0

My previous question was put on hold as question was not clear.

Posting it again with specific query

My requirement is to solve any mathematical complex expressions using PHP. For example,

If I have a string "1(1+2)/3 + 4", using BODMAS rule, I have to solve it. But I have to also get all individual steps.

Two sites I have referred are:
http://www.careerbless.com/calculators/ScientificCalculator/
http://web2.0calc.com/

My first question is , which is the ideal language for these kind of problems. Also, is there any built in solution available so that I don't have to reinvent the wheel

Thanks in advance

Mohammed Shareef C
  • 3,829
  • 25
  • 35
AVC
  • 67
  • 9
  • ircmaxell has provided an excellent (and safe) back-end formula evaluator in his answer to [this question](http://stackoverflow.com/questions/12692727/how-to-make-a-calculator-in-php)... to get the actual steps used, you can display the stack after parsing and before execution – Mark Baker Oct 15 '13 at 19:44
  • 2
    You know you can *edit* a closed question instead of opening a new one, right? We (the community) will generally reopen a question once its problems have been fixed. That's what *on hold* means. – kojiro Oct 15 '13 at 19:45
  • @Mark Baker, thanks, this is what I was looking for. Now I can built on that – AVC Oct 15 '13 at 19:48
  • eval() may be in action this kind of calculator. – jacouh Oct 15 '13 at 19:50
  • @kojiro, sorry, I will take care of that – AVC Oct 15 '13 at 19:51
  • Looks like someone needs help with their homework... – Gavin Oct 15 '13 at 19:52
  • @jacouh - if the expression is coming from user input, then there's a lot of risk in using eval() and if the formula contains anything like "3/0" then eval() is lousy for handling errors – Mark Baker Oct 15 '13 at 20:04
  • @Mark Baker, I'll make a mistake, that is to say: PHP is dangerous, Web is dangerous, to do nothing is the most secured... this is not the philosophy of Anglo-Saxon People, neither of those of classical etheny... OK, I agree that you must check carefully the input of eval(). – jacouh Oct 15 '13 at 20:14

1 Answers1

1

Stacks may help you, consider: 1 + (2 + 3)

Create two stacks, one of numbers, other with operators.

When you find a number, put in the stack of numbers, when you find a operator, put in the stack of operators, when you find "(" do nothing, and when you find a ")", get two numbers of a stack and one operator, do the calc of these two numbers and put again in the stack of numbers, see the code loop:

//1 + (2 + 3)
operators = []
numbers = []

//I put the number in the numbers stack
//+ (2 + 3)
operators = []
numbers = [1]
-- 
// I put the operator in the operators stack
// (2 + 3)
operators = [+]
numbers = [1]
-- 
// 2 + 3)
//find a "(" char, do nothing
operators = [+]
numbers = [1]
-- 
// + 3)
operators = [+]
numbers = [1, 2]
-- 
// 3)
operators = [+, +]
numbers = [1, 2]
-- 
// )
operators = [+, +]
numbers = [1, 2, 3]
--
//found a ")", get two items of stack of numbers (3,2), and one item of operators stack (+), result is 5, put back in the stack of numbers 
operators = [+]
numbers = [1, 5]
--
//When finished string, get two elements and one operator until finish the stack
// get two elements of numbers stack (1,5) and one of operators stack +, result is 6

I hope that it help you

viniciuswebdev
  • 1,286
  • 1
  • 11
  • 20