当前位置: 代码迷 >> Java相关 >> 我的计算器怎么运算不正确呢...(用栈了)_
  详细解决方案

我的计算器怎么运算不正确呢...(用栈了)_

热度:400   发布时间:2007-06-16 21:27:04.0
我的计算器怎么运算不正确呢...(用栈了)_

package caculator;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;


public class Caculator extends JApplet implements ActionListener {

private JTextField input = null;
private JTextField result = null;
private String text;
private String postfixExp = "";
private int result1 = 0;
StackReferenceBased stack = new StackReferenceBased();

private JButton one = null;
private JButton two = null;
private JButton three = null;
private JButton four = null;
private JButton five = null;
private JButton six = null;
private JButton seven = null;
private JButton eight = null;
private JButton nine = null;
private JButton zero = null;

private JButton left = null;
private JButton right = null;

private JButton add = null;
private JButton sub = null;
private JButton mul = null;
private JButton div = null;
private JButton equ = null;

private JButton clear = null;

public void init(){
Container container = getContentPane();
container.setLayout(new FlowLayout());

input = new JTextField(20);
result = new JTextField(6);
result.setEditable(false);


one = new JButton("1");
two = new JButton("2");
three = new JButton("3");
four = new JButton("4");
five = new JButton("5");
six = new JButton("6");
seven = new JButton("7");
eight = new JButton("8");
nine = new JButton("9");
zero = new JButton("0");
one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
seven.addActionListener(this);
eight.addActionListener(this);
nine.addActionListener(this);
zero.addActionListener(this);

left = new JButton("(");
right = new JButton(")");
left.addActionListener(this);
right.addActionListener(this);


add = new JButton("+");
sub = new JButton("-");
mul = new JButton("*");
div = new JButton("/");
equ = new JButton("=");

clear = new JButton("clear");
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
equ.addActionListener(this);
clear.addActionListener(this);

container.add(input);
container.add(result);
container.add(one);
container.add(two);
container.add(three);
container.add(four);
container.add(five);
container.add(six);
container.add(seven);
container.add(eight);
container.add(nine);
container.add(zero);

container.add(add);
container.add(sub);
container.add(mul);
container.add(div);
container.add(equ);
container.add(left);
container.add(right);

container.add(clear);

this.setSize(300,200);
// input.setBounds(3, 3, 23, 3);
// result.setBounds(5, 3, 11, 3);
// one.setBounds(7, 3, 13, 3);

}
public void actionPerformed(ActionEvent ae) {
// TODO Auto-generated method stub
if(ae.getSource() == one){
input.setText(input.getText() + "1");
}
else if(ae.getSource() == two){
input.setText(input.getText() + "2");
}
else if(ae.getSource() == three){
input.setText(input.getText() + "3");
}
else if(ae.getSource() == four){
input.setText(input.getText() + "4");
}
else if(ae.getSource() == five){
input.setText(input.getText() + "5");
}
else if(ae.getSource() == six){
input.setText(input.getText() + "6");
}
else if(ae.getSource() == seven){
input.setText(input.getText() + "7");
}
else if(ae.getSource() == eight){
input.setText(input.getText() + "8");
}
else if(ae.getSource() == nine){
input.setText(input.getText() + "9");
}
else if(ae.getSource() == zero){
input.setText(input.getText() + "0");
}
else if(ae.getSource() == add){
input.setText(input.getText() + "+");
}

else if(ae.getSource() == sub){
input.setText(input.getText() + "-");
}
else if(ae.getSource() == mul){
input.setText(input.getText() + "*");
}
else if(ae.getSource() == div){
input.setText(input.getText() + "/");
}
else if(ae.getSource() == clear){
input.setText("");
result.setText("");
}
else if(ae.getSource() == left){
input.setText(input.getText() + "(");
}
else if(ae.getSource() == right){
input.setText(input.getText() + ")");
}
else if(ae.getSource() == equ){
text = input.getText();
stack.toPostfixExp();
stack.postToResult();
result.setText(Integer.toString(result1));
}
}



class NodeChar
{
private char item;
private NodeChar next;

public NodeChar(char newItem){
item = newItem;
next = null;
}

public NodeChar(char newItem,NodeChar nextNodeChar){
item = newItem;
next = nextNodeChar;
}
//setter and gettter method
public void setItem(char newItem){
item = newItem;
}

public char getItem(){
return item;
}

public void setNext(NodeChar nextNodeChar){
next = nextNodeChar;
}

public NodeChar getNext(){
return next;
}
}



// 栈基于引用的实现
class StackReferenceBased
{
private NodeChar top;

public StackReferenceBased(){
top = null;
}

public boolean isEmpty(){
return top == null;
}

public void push(char newItem){
top = new NodeChar(newItem,top);
}

public char pop(){
NodeChar temp = top;
top = top.getNext();
return temp.getItem();
}

public void popAll(){
top = null;
}

public char peek(){
return top.getItem();
}


// 把中缀表达式转换为等价的后缀表达式
public void toPostfixExp()
{

for(int count=0;count<text.length();count++)
{
// 如为操作数,则添加入postfixExp中
if(Character.isDigit(text.charAt(count))){
postfixExp = postfixExp + text.charAt(count);
}

//如为左括号,则压入栈
else if(text.charAt(count) == '('){
stack.push(text.charAt(count));
}

//如为右括号,则弹出栈中的运算符加到postfixExp中,直到遇到与之匹配的左括号为止
else if(text.charAt(count) == ')'){
while(stack.peek() != '('){
postfixExp = postfixExp + stack.pop();
}
char delLeftParenthesis = stack.pop(); // 删除栈顶的左括号
}

// 如为运算符,则弹出栈中优先级较高或相等的运算符加到postfixExp中,直到遇到左括号或优先级较低
// 的运算符为止
else if(text.charAt(count) == '+' || text.charAt(count) == '-'
|| text.charAt(count) == '*' || text.charAt(count) == '/'){
while(!stack.isEmpty() && stack.peek() != '('
&& precedence(text.charAt(count)) <= precedence(stack.peek())){
postfixExp = postfixExp + stack.pop();
}
stack.push(text.charAt(count));
}
}

while(!stack.isEmpty()){
postfixExp = postfixExp + stack.pop();
}
}

// 对后缀表达式求值
public void postToResult()
{
for(int count=0;count<postfixExp.length();count++){
if(Character.isDigit(postfixExp.charAt(count))){
stack.push(postfixExp.charAt(count));
}

else{
int op2 = (int)stack.pop() - 48;
int op1 = (int)stack.pop() - 48;
char op = postfixExp.charAt(count);

switch(op){
case '+': result1 = op1 + op2;break;
case '-': result1 = op1 - op2;break;
case '*': result1 = op1 * op2;break;
case '/': result1 = op1 / op2;break;
}

stack.push((char)(result1+48));
}

}

}

// 设定优先级
public int precedence(char operator){
if(operator == '*' || operator == '/') return 2;
else return 1;
}


}

}

我是想在文本框输入型如"1+2*3-(3-2)"的文本,然后点击"="号结果出现在"result"框中..
中间用了个栈存储,可是算不出正确结果..
(栈是用的别人的,还不是太懂...)...

[此贴子已经被作者于2007-6-16 22:05:54编辑过]

搜索更多相关的解决方案: 计算器  运算  

----------------解决方案--------------------------------------------------------
  相关解决方案