Guest User

Untitled

a guest
Jul 23rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. import java.util.Scanner;
  2. import scravaTurtleGraphics.*;
  3. // This global variable holds the array
  4. // representing the argument/operand stack of the RPN calculator
  5. // All the methods read/write this variable
  6.  
  7.  
  8. double[] arguments = {}; // initialize to avoid NullPointerException!
  9.  
  10. void push(double v){
  11. double [] pushArray = copyArray(arguments, arguments.length+1);
  12. pushArray[pushArray.length-1] = v;
  13. arguments = pushArray;
  14. }// legger v på toppen av stacken
  15.  
  16. double peek(double d){
  17. if(arguments.length == 0){
  18. return d;
  19. }
  20. return arguments[arguments.length-1];
  21. }// returnerer øverste verdi eller, -1 om stacken er tom
  22.  
  23. double pop(double v){
  24. if(arguments.length == 0){
  25. return v;
  26. }
  27. double temp;
  28. temp = arguments[arguments.length-1];
  29. arguments = copyArray(arguments, arguments.length-1);
  30. return temp;
  31. }// fjerner verdien på toppen av stacken og returnerer den
  32.  
  33. double[] copyArray(double[] original, int newSize) {
  34. double[] newArray = new double[newSize];
  35. // copy the old elements into the same positions
  36.  
  37. System.arraycopy(original, 0, newArray, 0, Math.min(original.length, newArray.length));
  38. // return new array
  39. return newArray;
  40. }
  41.  
  42. // prints the arguments stack to Standard.out, with the provided
  43. // prefix, separator and suffix
  44. void print(String prefix, String separator, String suffix) {
  45. System.out.print(prefix);
  46. for (int i = 0; i < arguments.length; i++) {
  47. // make sure the separator is not printed before the first value
  48. if (i > 0) {
  49. System.out.print(separator);
  50. }
  51. System.out.print(arguments[i]);
  52. }
  53. System.out.print(suffix);
  54. }
  55.  
  56.  
  57.  
  58. void performOperation(char op){
  59. if(op == '+'){
  60. double summer = pop(0)+ pop(0);
  61. push(summer);
  62. }// summer to siste verdiene
  63. if(op == '-'){
  64. double subtraher = pop(0)- pop(0);
  65. push(subtraher);
  66. }// subtraherer siste verdi fra nest siste verdi
  67. if(op == '/'){
  68. double divider = pop(1.0)/ pop(1.0);
  69. push(divider);
  70. }//dividerer de to siste verdiene
  71. if(op == '*'){
  72. double multipliser = pop(1.0)* pop(1.0);
  73. push(multipliser);
  74. }//multipliserer de to siste verdiene
  75. if(op == ','){
  76. push(peek(0));
  77. }//dupliserer siste verdi
  78. if(op == '.'){
  79. pop(0);
  80. } //fjerner øverste verdi
  81. if(op == '~'){
  82. double swap = pop(0);
  83. double swap2 = pop(0);
  84. push(swap);
  85. push(swap2);
  86. }
  87. if(op == 'f'){
  88. STG.move(pop(0));
  89. }
  90. if(op == 't'){
  91. STG.turn(pop(0));
  92. }
  93. if(op == 'u'){
  94. STG.up();
  95. }
  96. if(op == 'd'){
  97. STG.down();
  98. }
  99. if(op == 'h'){
  100. STG.home(true);
  101. }
  102. if(op == 'c'){
  103. STG.setColor(pop(0), pop(0), pop(0), pop(0));
  104. }
  105. if(op == 'w'){
  106. STG.setWidth((int)pop(0));
  107. }
  108.  
  109. }
  110.  
  111. Scanner sc = new Scanner(System.in);
  112. System.out.println("Skriv inn noe:");
  113. while(sc.hasNextLine()){
  114. String input = sc.nextLine();
  115. if(input.length() == 0){
  116. break;
  117. }splitAndHandleInput(input);
  118. }
  119.  
  120. void handleInput(String input){
  121.  
  122. if(Character.isDigit(input.charAt(0))){
  123. push(Double.valueOf(input));
  124. }else{
  125. performOperation(input.charAt(0));
  126. }
  127. }
  128.  
  129.  
  130.  
  131. void splitAndHandleInput(String inn){
  132. String[] inputA = inn.split(" ");
  133. for(int i = 0; i<inputA.length; i++){
  134. handleInput(inputA[i]);
  135. }
  136. }
  137.  
  138. print("(",",",")");
Add Comment
Please, Sign In to add comment