chenwei
asked on
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?
Dos someone have any good idea?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The code comes from another expert, kangaroo.
ASKER
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.
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.
Funny...
/*
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(c
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*
{
assert(expression != 0);
expression = AdditiveExpression(express
return !expression || (expression && *expression) ? 0 : 1;
}
char* AdditiveExpression(char* expression)
{
assert(expression != 0);
/* multiplicative_expression */
expression = SkipWhite(MultiplicativeEx
/* additive_expression + multiplicative_expression */
while(expression && (*expression == '+' || *expression == '-'))
{
char* tmp = MultiplicativeExpression(e
if(tmp) printf("operator: %c\n", *expression);
expression = SkipWhite(tmp);
}
return expression;
}
char* MultiplicativeExpression(c
{
assert(expression != 0);
expression = SkipWhite( PrimairyExpression(express
while( expression
&& ( *expression == '*' || *expression == '/' || *expression == '%')
)
{
char* tmp = PrimairyExpression(express
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(AdditiveExpressi
if(!expression)
return expression;
else if(expression && *expression != ')')
Error("Missing ')' in expression");
else ++expression; // skip the brace
}
else
expression = LiteralExpression(expressi
return expression;
}
char* LiteralExpression(char* expression)
{
int cursor = 0;
assert(expression != 0);
expression = SkipWhite(expression);
while( isalnum(expression[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[
return 0;
}