题目一、设计一个可实现加、减、乘、除功能的计算器软件

说明:使用Visio实现加减乘除的流程图如下

 

 

说明:用java语法实现计算器的加减乘除功能,如下代码

package p;
import java.util.Scanner;

public class User {
    public static String symbol(Scanner scanner){
        System.out.println("请输入第一个数字");
        float m = scanner.nextFloat();
        System.out.println("请输入运算符");
        String symbol=scanner.next();
        System.out.println("请输入第二个数字");
        float n = scanner.nextFloat();
        if(symbol.equals("+")){
            float num=JiaFa(m,n);
            System.out.println(num);
        }
        else if(symbol.equals("-")){
            float num=JianFa(m,n);
            System.out.println(num);
        }
        else if(symbol.equals("*")){
            float num=Cheng(m,n);
            System.out.println(num);
        }
        else if(symbol.equals("/")){
            float num=Chu(m,n);
            System.out.println(num);
        }
        return symbol;
    }

    public static float JiaFa(float m,float n) {
        float c = 0;
        if (m> 10000 & n > 10000) {
            System.out.println("数字过大,请重新输入");
        }
        return c=m+n;

    }
    public static float JianFa(float a,float b){
        float c;
        if(a> 10000 & b > 10000){
            System.out.println("数字过大,请重新输入");
        }
        return c=a-b;
    }
    public static float Cheng(float a,float b){
        float c;
        if(a> 10000 & b > 10000){
            System.out.println("数字过大,请重新输入");
        }
        return c=a*b;
    }
    public static float Chu(float a,float b){
        float c;
        if(b==0){
            System.out.println("分母不能为0,请重新输入");
        }
        return c=a/b;
    }
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        while(true){
            symbol(scanner);
        }
    }

}

题目二、使用牛顿迭代法完成算术开方的运算

