Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Data Structure used to solve a simple math equation

Tags:

c++

When taking in a expression like (10+5*15) and following orders of operations.

How would one best solve a problem like this? What kind of data structure is best?

Thanks.

like image 759
RoR Avatar asked Apr 20 '11 21:04

RoR


People also ask

What is data structure maths?

A formal structure for the organization of information. Examples of data structures include the list, queue, stack, and tree.

Is data structures and algorithms have maths?

Not terribly much believe it or not. Algorithms and data structures is a very broad field. Obviously if you want to delve into some kinds of algorithms in particular, you need to know much more math.

What kind of math is algorithm?

An algorithm in math is a procedure, a description of a set of steps that can be used to solve a mathematical computation. For example, a step-by-step procedure used in long divisions is a common example of a mathematical algorithm.


2 Answers

I'd go with Dijkstra's Shunting yard algorithm to create the AST.

like image 180
Drahakar Avatar answered Oct 13 '22 00:10

Drahakar


Try parsing the expression using recursive descent. This would give you a parse tree respecting order of operations.

like image 44
Daniel Lubarov Avatar answered Oct 13 '22 00:10

Daniel Lubarov