 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
Travis13 Expert Cheater
Reputation: 0
Joined: 17 Feb 2007 Posts: 199
|
Posted: Wed Dec 31, 2008 10:36 am Post subject: |
|
|
sorry if this thread is too old and all but by just looking at the code.............in the switch statement u should add a:
default: cout << "That is an invalid number, Would u like to start over?"
break;
(put a do while statement from the beginning of the switch statement to her, making the user input Y or N) _________________
Learning C++, trying, failing, never gonna give up tho  |
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Thu Jan 01, 2009 3:25 pm Post subject: |
|
|
| nog_lorp wrote: | Next up: algebraic expression evaluator with correct order of operations!
Bonus points if you allow function definition in the form of "abc(args) = <expression in terms of args" |
Ah, didn't see this reply.
Though, I did write something like this a little while ago in C#. I may upload the project in a bit.
Edit:
Basically, you define a few interfaces (e.g., IToken, IOperation (inherits from IToken), IBasicOperation (inherits from IOperation; things like +, -), IFunction (inherits from IOperation; things like sin(), cos(), etc), and IConstant (inherits from IToken; things like PI, E)). Define a class, Number, which inherits from IToken, and merely has a double field, representing its value (excluding all of the interface implementations). IOperation has an integer field called OOPrecedence; IBasicOperation has an Operation() method, with two parameters (Numbers), and IFunction has an integer telling how many parameters, then an Operation() method, which has a parameter as an array of Numbers. Define all of the basic operations, functions, and constants that you like. IToken has a string, TextRepresentation, which would be things like "+", "-", etc.
In the main code, call a method which parses a given string, and returns true or false, with an out parameter being a List of IToken's. If it returns false, then exit, otherwise continue.
Then you do something like:
| Code: |
ParseParenthesis( Tokens ); //simply a method which would call ParseExpression() or whatever with each expression located inside of a set of parenthesis, and replace that entire set of ITokens with a Number.
Number nTemp;
int ind;
for( int i = 2; i < MaximumOO; i++ ) { //MaximumOO is a constant
for( int j = 0; j < Tokens.Count; j++ ) {
if( !(Tokens[j] is IOperation) )
continue;
IOperation op = Tokens[j] as Operation;
if( op.OOPrecedence != i )
continue;
if( op is IBasicOperation ) {
nTemp = (op as IBasicOperation).Operation( Tokens[j - 1] as Number, Tokens[ j + 1 ] as Number );
Tokens[j - 1] = nTemp;
Tokens.RemoveAt(j);
Tokens.RemoveAt(j);
}
else {
Number[] Params = getParams( Tokens, j, out ind ); //A function which would parse all of the things in parenthesis, separated by commas, until it found the ')', the index of the ')' is stored in ind
nTemp = (op as IFunction).Operation( Params );
Tokens.RemoveRange( j + 1, ind );
Tokens[j] = nTemp;
}
j = 0; //to be safe
}
}
return (Tokens[0] as Number).Value;
|
Edit again:
How would you like the user to be able to do matrices?
i.e., what's the input that I should accept for calculating? _________________
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|