思路图
package test;
public class Work {
public static void main(String[] args) {
Chars chars = new Chars();
Number num = new Number();
String data = "( 5 +( (2 + 3 )* (4*5)))";
data = data.replace(" ", "");
char[] outchar = data.toCharArray();
int i = 0;
do {
switch (outchar[i]){
case '(':
break;
case '+':
case '*':
case '-':
case '/':
chars.push(outchar[i]);
break;
case ')':
int one = num.pop();
int two = num.pop();
switch (chars.pop()) {
case '+' -> num.push(one + two);
case '-' -> num.push(one - two);
case '*' -> num.push(one * two);
case '/' -> num.push(one / two);
default -> System.out.println("符号错误");
}
break;
default:
String str = String.valueOf(outchar[i]);
int nums = Integer.parseInt(str);
num.push(nums);
break;
}
i++;
}while (i<outchar.length);
System.out.println("结果是"+num.pop());
}
}
class Chars {
char[] chararr = new char[10];
int top = -1;
public void push(char val){
int size = chararr.length;
if(top+1==size){
char[] temp = new char[size];
int tempTop = -1;
while (top > -1){
tempTop++;
temp[tempTop]=pop();
}
chararr = temp;
top = tempTop;
tempTop = -1;
char[] newArr = new char[size*2];
while (top > -1){
tempTop++;
newArr[tempTop]=pop();
}
top = tempTop;
chararr = newArr;
}
top++;
chararr[top]=val;
}
public char pop(){
char res =chararr[top];
top--;
return res;
}
}
class Number{
int[] numarr = new int[10];
int top = -1;
public void push(int val){
int size = numarr.length;
if(top+1==size){
int[] temp = new int[size];
int tempTop = -1;
while (top > -1){
tempTop++;
temp[tempTop]=pop();
}
numarr = temp;
top = tempTop;
tempTop = -1;
int[] newArr = new int[size*2];
while (top > -1){
tempTop++;
newArr[tempTop]=pop();
}
top = tempTop;
numarr = newArr;
}
top++;
numarr[top]=val;
}
public int pop(){
int res = numarr[top];
top--;
return res;
}
public int top(){
if (top == -1) return 0;
return numarr[top];
}
}
//计算表达式
public class Caculate {
public static void main(String[] args) {
//创建存储操作符和操作数栈对象
Ops ops = new Ops();
Val vals = new Val();
//定义测试数据,规定:本实例只支持个位数的整数加减乘除
String data = "( 5 +( (2 + 3 )* (4*5)))";
//使用字符串String类的replace()方法,去掉字符串中的空格。
data = data.replace(" ", "");
//把字符串转换成字符数组,只需要调用字符串的toCharArray()方法即可。
char[] chars = data.toCharArray();
//遍历字符数组获取单个的字符。
for(int x = 0; x < chars.length; x++) {
char s = chars[x];//拿到单个字符
//逐个判断当前字符是操作符还是操作数
//如果是操作符就压入操作符栈中。
if(s == '(');//我们规定计算式忽略左括号
else if (s == '+') ops.push(s);
else if (s == '-') ops.push(s);
else if (s == '*') ops.push(s);
else if (s == '/') ops.push(s);
else if (s == ')') {
//如果遇到右括号,此时我们需要从操作符栈中取出一个操作符,
//从操作数栈中取出两个操作数,然后计算结果并再次压入操作数栈中
char op = ops.pop();
int v = vals.pop();
//判断取出来的操作符op属于哪一种运算符
if(op == '+') v = vals.pop() + v;
else if(op == '-') v = vals.pop() - v;
else if(op == '*') v = vals.pop() * v;
else if(op == '/') v = vals.pop() / v;
//把计算完成之后还需要把数据再次压入操作数栈中
vals.push(v);
} else {
//把字符转成字符串
String str = String.valueOf(s);
//把字符串数据转成整数数据
int vl = Integer.parseInt(str);
//如果属于操作数就压入操作数栈中
vals.push(vl);
}
}
System.out.println(vals.pop());
System.out.println(vals.pop());
System.out.println(ops.pop());
}
}
//存放操作符的栈
class Ops {
//因为我们的栈是基于数组来实现的,因此我们需要先创建数组。
static char[] c = new char[10];
//栈的属性top
static int top = -1;
//入栈方法
public static void push(char ch) {
if(top < c.length) {
c[++top] = ch;
}
}
//出栈方法
public static char pop() {
if(top > -1) return c[top--];
return '!';
}
}
//存放操作数的栈
class Val {
//因为我们的栈是基于数组来实现的,因此我们需要先创建数组。
static int[] v = new int[10];
//栈的属性top
static int top = -1;
//入栈方法
public static void push(int value) {
if(top < v.length) v[++top] = value;
}
//出栈方法
public static int pop() {
if(top > -1) return v[top--];
return -2147483648;
}
}
评论 (0)