C++ functions

1 view
Skip to first unread message

Jacques Rioux

unread,
May 25, 1993, 6:55:08 PM5/25/93
to
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.

Thank you

Jacques Rioux

Paul J Lucas

unread,
May 26, 1993, 9:19:52 AM5/26/93
to
From article <jriou...@act.ulaval.ca>, by jri...@act.ulaval.ca (Jacques Rioux):

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

Jose A. Espinosa Villanueva

unread,
May 26, 1993, 9:30:40 AM5/26/93
to

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
_/_/ _/_/_/_/ | | espi...@alcala.dia.fi.upm.es
==============================================================================

BGAA

unread,
May 26, 1993, 2:01:31 AM5/26/93
to
You should be able to use the atoi function in standard c library to
convert ASCII to integer.


Reply all
Reply to author
Forward
0 new messages