我正在尝试制作一个计算器并考虑过一个想法,但不知道如何在Java中实现它。我正在尝试制作一个在用户输入时有效的计算器:
1+2-3/4 + 9 * 7= x
我应该得到的答案是63
问题
我没有接受以下方法的教导,需要在这些领域提供帮助:
从左边的字符串中的每个偶数字符或右边的每个奇数字符中收集运算符
收集要计算的数字是左边的每个奇数或者右边的每个奇数。
我目前制作了一个计算器程序,当您在单独的输入中输入每个数字和运算符时,该程序有效。但我需要做到这一点,以便用户在一个输入而不是单独的输入中输入等式。
这是我目前的计划:
import java.io.*;
public class Original_Calc
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
double no,k=0,result;
System.out.println("Enter number");
int no1=Integer.parseInt(br.readLine());
System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide)");
String operand=br.readLine();
String check="=";
switch (operand)
{
case "+":
result=no1;
System.out.println("Enter number");
no=Integer.parseInt(br.readLine());
result+=no;
System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
operand=br.readLine();
k=check.compareTo(operand);
for(;;)
{
if(k!=0)
{
switch (operand)
{
case "+":
System.out.println("Enter number");
no=Integer.parseInt(br.readLine());
result+=no;
System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
operand=br.readLine();
k=check.compareTo(operand);
break;
case "-":
System.out.println("Enter number");
no=Integer.parseInt(br.readLine());
result-=no;
System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
operand=br.readLine();
k=check.compareTo(operand);
break;
case "*":
System.out.println("Enter number");
no=Integer.parseInt(br.readLine());
result*=no;
System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
operand=br.readLine();
k=check.compareTo(operand);
break;
case "/":
System.out.println("Enter number");
no=Integer.parseInt(br.readLine());
result/=no;
System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
operand=br.readLine();
k=check.compareTo(operand);
break;
default:
System.out.println("Please follow the instructions carefully");
}
}
else
{
System.out.println("The result is "+result);
break;
}
}
break;
case "-":
result=no1;
System.out.println("Enter number");
no=Integer.parseInt(br.readLine());
result-=no;
System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
operand=br.readLine();
k=check.compareTo(operand);
for(;;)
{
if(k!=0)
{
switch (operand)
{
case "+":
System.out.println("Enter number");
no=Integer.parseInt(br.readLine());
result+=no;
System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
operand=br.readLine();
k=check.compareTo(operand);
break;
case "-":
System.out.println("Enter number");
no=Integer.parseInt(br.readLine());
result-=no;
System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
operand=br.readLine();
k=check.compareTo(operand);
break;
case "*":
System.out.println("Enter number");
no=Integer.parseInt(br.readLine());
result*=no;
System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
operand=br.readLine();
k=check.compareTo(operand);
break;
case "/":
System.out.println("Enter number");
no=Integer.parseInt(br.readLine());
result/=no;
System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
operand=br.readLine();
k=check.compareTo(operand);
break;
default:
System.out.println("Please follow the instructions carefully");
}
}
else
{
System.out.println("The result is "+result);
break;
}
}
break;
case "*":
result=no1;
System.out.println("Enter number");
no=Integer.parseInt(br.readLine());
result*=no;
System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
operand=br.readLine();
k=check.compareTo(operand);
for(;;)
{
if(k!=0)
{
switch (operand)
{
case "+":
System.out.println("Enter number");
no=Integer.parseInt(br.readLine());
result+=no;
System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
operand=br.readLine();
k=check.compareTo(operand);
break;
case "-":
System.out.println("Enter number");
no=Integer.parseInt(br.readLine());
result-=no;
System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
operand=br.readLine();
k=check.compareTo(operand);
break;
case "*":
System.out.println("Enter number");
no=Integer.parseInt(br.readLine());
result*=no;
System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
operand=br.readLine();
k=check.compareTo(operand);
break;
case "/":
System.out.println("Enter number");
no=Integer.parseInt(br.readLine());
result/=no;
System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
operand=br.readLine();
k=check.compareTo(operand);
break;
default:
System.out.println("Please follow the instructions carefully");
}
}
else
{
System.out.println("The result is "+result);
break;
}
}
break;
case "/":
result=no1;
System.out.println("Enter number");
no=Integer.parseInt(br.readLine());
result/=no;
System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
operand=br.readLine();
k=check.compareTo(operand);
for(;;)
{
if(k!=0)
{
switch (operand)
{
case "+":
System.out.println("Enter number");
no=Integer.parseInt(br.readLine());
result+=no;
System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
operand=br.readLine();
k=check.compareTo(operand);
break;
case "-":
System.out.println("Enter number");
no=Integer.parseInt(br.readLine());
result-=no;
System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
operand=br.readLine();
k=check.compareTo(operand);
break;
case "*":
System.out.println("Enter number");
no=Integer.parseInt(br.readLine());
result*=no;
System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
operand=br.readLine();
k=check.compareTo(operand);
break;
case "/":
System.out.println("Enter number");
no=Integer.parseInt(br.readLine());
result/=no;
System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
operand=br.readLine();
k=check.compareTo(operand);
break;
default:
System.out.println("Please follow the instructions carefully");
}
}
else
{
System.out.println("The result is "+result);
break;
}
}
break;
default:
System.out.println("Please follow the instructions carefully");
}
}
}
答案 0 :(得分:3)
关于你的问题,我认为最好的方法是编写一个算法,将你从用户获得的输入(Infix Notation)转换为使用Dijkstra的Shunting Yard算法的反向波兰表示法。这将允许您考虑运算符优先级。
这是一个解释它的视频,我认为它有一个C ++的后续视频,但将其转换为Java将是微不足道的。
https://www.youtube.com/watch?v=QzVVjboyb0s
这是Wiki页面:
答案 1 :(得分:0)
有没有听说过postfix expression或prefix expression?要正确评估答案,您必须将表达式转换为postfix / prefix&评估它。仅仅通过查看中缀,您可能无法正确评估表达式,因为运算符的优先顺序。您可以轻松地将任何数学表达式转换为postfix / prefix&然后用于评估posfix /前缀表达式的代码:)
修改:添加了教程链接。
将中缀转换为后缀教程:https://www.youtube.com/watch?v=vq-nUF0G4fI
评估帖子/前缀表达式:https://www.youtube.com/watch?v=MeRb_1bddWg