Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package oving2;
- public class ArgumentStack {
- // This global variable holds the array
- // representing the argument/operand stack of the RPN calculator
- // All the methods read/write this variable
- static double [] arguments = {};
- // initialize to avoid NullPointerException!
- public static double [] copyArray (double [] original, int newSize) {
- double [] newArray = new double [newSize];
- // copy the old elements into the same positions
- System.arraycopy(original, 0, newArray, 0, Math.min (original.length, newArray.length));
- // return new array
- return newArray;
- }
- public static double peek (double d) {
- double highest;
- if(arguments.length == 0) {
- highest = d;
- }
- else {
- highest = arguments[arguments.length-1];
- }
- return highest;
- }
- public static void push (double v) {
- double[] temp;
- temp = copyArray(arguments, arguments.length+1);
- temp[temp.length-1] = v;
- arguments = temp;
- }
- public static double pop (double d) {
- double resultat;
- if (arguments.length == 0) {
- resultat = d;
- }
- else {
- double [] temp;
- temp = copyArray(arguments, arguments.length-1);
- resultat = arguments[arguments.length];
- arguments = temp;
- }
- return resultat;
- }
- // prints the arguments stack to Standard.out, with the provided
- // prefix, separator and suffix
- public static void print (String prefix, String separator, String suffix) {
- System.out.print(prefix);
- for (int i = 0; i<arguments.length; i++) {
- // make sure the separator is not printed before the first value
- if (i > 0){
- System.out.print (separator);
- }
- System.out.print(arguments[i]);
- }
- System.out.print(suffix);
- }
- public static void main (String [] args) {
- }
- }
Add Comment
Please, Sign In to add comment