reading command line or text file...expression tree (sort of)

This is a discussion on reading command line or text file...expression tree (sort of) within the C++ Programming forums, part of the General Programming Boards category; I am trying to learn about C++, but I don't quite know how to do the following. I am currently ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    1

    reading command line or text file...expression tree (sort of)

    I am trying to learn about C++, but I don't quite know how to do the following. I am currently writing a program to compute the zeros, both complex and real, for a polynomial. I am doing this on my own to learn more about C++. I already have several methods (Madsen and various other newton methods) for finding the zeros. That isn't what I need help with.

    I am using the following structures:

    Code:
    //define complex numbers
    typedef struct {
      double r, i;
    } _comp_t;
    typedef _comp_t comp_t[1];
    
    //define polynomials, MAX_DEG is defined elsewhere
    typedef struct {
            comp_t coef[MAX_DEG+1];
            int    degree;
    } poly_t;
    //I also have other functions for dividing, multiplying, finding the GCD, etc for the polynomials

    I can create polynomials by doing the following, for example:

    Code:
    poly_t f;
    f.coef[0] -> r = 1.0; f.coef[0] -> i = 0.0;
    f.coef[1] -> r = 0.0; f.coef[1] -> i = 0.0;
    f.coef[2] -> r = 1.0; f.coef[1] -> i = 0.0;
    to create the polynomial x^2 + 1. However, modifying the source code each time I want to test a new polynomial is cumbersome and very time consuming. So, I wrote a program to take a polynomial off of the command prompt. For example, I could enter:

    >(1)x^2 + (-3)x + (7-2i);

    The only way I could get the coefficients is to wrap them in parantheses. I am also allowing for complex coefficients. And, I'm assuming that all the polynomials are written in the variable 'x'.

    I would like a way to enter a polynomial from the command prompt (or text file) without having to use parentheses around the coefficients. (Since this is for me, I can assume that 'x' is the only variable.) Maybe something like:

    >4*x^3 - 4*x + 4;

    would be fine.

    I thought about using an expression tree, but I don't have much experience with them. I can program algorithms no problem, but I have very little experience with iostreams and user interfaces. Most of the information I've found on expression trees are about evaluating the expressions. But, in this case I don't really want to evaluate the expression; I just need the coefficients and the degree of each term.

    Any help would be greatly appreciated. Or, even if someone could point me in the right direction, that would be great too. If I could just get a general idea on how to proceed, that would benefit me greatly, too.

    Cheers.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Basic idea:
    Read in a string.
    Read in characters:
    If we see an open parentheses, pass it off to the read-coefficient routine.
    If we see "x^", pass it off to the read power routine.
    Once we have both, put it in the struct. If we never get an "x^", assume constant.
    Repeat until tired.

    The read-coefficient routine needs to look for +/-, i, and ).
    The read power routine is just looking for a number.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,886
    But, in this case I don't really want to evaluate the expression; I just need the coefficients and the degree of each term.
    You could have interactive input: ask the user for the degree of the polynomial, and then request for each co-efficient.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. reading from text file
    By jamez in forum C Programming
    Replies: 3
    Last Post: 11-30-2005, 06:13 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21