栈-中缀表达式换成后缀
来源:互联网 发布:star法则 程序员 编辑:程序博客网 时间:2024/06/16 12:52
package c_stack.C_Infix;
/**
* @author Administrator 中缀表达式换成后缀
*/
public class InfixApp {
public static void main(String[] args) {
String input = " 3 + 4 - 6 / 2 * (6 + 3 * (4 - 1))";
InToPost theIt = new InToPost(input);
String result = theIt.doTrans();
System.out.println(result);
}
}package c_stack.C_Infix;
public class InToPost {
StackX theStackX;
String input;
String output = "";
public InToPost(String in) {
input = in;
int stackSize = in.length();
theStackX = new StackX(stackSize);
}
public String doTrans() {
for (int i = 0; i < input.length(); i++) {
char choice = input.charAt(i);
switch (choice) {
case '+':
case '-':
getOper(choice, 1);
break;
case '*':
case '/':
getOper(choice, 2);
break;
case '(':
theStackX.push(choice);
break;
case ')':
gotPraen(choice);
break;
default:// 必须是操作数,累加
output += choice;
break;
}
}
while (!theStackX.isEmpty()) {
output += theStackX.pop();
}
return output;
}
private void gotPraen(char ch) {
// 弹出所有数据项,当是(时结束循环
while (!theStackX.isEmpty()) {
char opTop = theStackX.pop();
if (opTop == '(')
break;
else {
output += opTop;
}
}
}
private void getOper(char opThis, int prec1) {
// 防止弹出的结束的不止一个,shile
// 不管怎样操作都会入栈,提出来放后面
while (!theStackX.isEmpty()) {
char opTop = theStackX.pop();
// 当出栈的是(时,放入,并结束循环
if (opTop == '(') {
theStackX.push(opTop);
break;
} else {// 判断运算符优先级,小于则在放回栈中,大于则弹出。进行下一次循环判断
int prec2;
if (opTop == '+' || opTop == '-')
prec2 = 1;
else
prec2 = 2;
if (prec2 < prec1) {
theStackX.push(opTop);
break;
}
output += opTop;
}
}
theStackX.push(opThis);
}
}package c_stack.C_Infix;
public class StackX {
private char arr[];
private int top;
private int maxSize;
public StackX(int s) {
maxSize = s;
arr = new char[maxSize];
top = -1;
}
public void push(char key) {
arr[++top] = key;
}
public char pop() {
return arr[top--];
}
public char peek() {
return arr[top];
}
public char peekN(int n) {
return arr[n];
}
public boolean isEmpty() {
return top == -1;
}
public boolean isFull() {
return top == maxSize - 1;
}
public int size() {
return top + 1;
}
}
/**
* @author Administrator 中缀表达式换成后缀
*/
public class InfixApp {
public static void main(String[] args) {
String input = " 3 + 4 - 6 / 2 * (6 + 3 * (4 - 1))";
InToPost theIt = new InToPost(input);
String result = theIt.doTrans();
System.out.println(result);
}
}package c_stack.C_Infix;
public class InToPost {
StackX theStackX;
String input;
String output = "";
public InToPost(String in) {
input = in;
int stackSize = in.length();
theStackX = new StackX(stackSize);
}
public String doTrans() {
for (int i = 0; i < input.length(); i++) {
char choice = input.charAt(i);
switch (choice) {
case '+':
case '-':
getOper(choice, 1);
break;
case '*':
case '/':
getOper(choice, 2);
break;
case '(':
theStackX.push(choice);
break;
case ')':
gotPraen(choice);
break;
default:// 必须是操作数,累加
output += choice;
break;
}
}
while (!theStackX.isEmpty()) {
output += theStackX.pop();
}
return output;
}
private void gotPraen(char ch) {
// 弹出所有数据项,当是(时结束循环
while (!theStackX.isEmpty()) {
char opTop = theStackX.pop();
if (opTop == '(')
break;
else {
output += opTop;
}
}
}
private void getOper(char opThis, int prec1) {
// 防止弹出的结束的不止一个,shile
// 不管怎样操作都会入栈,提出来放后面
while (!theStackX.isEmpty()) {
char opTop = theStackX.pop();
// 当出栈的是(时,放入,并结束循环
if (opTop == '(') {
theStackX.push(opTop);
break;
} else {// 判断运算符优先级,小于则在放回栈中,大于则弹出。进行下一次循环判断
int prec2;
if (opTop == '+' || opTop == '-')
prec2 = 1;
else
prec2 = 2;
if (prec2 < prec1) {
theStackX.push(opTop);
break;
}
output += opTop;
}
}
theStackX.push(opThis);
}
}package c_stack.C_Infix;
public class StackX {
private char arr[];
private int top;
private int maxSize;
public StackX(int s) {
maxSize = s;
arr = new char[maxSize];
top = -1;
}
public void push(char key) {
arr[++top] = key;
}
public char pop() {
return arr[top--];
}
public char peek() {
return arr[top];
}
public char peekN(int n) {
return arr[n];
}
public boolean isEmpty() {
return top == -1;
}
public boolean isFull() {
return top == maxSize - 1;
}
public int size() {
return top + 1;
}
}
0 0
- 栈-中缀表达式换成后缀
- 中缀表达式换成后缀表达式
- 前缀、中缀、后缀表达式(栈)
- 栈实现中缀表达式转为后缀表达式
- 栈 中缀表达式解析成后缀表达式
- 中缀表达式转为后缀表达式 ---- 栈
- 中缀表达式转换成后缀表达式 -- 栈
- 栈:中缀表达式改为后缀表达式
- 中缀转后缀表达式(栈实现)
- 数据结构栈之中缀表达式转后缀
- 中缀表达式变后缀:栈实现
- 中缀转后缀表达式
- 中缀,前缀,后缀表达式
- 前缀、中缀、后缀表达式
- 前缀、中缀、后缀表达式
- 表达式 中缀 后缀 转换
- 前缀、中缀、后缀表达式
- 前缀、中缀、后缀表达式
- 游戏服务端架构发展史(中)
- 十八掌大数据视频免费领
- JAVA编程思想笔记--第一章 对象导论
- CSDN如何转载别人的文章
- JS实现点击显示与隐藏
- 栈-中缀表达式换成后缀
- notify产生死锁的场景(备忘)
- TCP与UDP区别
- css之盒子模型box-sizing属性
- 极客学院:iOS安全攻防
- ubuntu16.04进入命令行
- 搜索引擎原理篇
- 2017成白电突破攻坚年,全方位备战成为重要竞争策略
- lazy ideas in programming