牛顿迭代法思想:牛顿迭代法(Newton's method)是一种用于求解方程的迭代数值方法。它基于以下思想:通过使用切线逼近曲线,找到函数的根。

设我们要求解方程 f(x) = 0 的根,牛顿迭代法的基本思路如下:

  1. 先猜测一个初始值 x₀;
  2. 在函数 f(x) 上找到点 (x₀, f(x₀)) 处的切线;
  3. 切线与 x 轴的交点作为新的猜测值 x₁;
  4. 重复步骤2和步骤3,直到满足所需的精度或达到最大迭代次数。
  5.  

数学推导如下:

设初始值为 x₀,对应的函数值为 f(x₀),切线斜率为 f'(x₀)。假设切线与 x 轴的交点为 x₁,那么有:

切线方程:y- f(x₀)= f'(x₀)(x-x₀)

令 y = 0,解得 x₁:

 x₁=x₀-f(x₀)=/f'(x₀)

这样就得到了新的猜测值 x₁。重复以上步骤,可以得到更接近真实根的近似值。

采用java语言对牛顿迭代法进行编写:

package p2;

import java.util.Scanner;
import static java.lang.StrictMath.abs;
public class kaiFang {
    public static float getSqrt(float input){
        float x=1.0f,y=0.0f,k=0.0f;
        if(input<0){
            System.out.println("负数无法开根");
            return 65535.0f;
        }else{
            while(abs(y)>=0.001f){
                k=2*x;
                x=x-(y*1.0f)/k;
                y=x*x-input;
            }
        }
        return x;
    }
    public static void mai(String[] args){
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入一个非负整数");
        float m = scanner.nextFloat();
        float z=getSqrt(m);
        System.out.println(z);
    }
}

 

三、计算器软件的UI设计

本次计算器软件的界面设计是由swing设计,其设计代码如下:

package cn.lidan.start;

import can.lidan.util.Const;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*JFrameJava Swing GUI工具包中的一个类,用于创建窗口应用程序*/
public class caculator extends JFrame implements ActionListener {
    //北面控件
    private JPanel jp_north = new JPanel();
    //为计算器添加输入框
    private JTextField input_text = new JTextField();//创建输入框
    private JButton c_Bth = new JButton("c");//创建一个按钮

    //中间控件
    private JPanel jp_center = new JPanel();//创建页面布局


    /* public static final int FRAME_W=300;
     public static final int FRAME_H=300;
     public static final int SCREEN_W= Toolkit.getDefaultToolkit().getScreenSize().width;
     public static final int SCREEN_H=Toolkit.getDefaultToolkit().getScreenSize().height;
     public int frame_x=(SCREEN_W-FRAME_W)/2;
     public int frame_y=(SCREEN_H-FRAME_H)/2;*/
    public caculator() throws HeadlessException {
        this.init();
        this.addNorthComponet();
        this.addCenterButton();

    }

    //进行初始化
    public void init() {
        this.setTitle(Const.TITLE);//设置名称
        this.setSize(Const.FRAME_W, Const.FRAME_H);//给窗体设置大小
        this.setLayout(new BorderLayout());
        this.setResizable(false);//设置窗体大小不可修改
        this.setLocation(Const.FRAME_x, Const.FRAME_y);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗体关闭

    }

    //添加北面控件
    public void addNorthComponet() {
        this.input_text.setPreferredSize(new Dimension(300, 50));
        jp_north.add(input_text);
        this.c_Bth.setForeground(Color.RED);
        jp_north.add(c_Bth);
        c_Bth.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                input_text.setText("");
            }
        });
        //添加输入框到窗体中
        this.add(jp_north, BorderLayout.NORTH);
        /*BorderLayout 将容器分为 EAST  SOUTH  WEST  NORTH  CENTER
        五个区域,普通组件可以被放置在这 5 个区域的任意一个中 */
    }

    //添加中间按钮
    public void addCenterButton() {
        String bth_text = "123+456-789*0.=/√";
        String regx = "[\\+\\-*/=.√]";
        this.jp_center.setLayout(new GridLayout(5, 4));
        for (int i = 0; i < 17; i++) {
            String temp = bth_text.substring(i, i + 1);
            JButton btn = new JButton();//创建数字按钮
            btn.setText(temp);
            if (temp.matches(regx)) {
                //设置字体
                btn.setFont(new Font("粗体", Font.BOLD, 17));
                btn.setForeground(Color.RED);
            }
            /*if(temp.equals("+")||temp.equals("-")||temp.equals("*")
                    ||temp.equals("/")||temp.equals(".")||temp.equals("="))
            {
                bth.setFont(new Font("粗体",Font.BOLD,16));
                bth.setForeground(Color.RED);
            }*/
            btn.addActionListener(this);

            jp_center.add(btn);

        }
        this.add(jp_center, BorderLayout.CENTER);
    }



    private String firstInput = null;
    private String operator=null;
    @Override
    public void actionPerformed(ActionEvent e) {
        String clickStr = e.getActionCommand();
        if ("√.0123456789".indexOf(clickStr) != -1) {
            this.input_text.setText(input_text.getText() + clickStr);
            this.input_text.setHorizontalAlignment(JTextField.RIGHT);
            //JOptionPane.showMessageDialog(this,clickStr);
        } else if (clickStr.matches("[\\+\\-*/√]{1}")) {
            operator = clickStr;
            firstInput = this.input_text.getText();
            this.input_text.setText("");
        } else if (clickStr.equals("=")) {
            Double a = Double.valueOf(firstInput);
            Double b = Double.valueOf(this.input_text.getText());
            Double result = null;
            switch (operator) {
                case "+":
                    result = a + b;
                    break;
                case "-":
                    result = a - b;
                    break;
                case "*":
                    result = a * b;
                    break;
                case "/":
                    if (b != 0) {
                        result = a / b;
                    }
                    break;
            }
            this.input_text.setText(result.toString());

        }
    }

    public static void main(String[] args) {
        caculator Caculator = new caculator();
        Caculator.setVisible(true);
    }
}

package can.lidan.util;

import cn.lidan.start.caculator;

import java.awt.*;

public class Const {
    public static final int FRAME_W=400;
    public static final int FRAME_H=500;
    public static final int SCREEN_W= Toolkit.getDefaultToolkit().getScreenSize().width;
    public static final int SCREEN_H=Toolkit.getDefaultToolkit().getScreenSize().height;
    public static final int FRAME_x=(SCREEN_W-FRAME_W)/2;
    public static final int FRAME_y=(SCREEN_H-FRAME_H)/2;

    public static final String TITLE="计算器";
}

四、对软件进行测试,其测试截屏如下:

posted on 2023-10-15 12:32  小福泥sxx  阅读(43)  评论(0编辑  收藏  举报