The saga continues: Algebraic simplification

This is a discussion on The saga continues: Algebraic simplification within the C++ Programming forums, part of the General Programming Boards category; Hi guys; As some of you may know, Ive written a calculator that in summary works like this: Scanner tokenises ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    19

    The saga continues: Algebraic simplification

    Hi guys;

    As some of you may know, Ive written a calculator that in summary works like this:

    Scanner tokenises input expression; passes tokens to parser

    Parser builds a binary tree as it recieves the tokens, it does this in reverse polish, so that correct operator precedence etc is preserved.

    Tree is evaluated.

    Finally value is output.

    Its quite a good design, largely based on code written by Bartosz Milewski which I obtained here:
    http://www.relisoft.com/book/lang/pr...urce/calc2.zip

    So far the major modification made has been to change all the variable declarations from double to complex<double>, and to add a handler to the scanner's case statement so that when it encounters a number or j, it sends a proper complex number, it took me a while, quite happy with it, if anyone would care to comment would appreciate.

    Code:
       case '0': case '1': case '2': case '3': case '4':
        case '5': case '6': case '7': case '8': case '9':
        case '.': case 'j':
        {
             _token = tNumber;
            double tmp_R, tmp_J;
            double tmp_num_val;
            double jcheck;
            char * p;
    
            if (_buf [_iLook] == 'j')
            {
             tmp_R = 0;
              tmp_J = 1;
              _iLook++;
            }
            else
            {
              tmp_num_val = strtod (&_buf [_iLook], &p);
              _iLook = p - _buf;
              if(_buf [_iLook] == 'j')
              {
                tmp_R = 0;
                tmp_J = tmp_num_val;
                _iLook++;
              }
              else
              {
               tmp_R = tmp_num_val;
                tmp_J = 0;
              }
              }
          complex<double> a(tmp_R,tmp_J);
           _number=a;
          break;
        }

    Now for my current challenge; I have to continue modifying the program so when it is passed for example:

    X - 4X + 2(X + X^2)

    Result:

    X^2 - X

    Here is what I think I need to do;

    - Write a class to define the type X
    - Define the operators for this type (operator overloading?)

    Basically the operator overloading would show the program how to add, subtract etc input of type X. So when the program has to add X + X, there is a definition of X, and a definition of what + is supposed to be in this context.

    And once I write this class, #include as a header file in files that would be handling type X, and tada, its done. Am I right?

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Well, you are sort of on the right track. You do need a placeholder type (for a variable). For dealing with polynomials, presumably, you could attach the coefficient and power directly to this type (although a more general solution would be to look at the multiplication operations individually). Then, you need a way to deal with order of operations. The best way I know of to do this is to parse your info into a tree structure with the lowest priority operation on the top, and then priorities at least as high in at each successive level.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    19
    Thanks zach. Im already parsing the info to a tree structure. So that part is done at least.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Ahh... Sorry about that. Somehow, I managed to misread a significant portion of the post (or rather, not read it ).

    In answer to your question, yes. That is pretty much all you need to do. As far as actual operator overloading for your class, that is just a syntactic niceity, and personally, I would prefer to use a named function in such a case, but it is your code, so have fun.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

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