1.中缀表达式转为后缀表达式
后缀表达式:也叫逆波兰表达式,将运算符卸载操作数之后
中缀形式:(10+20/2*3)/2+8
后缀形式:10 20 2/3*+2/8
方便计算机计算,但对用户不友好
从左到右遍历表达式的每个数字和符号,遇到是数字就进栈,遇到是符号,就将处于栈顶两个数字出栈,进行运算,运算结果进栈,一直到最终获得结果。分为俩个栈
package p2.Linearstru;
public class InfixToSuffix {
public static void main(String[] args) {
String expression = "(10+20/2*3)/2+8";
expression = infixToSuffix(expression);
System.out.println(expression);
}
public static String infixToSuffix(String expression) {
//操作符的栈
ArrayStack<String> opStack = new ArrayStack<>();
//后缀表达式的线性表
ArrayList<String> suffixList = new ArrayList<>();
expression = insertBlanks(expression);
String[] tokens = expression.split(" ");
for (String token : tokens) {
if (token.length() == 0) {
continue;
}
if (isOperator(token)) {
/*
1.什么时候操作符进栈
2.如果栈为空\
3.如果栈顶为操作符,且优先级比当前栈顶级别小
*/
while (true) {
if (opStack.isEmpty() || opStack.peek().equals("(") || priority(opStack.peek()) < priority(token)) {
opStack.push(token);
break;
}
suffixList.add(opStack.pop());
}
} else if (token.equals("(")) {
opStack.push(token);
} else if (token.equals(")")) {
while (!opStack.peek().equals("(")) {
suffixList.add(opStack.pop());
}
opStack.pop();
} else if (isNumber(token)) {
suffixList.add(token);
} else {
throw new IllegalArgumentException("wrong char:" + expression);
}
}
while (!opStack.isEmpty()) {
suffixList.add(opStack.pop());
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < suffixList.size(); i++) {
sb.append(suffixList.get(i));
sb.append(' ');
}
return sb.toString();
}
private static int priority(String token) {
if (token.equals("+") || token.equals("-")) {
return 0;
}
if (token.equals("*") || token.equals("/")) {
return 1;
}
return -1;
}
private static boolean isNumber(String token) {
return token.matches("\\d+");
}
private static boolean isOperator(String token) {
return token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/");
}
//对原表达式进行格式化处理 给所有的非数字字符两边添加空格
private static String insertBlanks(String expression) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < expression.length(); i++) {
char c = expression.charAt(i);
if (c == '(' || c == ')' || c == '+' || c == '-' || c == '*' || c == '/') {
sb.append(' ');
sb.append(c);
sb.append(' ');
} else {
sb.append(c);
}
}
return sb.toString();
}
}
2.用栈来实现后缀表达式(调用前面类中的方法)
package p2.Linearstru;
public class SuffixCalculator {
public static void main(String[] args) {
String infixExpression = "(10+20/2*3)/2+8";
String suffixExpression = InfixToSuffix.infixToSuffix(infixExpression);
int result = evaluateSuffix(suffixExpression);
System.out.println(result);
}
private static int evaluateSuffix(String Expression) {
ArrayStack<Integer> stack = new ArrayStack<>();
String[] tokens = Expression.split(" ");
for (String token : tokens) {
if (token.length() == 0) {
continue;
}
if (isNumber(token)) {
stack.push(new Integer(token));
} else {
processAnOperator(stack, token);
}
}
return stack.pop();
}
private static boolean isNumber(String token) {
return token.matches("\\d+");
}
private static void processAnOperator(ArrayStack<Integer> stack, String token) {
int num1 = stack.pop();
int num2 = stack.pop();
if (token.equals("+")) {
stack.push(num2 + num1);
} else if (token.equals("-")) {
stack.push(num2 - num1);
} else if (token.equals("*")) {
stack.push(num2 * num1);
} else if (token.equals("/")) {
stack.push(num2 / num1);
}
}
}
3.进制转化
(1)将十进制转化为十六进制
余数取倒法
package p2.Linearstru;
public class DecToHex {
public static void main(String[] args) {
int num =654321;
ArrayStack<String> stack=new ArrayStack<>();
while(num!=0){
int a =num%16;
if(a<10){
stack.push(a+"");
}else{
stack.push((char)(a+55)+"");
}
num/=16;
}
StringBuilder sb=new StringBuilder();
while ((!stack.isEmpty())){
sb.append(stack.pop());
}
System.out.println(sb.toString());
}
}
其中要注意的是十六进制中字母:将数字和字母的转化(char)(a+55)
(2)将十六进制转化为十进制
package p2.Linearstru;
public class HexToDec {
public static void main(String[] args) {
String hex ="9FBF1";
ArrayStack<Character> stack=new ArrayStack<>();
for(int i =0;i<hex.length();i++){
stack.push(hex.charAt(i));
}
int sum=0;
int mi=0;
while(!stack.isEmpty()){
char c=stack.pop();
sum+=getNumber(c)*Math.pow(16,mi);
mi++;
}
System.out.println(sum);
}
private static int getNumber(char c){
if(!(c>='0'&&c<='9'||c>='A'&&c<='F')){
throw new IllegalArgumentException("wrong char!") ;
}
if(c>'0'&&c<='9'){
return c-'0';
}else{
return c-'A'+10;
}
}
}
这里注意的是:c-'0';其实是要获得原本的数字(c的类型为字符类型)
c-'A'+10实现的其实是先得到与字符相符的ASCII码自动转换为字符