I'm writing a math program and I have a big problem. The program needs for input a math function like 3x+2. But if I read it with the scanf, it thinks that it is a string and not a function. I would like be evaluated this function for different values of x. Is it possible to convert that string to a mathematical function.
From article <jrioux1...@act.ulaval.ca>, by jrio...@act.ulaval.ca (Jacques Rioux):
> I'm writing a math program and I have a big problem. The program needs for > input a math function like 3x+2. But if I read it with the scanf, it > thinks that it is a string and not a function. I would like be evaluated > this function for different values of x. Is it possible to convert that > string to a mathematical function.
Does the word 'parser' mean anything to you? Ever heard of 'yacc'? If you have, then you know what you have to do; if you haven't, well... -- - Paul J. Lucas AT&T Bell Laboratories Naperville, IL
In article 2...@act.ulaval.ca, jrio...@act.ulaval.ca (Jacques Rioux) writes:
> I'm writing a math program and I have a big problem. The program needs for >input a math function like 3x+2. But if I read it with the scanf, it >thinks that it is a string and not a function. I would like be evaluated >this function for different values of x. Is it possible to convert that >string to a mathematical function.
I don't know if you have experience programming enough. The input for a program (using scanf or not) can be only strings or numbers. If you want convert the input string to a function, you should build lots of procedures and data structures to handle it. For example: you can convert the "formula" string into a binary tree and then build functions to evaluate it.
3 x + 2
root (+) / \ (*) 2 / \ 3 x struct tree { typenode node; struct tree *left,*right;
};
int evaluate (struct tree t) { if (number(t.node)) return (number (t.node)); return (operation (t.node,evaluate(t->left),evaluate(t->right)));
}
number :: function that return the value of numerical node. operation :: function that make the operation t.node with our parameters.
GREETINGS FROM SPAIN (SALUDOS)
=========================================================================== === _/_/_/_/_/_/_/_/_/_/ | | Jose Antonio Espinosa _/ _/ | | Laboratorio de Inteligencia Artificial _/_/_/_/_/_/ | | Facultad de Informatica _/ _/ _/ | | Universidad Politecnica de Madrid _/_/ _/_/_/_/ | | espin...@alcala.dia.fi.upm.es =========================================================================== ===
In article <jrioux1...@act.ulaval.ca> jrio...@act.ulaval.ca (Jacques Rioux) writes: >Hi everyone,
> I'm writing a math program and I have a big problem. The program needs for >input a math function like 3x+2. But if I read it with the scanf, it >thinks that it is a string and not a function. I would like be evaluated >this function for different values of x. Is it possible to convert that >string to a mathematical function.
You should be able to use the atoi function in standard c library to convert ASCII to integer.