# 使用中缀表达式实现一个简易计算机

class stack<E>{
private E[] arr;
private int size;

public stack(int capacity){
arr = (E[]) new Object[capacity];
}

//入栈
public void push(E val){
if (isFull()){
ensureCapacity();
}
arr[size] = val;
size = size+1;
}

//出栈
public E pop(){
//判空
if (isEmpty()){
throw new RuntimeException("空栈");
}
//出栈一次就要进行元素的移动
E val = arr[this.size-1];
this.size--;

this.arr = Arrays.copyOf(this.arr,size);
return val;
}

//判空
public boolean isEmpty(){
return this.size == 0;
}

//判满
public boolean isFull(){
return this.size == this.arr.length;
}

//扩容
public void ensureCapacity(){
if (size == arr.length){
this.arr = Arrays.copyOf(arr, arr.length *2 + 1);
}
}

//获取栈顶元素
public E peek(){
return this.arr[size-1];
}
}

public class ComputerDemo {
public static void main(String[] args) {
stack<Integer> number = new stack<>(10);
stack<Character> chars = new stack<>(10);
String expression = "7*2*2-5+1-5+3-4";
calculator(number,chars,expression);
}
//中缀
public static void calculator(stack<Integer> number,stack<Character> chars,String expression){
for (int i = 0; i < expression.length(); i++) {
if (Character.isDigit(expression.charAt(i))){
//表示当前字符为数字
String s = String.valueOf(expression.charAt(i));
int j = i+1;
String tmp = new String();
//这里出现70+2这种情况将数字拼接起来
while (j < expression.length()){
if (Character.isDigit(expression.charAt(j))){
tmp+=expression.charAt(j);
}else {
break;
}
j++;
}
i = j -1;
s += tmp;
Integer value = new Integer(s);
//将连续两个数字的字符拼接起来并入栈
number.push(value);
}else{
//运算符
if (chars.isEmpty()){
//字符栈为空直接入栈
chars.push(expression.charAt(i));
}else{
int priority = priority(expression.charAt(i));
Character peek = chars.peek();
int peeks = priority(peek);
//将当前字符与栈中的字符作比较
if (priority <= peeks){
int num1 = number.pop();
int num2 = number.pop();
Character pop = chars.pop();
int compute = compute(num1, num2, pop);
//将计算结果入栈
number.push(compute);
//将当前运算符入栈
chars.push(expression.charAt(i));
}else {
chars.push(expression.charAt(i));
}
}
}
}
//需要对其符号栈做出判空处理  如果为空则直接打印number栈即可，否则继续计算
while (!chars.isEmpty()){
Character pop = chars.pop();
Integer num1 = number.pop();
Integer num2 = number.pop();
int res = compute(num1, num2, pop);
number.push(res);
}

System.out.println(number.pop());
}

//设置运算符的优先级，优先级越高数字值越大
public static int priority(char oper){
if (oper == '*' || oper == '/'){
return 1;
}else if (oper == '+' || oper == '-'){
return 0;
}else{
return -1;
}
}

//计算方法
public static int compute(int num1,int num2,char oper){
int res = 0;
switch (oper){
case '+':
res = num1+num2;
break;
case '/':
res = num2/num1;
break;
case '*':
res = num1*num2;
break;
case '-':
res = num2 - num1;
break;
}
return res;
}

}


10-16 961
08-07 150
05-06 3726
04-06 279
09-22 2618
04-14 754
08-08 275
08-27 170
05-09 917
10-15 3万+