class Stack[1] { private float[] elements; private int topOfStack = -1; public Stack( int stackSize ) { elements = new float[ stackSize ]; } public void push( float item ) { elements[ ++topOfStack ] = item; } public float pop() { return elements[ topOfStack-- ]; } public boolean isEmpty() { if ( topOfStack < 0 ) return true; else return false; } public boolean isFull() { if ( topOfStack >= elements.length ) return true; else return false; } }
Stack me = new Stack( 20 ); me.push( 5 ); me.push( 12 ); System.out.println( me.pop() ); System.out.println( me.pop() );
struct Stack { float stack[100]; int topOfStack; }; void push(Stack& it, int item) { it.stack[(it.topOfStack)++] = item; } float pop(Stack& it) { return it.stack[--(it.topOfStack)]; } main() { Stack TryThisOut; // TryThisOut, Yours, Mine are Stack Yours, Mine; // Stack objects TryThisOut.topOfStack = 0; Yours.topOfStack = 0; push(TryThisOut, 5.0); push(Yours, 3.3); push(TryThisOut, 9.9); cout << pop(TryThisOut) << endl; }
class CalculatorEngine { private Stack operands = new Stack( 100 ); public float evaluate( String expression ) { StringTokenizer tokenList = new StringTokenizer( expression ); while ( tokenList.hasMoreTokens() ) processToken( tokenList.nextToken() ); return operands.pop(); } private void processToken( String token ) { if ( token.equals( "+" ) ) operands.push( operands.pop() + operands.pop() ); else if ( token.equals( "-" ) ) operands.push( operands.pop() - operands.pop() ); else if ( token.equals( "/" ) ) operands.push( operands.pop() / operands.pop() ); else if ( token.equals( "*" ) ) operands.push( operands.pop() * operands.pop() ); else operands.push( stringToFloat( token ) ); } private float stringToFloat( String number ) { return Float.valueOf( number ).floatValue(); } }
class CalculatorGUI { CalculatorEngine rpnEvaluator; public CalculatorGUI( CalculatorEngine myComputeEngine ) { rpnEvaluator = myComputeEngine; // code to build the gui interface removed } // code to make gui run removed }
public static void main( String[] arguments ) { CalculatorGUI fourBanger; fourBanger = new CalculatorGUI( new CalculatorEngine() ); }