我写了以下程序。我的程序可以从+
的值提供简单的算术计算,如-
,*
,/
,JTextField
。但我需要从多个值中获得运算符优先级BMDAS(括号,乘法,除法,加法和减法)的答案。示例:用户类型10-5+8/56*(3-5+3)
:答案为5
。我提到了我的实现代码。如何更新我的程序以获得上述要求?帮助我!
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class calculateValue extends JFrame implements ActionListener{
private JButton btn;
private JTextField txt;
String operator="+-*/()";
public static void main(String args[]){
new calculateValue();
}
public calculateValue(){
setSize(300, 200);
setLocationRelativeTo(null);
setTitle("Calcuate Multiple value!");
setLayout(null);
txt=new JTextField();
txt.setBounds(20,20, 150,25);
add(txt);
btn=new JButton("Show Answer");
btn.setBounds(20, 50,150, 25);
btn.addActionListener(this);
add(btn);
setVisible(true);
}
public char decideOperator(String s){
char c=' ';
for(int i=0;i<operator.length();i++){
if(s.contains(operator.charAt(i)+"")){
c=operator.charAt(i);
}
}
return c;
}
public int decideIndex(String s){
int index=100;
for(int i=0;i<operator.length();i++){
if(s.contains(operator.charAt(i)+"")){
index=s.indexOf(operator.charAt(i));
}
}
return index;
}
public void actionPerformed(ActionEvent e){
String temp=txt.getText().trim();
double d1=Double.parseDouble(temp.substring(0, decideIndex(temp)));
double d2=Double.parseDouble(temp.substring(decideIndex(temp)+1));
if(decideOperator(temp)=='+') System.out.println(d1+d2);
else if(decideOperator(temp)=='-') System.out.println(d1-d2);
if(decideOperator(temp)=='*') System.out.println(d1*d2);
if(decideOperator(temp)=='/') System.out.println(d1/d2);
}
}
答案 0 :(得分:1)
这看起来有点过分,但这是我对表达式解析器的实现:
(作为SSCCE):
import java.math.BigDecimal;
public class Main {
public static void main(String[] args) throws Exception{
System.out.println("Final Output: " + evalutateExpression("(56*3)*(4+3)"));
}
public static double evalutateExpression(String Input){
//This method is extremely sensitive to bad input, try to do some clean up before sending it to the main evaluation body.
Input = Input.replaceAll(" ", ""); //Example of a cleanup
return Double.parseDouble(recursiveEvalutation(Input));
}
private static String recursiveEvalutation(String I){
if(I.contains("(")){
int RIndex = I.indexOf(")");
int LIndex = I.lastIndexOf("(", RIndex);
return recursiveEvalutation(I.substring(0, LIndex) + recursiveEvalutation(I.substring(LIndex + 1, RIndex)) + I.substring(RIndex + 1, I.length()));
}else if(I.contains("^") || I.contains("√")){
int PowerIndex = I.indexOf("^");
int SQRTIndex = I.indexOf("√");
if(PowerIndex == -1){
PowerIndex = Integer.MAX_VALUE;
}
if(SQRTIndex == -1){
SQRTIndex = Integer.MAX_VALUE;
}
if(PowerIndex <= SQRTIndex){
int num2End = findNumberEnd(I.substring(PowerIndex + 1, I.length())) + PowerIndex + 1;
int num1Start = findNumberStart(I.substring(0, PowerIndex));
double Num1 = Double.parseDouble(I.substring(num1Start, PowerIndex));
double Num2 = Double.parseDouble(I.substring(PowerIndex + 1, num2End));
String Eval = new BigDecimal(Math.pow(Num1, Num2)).toPlainString();
return recursiveEvalutation(I.substring(0, num1Start) + Eval + I.substring(num2End, I.length()));
}else{
int num2End = findNumberEnd(I.substring(SQRTIndex + 1, I.length())) + SQRTIndex + 1;
int num1Start = findNumberStart(I.substring(0, SQRTIndex));
double Num1 = Double.parseDouble(I.substring(SQRTIndex + 1, num2End));
String Eval = new BigDecimal(Math.sqrt(Num1)).toPlainString();
return recursiveEvalutation(I.substring(0, num1Start) + Eval + I.substring(num2End, I.length()));
}
}else if(I.contains("*") || I.contains("/")){
int MultiplyIndex = I.indexOf("*");
int DivideIndex = I.indexOf("/");
if(MultiplyIndex == -1){
MultiplyIndex = Integer.MAX_VALUE;
}
if(DivideIndex == -1){
DivideIndex = Integer.MAX_VALUE;
}
if(MultiplyIndex <= DivideIndex){
int num2End = findNumberEnd(I.substring(MultiplyIndex + 1, I.length())) + MultiplyIndex + 1;
int num1Start = findNumberStart(I.substring(0, MultiplyIndex));
double Num1 = Double.parseDouble(I.substring(num1Start, MultiplyIndex));
double Num2 = Double.parseDouble(I.substring(MultiplyIndex + 1, num2End));
String Eval = new BigDecimal(Num1 * Num2).toPlainString();
return recursiveEvalutation(I.substring(0, num1Start) + Eval + I.substring(num2End, I.length()));
}else{
int num2End = findNumberEnd(I.substring(DivideIndex + 1, I.length())) + DivideIndex + 1;
int num1Start = findNumberStart(I.substring(0, DivideIndex));
double Num1 = Double.parseDouble(I.substring(num1Start, DivideIndex));
double Num2 = Double.parseDouble(I.substring(DivideIndex + 1, num2End));
String Eval = new BigDecimal(Num1 / Num2).toPlainString();
return recursiveEvalutation(I.substring(0, num1Start) + Eval + I.substring(num2End, I.length()));
}
}else if(I.contains("+") || I.contains("−")){
int AddIndex = I.indexOf("+");
int MinusIndex = I.indexOf("−");
if(AddIndex == -1){
AddIndex = Integer.MAX_VALUE;
}
if(MinusIndex == -1){
MinusIndex = Integer.MAX_VALUE;
}
if(AddIndex <= MinusIndex){
int num2End = findNumberEnd(I.substring(AddIndex + 1, I.length())) + AddIndex + 1;
int num1Start = findNumberStart(I.substring(0, AddIndex));
double Num1 = Double.parseDouble(I.substring(num1Start, AddIndex));
double Num2 = Double.parseDouble(I.substring(AddIndex + 1, num2End));
String Eval = new BigDecimal(Num1 + Num2).toPlainString();
return recursiveEvalutation(I.substring(0, num1Start) + Eval + I.substring(num2End, I.length()));
}else{
int num2End = findNumberEnd(I.substring(MinusIndex + 1, I.length())) + MinusIndex + 1;
int num1Start = findNumberStart(I.substring(0, MinusIndex));
double Num1 = Double.parseDouble(I.substring(num1Start, MinusIndex));
double Num2 = Double.parseDouble(I.substring(MinusIndex + 1, num2End));
String Eval = new BigDecimal(Num1 - Num2).toPlainString();
return recursiveEvalutation(I.substring(0, num1Start) + Eval + I.substring(num2End, I.length()));
}
}else{
return I;
}
}
private static int findNumberEnd(String I){
char[] expression = I.toCharArray();
for(int x = 0;x < expression.length;x++){
if(!Character.isDigit(expression[x]) && expression[x] != '.' && expression[x] != '-'){
return x;
}
}
return expression.length;
}
private static int findNumberStart(String I){
char[] expression = I.toCharArray();
for(int x = expression.length - 1;x >= 0;x--){
if(!Character.isDigit(expression[x]) && expression[x] != '.' && expression[x] != '-'){
return x + 1;
}
}
return 0;
}
}
遵循递归过程,按照运算符优先级连续计算运算符左右表达式。主要的评估者是recursiveEvalutation()方法。
限制包括:
请注意,对于减法,我使用正确的“减号”进行操作,使用连字符表示负数。
如果您有任何问题可以随意询问,请稍微研究一下代码。
答案 1 :(得分:0)
您需要使用Reverse Polish Notation解析和准备表达式进行计算(它使用堆栈按顺序解析数学运算优先级),或者(简单解决方案)检查以下SO帖子中提供的答案{{3 }}