广告位联系
返回顶部
分享到

Java实现简单计算器功能的代码

java 来源:转载 作者:秩名 发布时间:2021-07-19 21:42:16 人浏览
摘要

一 项目说明 实训目的:掌握 Java GUI 开发中的布局管理和事件处理机制。 实训要求: (1)要使用 java 的 GUI 设计出计算器界面。 (2)通过界面按钮,可以实现整数或浮点数的四则运算,并能将结果显示在界面中。 (3)计算可以有小数点,和正负整数的计算。

一 项目说明

实训目的:掌握 Java GUI 开发中的布局管理和事件处理机制。

实训要求:

(1)要使用 java 的 GUI 设计出计算器界面。

(2)通过界面按钮,可以实现整数或浮点数的四则运算,并能将结果显示在界面中。

(3)计算可以有小数点,和正负整数的计算。

(4)要有清零功能。

二、类设计

中缀表达式的计算solution(String str)
用来中算后缀表达式的值,并将结果返回。准备一个数字栈,一个运算符栈。大致的思路就是遇到,数字直接入数字栈,运算符看优先级进行处理,将要入运算符栈的运算符与栈顶的运算符进行比较,栈顶运算符优先级比较高的话,则把栈顶的运算符弹并且把数字栈的两个数字进行弹出,进行运算,并且把结果再次放到数字栈中,最后剩下的就是最终结果。如果运算符优先级比运算符栈顶的小,则把运算符进栈,最后把运算符都出栈。
计算加减乘除余caculateResult(char optemp, double num1, double num2)
通过传入的optemp(表达式符号)参数。是什么符号就进行什么样的运算
判断符号的优先级getOperlevel(char c)
先乘除后加减,通过0,1,2对运算符的优先级进行标记

三 项目实现设计

首先先设计一个GUI界面,先设置一个JFrame容器,容器中创建两个面板和若干按钮,先把按钮要显示的文字存入字符串数组,然后依次创建几个按钮。在设置一个文本框,用来接收用户输入的内容,centerPanel.setLayout(new GridLayout(4, 4, 12, 16));设置中间面板的布局为网格布局,并指定该容器的行数和列数以及组件之间的水平、垂直间距。centerPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));为面板创建一个框,框的头部为5像素,左为5像素,底部为5像素,右为5像素宽。再设置单行文本域的大小,字体,风格和字号,然后为各按钮设置监听器。定义一个StringBuffer类,用于保存触发产生的内容,在用txt.setText(r.toString());方法将内容输出在文本框中,clear按钮触发后,用r.delete(0,r.length());方法清空字符串中的内容并将结果显示在文本框中,“=”按钮是把触发器触发的数字保存StringBuffer里面,然后用该类的toString()方法返回StringBuffer缓冲区中的字符对象,用String类型的变量接收,该字符串接收到的就是一个中缀表达式,创建一个类,该类用于将输入的中缀表达式进行计算,把计算的结果返回给“=”按钮触发器中的result变量,把该变量转化为字符串输出在文本框中。

四 运行与测试











五 分析与总结

首先,我看到这个题的第一反应是这个界面的布局要用到网格布局,开始我是想直接在触发器里面实现相应的加减乘除功能的,发现如果要计算四则运算有点困难,单个的加减乘除还是挺容易的,后面写了一些代码后,果断删了重写,采用了数据结构中的中缀表达式的计算算法(要用到栈),不过那个时候用的语言是C语言,所以关于栈的书写就只能去百度了,之后我知道了栈和他的有关方法,自己也尝试这写了一段代码进行了测试,更加熟练的掌握了栈的用法。

还顺便看了一下广大网友的代码和算法,发现都大同小异,我自己也在他们写的算法的基础上写了一段代码,新增加了实现小数四则运算的功能,其中判断运算符的优先级那段代码直接搬运了网上的代码。

 

