用java 实现的计算器,原理看代码注释,具体内容如下
public class mycalculator {
public static void main(string[] args) {
string obj = "6+(8/2)+6/3+1*8 + 30";
arraylist arraylist = strformat(obj);
system.out.println(obj + "=" + calculator(arraylist));
}
/**
* 采用后续表达式计算结果
* 1、当前字符串为数字时,直接入栈
* 2、当前字符串为计算符时,取出栈中的前两个计算
* 3、将计算结果再放入到栈中,栈中最后剩余的一个元素就是要求的结果
*/
private static int calculator(arraylist obj) {
arraylist result = transform(obj);
system.out.println(result);
stack stack = new stack<>();
for (int i = 0; i < result.size(); i++) {
string symbol = result.get(i);
if (isdigital(symbol)) { //数字直接入栈
stack.push(integer.parseint(symbol));
} else { // 处理操作符
int num1, num2;
num1 = stack.pop(); //取出两个数
num2 = stack.pop();
switch (symbol) {
case "+":
stack.push(num2 + num1);
break;
case "-":
stack.push(num2 - num1);
break;
case "*":
stack.push(num2 * num1);
break;
case "/":
stack.push(num2 / num1);
break;
default:
break;
}
}
}
return stack.pop();
}
/**
* 中序遍历改为后续遍历
*/
private static arraylist transform(arraylist arraylist) {
stack stack = new stack<>();
arraylist result = new arraylist<>();
for (int index = 0; index < arraylist.size(); index++) {
string symbol = arraylist.get(index);
if (isdigital(symbol)) { //如果是数字直接输出
result.add(symbol);
} else if (symbol.equals(")")) {
string tmp;
while (!(tmp = stack.pop()).equals("(")) { // 匹配成功后停止
result.add(tmp);
}
} else {
if (stack.isempty()) {
stack.push(symbol);
continue;
}
string tmp = stack.peek();
while (outpriority(symbol) <= inpriority(tmp)) { //优先级小于栈内优先级,一直出栈
result.add(tmp);
stack.pop();
if (stack.isempty()) {
break;
}
tmp = stack.peek();
}
stack.push(symbol);
}
}
//将剩余的出栈
while (!stack.isempty()) {
result.add(stack.pop());
}
return result;
}
/**
* 首先对string 进行格式化 转化成arraylist
* @param src 3*5+8;
* @return arraylist 3 * 5 + 8
*/
private static arraylist strformat(string src) {
if (src == null || src.equals("")) {
return null;
}
arraylist arraylist = new arraylist<>();
stringbuilder comchar = new stringbuilder();
for (int i = 0; i
char ch = src.charat(i);
if (ch == ' ') {
continue; //去除空格
}
if (!character.isdigit(ch)) {
if (!comchar.tostring().trim().equals("")) {
arraylist.add(comchar.tostring().trim());
comchar.delete(0, comchar.length());
}
arraylist.add(ch + "");
continue;
}
comchar.append(ch);
}
if (!comchar.tostring().trim().equals("")) {
arraylist.add(comchar.tostring().trim());
}
return arraylist;
}
/**
* 判断是否为数字
* @param symbol 782 或者 + - * /
* @return true or false
*/
private static boolean isdigital(string symbol) {
return !symbol.equals("+") && !symbol.equals("-")
&& !symbol.equals("*") && !symbol.equals("/")
&& !symbol.equals("(") && !symbol.equals(")");
}
private static int inpriority(string ch) {
switch (ch) {
case "+":
case "-":
return 2;
case "*":
case "/":
return 4;
case ")":
return 7;
case "(":
return 1;
default:
return 0;
}
}
private static int outpriority(string ch) {
switch (ch) {
case "+":
case "-":
return 3;
case "*":
case "/":
return 5;
case ")":
return 1;
case "(":
return 7;
default:
return 0;
}
}
}
以上全部为本篇文章的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。
希望与广大网友互动??
点此进行留言吧!