Doing an large enterprise calculation system that requires complex math functions to be stored in the database and used in future simulation scenarios.
I thoroughly understand the process of using Reverse Polish Notation to convert the user-entered equations into a computer-readable form. However, I have problems with tokenizing the equation string. If anyone can point me in the right direction....that'll be awesome
For example, if the user enters:
z = sin(x) / 40 * cos(-50) + log(2.953 + pi)
I have to tokenize the input string into operators and operands. Having problems with this. The language is C#.
code:
string strInput=value.Trim().ToLower() ;
string strNew="";
System.Text.StringBuilder sbOutput=new StringBuilder() ;
string[] Operators=new string[]{"+","-","*","/","sin","cos","pi","tan","sinh","cosh","tanh","asin","acos","atan","log","ln"} ;
// remove whitespaces
foreach (char c in strInput.ToCharArray() )
{
if (!Char.IsWhiteSpace(c) )
strNew+=c;
}
strInput=strNew;
Stack numberStack=new Stack() ;
Stack letterStack=new Stack() ;
ArrayList tokens=new ArrayList() ;
// stuck!!!!! this next loop is wrong.
foreach (char c in strInput.ToCharArray() )
{
if (c=='x')
tokens.Add("x") ;
else if (c=='y')
tokens.Add("y") ;
else if (Char.IsDigit(c) || c=='.')
numberStack.Push(c) ;
else if (Char.IsLetter(c) )
letterStack.Push(c) ;
}
// convert to reverse polish notation