目录:
1实验要求
2实验算法
3实验成果
一.实验要求
1.能通过设计的按钮控件输入并实现算术表达式,表达式在文本框中显示,运算结果输出显示;保存和浏览历史运算记录;
2.能够检验算术表达式的合法性;
3.能够实现混合运算的求解,算术表达式中包括加、减、乘、除、括号等运算符;
4.要求交互界面友好,程序健壮。
二.实验算法
算法思想:
输入X,首先要区分X:
1.如果X是操作数,直接入队
2.如果X是运算符,再分以下情况:
(1).如果栈为空,直接入栈。
(2).如果X==”(",直接入栈。
(3).如果X==")“,则将栈里的元素逐个出栈,并入队到后缀表达式队列中,直到第一个配对的7(出栈。
(4).如果是其他操作符(+-*/) ,则和栈顶元素进行比较优先级。如果栈顶元素的优先级大于等于X,则出栈并把栈中弹出的元素入队,直到栈顶元素的优先级小于X或者栈为空。弹出完这些元素后,才将遇到的操作符压入到栈中 。
3.最后将栈中剩余的操作符全部入队。
后缀表达式的计算
使用一个栈Res_Stack来进行值的计算。 从左向右扫描后缀表达式,遇到操作数压入Res_Stack栈,遇到运算符,将栈顶的2个元素出栈计算,再将结果压入Res_Stack栈;直到结束,将栈中唯一的元素出栈
后缀表达式求解
算法思维
首先准备一个栈Res_Stack.
1、从左开始向右遍历后缀表达式的元素。
2、如果取到的元素是操作数,直接入栈Res_Stack,如果是运算符,从栈中弹出2个数进行运算,然后把运算结果入栈
3、当遍历完后缀表达式时,计算结果就保存在栈里了。
三.代码实现
package Calculator;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private String[] KEYS = { "7", "8", "9", "AC", "4", "5", "6", "-", "1", "2", "3", "+", "0", "e", "pi", "/", "sqrt",
"%", "x*x", "*", "(", ")", ".", "=" };
private JButton keys[] = new JButton[KEYS.length];
private JTextArea resultText = new JTextArea("0.0");
private JTextArea History = new JTextArea();
private JPanel jp1 = new JPanel();
private JPanel jp2 = new JPanel();
private JScrollPane gdt1 = new JScrollPane(resultText);
private JScrollPane gdt2 = new JScrollPane(History);
private JLabel label = new JLabel("历史记录");
private String b = "";
public Calculator() {
super("Caculator");
resultText.setBounds(20, 18, 255, 115);
resultText.setAlignmentX(RIGHT_ALIGNMENT);
resultText.setEditable(false);
History.setBounds(290, 40, 250, 370);
History.setAlignmentX(LEFT_ALIGNMENT);
History.setEditable(false);
label.setBounds(300, 15, 100, 20);
jp2.setBounds(290, 40, 250, 370);
jp2.setLayout(new GridLayout());
jp1.setBounds(20, 18, 255, 115);
jp1.setLayout(new GridLayout());
resultText.setLineWrap(true);
resultText.setWrapStyleWord(true);
resultText.setSelectedTextColor(Color.RED);
History.setLineWrap(true);
History.setWrapStyleWord(true);
History.setSelectedTextColor(Color.blue);
gdt1.setViewportView(resultText);
gdt2.setViewportView(History);
gdt1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
gdt2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
gdt2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jp1.add(gdt1);
jp2.add(gdt2);
this.add(jp1);
this.add(jp2);
this.setLayout(null);
this.add(label);
int x = 20, y = 150;
for (int i = 0; i < KEYS.length; i++) {
keys[i] = new JButton();
keys[i].setText(KEYS[i]);
keys[i].setBounds(x, y, 60, 40);
if (x < 215) {
x += 65;
} else {
x = 20;
y += 45;
}
this.add(keys[i]);
}
for (int i = 0; i < KEYS.length; i++)
{
keys[i].addActionListener(this);
}
this.setResizable(false);
this.setBounds(500, 200, 567, 480);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String label = e.getActionCommand();
if (label == "=")
{
resultText.setText(this.b);
History.setText(History.getText() + resultText.getText());
if (label == "=")
{
String s[] = houzhui(this.b);
String result = Result(s);
this.b = result + "";
resultText.setText(this.b);
History.setText(History.getText() + "=" + resultText.getText() + "\n");
}
} else if (label == "AC")
{
this.b = "";
resultText.setText("0");
} else if (label == "sqrt") {
String n = kfys(this.b);
resultText.setText("sqrt" + "(" + this.b + ")" + "=" + n);
History.setText(History.getText() + "sqrt" + "(" + this.b + ")" + "=");
this.b = n;
} else if (label == "x*x") {
String m = pfys(this.b);
resultText.setText(this.b + "^2" + "=" + m);
History.setText(History.getText() + this.b + "^2" + "=");
this.b = m;
} else if (label == "e" || label == "pi") {
if (label == "e") {
String m = String.valueOf(2.71828);
this.b = this.b + m;
resultText.setText(this.b);
}
if (label == "pi") {
String m = String.valueOf(3.14159265);
this.b = this.b + m;
resultText.setText(this.b);
}
}
else {
this.b = this.b + label;
resultText.setText(this.b);
}
}
private String[] houzhui(String str) {
String s = "";
char opStack[] = new char[100];
String postQueue[] = new String[100];
int top = -1, j = 0;
for (int i = 0; i < str.length(); i++)
{
if ("0123456789.".indexOf(str.charAt(i)) >= 0)
{
s = "";
for (; i < str.length() && "0123456789.".indexOf(str.charAt(i)) >= 0; i++) {
s = s + str.charAt(i);
}
i--;
postQueue[j] = s;
j++;
} else if ("(".indexOf(str.charAt(i)) >= 0) {
top++;
opStack[top] = str.charAt(i);
} else if (")".indexOf(str.charAt(i)) >= 0) {
for (;;)
{
if (opStack[top] != '(') {
postQueue[j] = opStack[top] + "";
j++;
top--;
} else {
top--;
break;
}
}
}
if ("*%/".indexOf(str.charAt(i)) >= 0)
{
if (top == -1) {
top++;
opStack[top] = str.charAt(i);
} else {
if ("*%/".indexOf(opStack[top]) >= 0) {
postQueue[j] = opStack[top] + "";
j++;
opStack[top] = str.charAt(i);
} else if ("(".indexOf(opStack[top]) >= 0) {
top++;
opStack[top] = str.charAt(i);
} else if ("+-".indexOf(str.charAt(i)) >= 0) {
postQueue[j] = opStack[top] + "";
j++;
opStack[top] = str.charAt(i);
}
}
} else if ("+-".indexOf(str.charAt(i)) >= 0) {
if (top == -1) {
top++;
opStack[top] = str.charAt(i);
} else {
if ("*%/".indexOf(opStack[top]) >= 0) {
postQueue[j] = opStack[top] + "";
j++;
opStack[top] = str.charAt(i);
} else if ("(".indexOf(opStack[top]) >= 0) {
top++;
opStack[top] = str.charAt(i);
} else if ("+-".indexOf(str.charAt(i)) >= 0) {
postQueue[j] = opStack[top] + "";
j++;
opStack[top] = str.charAt(i);
}
}
}
}
for (; top != -1;) {
postQueue[j] = opStack[top] + "";
j++;
top--;
}
return postQueue;
}
public String kfys(String str) {
String result = "";
double a = Double.parseDouble(str), b = 0;
b = Math.sqrt(a);
result = String.valueOf(b);
return result;
}
public String pfys(String str) {
String result = "";
double a = Double.parseDouble(str), b = 0;
b = Math.pow(a, 2);
result = String.valueOf(b);
return result;
}
public String Result(String str[]) {
String Result[] = new String[100];
int Top = -1;
for (int i = 0; str[i] != null; i++) {
if ("+-*%/".indexOf(str[i]) < 0) {
Top++;
Result[Top] = str[i];
}
if ("+-*%/".indexOf(str[i]) >= 0)
{
double x, y, n;
x = Double.parseDouble(Result[Top]);
Top--;
y = Double.parseDouble(Result[Top]);
Top--;
if ("*".indexOf(str[i]) >= 0) {
n = y * x;
Top++;
Result[Top] = String.valueOf(n);
}
if ("/".indexOf(str[i]) >= 0) {
if (x == 0)
{
String s = "error!";
return s;
} else {
n = y / x;
Top++;
Result[Top] = String.valueOf(n);
}
}
if ("%".indexOf(str[i]) >= 0)
{
if (x == 0)
{
String s = "error!";
return s;
} else {
n = y % x;
Top++;
Result[Top] = String.valueOf(n);
}
}
if ("-".indexOf(str[i]) >= 0) {
n = y - x;
Top++;
Result[Top] = String.valueOf(n);
}
if ("+".indexOf(str[i]) >= 0) {
n = y + x;
Top++;
Result[Top] = String.valueOf(n);
}
}
}
return Result[Top];
}
public static void main(String[] args) {
Calculator a = new Calculator();
}
}
四.实践