Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,424 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,957 people online right now. Registration is fast and FREE... Join Now!




Returning a value determined by an equation

 
Reply to this topicStart new topic

Returning a value determined by an equation, Parsing an equation in string form.

TheMagnitude
17 Jan, 2008 - 11:51 AM
Post #1

D.I.C Head
Group Icon

Joined: 12 Jan, 2008
Posts: 88


Dream Kudos: 125
My Contributions
Hey

I've had a hard think and searched around and I don't know where to begin doing this. What I want to know is how to:
depending on a user inputted equation as a string, assign it to a function so say the equation string was "7*x*x+3" how would i use this to make a function:
CODE
int f(int x)
{
    return 7*x*x+3;
}

This is done while the program is running. I understand that code cannot be edited while the program is running becuase its compiled but is there any way to do this?

Help is appreciated.

This post has been edited by TheMagnitude: 17 Jan, 2008 - 01:43 PM
User is offlineProfile CardPM
+Quote Post

VernonDozier
RE: Returning A Value Determined By An Equation
17 Jan, 2008 - 01:16 PM
Post #2

New D.I.C Head
*

Joined: 6 Jan, 2008
Posts: 46

You'd have to somehow parse the string into numeric literals (integers, doubles), identifiers (variables like x), and operators (like * and +). This can be pretty involved depending on your grammar rules and how complex an equation you are allowed to enter, and whether you can assume valid input. But for an equation like:

7 * x * x + 3

you would have to substitute each token into one of these categories:

1) Numeric literal ( 7, 3)
2) Identifier (x)
3) Operator (*, +)

Doing this can be quite involved. I'd start with simple grammar operations like:

6 + 3

and work onto more complex ones
User is offlineProfile CardPM
+Quote Post

TheMagnitude
RE: Returning A Value Determined By An Equation
17 Jan, 2008 - 01:54 PM
Post #3

D.I.C Head
Group Icon

Joined: 12 Jan, 2008
Posts: 88


Dream Kudos: 125
My Contributions
To do this I would also have to BODMAS the equation and parse the brackets.

I think a good way to initially parse (or simply replace) this is convert the identifiers into their corresponding numeric values eg:

x=4
(x+3)(x-7)
step 1: insert multiplication signs in between brackets, and in between identifiers and numbers (7x-------->7*x)
(x+3)*(x-7)
step 2: replace identifiers with true values
(4+3)*(4-7)
step 3: assign numeric values to arrays?
step 4: parse operators and do the sums
(7)*(-3) ----------> 7*-3 = -21

Im sure if we all work together we can get a pretty good script outta this.
User is offlineProfile CardPM
+Quote Post

VernonDozier
RE: Returning A Value Determined By An Equation
17 Jan, 2008 - 03:45 PM
Post #4

New D.I.C Head
*

Joined: 6 Jan, 2008
Posts: 46

Here's a program for a very simple grammar. It takes in as input an integer, then an operand, then an integer, and displays the result:

6*3=18
6-3=3
6+3=9
6/3=2

No error checking is implemented to make sure you have good input. This is one implementation of a very limited grammar. For more complex ones like the ones youa re mentioning, you may want to research Context Free Grammar and Recursive Descent Parsing. The program really doesn't parse, and certainly doesn't do it recursively, but it may give you a very small idea of how such a thing could be done.

CODE

#include <iostream>
using namespace std;

struct term
{
     int operand1;
     char binaryOperator;
     int operand2;
};


int f (term theTerm);


int main ()
{
    cout << "Enter a mathematical term.  A legal term "
         << "is an integer followed by '+', '-', '*', or "
         << "'/' followed by another integer: ";
        
    term theTerm;
    cin >> theTerm.operand1;
    cin >> theTerm.binaryOperator;
    cin >> theTerm.operand2;
    cin.ignore ();  // Pause to get rid of rest of line
    
    cout << theTerm.operand1 << theTerm.binaryOperator <<
            theTerm.operand2 << "=";
    cout << f(theTerm) << endl;
    
    
    
    cin.ignore ();  // Pause so console screen doesn't disappear.
    return 0;
}

    
int f (term theTerm)
{
    switch (theTerm.binaryOperator)
    {
           case '+':
                return theTerm.operand1 + theTerm.operand2;
                break;
           case '-':
                return theTerm.operand1 - theTerm.operand2;
                break;
           case '*':
                return theTerm.operand1 * theTerm.operand2;
                break;
           case '/':
                return theTerm.operand1 / theTerm.operand2;
                break;
           default:
                return -9999; // error code: illegal binOp value
     }
}




From a Context Free Grammar standpoint, this language would have one non-terminal:

TERM // short for "math term", not "terminal"

and two terminals:

NUMERIC_LITERAL
BINARY_OPERATOR


The starting Non-Terminal would be: TERM

It would have one rule:

<TERM> ---> NUMERIC_LITERAL BINARY_OPERATOR NUMERIC_LITERAL


<Term> goes to the above three terminals, in that order.


Obviously with parentheses and stuff, your grammar would be much more involved.
User is offlineProfile CardPM
+Quote Post

TheMagnitude
RE: Returning A Value Determined By An Equation
17 May, 2008 - 12:29 PM
Post #5

D.I.C Head
Group Icon

Joined: 12 Jan, 2008
Posts: 88


Dream Kudos: 125
My Contributions
Ive made a program that evaluates expressions at a very high level, this can be found at this topic: http://www.dreamincode.net/forums/showtopic52164.htm
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 04:37AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month