Friday 15 July 2011

How to evaluate a mathematical expression using strings and arrays in C -



How to evaluate a mathematical expression using strings and arrays in C -

i want evaluate mathematical look stored string, eg. "1+3/4*3".

my thought accomplish this:

read in string store in array, extract operators temporary array , sort according mathematical order of operations. extract values temporary array, in turn evaluates look using right operation.

does logic sound right? if so, can please show me illustration of basic version of 3+1-3.

look shunting yard algorithm , convert string reverse polish notation.

it help if separate out number detection symbol detection reading string 1 character @ time instead of jumping operators.

after that, rather easy perform computation, inputs ordered in manner makes stack based calculator easy implement.

yes, total fledged recursive descent parser; but, relatively simple matter of algebraic expressions, overkill.

--- edited because see there's controversy on tools vs techniques ---

i see lot of people mentioning lexx , yacc. great tools, overkill need. it's saying open carpentry shop when want replace board on fence. don't need larn language handle math language, , lexx , yacc require larn domain specific configuration language build parser domain specific language. that's lot of languages simple, solved problem.

lexx , yacc great if want build mathematical tree of data; however, rpn mathematical tree of info stored in list of 2 fundamental node types (data) , (operation).

(a)(b)(c) // 3 "data nodes" (a)(b)(+) // 2 "data nodes" , 1 operation node.

this allows 1 utilize stack based machine (very easy implement). machine has "instruction set" of

if (x data) { force x onto top of stack } if (x operation) { pop operation's required parameters. pass them operation. force result on stack }

so assuming had '+' operator, look 3 + 5 + 6 convert rpn 3 5 + 6 + , stack (during processing)

<empty> (3) (3)(5) (8) (8)(6) (14)

the primary reason convert rpn not because it's necessary, it's because makes rest of programme much easier implement. imagine 3 + 5 * 7 converted rpn, have 3 5 7 * + means don't have special evaluation "machine" language. note (3 + 5) * 7 converts rpn 3 5 + 7 *. in short, cut down complexity of evaluation engine massaging input info less ambiguous.

lexx , yacc provide lot of "configurability" allow accomplish same thing; however, not going configuring lexx , yacc special here, configurable interface doesn't purchase much. it's having multiple selection knob when need on-off switch.

now, if want build any kind of parser, not aware of any kind of future rules might added; definately take lexx , yacc. simple algebraic look parser far "solved" problem bring in big guns. utilize shunting algorthim , break problem downwards into

simple scanner identify main types of "tokens", number, +, -, /, *, (, , ) shunting algorithim convert "list" of tokens rpn list stack hold "memory" of evaluation "machine" "machine" pull / force / evaluate loop

this has added benefits of beingness able develop pieces independently, can verify each step without entire framework beingness in place. lexx , yacc promote adding code framework, unless take steps otherwise, might have debug entire thing 1 big piece.

c arrays string evaluation

No comments:

Post a Comment