PSP四则运算

PSP

PSP2.1

Personal Software Process Stages

预估耗时(分钟)

实际耗时(分钟)

Planning

计划

10

20

· Estimate

· 估计这个任务需要多少时间

10

10

Development

开发

360

600

· Analysis

· 需求分析 (包括学习新技术)

30

40

· Design Spec

· 生成设计文档

30

40

· Design Review

· 设计复审 (和同事审核设计文档)

10

15

· Coding Standard

· 代码规范 (为目前的开发制定合适的规范)

5

5

· Design

· 具体设计

40

80

· Coding

· 具体编码

300

500

· Code Review

· 代码复审

60

120

· Test

· 测试(自我测试,修改代码,提交修改)

180

120

Reporting

报告

120

60

· Test Report

· 测试报告+博客

120

120

· Size Measurement

· 计算工作量

10

10

· Postmortem & Process Improvement Plan

· 事后总结, 并提出过程改进计划

40

30

合计

1325

1770

项目要求

  • 能自动生成小学四则运算题目

解题思路

  • 了解四则运算的基本法则
  • 利用随机函数随机生成数字以及运算符
  • 用户输入答案程序需要判断答案是否正确

设计实现及代码说明

贴上java实训100以内加法运算代码

 1 import javax.swing.*;
 2 import java.awt.*;
 3 import java.awt.Button;
 4 import java.awt.FlowLayout;
 5 import java.awt.Frame;
 6 import java.awt.TextArea;
 7 import java.awt.TextField;
 8 import java.awt.event.ActionEvent;
 9 import java.awt.event.ActionListener;
10 import java.awt.event.WindowAdapter;
11 import java.awt.event.WindowEvent;
12 
13 import javax.swing.JOptionPane;
14 
15 public class De5_30_9 {
16     public static void main(String[] args) {
17         new ShowLayout1();
18     }
19 }
20 
21 class ShowLayout1 extends JFrame {
22     ShowLayout1() {
23         setLayout(null);
24         setBounds(10, 10, 470, 200);
25         Container c = getContentPane();
26         c.setBackground(Color.yellow);
27         init1();
28         setTitle("100以内的加法测试");
29         setVisible(true);
30         setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
31         validate();
32     }
33 
34     void init1() {// 验证码
35         JButton button1 = new JButton("获取题目");
36         JButton button2 = new JButton("确认答案");
37         TextArea t=new TextArea(10,40);
38         JTextField text = new JTextField();
39         JTextField text1 = new JTextField();
40         JTextField text2 = new JTextField();
41         JButton button3 = new JButton("重置");
42         button1.setBounds(12, 12, 100, 25);
43         button2.setBounds(12, 44, 100, 25);
44         button3.setBounds(310, 12, 100, 25);
45         text1.setBounds(112, 12, 180, 25);
46         text2.setBounds(112, 44, 180, 25);
47         add(button1);
48         add(text1);
49         add(text2);
50         add(button2);
51         add(button3);
52         // 设置按钮功能
53 
54         button1.addActionListener(new ActionListener() {
55             public void actionPerformed(ActionEvent e) {
56                 // 获取文本框的内容,并去除字符串前面和后面的空格
57                 int num1 = (int) (Math.random() * 100);
58                 int num2 = (int) (Math.random() * 100);
59                 int sum1 = num1 + num2;
60                 String sum = String.valueOf(sum1);
61                 String timu = num1 + "+" + num2;
62                 text1.setText(timu);
63                 text.setText(sum);
64             }
65         });
66 //重置
67         button3.addActionListener(new ActionListener() {
68             public void actionPerformed(ActionEvent e) {
69                 t.setText(null);
70                 text.setText(null);
71                 text1.setText(null);
72                 text2.setText(null);
73                 // 获取光标,使光标一直在文本框内
74                 text1.requestFocus();
75             }
76         });
77         // 确认答案
78         button2.addActionListener(new ActionListener() {
79             public void actionPerformed(ActionEvent e) {
80                 // 获取文本框的内容,并去除字符串前面和后面的空格
81                 String s = text2.getText().trim();
82                 String sum = text.getText().trim();
83                 if (s.equals(sum)) {
84                     t.setText("答案正确");    
85                     JOptionPane.showMessageDialog(button2, "答案正确", "消息对话框", JOptionPane.WARNING_MESSAGE);
86 
87                 } else {
88                     t.setText("答案错误");
89                     JOptionPane.showMessageDialog(button2, "答案错误", "消息对话框", JOptionPane.WARNING_MESSAGE);
90                 }
91             }
92         });
93     }
94 }

