import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EmptyStackException;
import java.util.Stack;
import javax.swing.*;
import javax.swing.JFrame;
public class calculate{
public static void main(String[] args)
{
JFrame frame = new JFrame("CCNU_FK");
frame.setSize(350,240);
frame.setFont(new Font("宋体", Font.PLAIN, 30) );
JPanel panel = new JPanel();
panel.setLayout(null);
JLabel label1 = new JLabel("输入: ");
label1.setBounds(10,10,100,40);
label1.setFont(new Font("宋体", Font.PLAIN, 30));
panel.add(label1);
JTextField text1 = new JTextField(10);
text1.setBounds(100,10,200,40);
text1.setFont(new Font("宋体", Font.PLAIN, 30) );
panel.add(text1);
JButton button1 = new JButton("计算");
panel.add(button1);
button1.setBounds(120,120,100,60);
button1.setFont(new Font("宋体", Font.PLAIN, 30) );
JLabel label2 = new JLabel("结果:");
label2.setBounds(10,70,100,40);
label2.setFont(new Font("宋体", Font.PLAIN, 30));
panel.add(label2);
JTextField text2 = new JTextField(10);
panel.add(text2);
text2.setBounds(100,70,200,40);
text2.setFont(new Font("宋体", Font.PLAIN, 30) );
frame.add(panel);
frame.setVisible(true);
button1.addActionListener(new listen(button1,text1,text2));
}
static class listen implements ActionListener
{
JButton button1;
JTextField text1;
JTextField text2;
public listen(JButton button1,JTextField text1,JTextField text2)
{
this.button1 = button1;
this.text1 = text1;
this.text2 = text2;
}
public void actionPerformed(ActionEvent e)
{
String s = text1.getText();
System.out.println(s);
Stack<Double> stk1 = new Stack<Double>();
Stack<Character> stk2 = new Stack<Character>();
for (int i = 0; i < s.length(); ++i)
{
char temp = s.charAt(i); // String 取其某一个元素(返回指定索引处的字符)
if ((temp >= '0' && temp <= '9') || temp == '.')//当索引处是数字或者.时
{
double sum = temp - '0';
int j = i + 1;
boolean flag = true;//标识符
int index = 1;
while(j < s.length())
{
temp = s.charAt(j);
if (temp == '.'){
flag = false;
index = 1;
++j;
continue;
}
else if (flag && (temp >= '0' && temp <= '9'))//没遇到小数点就往前面放
{
sum = sum * 10 + temp - '0';
++j;
}
else if (!flag && (temp >= '0' && temp <= '9'))//遇到小数点就往小数点后面放
{
sum += Math.pow(0.1,index) * (temp - '0');
index ++;
++j;
}
else break;
}
stk1.push(new Double(sum));//最终把sum放到操作数栈里面
i = j - 1;//继续去找数
}
else if (temp == '+' || temp == '-' || temp == '*' || temp == '/' || temp == '(' )//当索引处是运算符时
{
if (stk2.empty() || get_out_priority(temp) > get_in_priority(stk2.peek().charValue()))
stk2.push(new Character(temp));//运算符栈加入新字符,空了就加,temp不小于运算符栈顶数,也加入栈顶
else
{
while(!stk2.empty() && (get_out_priority(temp) <= get_in_priority(stk2.peek().charValue())))
{
char c = stk2.peek().charValue();
stk2.pop();
double num2 = stk1.peek().doubleValue();
stk1.pop();
double num1 = stk1.peek().doubleValue();
stk1.pop();
if (c == '+') stk1.push(new Double(num1 + num2));
if (c == '-') stk1.push(new Double(num1 - num2));
if (c == '*') stk1.push(new Double(num1 * num2));
if (c == '/')
if (num2 == 0) text2.setText("输入有误!");
else stk1.push(new Double(num1 / num2));
}
stk2.push(new Character(temp));
}
}
else if (temp == ')')//半括号
{
try {
if(stk2.isEmpty() || stk2.peek().charValue() ==')')
{
text2.setText("输入有误!");
}
else while(stk2.peek().charValue() != '(')
{
char c = stk2.peek().charValue();
stk2.pop();
double num2 = stk1.peek().doubleValue();
stk1.pop();
double num1 = stk1.peek().doubleValue();
stk1.pop();
if (c == '+') stk1.push(new Double(num1 + num2));
if (c == '-') stk1.push(new Double(num1 - num2));
if (c == '*') stk1.push(new Double(num1 * num2));
if (c == '/') stk1.push(new Double(num1 / num2));
}
}
catch(EmptyStackException a)
{
text2.setText("输入有误!");
}
stk2.pop();//把'('也pop出去
}
}//for循环结束
try {
if (!stk2.empty()) //如果运算符不空的话,做最后处理
{
while (!stk2.empty()) {
char c = stk2.peek().charValue();
stk2.pop();
double num2 = stk1.peek().doubleValue();
stk1.pop();
try {
double num1 = stk1.peek().doubleValue();
stk1.pop();
if (c == '+') stk1.push(new Double(num1 + num2));
if (c == '-') stk1.push(new Double(num1 - num2));
if (c == '*') stk1.push(new Double(num1 * num2));
if (c == '/') stk1.push(new Double(num1 / num2));
} catch (EmptyStackException a) {
text2.setText("输入有误!");
}
}
text2.setText((stk1.peek().toString()));
System.out.println(stk1.peek().doubleValue());
}
}
catch (EmptyStackException a)
{
text2.setText("输入有误!");
}
}
int get_in_priority(char temp)
{
if (temp == '+') return 1;
if (temp == '-') return 1;
if (temp == '*') return 2;
if (temp == '/') return 2;
if (temp == '(') return 0;
return 0;
}
int get_out_priority(char temp)
{
if (temp == '+') return 1;
if (temp == '-') return 1;
if (temp == '*') return 2;
if (temp == '/') return 2;
if (temp == '(') return 3;
return 0;
}
}
}
java计算器(使用栈完成加减乘除括号的优先级运算)
最新推荐文章于 2022-04-18 19:44:35 发布