how to change string to formular?

I am going to write a program which should handle the math. formular entered by the user. Excactly to say: if the program is statrd, it shows a edit window and the user can enter a math. formular in any form such as (a+b)*x/y, 5*sin(x+a) etc.. The problem is, the program reads the formular as a string, not as a formular. I was told it's quite complicated to write a program to translate the string to a formular.

Dos someone have any good idea?
chenweiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jhanceCommented:
Yes, it is quite complicated to do this as you must parse the input string into tokens your program understands, build the formula dynamically in your program, and then finally solve the function.

The tools YACC and LEX (or their GNU counterparts BISON and FLEX) are quite useful for building C code that can parse and tokenize input strings based on rules you setup.  It's still complicated but it beats the heck out of hand coding such a parser.

O'Reilly publishes a book on YACC and LEX that is a great starting point and has a number of examples similar to this problem.  

http://www.oreilly.com/catalog/lex/
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nietodCommented:
The following code handles the basic math operations (+, -, *, /, and %) with parenthesis.  It converts the "infix" expression to posfix (you'll need to do a little modification for this).  Then the posfix can easily be evaluated.

/*
      Infix.C

      Simple Infix Expression Grammar:

      primairy_expression:
      literal
      (expression)

      multiplicative_expression:
      primairy_expression
      multiplicative_expression * primairy_expression
      multiplicative_expression / primairy_expression
      multiplicative_expression % primairy_expression

      additive_expression:
      multiplicative_expression
      additive_expression + multiplicative_expression
      additive_expression - multiplicative_expression

         This grammar is easily and directly translated into a program. It only
         parses additive (+ and -) and multiplicative (*, / and%) expressions to
         demonstrate implementation of precedence. Other operators and their
         precedence can easily be added.

         Note: Postfix output is obtained by replacing the printf() statements
          with pushes onto the postfix expression (rpn) stack.
      */

      #include <stdio.h>
      #include <ctype.h>
      #include <assert.h>

      void  Error(char*);
      char* SkipWhite(char*);
      int   ParseInfixExpression(char*);
      char* AdditiveExpression(char*);
      char* MultiplicativeExpression(char*);
      char* PrimairyExpression(char*);
      char* LiteralExpression(char*);

      void Error(char* msg)
      {
      assert(msg != 0);
      printf("Error: %s\n", msg);
      }

      char* SkipWhite(char* expression)
      {
      while(expression && isspace(*expression)) ++expression;
         return expression;
      }

      int ParseInfixExpression(char* expression)
      {
      assert(expression != 0);
      expression = AdditiveExpression(expression);
      return !expression || (expression && *expression) ? 0 : 1;
      }


      char* AdditiveExpression(char* expression)
      {
      assert(expression != 0);

      /* multiplicative_expression */
         expression = SkipWhite(MultiplicativeExpression(expression));

      /* additive_expression + multiplicative_expression */
      while(expression && (*expression == '+' || *expression == '-'))
      {
      char* tmp = MultiplicativeExpression(expression+1);
            if(tmp) printf("operator: %c\n", *expression);
            expression = SkipWhite(tmp);
         }
      return expression;
      }

      char* MultiplicativeExpression(char* expression)
      {
      assert(expression != 0);

         expression = SkipWhite( PrimairyExpression(expression) );
      while( expression
          && ( *expression == '*' || *expression == '/' || *expression == '%')
              )
      {
      char* tmp = PrimairyExpression(expression + 1);
            if(tmp) printf("operator: %c\n", *expression);
            expression = SkipWhite(tmp);
         }
      return expression;
      }

      char* PrimairyExpression( char* expression )
      {
      assert(expression != 0);
      expression = SkipWhite(expression);
      if(*expression == '(')
      {
      expression = SkipWhite(AdditiveExpression(expression+1));
      if(!expression)
             return expression;
            else if(expression && *expression != ')')
             Error("Missing ')' in expression");
      else ++expression; // skip the brace
      }
      else
      expression = LiteralExpression(expression);
      return expression;
      }

      char* LiteralExpression(char* expression)
      {
         int cursor = 0;
      assert(expression != 0);
         expression = SkipWhite(expression);

      while( isalnum(expression[cursor]) ) ++cursor;
         if(cursor)
      {
      char tmp = expression[cursor];
            expression[cursor] = '\0';
            printf("operand:  %s\n", expression);
            expression[cursor] = tmp;
         return expression + cursor;
         }
         else
         {
          Error("Bad Literal Expression");
      return 0;
         }
      }

      int main(int argc, char** argv)
      {
      if(argc < 2) return 1;
      printf("Parsing ParseExpression: %s\n",argv[1]);
      ParseInfixExpression(argv[1]);
      return 0;
      }
0
nietodCommented:
The code comes from another expert, kangaroo.
0
chenweiAuthor Commented:
Thanks for the information.

I find it'S pity that I can't give the points to you both. But I will renew my question again and hope Nietod can reply my question again so you can get the points.
0
KangaRooCommented:
Funny...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.