C/C++: Passing a string read to a loop inside a program in C -


i working in program should read user inputted string , use string inside loop basic calculation. general problem use of string inside loop and, despite fact later have more, need test bolzano's theorem (in particular find possible intervals roots) inputted expression. started working function reads input char char , switch controls calculate infix expression. however, know if there way that:

{     /*already stored doubles: a, b, precision.*/     printf("enter expression want calculate");     scanf("%s", expression);     /*user input: (3*i)+(i*i)-3*/     while( <= b ){        fa = (3*i)+(i*i)-3; /*how insert string here*/        fb = (3*(i+precision))+((i+precision)*(i+precision))-3;        if( fa*fb < 0 )           printf("there @ least 1 root in interval: [ %g, %g ].", a, a+precision);        /*and goes on...*/ } 

supposing user not input invalid expression use in loop (such i^7) or implemented controls convert expressions (something read string , change postfix or prefix notation doing necessary conversion), there way use stored string in assignment loop?

i think you're asking is: given string mathematical expression, how evaluate expression?

i don't know if can make assumptions syntax of expression; if simplify code. if not, have "tokenize" string first, above string sequence of tokens this:

lbracket, num, binop, var, rbracket, binop, lbracket, var, binop, var, rbracket, binop, num 

you'd have build little parser makes sure expression formed based on syntactic rules; parser consume sequence of tokens. output of parser data structure (usually tree) represents expression. given tree, can evaluate expression computing sub-expressions bottom up.

fa =                       -                           / \                          +   3                         / \                        *   *                       / \ / \                      3  i  

it matter of adding new nodes tree add precision operands.

there's stuff online on how this. search "how build expression evaluator" , example this page comes more details.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -