编写一个按 DMAS 顺序执行运算的计算器
我创建了一个 GUI 计算器程序,它以线性顺序执行计算,但我希望它以“除、乘、加、减”的顺序执行计算。
我是初学者,所以请建议我是否有其他更简单的方法来实现我编码的相同内容。
请帮我解决“DMAS”问题。
我的代码:
public class keypadGUI extends JFrame{
private JTextField lcd;
private JButton n1,n2,n3,n4,n5,n6,n7,n8,n9,n0;
private JButton plus,minus,multiply,divide,equalTo,dot;
//private JButton[] buttons = new JButton[10];
//private JButton[] opButtons = new JButton[6];
private double ans = 0; //For storing final ans
char[] op; //Character array for stroing operators
private double[] nums; //For storing the numbers
public JPanel keypad;
private StringBuilder sb = new StringBuilder(); //For storing the input of the user as string
public keypadGUI(){
setLayout(new BorderLayout(10,10));
lcd = new JTextField();
lcd.setEditable(false);
Border low = BorderFactory.createLoweredSoftBevelBorder();
lcd.setBorder(low);
lcd.setHorizontalAlignment(JTextField.RIGHT);
lcd.setFont(new Font("Serif",Font.BOLD,30));
lcd.setText("0");
Font numFont = new Font("Serif",Font.BOLD,20);
n1 = new JButton("1"); n2 = new JButton("2"); n3 = new JButton("3"); n4 = new JButton("4");
n5 = new JButton("5"); n6 = new JButton("6"); n7 = new JButton("7"); n8 = new JButton("8");
n9 = new JButton("9"); n0 = new JButton("0"); dot = new JButton(".");
plus = new JButton("+");
minus = new JButton("-");
multiply = new JButton("*");
divide = new JButton("/");
equalTo = new JButton("=");
n1.setFont(numFont); n2.setFont(numFont); n3.setFont(numFont); n4.setFont(numFont);
n5.setFont(numFont); n6.setFont(numFont); n7.setFont(numFont); n8.setFont(numFont);
n9.setFont(numFont); n0.setFont(numFont); dot.setFont(numFont); plus.setFont(numFont);
minus.setFont(numFont); multiply.setFont(numFont); divide.setFont(numFont); equalTo.setFont(numFont);
plus.setForeground(Color.DARK_GRAY); minus.setForeground(Color.DARK_GRAY); multiply.setForeground(Color.DARK_GRAY);
divide.setForeground(Color.DARK_GRAY); equalTo.setForeground(Color.DARK_GRAY);
listen listener = new listen();
n1.addActionListener(listener); n2.addActionListener(listener); n3.addActionListener(listener); n4.addActionListener(listener);
n5.addActionListener(listener); n6.addActionListener(listener); n7.addActionListener(listener); n8.addActionListener(listener);
n9.addActionListener(listener); n0.addActionListener(listener); plus.addActionListener(listener); minus.addActionListener(listener);
multiply.addActionListener(listener); divide.addActionListener(listener);
equalTo.addActionListener(new equalListener());
keypad = new JPanel(new GridLayout(4,4,10,10));
keypad.add(n7); keypad.add(n8); keypad.add(n9); keypad.add(divide);
keypad.add(n4); keypad.add(n5); keypad.add(n6); keypad.add(multiply);
keypad.add(n1); keypad.add(n2); keypad.add(n3); keypad.add(plus);
keypad.add(n0); keypad.add(dot); keypad.add(equalTo); keypad.add(minus);
add(lcd,BorderLayout.NORTH);
add(keypad,BorderLayout.CENTER);
setVisible(true);
setLookAndFeel();
setSize(280,280);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("MY Calc");
}
private void setLookAndFeel(){
try{
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
}catch(Exception ae){}
}
private class listen implements ActionListener{
@Override
public void actionPerformed(ActionEvent e){
String bName = ((JButton)e.getSource()).getText();
sb.append(bName);
lcd.setText(sb.toString());
}
}
private class equalListener implements ActionListener{
public void actionPerformed(ActionEvent e){
String[] tokkens = sb.toString().split("-|\\+|\\*|\\/");
nums = new double[tokkens.length];
for(int i=0;i<nums.length;i++){
nums[i] = Double.parseDouble(tokkens[i]);
}
op = new char[10];
op[0]='+';
int c =1;
for(int i=0;i<sb.length();i++){
if(sb.charAt(i)=='+' || sb.charAt(i)=='-' || sb.charAt(i)=='*' || sb.charAt(i)=='/')
{ op[c]=sb.charAt(i); c++; }
}
performOP();
sb = sb.delete(0, sb.length());
sb.append(ans);
lcd.setText(sb.toString());
System.out.println(op);
System.out.println(Arrays.toString(tokkens));
}
}
private void performOP(){
for(int i=0;i<nums.length;i++){
switch(op[i]){
case '+':
ans = ans + nums[i];
break;
case '-':
ans = ans - nums[i];
break;
case '*':
ans = ans * nums[i];
break;
case '/':
ans = ans / nums[i];
break;
default:
System.out.println("INVALID CASE !!!");
}
}
}
}
I have created a GUI calculator program, that performs calculations in a linear order, but i want it to perform calculations in an order of "Divide, multiply, add, subtract".
I'm a beginner, so please suggest me if there is any other simpler way to implement the same thing, which I have coded.
Please help me with the 'DMAS' thing.
My code:
public class keypadGUI extends JFrame{
private JTextField lcd;
private JButton n1,n2,n3,n4,n5,n6,n7,n8,n9,n0;
private JButton plus,minus,multiply,divide,equalTo,dot;
//private JButton[] buttons = new JButton[10];
//private JButton[] opButtons = new JButton[6];
private double ans = 0; //For storing final ans
char[] op; //Character array for stroing operators
private double[] nums; //For storing the numbers
public JPanel keypad;
private StringBuilder sb = new StringBuilder(); //For storing the input of the user as string
public keypadGUI(){
setLayout(new BorderLayout(10,10));
lcd = new JTextField();
lcd.setEditable(false);
Border low = BorderFactory.createLoweredSoftBevelBorder();
lcd.setBorder(low);
lcd.setHorizontalAlignment(JTextField.RIGHT);
lcd.setFont(new Font("Serif",Font.BOLD,30));
lcd.setText("0");
Font numFont = new Font("Serif",Font.BOLD,20);
n1 = new JButton("1"); n2 = new JButton("2"); n3 = new JButton("3"); n4 = new JButton("4");
n5 = new JButton("5"); n6 = new JButton("6"); n7 = new JButton("7"); n8 = new JButton("8");
n9 = new JButton("9"); n0 = new JButton("0"); dot = new JButton(".");
plus = new JButton("+");
minus = new JButton("-");
multiply = new JButton("*");
divide = new JButton("/");
equalTo = new JButton("=");
n1.setFont(numFont); n2.setFont(numFont); n3.setFont(numFont); n4.setFont(numFont);
n5.setFont(numFont); n6.setFont(numFont); n7.setFont(numFont); n8.setFont(numFont);
n9.setFont(numFont); n0.setFont(numFont); dot.setFont(numFont); plus.setFont(numFont);
minus.setFont(numFont); multiply.setFont(numFont); divide.setFont(numFont); equalTo.setFont(numFont);
plus.setForeground(Color.DARK_GRAY); minus.setForeground(Color.DARK_GRAY); multiply.setForeground(Color.DARK_GRAY);
divide.setForeground(Color.DARK_GRAY); equalTo.setForeground(Color.DARK_GRAY);
listen listener = new listen();
n1.addActionListener(listener); n2.addActionListener(listener); n3.addActionListener(listener); n4.addActionListener(listener);
n5.addActionListener(listener); n6.addActionListener(listener); n7.addActionListener(listener); n8.addActionListener(listener);
n9.addActionListener(listener); n0.addActionListener(listener); plus.addActionListener(listener); minus.addActionListener(listener);
multiply.addActionListener(listener); divide.addActionListener(listener);
equalTo.addActionListener(new equalListener());
keypad = new JPanel(new GridLayout(4,4,10,10));
keypad.add(n7); keypad.add(n8); keypad.add(n9); keypad.add(divide);
keypad.add(n4); keypad.add(n5); keypad.add(n6); keypad.add(multiply);
keypad.add(n1); keypad.add(n2); keypad.add(n3); keypad.add(plus);
keypad.add(n0); keypad.add(dot); keypad.add(equalTo); keypad.add(minus);
add(lcd,BorderLayout.NORTH);
add(keypad,BorderLayout.CENTER);
setVisible(true);
setLookAndFeel();
setSize(280,280);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("MY Calc");
}
private void setLookAndFeel(){
try{
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
}catch(Exception ae){}
}
private class listen implements ActionListener{
@Override
public void actionPerformed(ActionEvent e){
String bName = ((JButton)e.getSource()).getText();
sb.append(bName);
lcd.setText(sb.toString());
}
}
private class equalListener implements ActionListener{
public void actionPerformed(ActionEvent e){
String[] tokkens = sb.toString().split("-|\\+|\\*|\\/");
nums = new double[tokkens.length];
for(int i=0;i<nums.length;i++){
nums[i] = Double.parseDouble(tokkens[i]);
}
op = new char[10];
op[0]='+';
int c =1;
for(int i=0;i<sb.length();i++){
if(sb.charAt(i)=='+' || sb.charAt(i)=='-' || sb.charAt(i)=='*' || sb.charAt(i)=='/')
{ op[c]=sb.charAt(i); c++; }
}
performOP();
sb = sb.delete(0, sb.length());
sb.append(ans);
lcd.setText(sb.toString());
System.out.println(op);
System.out.println(Arrays.toString(tokkens));
}
}
private void performOP(){
for(int i=0;i<nums.length;i++){
switch(op[i]){
case '+':
ans = ans + nums[i];
break;
case '-':
ans = ans - nums[i];
break;
case '*':
ans = ans * nums[i];
break;
case '/':
ans = ans / nums[i];
break;
default:
System.out.println("INVALID CASE !!!");
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前,您可以采用线性方法来理解表达式,构建一个运算符列表并按顺序执行它们。
您需要构建更像树的东西,以便可以赋予所需的含义
,并且您可能也希望允许使用括号
。这是一个非常常见的解析问题,通常称为递归下降解析。您将能够找到实现此类解析器的库。例如ANTLR。从你所处的位置到这类事情,这是一个很大的跳跃。如果您深入挖掘,您可能会发现可以克隆的简单实现,但如果您真的想了解该领域,那么您需要进行大量研究:诸如 这可能会有所帮助。
You currently have a linear approach to understanding the expression, you build a list of operators and execute them in order.
You need to build something more like a tree so that you can give your desired meaning to
and you probably want to allow parentheses too
This is a very common parsing problem, generally known as recursive descent parsing. You will be able to find libraries that implement such parsers. For example ANTLR. It's quite a big jump from where you are to this sort of stuff. If you dig around you'll probably find simple implementations you can clone, but if you really want to understand the area then you'd need to study quite a bit: articles such as this may help.
嗯,如果您将作为操作数和运算符的所有字符存储在一个链接列表中怎么样?根据优先级将所有运算符存储在数组中。如arr[]={'/','*','+','-'},迭代这些运算符。当您执行此操作时,首先检测需要应用的任何操作,将其答案存储在新节点中,然后删除包含检测到的操作和操作数的那些节点。将新节点链接到执行该操作的最后一个操作数之后的节点正在执行。您可以重复此过程,直到基节点或头节点的下一个指针变为零。是的,老实说,我也是这件事的初学者,所以这个解决方案显然不是很好。我现在也有这样的想法,当然我自己还没有实现这个想法。但是,是的,如果你愿意的话,你可以尝试一下。希望它能有所帮助,即使只是一点点。
Umm , how about if you just store all the characters that you take as operands and operators in a linked list. Store all the operators in an array, according to their precedence. Such as, arr[]={'/','*','+','-'}, iterate through these operators. While you are doing that detect any of the operation that needs to be applied first , store its answer in a new node, then delete those nodes containing the detected operations and operands.Link the new node to the node after the last operand upon which operation was being performed upon. You can repeat this process, until the base or head node's next pointer becomes zero.And yeah honestly, iam also kind of a beginner to this matter,so this solution is obviously not great. I also kind of thought this just right now and ofcourse havenot implemented this idea myself. But yeah , you can give it a shot, if you want to though.Hope it helps, even if just a bit.