progJ.pdf progJ-s.pdf progJ-e.pdf 記事一覧 |
![]() 派生クラスの追加 |
progJ-e.pdf へ [#38%22] を追加しました.
※ [#29%4],[#38%2] も少し変更.
//Stack.java
package myproj; import java.util.*;
class Stack extends List {
void push(String s){
if(sz >= 0){ins(s, 1);}
}
String pop( ){
Cell c = top.n;
if(sz <= 0){sz--; return "0";}
del(1); return c.d;
}
int popInt( ){
return Integer.parseInt(pop( ));
}
void pushInt(int n){
push(Integer.toString(n));
}
void addInt( ){
pushInt(popInt() + popInt( ));
}
void multInt( ){
pushInt(popInt() * popInt( ));
}
public static
void main(String[ ] args) {
Stack d = new Stack( );
String expr = "123 45 + 6 * =";
String s; char c;
StringTokenizer st;
st = new StringTokenizer(expr);
while(st.hasMoreTokens( )){
s = st.nextToken( );
c = s.charAt(0);
if(c == '=') break;
if(c == '+'){d.addInt( );}
else if (c == '*'){d.multInt( );}
else{d.push(s);}
}
s = d.pop( ); if(d.sz != 0){s = "?";}
System.out.println(s);
}
}