Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import scravaTurtleGraphics.*;
- // This global variable holds the array
- // representing the argument/operand stack of the RPN calculator
- // All the methods read/write this variable
- double[] arguments = {}; // initialize to avoid NullPointerException!
- void push(double v){
- double [] pushArray = copyArray(arguments, arguments.length+1);
- pushArray[pushArray.length-1] = v;
- arguments = pushArray;
- }// legger v på toppen av stacken
- double peek(double d){
- if(arguments.length == 0){
- return d;
- }
- return arguments[arguments.length-1];
- }// returnerer øverste verdi eller, -1 om stacken er tom
- double pop(double v){
- if(arguments.length == 0){
- return v;
- }
- double temp;
- temp = arguments[arguments.length-1];
- arguments = copyArray(arguments, arguments.length-1);
- return temp;
- }// fjerner verdien på toppen av stacken og returnerer den
- 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;
- }
- // prints the arguments stack to Standard.out, with the provided
- // prefix, separator and suffix
- 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);
- }
- void performOperation(char op){
- if(op == '+'){
- double summer = pop(0)+ pop(0);
- push(summer);
- }// summer to siste verdiene
- if(op == '-'){
- double subtraher = pop(0)- pop(0);
- push(subtraher);
- }// subtraherer siste verdi fra nest siste verdi
- if(op == '/'){
- double divider = pop(1.0)/ pop(1.0);
- push(divider);
- }//dividerer de to siste verdiene
- if(op == '*'){
- double multipliser = pop(1.0)* pop(1.0);
- push(multipliser);
- }//multipliserer de to siste verdiene
- if(op == ','){
- push(peek(0));
- }//dupliserer siste verdi
- if(op == '.'){
- pop(0);
- } //fjerner øverste verdi
- if(op == '~'){
- double swap = pop(0);
- double swap2 = pop(0);
- push(swap);
- push(swap2);
- }
- if(op == 'f'){
- STG.move(pop(0));
- }
- if(op == 't'){
- STG.turn(pop(0));
- }
- if(op == 'u'){
- STG.up();
- }
- if(op == 'd'){
- STG.down();
- }
- if(op == 'h'){
- STG.home(true);
- }
- if(op == 'c'){
- STG.setColor(pop(0), pop(0), pop(0), pop(0));
- }
- if(op == 'w'){
- STG.setWidth((int)pop(0));
- }
- }
- Scanner sc = new Scanner(System.in);
- System.out.println("Skriv inn noe:");
- while(sc.hasNextLine()){
- String input = sc.nextLine();
- if(input.length() == 0){
- break;
- }splitAndHandleInput(input);
- }
- void handleInput(String input){
- if(Character.isDigit(input.charAt(0))){
- push(Double.valueOf(input));
- }else{
- performOperation(input.charAt(0));
- }
- }
- void splitAndHandleInput(String inn){
- String[] inputA = inn.split(" ");
- for(int i = 0; i<inputA.length; i++){
- handleInput(inputA[i]);
- }
- }
- print("(",",",")");
Add Comment
Please, Sign In to add comment