运行结果如图

PSP四则运算思路

 

思路:四则运算加减乘除,采用两个随机数,由于不能出现负数,所以在对两个随机数进行减法运算的时候,需要进行比较大小,而除法在运算中,除数不能取0。

实现过程

定义函数:用def szys()实现随机生成四则运算,用def test()通过调用syzs()实现题库的制作.

所以代码分为三个部分,第一部分是生成随机四则运算,最后输出算式并返回正确答案;第二部是用户通过输入一个整数来输出所需要的算式,当输出的算式达到目标数量后,便一一输出答案;第三部分为主函数,需要用户自己选择模式,当输入是‘1’时,是为四则运算,通过调用syzs()函数得到算式和返回值,用户输入答案后,便与用户输入值进行比较。当输入是‘2’时,是生成题库,并在最后输出答案。

代码如下:

import profile
import random
from fractions import Fraction

#四则运算

def szys():

    sym = ['', '', '×', '÷']

    f= random.randint(0, 3)

    z = random.randint(0, 1)#设置一个随机值,如果是1就进行整数运算,如果是0进行分数运算

    n1 = random.randint(1, 20)

    n2 = random.randint(1, 20)

    n3 = random.randint(1, 20)

    n4 = random.randint(1, 20)

    result = 0

    if z==0 :

        n1, n2 = max(n1, n2), min(n1, n2)

        if f == 0:#加法

            result  = n1 + n2

        elif f == 1:#减法,要先比较大小,防止输出负数

            n1, n2 = max(n1, n2), min(n1, n2)

            result  = n1 - n2

        elif f== 2:#乘法

            result  = n1 * n2

        elif f == 3:#除法,要比较大小,并循环取整除

            n1, n2 = max(n1, n2), min(n1, n2)

            while n1 % n2 != 0:

                n1 = random.randint(1, 10)

                n2 = random.randint(1, 10)

                n1, n2 = max(n1, n2), min(n1, n2)

            result  = int(n1 / n2)

        print(n1, sym[f], n2, '= ', end='')

        return result

    if z == 1:

        n1, n2 = min(n1, n2), max(n1, n2)#把n1,n2中小的放在前面,保证f1为真分数

        n3, n4 = min(n3, n4), max(n3, n4)#把n3,n4中小的放在前面,保证f2为真分数

        f1 = Fraction(n1, n2)#初始化f1为n1/n2

        f2 = Fraction(n3, n4)#初始化f2为n3/n4

        if f == 0:#加法

            result  = f1 + f2

        elif f == 1:#减法,要先比较大小,防止输出负数

            f1, f2 = max(f1, f2), min(f1, f2)

            result  = f1 - f2

        elif f== 2:#乘法

            result  = f1 * f2

        elif f == 3:#除法,要比较大小,并循环取整除
            if n1<n2:
                result=0
            
        else:
            result=int(n1/n2)
            
        print(f1, sym[f], f2, '= ', end='')
        return result
    
print('输入go进行四则运算')
print('输入test进行性能测试')

n=input()

#当输入go时,进行四则运算,调用函数syzs()

if n=="go":

    while True:

        result  = szys()

        j= input()

        try:
            if int(j)== result :
                print('正确')
            else:
                print('错误,正确答案是', result )
        except:
            print("出现异常,结束运算")
            break
if n=="test":
    profile.run('szys()')

 运行结果如图:

原文地址:https://www.cnblogs.com/Adaran/p/15345467.html