队列-运算后缀表达式
来源:互联网 发布:虚拟充值软件靠谱吗 编辑:程序博客网 时间:2024/06/07 01:27
package d_queue.E_SuffixExpressionValue;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 运算后缀表达式
*
* @author Administrator
*
*/
public class InfixApp {
public static void main(String[] args) throws IOException {
String input;
int output;
while (true) {
System.out.print("Enter infix:");
System.out.flush();
input = getString();
if (input.equals(""))
break;
ParsePost theParse = new ParsePost(input);
output = theParse.doTrans();
System.out.println("Postfix is " + output);
}
}
public static String getString() throws IOException {
String s = new BufferedReader(new InputStreamReader(System.in))
.readLine();
return s;
}
}package d_queue.E_SuffixExpressionValue;
public class ParsePost {// infix to postfix conversion 中缀转后缀转换
private StackX theStackX;
private String input;
public ParsePost(String in) {
input = in;
theStackX = new StackX(input.length());
}
public int doTrans() {// do translation to postfix 转变后缀
int num1, num2, interAns;
for (int j = 0; j < input.length(); j++) {
char ch = input.charAt(j);
theStackX.displaysStack("For:" + ch);
if (ch >= '0' && ch <= '9')
theStackX.push((int) (ch - '0'));
else {
num2 = theStackX.pop();
num1 = theStackX.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;
}
theStackX.push(interAns);}
}
interAns=theStackX.pop();
return interAns;
}
}package d_queue.E_SuffixExpressionValue;
public class StackX {
private int maxSize;
private int[] stackArray;
private int top;
public StackX(int s) {
maxSize = s;
stackArray = new int[maxSize];
top = -1;
}
public void push(int i) {
stackArray[++top] = i;
}
public int pop() {
return stackArray[top--];
}
public int peek() {
return stackArray[top];
}
public boolean isEmpty() {
return top == -1;
}
public int size() {
return top + 1;
}
public int peekN(int n) {
return stackArray[n];
}
/*
* 显示堆栈
*/
public void displaysStack(String s) {
System.out.print(s);
System.out.print(" Stack {bottom-->top}:");
for (int j = 0; j < size(); j++) {
System.out.print(peekN(j));
System.out.print(" ");
}
System.out.println();
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 运算后缀表达式
*
* @author Administrator
*
*/
public class InfixApp {
public static void main(String[] args) throws IOException {
String input;
int output;
while (true) {
System.out.print("Enter infix:");
System.out.flush();
input = getString();
if (input.equals(""))
break;
ParsePost theParse = new ParsePost(input);
output = theParse.doTrans();
System.out.println("Postfix is " + output);
}
}
public static String getString() throws IOException {
String s = new BufferedReader(new InputStreamReader(System.in))
.readLine();
return s;
}
}package d_queue.E_SuffixExpressionValue;
public class ParsePost {// infix to postfix conversion 中缀转后缀转换
private StackX theStackX;
private String input;
public ParsePost(String in) {
input = in;
theStackX = new StackX(input.length());
}
public int doTrans() {// do translation to postfix 转变后缀
int num1, num2, interAns;
for (int j = 0; j < input.length(); j++) {
char ch = input.charAt(j);
theStackX.displaysStack("For:" + ch);
if (ch >= '0' && ch <= '9')
theStackX.push((int) (ch - '0'));
else {
num2 = theStackX.pop();
num1 = theStackX.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;
}
theStackX.push(interAns);}
}
interAns=theStackX.pop();
return interAns;
}
}package d_queue.E_SuffixExpressionValue;
public class StackX {
private int maxSize;
private int[] stackArray;
private int top;
public StackX(int s) {
maxSize = s;
stackArray = new int[maxSize];
top = -1;
}
public void push(int i) {
stackArray[++top] = i;
}
public int pop() {
return stackArray[top--];
}
public int peek() {
return stackArray[top];
}
public boolean isEmpty() {
return top == -1;
}
public int size() {
return top + 1;
}
public int peekN(int n) {
return stackArray[n];
}
/*
* 显示堆栈
*/
public void displaysStack(String s) {
System.out.print(s);
System.out.print(" Stack {bottom-->top}:");
for (int j = 0; j < size(); j++) {
System.out.print(peekN(j));
System.out.print(" ");
}
System.out.println();
}
}
0 0
- 队列-运算后缀表达式
- 栈-运算后缀表达式
- 后缀表达式的运算求值
- 队列-解析算数表达式:后缀表达式
- 后缀表达式实现六则运算
- 【stack 实现前缀、后缀、中缀表达式运算】
- 栈的应用-后缀表达式运算求值
- 栈与队列(三)中缀表达式转后缀表达式
- 数学运算后缀表达式转换成中缀表达式
- 用数组顺序栈实现表达式运算(后缀表达式)
- 表达式的前中后缀表示和表达式运算
- 后缀表达式+栈的应用=四则表达式运算
- 实现逆波兰表达式(后缀表达式)的运算结果
- 漫谈栈队列及后缀表达式,后缀中缀表达式间的转换
- 后缀表达式 (2) —— 表达树与后缀表达式的运算
- [栈和队列]从中缀向后缀转换表达式
- 第六周--数据结构--队列的应用之后缀表达式(栈)
- 用栈的方法运算后缀表达式的值
- 队列-解析算数表达式:后缀表达式
- C#开发人员应该知道的13件事情
- 双向队列
- 如何在Linux下干净卸载db2数据库
- git warning: LF will be replaced by CRLF in 解决办法
- 队列-运算后缀表达式
- mysql 时间索引失效
- 队列-实现栈
- sql执行顺序
- 5.2 链表
- JavaSE_27th_数组——冒泡排序
- 如果看了此文你还不懂傅里叶变换,那就过来掐死我吧【完整版】
- 链表-A-单链表
- Linux~连接windows的ftp,unzip出现的问题