下面是用栈结构解析算术表达式并计算的方法,只适用于操作数是个位数的(如果需要处理多位数,则修改后缀表达式的存储方式即可)。
由中缀表达式获得后缀表达式
public class StackX {
private int maxSize;
private char[] stackArray;
private int top;
public StackX(int maxSize){
this.maxSize=maxSize;
this.stackArray=new char[maxSize];
this.top=-1;
}
public void push(char c){
stackArray[++top]=c;
}
public char pop(){
return stackArray[top--];
}
public char peek() {
return stackArray[top];
}
public int size(){
return top+1;
}
public boolean isEmpty(){
return top==-1;
}
public char peekN(int index){
return stackArray[index];
}
public void displayStack(String s){
System.out.print(s);
System.out.print(" bottom --> top :");
for (int i=0;i<size();i++){
System.out.print(peekN(i)+" ");
}
System.out.println();
}
public static void main(String[] args) throws IOException {
String out;
while (true){
String s=getString();
if (s=="")
break;
InToPost inToPost=new InToPost(s);
out= inToPost.doTrans();
System.out.println(out);
}
}
public static String getString() throws IOException {
InputStreamReader inputStreamReader=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(inputStreamReader);
String s= br.readLine();
return s;
}
}
class InToPost{
private StackX theStack;
private String in;
private String out="";
public InToPost(String in){
this.in=in;
int stackSize=in.length();
theStack=new StackX(stackSize);
}
public String doTrans(){
for (int i=0; i<in.length();i++){
char c=in.charAt(i);
this.theStack.displayStack("For "+c+" ");
switch (c){
case '+':
case '-':
getOper(c,1);
break;
case '*':
case '/':
getOper(c,2);
break;
case '(':
theStack.push(c);
break;
case ')':
getParen(c);
break;
default:
out+=c;
}
}
while (!theStack.isEmpty()){
theStack.displayStack("while ");
out+=theStack.pop();
}
theStack.displayStack(" end ");
return out;
}
private void getParen(char c) {
while (!this.theStack.isEmpty()){
char cc=theStack.pop();
if(cc=='(')
break;
else {
out+=cc;
}
}
}
public void getOper(char c,int prec){
while (!this.theStack.isEmpty()){
char top=this.theStack.pop();
if (top=='('){
theStack.push(top);
break;
}else {
int prec2;
if (top =='+' ||top =='-'){
prec2=1;
}else {
prec2=2;
}
if (prec2<prec){
theStack.push(top);
break;
}else {
out+=top;
}
}
}
theStack.push(c);
}
}
计算后缀表达式:
class ParsePost{
private StackO theStack;
private String input;
ParsePost(String input){
this.input=input;
}
public int doParse(){
theStack=new StackO(20);
char ch;
int j;
int num1,num2,interAns;
for (j=0;j<input.length();j++){
ch=input.charAt(j);
theStack.displayStack(""+ch+" ");
if (ch>='0' && ch<='9'){
theStack.push((int)(ch-'0'));
}
else {
num2=theStack.pop();
num1=theStack.pop();
switch (ch){
case '+':
interAns=num1+num2;
break;
case '-':
interAns=num1-num2;
break;
case '*':
interAns=num1*num2;
break;
case '/':
interAns=num1/num2;
break;
default:
interAns=0;
}
theStack.push(interAns);
}
}
interAns=theStack.pop();
return interAns;
}
}
class StackO {
private int maxSize;
private int[] stackArray;
private int top;
public StackO(int maxSize){
this.maxSize=maxSize;
this.stackArray=new int[maxSize];
this.top=-1;
}
public void push(int c){
stackArray[++top]=c;
}
public int pop(){
return stackArray[top--];
}
public int peek() {
return stackArray[top];
}
public int size(){
return top+1;
}
public boolean isEmpty(){
return top==-1;
}
public int peekN(int index){
return stackArray[index];
}
public void displayStack(String s){
System.out.print(s);
System.out.print(" bottom --> top :");
for (int i=0;i<size();i++){
System.out.print(peekN(i)+" ");
}
System.out.println();
}
public static void main(String[] args) throws IOException {
String out;
while (true){
System.out.println("Enter postfix: ");
System.out.flush();
String s=getString();
if (s=="")
break;
InToPost inToPost=new InToPost(s);
out= inToPost.doTrans();
ParsePost parsePost=new ParsePost(out);
int result= parsePost.doParse();
System.out.println(out);
System.out.println(result);
System.out.println("------------");
}
}
public static String getString() throws IOException {
InputStreamReader inputStreamReader=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(inputStreamReader);
String s= br.readLine();
return s;
}
}