Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How to create a calculator using stack in Java?

stack calculator

  • Please log in to reply
3 replies to this topic

#1 ali1

ali1

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 30 October 2011 - 11:55 AM

I am creating rpn calculator by using stack.I don't know why my codes does not work in stacks.can someone help to fix my codes.I know it's about stack.here is my codes.

/**
 
*/
 import java.util.Stack;
 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 import java.math.*;
 
public class JavaCalculator extends JFrame{
 Stack s = new Stack();
 
private JFrame f;
 private JPanel p1,p2;
 private JTextField jtfMain;
 private JButton jbtnOn, jbtnMRC, jbtnMminus, jbtnMadd, jbtnDivide, jbtnOff, jbtnSeven, jbtnEight, jbtnNine, jbtnMultiply,
 jbtnSqrt, jbtnFour, jbtnFive, jbtnSix, jbtnMinus, jbtnSq, jbtnOne, jbtnTwo, jbtnThree, jbtnPlus, jbtn1x, jbtnZero, jbtnDecimal,
 jbtnEqual, jbtnEnter;
 
public JavaCalculator() {
 


setSize(400,190);
 setTitle("Reverse Polish Notation Calculator");
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 setLayout(new BorderLayout());
 
jtfMain = new JTextField("");
 
jbtnOn = new JButton("ON/AC");
 jbtnMRC = new JButton("MRC");
 jbtnMminus = new JButton("M-");
 jbtnMadd = new JButton("M+");
 jbtnDivide = new JButton("/");
 jbtnOff = new JButton("OFF");
 jbtnSeven = new JButton("7");
 jbtnEight = new JButton("8");
 jbtnNine = new JButton("9");
 jbtnMultiply = new JButton("X");
 jbtnSqrt = new JButton("SQRT");
 jbtnFour = new JButton("4");
 jbtnFive = new JButton("5");
 jbtnSix = new JButton("6");
 jbtnMinus = new JButton("-");
 jbtnSq = new JButton("SQ");
 jbtnOne = new JButton("1");
 jbtnTwo = new JButton("2");
 jbtnThree = new JButton("3");
 jbtnPlus = new JButton("+");
 jbtn1x = new JButton("1/x");
 jbtnZero = new JButton("0");
 jbtnDecimal = new JButton(".");
 jbtnEqual = new JButton("=");
 jbtnEnter = new JButton("ENTER");
 
p1 = new JPanel();
 p1.setLayout(new GridLayout(1,1));
 
p2 = new JPanel();
 p2.setLayout(new GridLayout(5,5));
 
p1.add(jtfMain);
 
p2.add(jbtnOn);
 p2.add(jbtnMRC);
 p2.add(jbtnMadd);
 p2.add(jbtnMminus);
 p2.add(jbtnDivide);
 p2.add(jbtnSeven);
 p2.add(jbtnEight);
 p2.add(jbtnNine);
 p2.add(jbtnMultiply);
 p2.add(jbtnSqrt);
 p2.add(jbtnFour);
 p2.add(jbtnFive);
 p2.add(jbtnSix);
 p2.add(jbtnMinus);
 p2.add(jbtnSq);
 p2.add(jbtnOne);
 p2.add(jbtnTwo);
 p2.add(jbtnThree);
 p2.add(jbtnPlus);
 p2.add(jbtn1x);
 p2.add(jbtnZero);
 p2.add(jbtnDecimal);
 p2.add(jbtnEqual);
 p2.add(jbtnEnter);
 p2.add(jbtnOff);
 
jbtnOn.addActionListener(new ButtonHandler());
 jbtnMRC.addActionListener(new ButtonHandler());
 jbtnMminus.addActionListener(new ButtonHandler());
 jbtnDivide.addActionListener(new ButtonHandler());
 jbtnOff.addActionListener(new ButtonHandler());
 jbtnSeven.addActionListener(new ButtonHandler());
 jbtnEight.addActionListener(new ButtonHandler());
 jbtnNine.addActionListener(new ButtonHandler());
 jbtnMultiply.addActionListener(new ButtonHandler());
 jbtnSqrt.addActionListener(new ButtonHandler());
 jbtnFour.addActionListener(new ButtonHandler());
 jbtnFive.addActionListener(new ButtonHandler());
 jbtnSix.addActionListener(new ButtonHandler());
 jbtnMinus.addActionListener(new ButtonHandler());
 jbtnSq.addActionListener(new ButtonHandler());
 jbtnOne.addActionListener(new ButtonHandler());
 jbtnTwo.addActionListener(new ButtonHandler());
 jbtnThree.addActionListener(new ButtonHandler());
 jbtnPlus.addActionListener(new ButtonHandler());
 jbtn1x.addActionListener(new ButtonHandler());
 jbtnZero.addActionListener(new ButtonHandler());
 jbtnDecimal.addActionListener(new ButtonHandler());
 jbtnEqual.addActionListener(new ButtonHandler());
 jbtnEnter.addActionListener(new ButtonHandler());
 
add(p1, BorderLayout.NORTH);
 add(p2, BorderLayout.CENTER);
 
setVisible(true);
 }
 
public class ButtonHandler implements ActionListener
 {
 public void actionPerformed(ActionEvent evt)
 {
 if(evt.getSource()== jbtnOn)
 {
 jtfMain.setText("");
 }
 else if(evt.getSource()== jbtnOne)
 {
 s.push("1");
 
jtfMain.setText(jtfMain.getText() + "1");
 
}
 else if(evt.getSource()== jbtnTwo)
 {
 s.push("2");
 jtfMain.setText(jtfMain.getText() + "2");
 
}
 else if(evt.getSource()== jbtnThree)
 {
 s.push("3");
 jtfMain.setText(jtfMain.getText() + "3");
 
}
 else if(evt.getSource()== jbtnFour)
 {
 s.push("4");
 jtfMain.setText(jtfMain.getText() + "4");
 
}
 else if(evt.getSource()== jbtnFive)
 {
 s.push("5");
 jtfMain.setText(jtfMain.getText() + "5");
 
}
 else if(evt.getSource()== jbtnSix)
 {
 s.push("6");
 jtfMain.setText(jtfMain.getText() + "6");
 
}
 else if(evt.getSource()== jbtnSeven)
 {
 s.push("7");
 jtfMain.setText(jtfMain.getText() + "7");
 
}
 else if(evt.getSource()== jbtnEight)
 {
 s.push("8");
 
jtfMain.setText(jtfMain.getText() + "8");
 
}
 else if(evt.getSource()== jbtnNine)
 {
 s.push("9");
 jtfMain.setText(jtfMain.getText() + "9");
 
}
 else if(evt.getSource()== jbtnZero)
 {
 s.push("0");
 jtfMain.setText(jtfMain.getText() + "0");
 
}
 else if(evt.getSource()== jbtnDivide)
 {
 s.push("/");
 jtfMain.setText(jtfMain.getText() + "/");
 
}
 else if(evt.getSource()== jbtnMultiply)
 {
 s.push("X");
 jtfMain.setText(jtfMain.getText() + "X");
 
}
 else if(evt.getSource()== jbtnPlus)
 {
 s.push("+");
 jtfMain.setText(jtfMain.getText() + "+");
 

}
 else if(evt.getSource()== jbtnMinus)
 {
 s.push("-");
 jtfMain.setText(jtfMain.getText() + "-");
 

}
 else if(evt.getSource() == jbtnEnter)
 {
 s.push(" ");
 jtfMain.setText(evaluateExpr(jtfMain.getText()) + " ");
 }
 }
 }
 //Implement RPN algorithm
 //pre-condition: The given input is in post-fix notation with tokens separated by a single space.
 //Assumes binary operators only
 
private int evaluateExpr(String expr)
 {
 for(String token: expr.split(" "))
 {
 if(!isOperator(token))
 s.push(token);
 else
 {
 
}
 }
 return s.pop();
 }
 
public static void main(String[] args) {
 JavaCalculator main = new JavaCalculator();
 }
 }

Edited by Alyn, 02 November 2011 - 02:33 PM.
added code tag

  • 0

#2 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 31 October 2011 - 07:01 AM

That's quite a lot of code. It would help us in determining your problem if you could specify the exact nature of your problem. (i.e., won't compile, uncaught exception, invalid results, etc.)
Also, it would help greatly if you use the CODE tags around your code.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3 ali1

ali1

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 31 October 2011 - 09:32 AM

i am receiving compiling error

Edited by Alyn, 02 November 2011 - 02:35 PM.

  • 0

#4 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 01 November 2011 - 08:47 AM

CODE tags work like this:

[CODE ]
// Your code goes here...
[/CODE]

(just make sure you remove the space in the opening tag. I put that in there to avoid auto formatting.

You can also just go up and edit your previous post rather than posting it all again.

What is the compiler error you're receiving? Copy/paste the message here please.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download