a bit of a C++ problem

Discussion in 'Computer Science & Culture' started by RubiksMaster, Mar 5, 2005.

Thread Status:
Not open for further replies.
  1. RubiksMaster Real eyes realize real lies Registered Senior Member

    Messages:
    1,646
    Keep in mind, I am sort of new at C++, but I know all the basics.

    A few days ago I was writing a little program to calculate Riemann Sums (if you don't know what it is, don't worry). One of the requirements is that you have an equation f(x) for which to calculate the sum.

    The problem is, I don't know how to get the equation by a user input. I have to hard-code the equation and re-compile for each calculation.

    I can get the equation into a text string, but that doesn't help much.

    Can anyone help me?
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. okinrus Registered Senior Member

    Messages:
    2,669
    You have to parse the equation somehow. If the grammar your using is sufficiently complicated, you'll want to use yacc and flex; but for simple grammars you might be able to work out a way yourself, by either using a stack or simply looking at the string. It all really depends on how complicated you want to go, but if you want to anything like algebra notation, I'd either use recursive descent parsing or bison and yacc. There should be tutorials on the web on how to do these two.
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. RubiksMaster Real eyes realize real lies Registered Senior Member

    Messages:
    1,646
    I can write a string parsing function just fine. It's the next step I have trouble with. Let's say my input was "x+2" just for simplicity. All I would need is the simple +-*/ operaters. How is it possible to get the character "x" to be turned into an integer variable. And would I need something like a "switch...case" setup for each operator (plus, minus, divide)?

    Everything I try seems to require an "if..then" block for every possible equation

    Can anyone give me some pseudocode (or actual C++ code) or a flowchart on how this is even possible? I've tried everything I can think of, and it just seems I always have to step in and give the computer information; it can't figure it out by itself.
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. AntonK Technomage Registered Senior Member

    Messages:
    1,083
    Haha, sorry to laugh a bit, but what you're actually trying to do with converting the string to the actual equation is CALLED parsing. Consider reading up lex and yacc (or flex and bison). These are very good tools for taking a grammar and building a parser. If you're not sure what a grammar is, read up on formal languages and automata theory. What you're trying to do is not really a simple task. Especially if you're trying to make it a very generic equation parser. I wish you luck.

    -AntonK
     
  8. okinrus Registered Senior Member

    Messages:
    2,669
    The word parsing is usually reserved for defining the structure of the input. So for instance, if your input is "(4 + 3) * 3" your logical structure looks something like a tree, with * as a root operator, the results of "4 + 3" and 3 being applied to *. During lexical analysis--that is when you scan the string and look for tokens--you can deduce the basic types. For instance, your scanner would know that "x" is variable and not a number because all numbers must start with a digit and no variable starts with a digit. If your writing an interpreter, you'll likely want to look up "x" inside the current environment to find x's value. If x has been defined before, you'll be able to find x's value and use that value. If x hasn't been defined, you'll have to either tell users they've made a mistake or use a default value for x.
     
  9. Blindman Valued Senior Member

    Messages:
    1,425
    Just use an interpreted language Most interpreted languages come with eval functions that will parse and run a text string. I recommend JScript and a browser. You should be able to get the job done in under 20 lines of code. I created a simple one in 13 lines of code (use IE and replace [ and ] with < and >).
    Code:
    	[script]
    	function doThis()
    	{
    		equationResult = 0
    		eval(vars.value)
    		eval("equationResult = "+equation.value)
    		out.innerHTML = equationResult
    	}
    	[/script]
    	define vars [input id="vars" maxlength="100" type="text"] [br]
    	equation [id="equation" maxlength="100" type="text"] [br]
    	[button onclick="doThis();"]Calc[/button]
    	[span id = out][/span]
    
     
  10. Astragalus Registered Member

    Messages:
    1
    I'm just gonna jump in here, if I may... I have a similar problem: parsing a simple equation string so that I can evaluate the given function of a complex number. Creating a tree with nodes +,*,etc with 1 or 2 branches each is no problem. The problem is speed. This function needs to be evaluated in a tight loop as fast as possible. Won't all those pointer dereferences take forever? Does anyone know a faster way?

    Please Register or Log in to view the hidden image!

     
  11. Crunchy Cat F-in' *meow* baby!!! Valued Senior Member

    Messages:
    8,423
    I had an assignment like this in an old CS-101-ish class a gazillion years
    ago. I found 2 ways to implement it. One was to use a stack for operators
    and operands. I would parse the string, stack up the equation, pop the
    elements off and perform calculations based on what the individual characters
    were. The second method I used was finite state automation; however,
    unless it's something that's been taught, I would avoid it for now and stick
    with the data structure.

    P.S. using an interpretive language will most likely be viewed as cheating
    if the assignment is for C++. Similarly Lex and Yac (sp?) are tools that
    help for the builiding of compilers. They are way overkill for something like
    this.
     
  12. Speaking of C++
    i need a FREE Compiler
    and i can't be bothered to sign up or anything, and i dont't want it mailed either.
    So if anyone could enlighten me then i'm be Muchos Happy

    Ciao
     
Thread Status:
Not open for further replies.

Share This Page