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?