经过测试,发现精度有一点问题,运算的结果有时是正确的,有时是无限接近正确结果(小数点后面的小数位太多了),还有就是实现不了负数的运算,但可以实现浮点数的四则运算。以我现在的水平,这个bug暂时还解决不了。所以就没在修改了然后利用对象的调用把运算结果输出在文本框里面。有一段时间这个程序的界面老是显示不出来,控制台console那里老是闪一下就灭了,我也非常纳闷,之前我还可以显示出来的啊,现在怎么就这样的,百度了很久也没找到答案,后面去请教同学,才发现原来我的聊天窗口没有设置为可见frame.setVisible(true);。所以一般在设置容器的时候,就在他的后面写完他的所有属性,不要写完运行出错了,才发现没有写。`

Calculater:

package com.itcase_eight;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.util.Date;

/**
 * @author 罗志刚
 * @date 2020/12/16 22:09
 */
public class Calculater {
    public static void createAndShowGUI() {
        Date date=new Date();
        DateFormat format=DateFormat.getDateInstance(DateFormat.SHORT);
        // 对计算器整体框架的建立start
        JFrame f = new JFrame("计算器");// 窗口
        JPanel centerPanel = new JPanel(); // 中间面板
        JPanel startPanel=new JPanel();
        // 初始化功能键
        JButton  left=new JButton("(");
        JLabel  data=new JLabel(format.format(date),JLabel.CENTER);
        data.setFont(new Font("Times New Roman",Font.BOLD,17));
        JButton clear=new JButton("Clear");
        JButton  right=new JButton(")");
        String button[] = { "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", ".", "0", "=", "+"};
        JButton but0 = new JButton(button[0]);
        JButton but1 = new JButton(button[1]);
        JButton but2 = new JButton(button[2]);
        JButton but3 = new JButton(button[3]);
        JButton but4 = new JButton(button[4]);
        JButton but5 = new JButton(button[5]);
        JButton but6 = new JButton(button[6]);
        JButton but7 = new JButton(button[7]);
        JButton but8 = new JButton(button[8]);
        JButton but9 = new JButton(button[9]);
        JButton but10 = new JButton(button[10]);
        JButton but11 = new JButton(button[11]);
        JButton but12 = new JButton(button[12]);
        JButton but13 = new JButton(button[13]);
        JButton but14 = new JButton(button[14]);
        JButton but15 = new JButton(button[15]);
        // 单行输入文本框
        JTextField txt = new JTextField();
        // 使用网格布局方式
        centerPanel.setLayout(new GridLayout(5, 4, 12, 16)); // 左右上下间隔
        centerPanel.add(left);
        centerPanel.add(clear);
        centerPanel.add(right);
        centerPanel.add(data);
        centerPanel.add(but0);
        centerPanel.add(but1);
        centerPanel.add(but2);
        centerPanel.add(but3);
        centerPanel.add(but4);
        centerPanel.add(but5);
        centerPanel.add(but6);
        centerPanel.add(but7);
        centerPanel.add(but8);
        centerPanel.add(but9);
        centerPanel.add(but10);
        centerPanel.add(but11);
        centerPanel.add(but12);
        centerPanel.add(but13);
        centerPanel.add(but14);
        centerPanel.add(but15);
        centerPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        // 设置容器大小
        txt.setPreferredSize(new Dimension(465, 40));
        // 设置字体,风格和字号
        txt.setFont(new Font("宋体", Font.PLAIN, 28));
        f.add(startPanel);
        f.add(txt, BorderLayout.NORTH); // 将单行文本框添加到窗口的 北部
        f.add(centerPanel, BorderLayout.SOUTH); // 将中间面板添加到窗口的南部
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 点X关闭窗口
        f.setLocation(400, 200); // 初始化时定位
        f.setSize(500, 300);
        // 展示JFrame窗口
        f.setVisible(true);
        f.setResizable(false); // 禁止拖曳改变窗口大小
        f.pack(); // 让窗口的大小自适应
        // 对计算器整体框架的建立end
        // 为按钮事件添加自定义监听器start
        StringBuffer r=new StringBuffer();
        but0.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                r.append("7");
                txt.setText(r.toString());
            }
        });
        but1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                r.append("8");
                txt.setText(r.toString());
            }
        });
        but2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                r.append("9");
                txt.setText(r.toString());
            }
        });
        but4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                r.append("4");
                txt.setText(r.toString());
            }
        });
        but5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                r.append("5");
                txt.setText(r.toString());
            }
        });
        but6.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                r.append("6");
                txt.setText(r.toString());
            }
        });
        left.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                r.append("(");
                txt.setText(r.toString());
            }
        });
        right.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                r.append(")");
                txt.setText(r.toString());
            }
        });
        but8.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                r.append("1");
                txt.setText(r.toString());
            }
        });
        but9.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                r.append("2");
                txt.setText(r.toString());
            }
        });
        but10.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                r.append("3");
                txt.setText(r.toString());
            }
        });
        but13.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                r.append("0");
                txt.setText(r.toString());
            }
        });
        but15.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                r.append("+");
                txt.setText(r.toString());
            }
        });
        but3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                r.append("/");
                txt.setText(r.toString());
            }
        });
        but7.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                r.append("*");
                txt.setText(r.toString());
            }
        });
        but12.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                r.append(".");
                txt.setText(r.toString());
            }
        });
        but11.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                r.append("-");
                txt.setText(r.toString());
            }
        });
        clear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                r.delete(0,r.length());   //清空字符串中的内容
                txt.setText(r.toString());  //将结果显示在文本框中
            }
        });
        but14.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                r.append("=");
                String str=r.toString();
                txt.setText("");
                double result= Computer.solution(str);
                String string=String.valueOf(result);
                r.delete(0,r.length());
                r.append(string);
                txt.setText(string);
            }
        });
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(Calculater::createAndShowGUI);
    }
}

Computer类:

package com.itcase_eight;

import java.util.Stack;

/**
 * @author 罗志刚
 * @date 2020/12/16 22:05
 */
public class Computer {
    public static double solution(String str) {
        Stack<Double> numStack = new Stack<>();
        Stack<Character> signalStack = new Stack<>();
        int index = 0;// 记录已经执行的符号数
        int len = str.length();
        while (index < len) {
            char c = str.charAt(index); // 取出这一步的符号
            if (c == '(') {
                signalStack.push(c);// 若是左括号就进栈
            }
            // 否则要先判断优先级
            else if (c == '+' || c == '-' || c == '*' || c == '/') {
                int currOperLevel = getOperlevel(c);// 当前符号的优先级
                while (true) {
                    int stackOperLevel = 0;// 栈顶元素的优先级
                    if (!signalStack.isEmpty()) {
                        Object obj = signalStack.peek();
                        stackOperLevel = getOperlevel((char) obj);
                    }
                    // 若当前元素优先级大于栈顶元素的优先级则入栈
                    if (currOperLevel > stackOperLevel) {
                        signalStack.push(c);
                        break;// 直到让比自己优先级高的符号都出栈运算了再把自己进栈
                    } else {// 不能入栈就进行计算
                        try {
                            char optemp = '0';
                            double num1 = 0;
                            double num2 = 0;
                            if (!signalStack.isEmpty()) {
                                optemp = (char) signalStack.pop();// 取出优先级大的那个符号
                            }
                            if (!numStack.isEmpty()) {
                                num1 = (double) numStack.pop();
                                num2 = (double) numStack.pop();// 取出数据栈中的两个数
                            }
                            numStack.push(caculateResult(optemp, num2, num1));// 将算出来的结果数据再次进入数据栈
                        } catch (Exception e) {
                            // TODO: handle exception
                            e.printStackTrace();
                        }
                    }
                }
            } else if (c == ')') {// 右括号就返回栈顶元素,右括号是不进栈的
                while (true) {
                    char theop = (char) signalStack.pop();
                    if (theop == '(') {
                        break;
                    } else {
                        try {
                            double num1 = (double) numStack.pop();
                            double num2 = (double) numStack.pop();
                            numStack.push(caculateResult(theop, num2, num1));// 运算括号内的内容
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            } else if (c >= '0' && c <= '9') {
                int tempIndex = index + 1;
                while (tempIndex < len) {
                    char temp = str.charAt(tempIndex);// 取字符串中处于当前字符的下一位
                    if ((temp >= '0' && temp <= '9') || temp == '.') {
                        tempIndex++;// 若为数字则继续向后取
                    } else {
                        break;// 证明数字去完
                    }
                }
                String numstr = str.substring(index, tempIndex);// 截取这个字符串则为两个符号之间的数字
                try {
                    double numnum = Double.parseDouble(numstr);// 将数字转换成整型便于运算
                    numStack.push(numnum);
                    index = tempIndex - 1;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            index++;
        }
        // 检查符号栈是否为空
        while (true) {
            Object obj = null;
            if (signalStack.isEmpty() == false) {
                obj = signalStack.pop();
            }
            if (obj == null) {
                break;// 为空证明运算已结束
            } else {// 不为空就出栈运算
                char opterTemp = (char) obj;
                double num1 = (double) numStack.pop();
                double num2 = (double) numStack.pop();
                numStack.push(caculateResult(opterTemp, num2, num1));
            }
        }
        double result = 0;
        try {
            result = (double) numStack.pop();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return result;
    }

    //计算加减乘除余
    private static Double caculateResult(char optemp, double num1, double num2) {

        switch (optemp) {
            case '+':
                return num1 + num2;
            case '-':
                return num1 - num2;
            case '*':
                return num1 * num2;
            case '/':
                return num1 / num2;
        }
        return 0.0;
    }

    //返回符号优先级
    private static int getOperlevel(char c) {

        switch (c) {
            case '(':
                return 0;
            case '+':
            case '-':
                return 1;
            case '*':
            case '/':
                return 2;
            default:
                return 0;
        }
    }
}


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 : https://blog.csdn.net/dwjdj/article/details/104307986
相关文章
  • SpringBoot自定义错误处理逻辑介绍

    SpringBoot自定义错误处理逻辑介绍
    1. 自定义错误页面 将自定义错误页面放在 templates 的 error 文件夹下,SpringBoot 精确匹配错误信息,使用 4xx.html 或者 5xx.html 页面可以打印错误
  • Java实现手写一个线程池的代码

    Java实现手写一个线程池的代码
    线程池技术想必大家都不陌生把,相信在平时的工作中没有少用,而且这也是面试频率非常高的一个知识点,那么大家知道它的实现原理和
  • Java实现断点续传功能的代码

    Java实现断点续传功能的代码
    题目实现:网络资源的断点续传功能。 二、解题思路 获取要下载的资源网址 显示网络资源的大小 上次读取到的字节位置以及未读取的字节
  • 你可知HashMap为什么是线程不安全的
    HashMap 的线程不安全 HashMap 的线程不安全主要体现在下面两个方面 在 jdk 1.7 中,当并发执行扩容操作时会造成环形链和数据丢失的情况 在
  • ArrayList的动态扩容机制的介绍

    ArrayList的动态扩容机制的介绍
    对于 ArrayList 的动态扩容机制想必大家都听说过,之前的文章中也谈到过,不过由于时间久远,早已忘却。 所以利用这篇文章做做笔记,加
  • JVM基础之字节码的增强技术介绍

    JVM基础之字节码的增强技术介绍
    字节码增强技术 在上文中,着重介绍了字节码的结构,这为我们了解字节码增强技术的实现打下了基础。字节码增强技术就是一类对现有字
  • Java中的字节码增强技术

    Java中的字节码增强技术
    1.字节码增强技术 字节码增强技术就是一类对现有字节码进行修改或者动态生成全新字节码文件的技术。 参考地址 2.常见技术 技术分类 类
  • Redis BloomFilter布隆过滤器原理与实现

    Redis BloomFilter布隆过滤器原理与实现
    Bloom Filter 概念 布隆过滤器(英语:Bloom Filter)是1970年由一个叫布隆的小伙子提出的。它实际上是一个很长的二进制向量和一系列随机映射
  • Java C++算法题解leetcode801使序列递增的最小交换次

    Java C++算法题解leetcode801使序列递增的最小交换次
    题目要求 思路:状态机DP 实现一:状态机 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Solution { public int minSwap(int[] nums1, int[] nums2) { int n
  • Mybatis结果集映射与生命周期介绍

    Mybatis结果集映射与生命周期介绍
    一、ResultMap结果集映射 1、设计思想 对简单的语句做到零配置,对于复杂一点的语句,只需要描述语句之间的关系就行了 2、resultMap的应用场
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计