计算机软件实习每日学习打卡(4)20201206

实验序号:实验一
实验名称:简单计算器的实现
实验要求

  1. 学习图形界面的设计,利用 MFC 应用程序(Java swing 或 QT 框架,或 C#)创建基于对话框的应用程序,添加按钮、编辑框等控件;
  2. 能通过设计的按钮控件输入并实现简单算术运算,要求表达式在编辑框中显示,能将运算结果,输出在编辑框内显示;并保存历史的表达式运算记录。
  3. 也能够实现混合运算的算术表达式求解,算术表达式中包括加、减、乘、除、括号等运算符;并且能够识别括号,优先级正确。

日志
1.完成的部分:目前已完成符合要求的1.0版本
2.代码如下:

package experiment1;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;

/**
 * 计算器1.0
 * @author zoter
 * 仅支持一重括号的四则正整数运算
 * 支持第二次运算保持上次运算的值
 * 运算范围-2^1024 ~ +2^1024
 */

public class Calculator {
	public JTextField view = null;
	public String output = "";
	private static Stack<Double> numbers;
	private static Stack<String> operators;
	public double results=0;
	
	public class Layout extends JFrame{
		private static final long serialVersionUID = 1L;
		private JFrame jf = new JFrame("计算器1.0");

		Layout() {
	        jf.setSize(150,150);
	        jf.setResizable(false);
	        
	    	view = new JTextField("0");
	    	view.setHorizontalAlignment(JTextField.RIGHT);
	    	//设置为只读,设置背景色,设置字体 粗细 大小
	    	view.setEditable(false);
	    	view.setBackground(new Color(192, 192, 192));
	    	view.setFont(new Font("Cambria", Font.BOLD, 20)); 
	    	
	        //键盘布局构建
	        JPanel panel01 = new JPanel(new GridLayout(3, 4, 10, 10));
	        JPanel panel02 = new JPanel(new GridLayout(1, 3, 10, 10));
	        JPanel panel03 = new JPanel(new GridLayout(1, 1, 10, 10));
	        JPanel panel04 = new JPanel(new GridLayout(1, 1, 10, 10));
	        JPanel panel05 = new JPanel(new GridLayout(1, 1, 10, 10));

	        //定义按钮
	        JButton btn01 = new JButton("1");
	        btn01.setBackground(Color.white);
	        btn01.setBorder(new LineBorder(Color.black,1));
	        btn01.addActionListener(new Get());
	        
	        JButton btn02 = new JButton("2");
	        btn02.setBackground(Color.white);
	        btn02.setBorder(new LineBorder(Color.black,1));
	        btn02.addActionListener(new Get());
	        
	        JButton btn03 = new JButton("3");
	        btn03.setBackground(Color.white);
	        btn03.setBorder(new LineBorder(Color.black,1));
	        btn03.addActionListener(new Get());
	        
	        JButton btn04 = new JButton("4");
	        btn04.setBackground(Color.white);
	        btn04.setBorder(new LineBorder(Color.black,1));
	        btn04.addActionListener(new Get());
	        
	        JButton btn05 = new JButton("5");
	        btn05.setBackground(Color.white);
	        btn05.setBorder(new LineBorder(Color.black,1));
	        btn05.addActionListener(new Get());
	        
	        JButton btn06 = new JButton("6");
	        btn06.setBackground(Color.white);
	        btn06.setBorder(new LineBorder(Color.black,1));
	        btn06.addActionListener(new Get());
	        
	        JButton btn07 = new JButton("7");
	        btn07.setBackground(Color.white);
	        btn07.setBorder(new LineBorder(Color.black,1));
	        btn07.addActionListener(new Get());
	        
	        JButton btn08 = new JButton("8");
	        btn08.setBackground(Color.white);
	        btn08.setBorder(new LineBorder(Color.black,1));
	        btn08.addActionListener(new Get());
	        
	        JButton btn09 = new JButton("9");
	        btn09.setBackground(Color.white);
	        btn09.setBorder(new LineBorder(Color.black,1));
	        btn09.addActionListener(new Get());
	        
	        JButton btn10 = new JButton("0");
	        btn10.setBackground(Color.white);
	        btn10.setBorder(new LineBorder(Color.black,1));
	        btn10.addActionListener(new Get());
	        
	        JButton btn11 = new JButton("+");
	        btn11.setBackground(Color.white);
	        btn11.setBorder(new LineBorder(Color.black,1));
	        btn11.addActionListener(new Get());
	        
	        JButton btn12 = new JButton("-");
	        btn12.setBackground(Color.white);
	        btn12.setBorder(new LineBorder(Color.black,1));
	        btn12.addActionListener(new Get());
	        
	        JButton btn13 = new JButton("*");
	        btn13.setBackground(Color.white);
	        btn13.setBorder(new LineBorder(Color.black,1));
	        btn13.addActionListener(new Get());
	        
	        JButton btn14 = new JButton("/");
	        btn14.setBackground(Color.white);
	        btn14.setBorder(new LineBorder(Color.black,1));
	        btn14.addActionListener(new Get());
	        
	        JButton btn15 = new JButton("(");
	        btn15.setBackground(Color.white);
	        btn15.setBorder(new LineBorder(Color.black,1));
	        btn15.addActionListener(new Get());
	        
	        JButton btn16 = new JButton(")");
	        btn16.setBackground(Color.white);
	        btn16.setBorder(new LineBorder(Color.black,1));
	        btn16.addActionListener(new Get());
	        
	        JButton btn17 = new JButton("C");
	        btn17.setBackground(Color.white);
	        btn17.setBorder(new LineBorder(Color.black,1));
	        btn17.addActionListener(new Clear());
	        
	        JButton btn18 = new JButton("=");
	        btn18.setBackground(Color.white);
	        btn18.setBorder(new LineBorder(Color.black,1));
	        btn18.addActionListener(new equal());
	        
	        
	        btn01.setPreferredSize(new Dimension(60,60));
	        btn04.setPreferredSize(new Dimension(60,60));
	        btn10.setPreferredSize(new Dimension(130,60));
	        btn17.setPreferredSize(new Dimension(60,60));
	        btn18.setPreferredSize(new Dimension(60,130));
	        view.setPreferredSize(new Dimension(270,60));
	        
	        panel01.add(btn11);
	        panel01.add(btn12);
	        panel01.add(btn13);
	        panel01.add(btn14);
	        panel01.add(btn07);
	        panel01.add(btn08);
	        panel01.add(btn09);
	        panel01.add(btn15);
	        panel01.add(btn04);
	        panel01.add(btn05);
	        panel01.add(btn06);
	        panel01.add(btn16);
	        panel02.add(btn01);
	        panel02.add(btn02);
	        panel02.add(btn03);
	        panel03.add(btn10);
	        panel04.add(btn17);
	        panel05.add(btn18);
	        
	        //装箱布局
	        //创建间隔
	        Component vStrut01 = Box.createVerticalStrut(10);
	        Component vStrut02 = Box.createVerticalStrut(10);
	        Component vStrut03 = Box.createVerticalStrut(10);
	        Component vStrut04 = Box.createVerticalStrut(10);
	        Component vStrut05 = Box.createVerticalStrut(10);
	        Component hStrut01 = Box.createHorizontalStrut(10);
	        Component hStrut02 = Box.createHorizontalStrut(10);
	        Component hStrut03 = Box.createHorizontalStrut(10);
	        Component hStrut04 = Box.createHorizontalStrut(10);
	        
	        Box hBox01 = Box.createHorizontalBox();
	        hBox01.add(panel03);
	        hBox01.add(hStrut01);
	        hBox01.add(panel04);
	        
	        Box vBox01 = Box.createVerticalBox();
	        vBox01.add(panel02);
	        vBox01.add(vStrut01);
	        vBox01.add(hBox01);
	        
	        Box hBox02 = Box.createHorizontalBox();
	        hBox02.add(vBox01);
	        hBox02.add(hStrut02);
	        hBox02.add(panel05);
	        
	        Box vBox02 = Box.createVerticalBox();
	        vBox02.add(vStrut02);
	        vBox02.add(view);
	        vBox02.add(vStrut03);
	        vBox02.add(panel01);
	        vBox02.add(vStrut04);
	        vBox02.add(hBox02);
	        vBox02.add(vStrut05);
	        
	        Box hBox03 = Box.createHorizontalBox();
	        hBox03.add(hStrut03);
	        hBox03.add(vBox02);
	        hBox03.add(hStrut04);

	        jf.setContentPane(hBox03);
	        jf.pack();
	        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	        jf.setLocationRelativeTo(null);
	        jf.setVisible(true);
		}	
	}
	
	class Get implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			String input = e.getActionCommand();
			output += input;
			output += " ";
			view.setText(output);
		}
	}

	class Clear implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			output = "";
			view.setText(output);	
		}	
	}
	
	class equal implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			output += " ";
			results=calculate(output);
			output += "=";
			output += results;
			view.setText(output);
			output = "";
		}	
	}

    public static Double calculate(String expression)
    {
        numbers = new Stack<>();
        operators = new Stack<>();
        String[] strings = expression.split(" ");
        a:for (int i = 0; i < strings.length; i++)
        {
            String each = strings[i];
            Double number;
            //将字符串转数字。
            try
            {
                number = Double.parseDouble(each);
                //转换成功就压入数栈
                numbers.push(number);
            }
            //如果转换失败,说明是一个运算符或括号
            catch (NumberFormatException e)
            {
                //如果是左括号,直接入符号栈,结束当前循环
                if ("(".equals(each))
                {
                    operators.push(each);
                    continue;
                }
                //如果是右括号,一直进行计算,直到遇到左小括号
                if (")".equals(each))
                {
                    String operator;
                    while (true){
                        operator = operators.pop();
                        if ("(".equals(operator))
                        { continue a; }
                        operator(numbers.pop(),numbers.pop(),operator,numbers);
                    }
                }
                //如果符号栈内有符号,就比较和栈顶的优先级别
                if (operators.size() >= 1)
                {
                    if (compare(each) < compare(operators.peek()))
                    { operator(numbers.pop(),numbers.pop(),operators.pop(),numbers); }
                }
                //右括号不入栈
                if (!")".equals(each)){ 
                	operators.push(each); 
                }
            }
        }
        //收尾工作
        while (operators.size() > 0){ 
        	operator(numbers.pop(),numbers.pop(),operators.pop(),numbers); 
        }
        return numbers.pop();
    }

    //判断运算符的等级
    public static int compare(String operator)
    {
        if ("*".equals(operator) || "/".equals(operator)){ 
        	return 2; 
        }
        if ("+".equals(operator) || "-".equals(operator)){
        	return 1; 
        }
        else {
        	return 0;
        }
    }

    //进行运算并且压入数栈
    public static void operator(Double one, Double tow, String operator, Stack<Double> numbers)
    {
        if ("+".equals(operator)){
        	numbers.push(one+tow); 
        }
        if ("-".equals(operator)){
        	numbers.push(tow-one); 
        }
        if ("*".equals(operator)){ 
        	numbers.push(one*tow); 
        }
        if ("/".equals(operator)){ 
        	numbers.push(tow/one); 
        }
        else{  }
    }    
    
	public static void main(String[] args) {
		Calculator a = new Calculator();
		a.new Layout ();
	}
}

3实例如下
在这里插入图片描述

5 .明天的目标
1.完成实验二

学习的资料

《Java程序设计教程》机械工业出版社 程科 潘磊主编 ISBN 978-7-111-50902-8

20201206

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值