Java RPN计算器电源上的运算符优先级不起作用
除了幂(^)和模(%)运算符,结果和所有输入都正常工作。有人能告诉我我的错误在哪里吗。 这是我的Github链接: 如果输入为3*(2+3)-7+8*8,则工作正常 如果输入为8%2或2^3,则会显示一个错误 后缀和计算器类在这里Java RPN计算器电源上的运算符优先级不起作用,java,data-structures,operator-precedence,rpn,Java,Data Structures,Operator Precedence,Rpn,除了幂(^)和模(%)运算符,结果和所有输入都正常工作。有人能告诉我我的错误在哪里吗。 这是我的Github链接: 如果输入为3*(2+3)-7+8*8,则工作正常 如果输入为8%2或2^3,则会显示一个错误 后缀和计算器类在这里 import java.util.ArrayList; public class Calculator{ public double EvaluatePostfix(ArrayList<String> postfix) {
import java.util.ArrayList;
public class Calculator{
public double EvaluatePostfix(ArrayList<String> postfix) {
Stack result = new Stack();
for (int i = 0; i < postfix.size(); i++) {
String token=postfix.get(i);
if(tryParseInt(token)){
result.push(Double.parseDouble(token));
}
else {
double operand1=result.pop();
double operand2=result.pop();
System.out.println(operand2);
System.out.println(operand1);
System.out.println(token);
double calc= calculate(operand1, operand2, token);
System.out.println(calc);System.out.println();
result.push(calc);
}
}
return result.pop();
}
private static double calculate(double operand1, double operand2, String token) {
double result = 0.0;
switch (token)
{
case "+":
result= operand2 + operand1;
return result;
case "-":
result= operand2 - operand1;
return result;
case "/":
result= operand2 / operand1;
return result;
case "*":
result= operand2 * operand1;
return result;
case "%":
result=operand2 % operand1;
return result;
case "^":
result=operand1;
for(int i=1; i<= operand2; i++) {
result= result * operand1;}
return result;
}
return result;
}
public boolean tryParseInt(String s) {
try {
Double.parseDouble(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}
class Stack {
static final int MAX = 10;
int top;
double a[] = new double[MAX]; // Maximum size of Stack
boolean isEmpty()
{
return (top < 0);
}
Stack()
{
top = -1;
}
boolean push(double d){
if (top >= (MAX - 1)) {
System.out.println("Stack Overflow");
return false;
}
else {
a[++top] = d;
System.out.println(d + " pushed into stack");
return true;
}
}
double pop()
{
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
}
else {
double x = a[top--];
return x;
}
}
double peek()
{
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
}
else {
double x = a[top];
return x;
}
}
}
导入java.util.ArrayList;
公共类计算器{
公共双EvaluatePostfix(ArrayList后缀){
堆栈结果=新堆栈();
对于(int i=0;i
导入java.util.ArrayList;
公共类PostfixCreater{
私有枚举优先级
{
lp(0),rp(1),加(2),减(3),除(4),多(5),模(6),功率(7),无(8),数(9);
私有整数索引;
优先级(整数索引)
{
这个指数=指数;
}
public int getIndex()
{
收益指数;
}
}
/**堆栈内优先级**/
私有静态final int[]isp={0,19,12,12,13,13,13,14,0};
/**传入字符优先级**/
私有静态final int[]icp={20,19,12,12,13,13,13,14,0};
/**操作员**/
私有静态最终字符串[]运算符={“{”、“}”、“+”、“-”、“/”、“*”、“%”、“^”、“};
公共优先级getToken(字符串符号)
{
开关(符号)
{
大小写“(”:返回priority.lp;
case“)”:返回priority.rp;
大小写“+”:返回priority.add;
大小写“-”:返回优先级.减号;
大小写“/”:返回优先级.divide;
大小写“*”:返回priority.mult;
大小写“%”:返回priority.mod;
案例“^”:返回priority.pow;
案例“”:返回priority.noth;
默认值:返回priority.number;
}
}
公共布尔tryParseInt(字符串s){
试试{
Double.parseDouble(s);
返回true;
}捕获(数字格式){
返回false;
}
}
公共ArrayList后缀(字符串中缀)
{
ArrayList后缀=新的ArrayList();
String[]t=infix.split(“(?将正则表达式更改为String[]t=infix.split(“(?将正则表达式更改为String[]t=infix.split(”)?
import java.util.ArrayList;
public class PostfixCreater {
private enum Precedence
{
lp(0), rp(1), add(2), minus(3), divide(4), mult(5), mod(6),pow(7), noth(8), number(9);
private int index;
Precedence(int index)
{
this.index = index;
}
public int getIndex()
{
return index;
}
}
/** in stack precedence **/
private static final int[] isp = {0, 19, 12, 12, 13, 13, 13, 14, 0};
/** incoming character precedence **/
private static final int[] icp = {20, 19, 12, 12, 13, 13, 13, 14, 0};
/** operators **/
private static final String[] operators = {"{", "}", "+", "-", "/", "*", "%", "^", " "};
public Precedence getToken(String symbol)
{
switch (symbol)
{
case "(" : return Precedence.lp;
case ")" : return Precedence.rp;
case "+" : return Precedence.add;
case "-" : return Precedence.minus;
case "/" : return Precedence.divide;
case "*" : return Precedence.mult;
case "%" : return Precedence.mod;
case "^" : return Precedence.pow;
case " " : return Precedence.noth;
default : return Precedence.number;
}
}
public boolean tryParseInt(String s) {
try {
Double.parseDouble(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}
public ArrayList<String> postfix(String infix)
{
ArrayList<String> postfix =new ArrayList<String>();
String[] t = infix.split("(?<=[-+*/()])|(?=[-+*/()])");
Stack stack = new Stack();
Precedence pretoken;
for (int i = 0; i < t.length; i++)
{
String token =t[i];
pretoken=getToken(t[i]);
/** if token is operand append to postfix **/
if (tryParseInt(token)){
postfix.add(t[i]);
}
/** if token is right parenthesis pop till matching left parenthesis **/
else if(pretoken==Precedence.rp) {
while (stack.peek() != Precedence.lp)
postfix.add(operators[stack.pop().getIndex()]);
/** discard left parenthesis **/
stack.pop();
}
else {
System.out.print(pretoken.getIndex());
System.out.println();
while (!stack.isEmpty() && isp[stack.peek().getIndex()] >= icp[pretoken.getIndex()])
{
postfix.add(operators[stack.pop().getIndex()]);
}
stack.push(pretoken);
}
}
while(!stack.isEmpty())
postfix.add(operators[stack.pop().getIndex()]);
return postfix;
}
class Stack {
static final int MAX = 10;
int top;
Precedence a[] = new Precedence[MAX]; // Maximum size of Stack
boolean isEmpty()
{
return (top < 0);
}
Stack()
{
top = -1;
}
boolean push(Precedence x)
{
if (top >= (MAX - 1)) {
System.out.println("Stack Overflow");
return false;
}
else {
a[++top] = x;
System.out.println(x + " pushed into stack");
return true;
}
}
Precedence pop()
{
// if (top < 0) {
// System.out.println("Stack Underflow");
// return 0;
// }
// else {
Precedence x = a[top--];
return x;
// }
}
Precedence peek()
{
// if (top < 0) {
// System.out.println("Stack Underflow");
// return 0;
// }
// else {
Precedence x = a[top];
return x;
// }
}
}
}