• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

how to evaluate mathematical expression java

Abdullah

New Member
Joined
Feb 15, 2012
Messages
1 (0.00/day)
Hi guys ...
I'm working on java project I want to build a calculator where the user enters an String and I should take it and covert it to mathematical expression and do the operations , my question is how do I convert this String and evaluate it to mathematical expression , is there are way to do it on Complex numbers ? :confused:

Thank you a lot..
 

xbonez

New Member
Joined
Nov 29, 2010
Messages
1,182 (0.24/day)
Location
Philly, PA (US)
System Name Winter
Processor AMD Phenom II x4 965 BE @ 4.0Ghz
Motherboard MSI 790FX-GD70
Cooling Corsair H50 Liquid Cooling
Memory 2 x 2Gb Gskill Ripjaws 1600Mhz (7-7-7-24@1.6V)
Video Card(s) Asus GTX 470 @ Stock (Zalman VF3000 cooler)
Storage 2 x Samsung Spinpoint F3 500GB (RAID 0)
Display(s) Hanns G 28" @ 1920x1200
Case Antec 1200
Audio Device(s) Onboard -- TosLink --> Z5500
Power Supply Corsair 850TX 850W PSU
Software Win 7 64-bit Ultimate
If you're looking to convert strings like this: "2+3x4.5" and "2+(2*4+5)*8" etc into mathematical expressions, you'll have to write a basic parser of your own that expects numbers, decimal numbers, parenthesis, mathematical operators etc and builds a tree like structure that you can then traverse and compute.
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
Yes, you will need some kind of parser and tokenizer so the program understands what to do with the various input characters. You also will have to consider operator precidence and error checking for both invalid characters and valid characters that produce an invalid expression.
 
Joined
Aug 10, 2007
Messages
4,267 (0.70/day)
Location
Sanford, FL, USA
Processor Intel i5-6600
Motherboard ASRock H170M-ITX
Cooling Cooler Master Geminii S524
Memory G.Skill DDR4-2133 16GB (8GB x 2)
Video Card(s) Gigabyte R9-380X 4GB
Storage Samsung 950 EVO 250GB (mSATA)
Display(s) LG 29UM69G-B 2560x1080 IPS
Case Lian Li PC-Q25
Audio Device(s) Realtek ALC892
Power Supply Seasonic SS-460FL2
Mouse Logitech G700s
Keyboard Logitech G110
Software Windows 10 Pro
I suppose for a decent calculator you'll need to do the above. Otherwise you can cheap out and just eval() the expression after replacing x's with *. ;)
 

xbonez

New Member
Joined
Nov 29, 2010
Messages
1,182 (0.24/day)
Location
Philly, PA (US)
System Name Winter
Processor AMD Phenom II x4 965 BE @ 4.0Ghz
Motherboard MSI 790FX-GD70
Cooling Corsair H50 Liquid Cooling
Memory 2 x 2Gb Gskill Ripjaws 1600Mhz (7-7-7-24@1.6V)
Video Card(s) Asus GTX 470 @ Stock (Zalman VF3000 cooler)
Storage 2 x Samsung Spinpoint F3 500GB (RAID 0)
Display(s) Hanns G 28" @ 1920x1200
Case Antec 1200
Audio Device(s) Onboard -- TosLink --> Z5500
Power Supply Corsair 850TX 850W PSU
Software Win 7 64-bit Ultimate
If you decide to go the Parser route, I would recommend either Coco/R or ANTLR. Both are Parser generators that take a grammar file and generate the required parser. I've personally worked with both (Google Dart uses ANTLR, btw) and like Coco/R for smaller projects. Writing the grammar file will be fairly trivial in your case.
 

Dolph

New Member
Joined
Feb 8, 2012
Messages
114 (0.03/day)
I did this for one of my c class projects, it was just a simple calculator, i used a binary tree to sort the expression. Ex would be like, as you parse the string, if it sees a number, then an operator, it createsthat node with the root ofthe operator, with a series of push/pops onto stacks you eventually create a fully expressed binary tree which when you do an in-order traversal it reads it back as you typed it, and can evaluate it based on depths of the tree for order of operations. I feel like this method might be alittle complex for an OO language but, for C that was the easiest to do. The technique coudl still be transferable.
 
Top