结对编程总结:简单的四则运算生成程序

1.题目描述:

          面向小学生,随机生成30个100以内的四则运算,并且用户可以录入结果,检查用户录入的结果是否是正确的。

2.结对编程说明

         结对对象:陈高瑶   博客地址:http://www.cnblogs.com/ccgy/

         项目已同步至GitHub: https://github.com/JackChuahe/pair_program

         双方贡献:2:1

3.本程序描述:

     a. 本程序在原有题目的基础上加以了部分改进:

         1. 随机生成四则运算的个数不受30的限制,用户可以输入数量以确定四则运算的个数。

         2.四则运算的运算值域不受100的限制,用户可以输入具体的数值以确定四则运算的数值范围。

         3.对随机生成表达式的过程做了优化,不会产生任何例如除不尽、减法结果为负数、加法或乘法结果大于规定值。

    b. 本程序符合一定的编码规范,在编码过程中,使用了规范的注释说明,通过文档生成工具,能够自动生成注释说明文档。

4.结对编程照片:

 

5.源程序代码:

package com.jackchua.dijkstra;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

/**
 * @author JackChua
 * @brief This is a program to automatically generate expressions
 * */
public class RationalComputing {

    private static int NUMS = 5; // 表达式个数的默认值
    private static int MAX_VALUE = 100; // 表达式的数值范围的默认值 代表 100 以内
    /** <Const number */

    private static List<Rational> listArr = new ArrayList<Rational>();
    /** <List To store Rational objects */
    private static final String[] OPREATORS = { "+", "-", "*", "/" };
    /** <a constant array to store operators */
    private static Scanner scanner = new Scanner(System.in);

    /** <a scanner to get data from user */

    /**
     * @brief This is the main function of this program
     * @param [in] args
     */
    public static void main(String[] args) {

        getInitMaxValueAndExpNum(); // 用户输入以确定生成几以内的表达式,以及表达式的个数

        int i = NUMS;
        listArr.clear();

        Random random = new Random(System.currentTimeMillis());
        while ((--i) >= 0) {
            int opA = random.nextInt(MAX_VALUE + 1);
            int opB = random.nextInt(MAX_VALUE + 1);
            String com = OPREATORS[random.nextInt(4)];
            boolean isWrong = true;
            // 判断是否减法有负数值存在 存在则必须重新生成
            while (isWrong) {
                if (com == "/" && (opB == 0 || (opA % opB) != 0)) { // 除数不能为0
                                                                    // 或者能被整出
                    opB = random.nextInt(MAX_VALUE + 1);
                } else if (com == "-" && opA < opB) { // 减法不能有负数
                    opA = random.nextInt(MAX_VALUE + 1);
                    opB = random.nextInt(MAX_VALUE + 1);
                } else if (com == "+" && (opA + opB) > MAX_VALUE) { // 运算的值不能大于MAX_VALUE
                    opA = random.nextInt(MAX_VALUE + 1);
                    opB = random.nextInt(MAX_VALUE + 1);
                } else if (com == "*" && (opA * opB) > MAX_VALUE) { // 运算的值不能大于
                                                                    // MAX_VALUE
                    opA = random.nextInt(MAX_VALUE + 1);
                    opB = random.nextInt(MAX_VALUE + 1);
                } else {
                    break;
                }
            }
            Rational tempRa = new Rational(opA, opB, com);
            listArr.add(tempRa);
        }
        //
        ouput(); // 输出
        input(); // 用户输入结果
        outputResult();// 输出用户是否输入正确
    }

    /**
     * @brief to determine the MAX_VLAUE of the expressions
     * @return void
     */
    public static void getInitMaxValueAndExpNum() {

        // 输入表达式计算的数值范围
        while (true) {
            System.out.println("请输入一个大于0 小于等于10000的整数,以确定表达式的数值范围:");
            if (scanner.hasNextInt()) {
                MAX_VALUE = scanner.nextInt();
                if (MAX_VALUE > 0 && MAX_VALUE <= 10000) {
                    break;
                } else {
                    System.out.println("请输入正确的范围!");
                }
            } else {
                System.out.println("请正确输入整数值!");
            }
        }

        // 输入要生成的表达式的个数
        while (true) {
            System.out.println("请输入一个大于0 小于等于100整数,代表自动生成多少个表达式:");
            if (scanner.hasNextInt()) {
                NUMS = scanner.nextInt();
                if (NUMS > 0 && NUMS <= 100) {
                    break;
                } else {
                    System.out.println("请输入正确的范围!");
                }
            } else {
                System.out.println("请正确输入整数值!");
            }
        }

    }

