📄 infixapp.java
字号:
package StackQueue;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class InfixApp {
public static void main(String[] args) throws IOException {
String input,output;
while(true){
System.out.print("Enter infix:");
System.out.flush();
input=getString();
if(input.equals(""))
break;
IntoPost theTrans=new IntoPost(input);
output=theTrans.doTrans();
System.out.println("Postfix is"+output+'\n');
}
}
public static String getString() throws IOException{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String s=br.readLine();
return s;
}
}
// ///////////////////////////////////////////////////class InToPost
class IntoPost{
private PublicStack theStack;
private String input;
private String output="";
public IntoPost(String in){// 构造函数
input=in;
int stackSize=input.length();
theStack=new PublicStack(stackSize);// 栈对象
}
public String doTrans(){
for(int j=0;j<input.length();j++){
char ch=input.charAt(j);
switch(ch){
case '+':
case '-':
gotOper(ch,1);
break;
case '*':
case '/':
gotOper(ch,2);
break;
case '(':
theStack.push(ch);
break;
case ')':
gotParen(ch);
break;
default:
output=output+ch;
break;
}// end switch
}// end for
while(!theStack.isEmpty()){
output=output+theStack.pop();
}
return output;
}
public void gotOper(char opThis,int prec1){
while(!theStack.isEmpty()){
char opTop=theStack.pop();
if(opTop=='('){
theStack.push(opTop);
break;
}
else{
int prec2;
if(opTop=='+'||opTop=='-'){
prec2=1;
}
else{
prec2=2;
}
if(prec2<prec1){
theStack.push(opTop);
break;
}
else
output=output+opTop;
}// end else
}// end while
theStack.push(opThis);
}// end gotOper
public void gotParen(char ch){
while(!theStack.isEmpty()){
char chx=theStack.pop();
if(chx=='(')
break;
else
output=output+chx;
}// end while
}// end gotParen
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -