### 逆ポーランド記法への変換
中置記法から逆ポーランド記法へ変換するコードを作成しています.
正直プログラミング初心者で,どこが間違っているのか検討もついていません.
投げやりな質問で申し訳ありませんが,力をお貸しいただきたいです…!
発生している問題・エラーメッセージ
1 + 2 Exception in thread "main" java.lang.NullPointerException at Calculator2.compute(Calculator2.java:108) at Calculator2.main(Calculator2.java:155)
該当のソースコード
java
1import java.io.BufferedReader; 2import java.io.IOException; 3import java.io.InputStreamReader; 4import java.io.PushbackReader; 5import java.io.StringReader; 6import java.util.Arrays; 7import java.util.EmptyStackException;; 8 9class MyStack { 10 private char stack[]; 11 private int stackSize; 12 private int sp; 13 14 private static final int DEFAULT_STACK_SIZE = 100; 15 16 public MyStack() { 17 this(DEFAULT_STACK_SIZE); 18 } 19 20 public MyStack(int size) { 21 stack = new char[size]; 22 stackSize = size; 23 sp = 0; 24 } 25 26 public void clear() { 27 Arrays.fill(stack, '\u0000'); 28 sp = 0; 29 } 30 31 public void push(char x) { 32 if (sp >= stackSize) { 33 throw new IllegalStateException("Stack overflow"); 34 } 35 stack[sp++] = x; 36 } 37 38 public char pop() { 39 if (sp <= 0) { 40 throw new EmptyStackException(); 41 } 42 char value = stack[--sp]; 43 stack[sp] = '\u0000'; 44 return value; 45 } 46 47 public char check() { 48 if (sp <= 0) { 49 throw new EmptyStackException(); 50 } 51 char value = stack[sp - 1]; 52 return value; 53 } 54 55 public boolean isEmpty() { 56 return sp == 0; 57 } 58 59 public String toString() { 60 String s = "MyStack=["; 61 for (int i = 0; i < sp; i++) { 62 s = s + stack[i]; 63 if (i < sp - 1) 64 s = s + ","; 65 } 66 s = s + "]"; 67 return s; 68 } 69} 70 71public class Calculator2 { 72 private static MyStack stackA; 73 private static MyStack stackB; 74 75 public Calculator2() { 76 stackA = new MyStack(); 77 stackB = new MyStack(); 78 } 79 80 public static int rank(char x) { 81 if (x == '*' || x == '/' || x == '%') { 82 return 2; 83 } else if (x == '+' || x == '-') { 84 return 1; 85 } else { 86 return 0; 87 } 88 } 89 90 public static void allot(char num, char ch) { 91 if (num != '\u0000') { 92 stackA.push(num); 93 num = '\u0000'; 94 } else if (stackB.isEmpty() != true) { 95 if (rank(ch) >= rank(stackB.check())) { 96 if (ch == ')') { 97 stackB.pop(); 98 } else { 99 stackB.push(ch); 100 } 101 } else { 102 stackA.push(ch); 103 } 104 } 105 } 106 107 public static void compute(String exp) throws IOException { 108 stackA.clear(); 109 stackB.clear(); 110 111 PushbackReader input = new PushbackReader(new StringReader(exp + ";")); 112 113 int c; 114 char num = '\u0000'; 115 while ((c = input.read()) != ';') { 116 char ch = (char) c; 117 118 if (Character.isDigit(ch)) { 119 num = ch; 120 } else if (ch == '(') { 121 stackB.push(ch); 122 } else if (ch == ')') { 123 allot(num, ch); 124 } else if (ch == '-') { 125 allot(num, ch); 126 } else if (ch == '+') { 127 allot(num, ch); 128 } else if (ch == '*') { 129 allot(num, ch); 130 } else if (ch == '/') { 131 allot(num, ch); 132 } else if (ch == '%') { 133 allot(num, ch); 134 } else if (ch == ' ') { 135 136 } else if (ch == '\t') { 137 138 } else if (ch == '\r') { 139 140 } else { 141 throw new IllegalArgumentException("不正な文字" + ch + "がありました。"); 142 } 143 } 144 while (stackA.check() != '\u0000') { 145 stackB.push(stackA.pop()); 146 } 147 } 148 149 public static void main(String args[]) throws IOException { 150 BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); 151 152 String line; 153 while ((line = input.readLine()) != null) { 154 try { 155 compute(line); 156 System.out.println(stackB.toString()); 157 } catch (EmptyStackException e) { 158 System.out.println("式が正しくありません。"); 159 } 160 } 161 } 162} 163
回答3件
あなたの回答
tips
プレビュー