    /**
     * @brief this function to output the result
     * @return void
     */
    public static void outputResult() {
        int i = 1;
        for (Rational re : listArr) {
            if (re.isRight()) {
                System.out.println("第 " + i + "题结果是: 正确的!" + re.getOpA()
                        + re.getCom() + re.getOpB() + " = " + re.getValue());
            } else {
                System.out.println("第 " + i + "题结果是: 错误的!" + re.getOpA()
                        + re.getCom() + re.getOpB() + " = " + re.getValue());
            }

            i++;
        }
    }

    /**
     * @brief this function to get data from user ,than compute the result
     * @return void
     */
    public static void input() {
        int i = 0;
        System.out.println("请依次输入结果");
        for (i = 0; i < listArr.size(); i++) {
            System.out.println("还剩下" + (listArr.size() - i) + "道题");
            int result = 0;
            while (true) {
                if (scanner.hasNextInt()) {
                    result = scanner.nextInt();
                    break;
                } else {
                    scanner.next();
                    System.out.println("请输入有效的值");
                }
            }

            listArr.get(i).computing(result); // 计算本身的结果
        }
    }

    /**
     * @brief to show the expressions which are generated automatically
     * @return void
     */
    public static void ouput() {
        for (Rational ra : listArr) {
            System.out.println(ra.getOpA() + ra.getCom() + ra.getOpB());
        }

    }

}

/**
 * @brief This is a class of model of expression
 * @author JackCai
 *
 */
class Rational {

    private int opA;
    /** <This is the first operator of one expression */
    private int opB;
    /** <This is the second operator of one expression */
    private String com;
    /** <The operator of this expression */
    private int value;
    /** <The right result of this expression */
    private boolean isRight = false;
    /** <to indicate whether right or not */
    private int userValue;

    /** <to indicate the result of user input */

    /**
     * @brief This is the constructor of Rational
     * @param [in] opA The first operator
     * @param [in] opB The second operator
     * @param [in] com The operator of this expression
     * */
    public Rational(int opA, int opB, String com) {

        this.opA = opA;
        this.opB = opB;
        this.com = com;
        computeValue();
    }

    public boolean isRight() {
        return isRight;
    }

    public int getUserValue() {
        return userValue;
    }

    public int getOpA() {
        return opA;
    }

    public int getOpB() {
        return opB;
    }

    public String getCom() {
        return com;
    }

    public int getValue() {
        return value;
    }

    /**
     * @brief This function to compute the right result
     * @return void
     * */
    public void computeValue() {
        switch (com) {
        case "+":
            value = opA + opB;
            break;
        case "-":
            value = opA - opB;
            break;
        case "*":
            value = opA * opB;
            break;
        case "/":
            value = opA / opB;
            break;

        }
    }

    /***
     * @brief to judge whether right or not of user input
     * @param [in] value
     * @return void
     */
    public void computing(int value) {
        this.userValue = value;
        if (userValue == this.value) {
            this.isRight = true;
        }
    }
}

6.运行结果

1. 测试输入生成5个表达式,并计算数据范围为5以内,并且表达式结果都输入正确的情况如下:

 

2. 测试输入生成3个表达式,并计算数据范围为3以内,并且表达式结果输入不全正确的情况运行如下:

3.测试输入无效的表达式个数(表达式个数<= 0 或者 大于 100) ,及无效的计算数据范围(<=0 或者大于 10000)。

总结:

    总得来说这次的结对编程程序比较简单,也容易完成。但考虑到是结对编程,和学习了编码规范的相关知识,所以我们在原有基础上做了一些增加,并且编码也符合一定的规         范。结对编程的整个过程,也相对的轻松和愉快,在完成过程中,大家互相交流和讨论,做到及时纠正问题等。

    同时附:由Doxygen 自动生成的注释说明文档截图

 

转载于:https://www.cnblogs.com/compilers/p/5370291.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值