我做了一个简单的计算器。会发生什么是我将用户输入转换为infixtopostfix然后评估它。一切正常,除非用户执行3^2
之类的操作,程序不会将其正确转换为修复后。它应该将其转换为32^
,但它会将其转换为^32
。有人可以向我解释一下导致这个问题的转换方法有什么问题。
转换方法:
static String convert(String infix)
{
ArrayStack<Character> as = new ArrayStack<Character>(infix.length());
String post_expression = "";
int index = 0;
while(index < infix.length()) {
char current = infix.charAt(index);
if(current == ' ') {
index++;
continue;
}
else if(current == '(') {
as.push(current);
}
else if(current == ')') {
while(as.top() != '(') {
post_expression += as.pop();
}
as.pop();
}
else if(current == '=')
{
index++;
continue;
}
else if(current == '^')
{
post_expression += as.pop();
}
else {
while(!as.isEmpty() && as.top() > current) {
post_expression += as.pop();
}
as.push(current);
}
index++;
}
while(!as.isEmpty()) {
post_expression += as.pop();
}
return post_expression;
}
完成计划:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.*;
import java.io.*;
class Calculator2 extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn_add = new JButton("+");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
JButton btn6 = new JButton("6");
JButton btn_sub = new JButton("-");
JButton btn7 = new JButton("7");
JButton btn8 = new JButton("8");
JButton btn9 = new JButton("9");
JButton btn_mult = new JButton("*");
JButton btn0 = new JButton("0");
JButton btn_dot = new JButton(".");
JButton btn_del = new JButton("DEL");
JButton btn_div = new JButton("/");
JButton btn_lpr = new JButton("(");
JButton btn_rpr = new JButton(")");
JButton btn_pow = new JButton("^");
JButton btn_equ = new JButton("=");
JTextArea txt = new JTextArea();
String str_number = "";
public Calculator2() {
JFrame frame = new JFrame("Simple Java Caculator");
frame.setSize(320,420);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
frame.setLayout(new GridLayout(2,1));
JPanel HeadPanel = new JPanel();
JPanel NumberPanel = new JPanel();
JPanel LabelPanel = new JPanel();
LabelPanel.setBackground(Color.LIGHT_GRAY);
HeadPanel.setBackground(Color.LIGHT_GRAY);
NumberPanel.setLayout(new GridLayout(5,4));
//LabelPanel.setLayout(new BorderLayout());
LabelPanel.setLayout(new GridLayout(1,1));
NumberPanel.add(btn1);
btn1.addActionListener(this);
NumberPanel.add(btn2);
btn2.addActionListener(this);
NumberPanel.add(btn3);
btn3.addActionListener(this);
NumberPanel.add(btn_add);
btn_add.addActionListener(this);
NumberPanel.add(btn4);
btn4.addActionListener(this);
NumberPanel.add(btn5);
btn5.addActionListener(this);
NumberPanel.add(btn6);
btn6.addActionListener(this);
NumberPanel.add(btn_sub);
btn_sub.addActionListener(this);
NumberPanel.add(btn7);
btn7.addActionListener(this);
NumberPanel.add(btn8);
btn8.addActionListener(this);
NumberPanel.add(btn9);
btn9.addActionListener(this);
NumberPanel.add(btn_mult);
btn_mult.addActionListener(this);
NumberPanel.add(btn0);
btn0.addActionListener(this);
NumberPanel.add(btn_dot);
btn_dot.addActionListener(this);
NumberPanel.add(btn_del);
btn_del.addActionListener(this);
NumberPanel.add(btn_div);
btn_div.addActionListener(this);
LabelPanel.add(txt);
//LabelPanel.add(btn_equ);
NumberPanel.add(btn_lpr);
btn_lpr.addActionListener(this);
NumberPanel.add(btn_rpr);
btn_rpr.addActionListener(this);
NumberPanel.add(btn_pow);
btn_pow.addActionListener(this);
NumberPanel.add(btn_equ);
btn_equ.addActionListener(this);
txt.setEditable(false);
//btn_del.setEnabled(false);
HeadPanel.add(new JLabel("A Java Calculator"));
frame.add(LabelPanel);
frame.add(NumberPanel);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn1) {
str_number+="1";
txt.setText(str_number); }
else if(e.getSource()==btn2) {
str_number+="2";
txt.setText(str_number); }
else if(e.getSource()==btn3) {
str_number+="3";
txt.setText(str_number); }
else if(e.getSource()==btn4) {
str_number+="4";
txt.setText(str_number); }
else if(e.getSource()==btn5) {
str_number+="5";
txt.setText(str_number); }
else if(e.getSource()==btn6) {
str_number+="6";
txt.setText(str_number); }
else if(e.getSource()==btn7) {
str_number+="7";
txt.setText(str_number); }
else if(e.getSource()==btn8) {
str_number+="8";
txt.setText(str_number); }
else if(e.getSource()==btn9) {
str_number+="9";
txt.setText(str_number); }
else if(e.getSource()==btn0) {
str_number+="0";
txt.setText(str_number); }
else if(e.getSource()==btn_lpr) {
str_number+="(";
txt.setText(str_number); }
else if(e.getSource()==btn_rpr) {
str_number+=")";
txt.setText(str_number); }
else if(e.getSource()==btn_pow) {
str_number+="^";
txt.setText(str_number); }
else if(e.getSource()==btn_add) {
str_number+="+";
txt.setText(str_number);}
else if(e.getSource()==btn_sub) {
str_number+="-";
txt.setText(str_number);}
else if(e.getSource()==btn_mult) {
str_number+="*";
txt.setText(str_number);}
else if(e.getSource()==btn_div) {
str_number+="/";
txt.setText(str_number);}
else if(e.getSource()==btn_equ) {
System.out.println("you clicked equal sign!");
str_number+="=";
txt.setText(str_number);
String expression = convert(str_number);
double eval = evaluate(expression);
str_number+=eval;
txt.setText(str_number);
}
else if(e.getSource()==btn_dot) {
System.out.println("you clicked dot button!");
str_number+=".";
txt.setText(str_number);
}
else if(e.getSource()==btn_del) {
System.out.println("you clicked DEL button!");
if(str_number.length() > 0)
{
str_number = str_number.substring(0, str_number.length() - 1);
}
txt.setText(str_number);
}
}
static String convert(String infix)
{
ArrayStack<Character> as = new ArrayStack<Character>(infix.length());
String post_expression = "";
int index = 0;
while(index < infix.length()) {
char current = infix.charAt(index);
if(current == ' ') {
index++;
continue;
}
else if(current == '(') {
as.push(current);
}
else if(current == ')') {
while(as.top() != '(') {
post_expression += as.pop();
}
as.pop();
}
else if(current == '=')
{
index++;
continue;
}
else if(current == '^')
{
post_expression += as.pop();
}
else {
while(!as.isEmpty() && as.top() > current) {
post_expression += as.pop();
}
as.push(current);
}
index++;
}
while(!as.isEmpty()) {
post_expression += as.pop();
}
return post_expression;
}
static double evaluate(String postfix) {
ArrayStack as = new ArrayStack(postfix.length());
char nextChar = 'm';
int index = 0;
double temp = 0.00;
while(index < postfix.length()) {
char current = postfix.charAt(index);
if(current == ' ') {
index++;
continue;
}
if(!(current == '+' || current == '-' || current == '*' || current == '/'))
{
as.push(current);
temp = (double) (current - '0');
}
else {
char a = (char) as.pop();
char b = (char) as.pop();
double c = (double) (a - '0');
double d = (double) (b - '0');
switch(current) {
case '+' :
temp = d + c;
break;
case '-' :
temp = d - c;
break;
case '*' :
temp = d * c;
break;
case '/' :
temp = d / c;
break;
case '^' :
temp = Math.pow(d, c);
break;
}
as.push(nextChar);
nextChar++;
}
index++;
}
if(as.size() == 1) {
as.pop();
}
else {
System.out.println("There is an error");
}
return temp;
}
public static void main(String[] args) {
new Calculator2();
}
}
ArrayStack代码
public class ArrayStack<E> implements Stack<E> {
protected int capacity;
public static final int CAPACITY = 1000;
protected E S[];
protected int top = -1;
public ArrayStack() {
this(CAPACITY);
}
public ArrayStack(int cap) {
capacity = cap;
S = (E[]) new Object[capacity];
}
@Override
public int size() {
return top+1;
}
@Override
public boolean isEmpty() {
return (top < 0);
}
@Override
public E top() throws EmptyStackException {
if(isEmpty())
throw new EmptyStackException("Stack is empty!");
return S[top];
}
@Override
public void push(E element) {
if(size() == capacity)
throw new FullStackException("Stack is full");
S[++top] = element;
}
@Override
public E pop() throws EmptyStackException {
E element;
if(isEmpty())
throw new EmptyStackException("Stack is empty");
element = S[top];
S[top--] = null;
return element;
}
@Override
public String toString() {
String s;
s = "[";
if(size() > 0) s += S[0];
if(size() > 1)
for(int i = 1; i <= size()-1; i++) {
s += ", " + S[i];
}
return s + "]";
}
}