**
Java简易计算器–处理四则运算优先级
大一老师让我们做一个计算器,发现没有想象的简单,做了快一周才做出来
效果如下
其中
cur_result 是输入一个表达式后得出的结果
result_used是判断cur_result使不使用和有没有使用过
cur_show是显示的字符串
temp_num用于存储单个数字
input_num用于分开存储数和符号(带乘除的)
two是个中间数组
get_result是存储数和符号(不带乘除)最后做加法的
input_num到get_result的过程我还做了一个演示的图
代码如下:
package test;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Date;
public class calculator{
//布局用
static JFrame frame;
Container mainContainer;
static JPanel contentPanel;
static JTextArea textField;
JScrollPane jscp;
JButton button[];
//实现功能用
String input_num[] = {};//用于分开存储数字和符号
static String[] history = new String[20];//用于存储历史记录
static String cur_show= new String("");//用于实时显示文本
static double cur_result;
static boolean result_used = false;
static String get_result[] = {"0","+"};
static String temp_num = "";
public static void main(String[] args) {
// TODO Auto-generated method stub
calculator calculator1= new calculator();
calculator1.layout();
calculator1.add_listener();
}
public calculator() {
// 鸡肋的构造函数
frame = new JFrame("计算器");
mainContainer = frame.getContentPane();
contentPanel = new JPanel();
jscp= new JScrollPane();
textField = new JTextArea();
button = new JButton[20];
jscp.setViewportView(textField);
textField.setLineWrap(true);
}
@SuppressWarnings("deprecation")
public void layout()
{
// 计算器布局方法
frame.setSize(500, 600);
mainContainer.setLayout(new BorderLayout());
textField.setFont(new Font("微软雅黑",Font.BOLD,80));
textField.setEditable(false);
Font f1 = new Font("微软雅黑",Font.BOLD,16);
Date d1 = new Date();
JLabel l1 =new JLabel(d1.getMonth() +"月"+ d1.getDate(),SwingConstants.CENTER);
JLabel l2 =new JLabel("星期" +d1.getDay(),SwingConstants.CENTER);
l1.setFont(f1);
l2.setFont(f1);
contentPanel.setLayout(new GridLayout(5, 4));
String lable[] = {"C","backspace","+","-","*","/","1","4","7",".","2","5","8","0","3","6","9","="};
contentPanel.add(l1);
contentPanel.add(l2);
for(int i = 0;i<18;i++)
{
button[i] = new JButton(lable[i]);
button[i].setBackground(new Color(240,240,240));
button[i].setBorder(BorderFactory.createRaisedBevelBorder());
contentPanel.add(button[i]);
}
mainContainer.add(jscp, BorderLayout.NORTH);
mainContainer.add(contentPanel, BorderLayout.CENTER);
frame.setVisible(true);
}
public void add_listener() {
// 添加监听器方法
MybuttonLisener lisener = new MybuttonLisener();
for(int i = 0;i<18;i++)
{
button[i].addActionListener(lisener);
}
}
//内部类
class MybuttonLisener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e)
{
// -------------------------------------------------------等号处理start------------------------------------------------------
if(e.getActionCommand()=="=")
{
if(temp_num == "")
{
if(input_num.length > 0)
{
JOptionPane.showMessageDialog(contentPanel, "语法错误");
}
}
else
{
if(input_num.length == 0)
{
cur_result = Double.parseDouble(temp_num);
result_used = true;
return;
}
add_input_num(temp_num);
temp_num = "";
if(input_num.length >1)
{
String two[] = new String[3];
for(int i =0;i<input_num.length;i++)
{
if(two[2]==null)
{
two[2] = input_num[i];
if(input_num.length >0)
{
if(two[1] == null)
{
two[1] = two[2];
two[2] = null;
}
else if(two[0] == null)
{
two[0] =two[1];
two[1] =two[2];
two[2] = null;
}
}
}
if(two[2] != null)
{
if(two[1] =="+" || two[1] =="-")
{
if(i == input_num.length-1)
{
add_get_result(two[0]);
add_get_result(two[1]);
add_get_result(two[2]);
two[0] = null;
two[1] = null;
two[2] = null;
}
else {
add_get_result(two[0]);
add_get_result(two[1]);
two[0] = two[2];
two[1] = null;
two[2] = null;
}
}
else if(two[1] == "*")
{
two[0] = Double.parseDouble(two[0]) * Double.parseDouble(two[2]) + "";
two[1] = null;
two[2] = null;
if(i == input_num.length-1)
{
add_get_result(two[0]);
}
}
else if(two[1] == "/")
{
two[0] = Double.parseDouble(two[0]) / Double.parseDouble(two[2]) + "";
two[1] = null;
two[2] = null;
if(i == input_num.length-1)
{
add_get_result(two[0]);
}
}
}
}
double a = 0;
for(int n =0;n<get_result.length;n++)
{
if(n==0)
a = Double.parseDouble(get_result[0]);
else if(get_result[n] == "+")
{
a += Double.parseDouble(get_result[n+1]);
n++;
}
else if(get_result[n] == "-")
{
a -= Double.parseDouble(get_result[n+1]);
n++;
}
}
cur_result = a;
cur_show = cur_result + "";
result_used = true;
while(input_num.length >0 )
dec_input_num();
while(get_result.length >2)
dec_get_result();
}
else
{
cur_result = Double.parseDouble(temp_num) + 0;
result_used = true;
}
}
}
// -------------------------------------------------------等号处理end------------------------------------------------------
// -------------------------------------------------四则运算符处理start---------------------------------------------------
else if(e.getActionCommand() == "+" || e.getActionCommand() == "-" || e.getActionCommand() =="*" || e.getActionCommand()=="/")
{
if(temp_num =="")
{
if(input_num.length == 0)
{
if(result_used == true)
{
add_input_num(cur_result + "");
add_input_num(e.getActionCommand());
cur_show += e.getActionCommand();
result_used = false;
}
else
{
if(e.getActionCommand() == "+" || e.getActionCommand() == "-")
{
temp_num +=e.getActionCommand();
cur_show +=e.getActionCommand();
}
}
}
else if (input_num.length>0) {
input_num[input_num.length-1] = e.getActionCommand();
cur_show = cur_show.substring(0, cur_show.length()-1) + e.getActionCommand();
}
}
else
{
add_input_num(temp_num);
add_input_num(e.getActionCommand());
temp_num = "";
cur_show +=e.getActionCommand();
}
}
// -------------------------------------------------四则运算符处理end---------------------------------------------------
// ------------------------------------------------回退键处理start-----------------------------------------------------------
else if(e.getActionCommand() == "backspace")
{
if(temp_num =="")
{
if(input_num.length > 0)
{
dec_input_num();
cur_show = cur_show.substring(0, cur_show.length()-1);
}
else if(result_used ==true)
{
cur_show = cur_show.substring(0,cur_show.length()-1);
result_used =false;
}
else if(cur_show != "")
cur_show = cur_show.substring(0,cur_show.length()-1);
}
else
{
temp_num = temp_num.substring(0, temp_num.length()-1);
cur_show = cur_show.substring(0,cur_show.length()-1);
}
}
// ------------------------------------------------回退键处理end-----------------------------------------------------------
// -------------------------------------------------清空键处理start--------------------------------------------------------
else if(e.getActionCommand() == "C")
{
temp_num = "";
cur_show = "";
if(result_used == true)
result_used = false;
else if(input_num.length!=1)
{
while(input_num.length>0)
dec_input_num();
}
}
// -------------------------------------------------清空键处理start--------------------------------------------------------
// ------------------------------------------------数字键处理start----------------------------------------------------------
else {
if(result_used==true)
result_used =false;
else
{
temp_num +=e.getActionCommand();
cur_show += e.getActionCommand();
}
}
// ------------------------------------------------数字键处理end----------------------------------------------------------
textField.setText(cur_show);
}
//----------------------------------------数组元素添加方法--------------------------------------------------
public void add_get_result(String s1) {
String temp_arry[]= new String[get_result.length+1];
for(int n=0;n<get_result.length;n++)
temp_arry[n] = get_result[n];
temp_arry[get_result.length] = s1;
get_result = temp_arry;
}
public void add_input_num(String s1) {
String tempArry[] = new String[input_num.length + 1];
for(int m=0;m<input_num.length ;m++)
tempArry[m] = input_num[m];
tempArry[input_num.length] = s1;
input_num = tempArry;
}
//------------------------------------------数组元素删减方法------------------------------------------------------------
public void dec_input_num() {
String tempArry[] = new String[input_num.length-1];
for(int m= 0;m<tempArry.length;m++)
tempArry[m] = input_num[m];
input_num = tempArry;
}
public void dec_get_result() {
String tempArry[] = new String[get_result.length - 1];
for(int m=0;m<tempArry.length;m++)
tempArry[m] = get_result[m];
get_result = tempArry;
}
}
}
基本的功能实现了,再加功能的话问题不大
推荐都去敲一敲计算器,十分练手
敲了十几个小时,我只能说,
程序员的水太深,你把握不住