Chapter4:栈的应用-将中缀表达式转换为后缀表达式
2015-05-07 10:50
471 查看
package chapter4;
import java.util.Scanner;
public class Infix {
/**
* @将中缀表达式转化为后缀表达式,为表达式求值做准备
* (A+B)*C-D/(E+F)
* AB+C*DEF+/-
*/
public static void main(String[] args) {
while(true){
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
if(input=="")
break;
InToPost thePost = new InToPost(input);
String output = thePost.doTrans();
System.out.println("output: "+output);
}
}
}
class StackA{
private int maxSize;
private char[] stackArray;
private int top;
public StackA(int max){
maxSize = max;
stackArray = new char[maxSize];
top=-1;
}
public void push(char value){
stackArray[++top] = value;
}
public char pop(){
return stackArray[top--];
}
public char peek(){
return stackArray[top];
}
public boolean isEmpty(){
return top==-1;
}
public int size(){
return top+1;
}
public char peekN(int n){
return stackArray
;
}
public void displayStack(String s){
System.out.println(s);
System.out.println("display stack from bottom-->top:");
for(int i=0;i<size();i++){
System.out.print(peekN(i)+" ");
}
System.out.println();
}
}
class InToPost{
private String input;
private String output;
private StackA theStack;
public InToPost(String in){
input = in;
output = "";
theStack = new StackA(input.length());
}
public String doTrans(){
for(int i=0;i<input.length();i++){
char ch = input.charAt(i);
System.out.println("push "+ch+" to stack");
//theStack.displayStack("For "+ch+" ");
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;
}
}
while(!theStack.isEmpty()){
char temp = theStack.pop();
output = output+temp;
}
return output;
}
public void gotOper(char opThis,int pre1){
while(!theStack.isEmpty()){
char opTop = theStack.pop();
if(opTop=='('){
theStack.push(opTop);
break;
}
else{
int pre2;
if(opTop=='+' || opTop=='-')
pre2 = 1;
else
pre2 = 2;
if(pre2<pre1){
theStack.push(opTop);
break;
}
else
output = output+opTop;
}
}
theStack.push(opThis);
}
public void gotParen(char opThis){
while(!theStack.isEmpty()){
char opTop = theStack.pop();
if(opTop=='(')
break;
else
output = output+opTop;
}
}
}
import java.util.Scanner;
public class Infix {
/**
* @将中缀表达式转化为后缀表达式,为表达式求值做准备
* (A+B)*C-D/(E+F)
* AB+C*DEF+/-
*/
public static void main(String[] args) {
while(true){
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
if(input=="")
break;
InToPost thePost = new InToPost(input);
String output = thePost.doTrans();
System.out.println("output: "+output);
}
}
}
class StackA{
private int maxSize;
private char[] stackArray;
private int top;
public StackA(int max){
maxSize = max;
stackArray = new char[maxSize];
top=-1;
}
public void push(char value){
stackArray[++top] = value;
}
public char pop(){
return stackArray[top--];
}
public char peek(){
return stackArray[top];
}
public boolean isEmpty(){
return top==-1;
}
public int size(){
return top+1;
}
public char peekN(int n){
return stackArray
;
}
public void displayStack(String s){
System.out.println(s);
System.out.println("display stack from bottom-->top:");
for(int i=0;i<size();i++){
System.out.print(peekN(i)+" ");
}
System.out.println();
}
}
class InToPost{
private String input;
private String output;
private StackA theStack;
public InToPost(String in){
input = in;
output = "";
theStack = new StackA(input.length());
}
public String doTrans(){
for(int i=0;i<input.length();i++){
char ch = input.charAt(i);
System.out.println("push "+ch+" to stack");
//theStack.displayStack("For "+ch+" ");
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;
}
}
while(!theStack.isEmpty()){
char temp = theStack.pop();
output = output+temp;
}
return output;
}
public void gotOper(char opThis,int pre1){
while(!theStack.isEmpty()){
char opTop = theStack.pop();
if(opTop=='('){
theStack.push(opTop);
break;
}
else{
int pre2;
if(opTop=='+' || opTop=='-')
pre2 = 1;
else
pre2 = 2;
if(pre2<pre1){
theStack.push(opTop);
break;
}
else
output = output+opTop;
}
}
theStack.push(opThis);
}
public void gotParen(char opThis){
while(!theStack.isEmpty()){
char opTop = theStack.pop();
if(opTop=='(')
break;
else
output = output+opTop;
}
}
}
相关文章推荐
- 栈的应用实例——中缀表达式转换为后缀表达式
- 【数据结构】栈的应用---四则运算表达式求值(中缀表达式与后缀表达式转换)
- 栈的应用— 中缀表达式到后缀表达式的转换
- 栈的应用:后缀表达式到中缀表达式的转换
- C++栈的应用——后缀表达式求值、中缀表达式到后缀表达式的转换
- 栈的应用 后缀表达式求值 后缀表达式与中缀表达式的转换
- Java堆栈的应用2----------中缀表达式转为后缀表达式的计算Java实现
- 中缀表达式直接转换为表达式二叉树、前缀表达式、后缀表达式,表达式求值
- [置顶] 栈应用:中缀表达式转后缀表达式(运算结果保证正确)
- 数据结构—中缀表达式转后缀表达式算法及实现—栈的应用—计算表达式(C++代码实现)(1)
- 数据结构(C++实现):栈的运用--中缀表达式转换为后缀表达式既 nyoj 257
- 中缀表达式转换成为后缀表达式
- 利用STL栈将中缀表达式转换成后缀表达式输出
- 中缀表达式转换为后缀表达式
- 栈应用2 中缀表达式转后缀表达式
- 将中缀表达式转换为后缀表达式
- [Java算法分析与设计]利用栈操作中缀表达式和后缀表达式的转换和运算
- 前缀表达式、中缀表达式、后缀表达式及转换实现
- 栈的运用(中缀表达式转换为后缀表达式,并对后缀表达式求值。)
- 将中缀表达式转换为后缀表达式的简便方法