合作编程

结对编程1-模块化

合作成员:
兰天宇(201421122091)代码仓库点击此处
郭文茜(201421122005)代码仓库点击此处

需求分析

  • 选择语言功能
  • 在生成题目后开始计时,并在提交题目后停止计时并统计正误数量

程序设计

选择语言

1212395-20171022163054006-534242102.jpg

计时器

1212395-20171022163101646-190616196.jpg


代码说明

HTML代码

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title>Mathematics</title>
        <style type="text/css">
            #sub {
                display: block;
            }
        </style>
        <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet">
    </head>

    <body>
        <div class="container">
            <div class="form-group">
                <label for="">Number of question</label>
                <input class="form-control" type="number" id="ex_n" name="ex_n" />
            </div>
            <div class="form-group">
                <label for="">Range of value</label>
                <input class="form-control" type="number" id="ex_r" name="ex_r" />
            </div>
            <button id="confirm" class="btn btn-primary" type="button" onclick="subHandle()">Confirm</button>
            <div id="right_num">Correct number:<span>0</span></div>
            <div id="wrong_num">Wrong number:<span>0</span></div>
            <div id="timer">Time:00:00:00</div>
            <table id="exer" class="table table-striped">
                <tr>
                    <th>Question</th>
                    <th>Answer</th>
                    <th>Correct/Wrong</th>
                </tr>
            </table>
        </div>
        <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
        <script type="text/javascript">
            //js代码详见下方
        </script>
    </body>

</html>

JS代码

  • 计时器对象
var timer = {
    //时分秒属性
    sec: '0' + 0,
    min: '0' + 0,
    hou: '0' + 0,
    //开始计时方法
    startTimer: function() {
        var timeShow = document.getElementById('timer');
        this.sec = '0' + 0, this.min = '0' + 0, this.hou = '0' + 0;
        this.sec = '0' + 1;
        //定义全局定时触发器
        setTimer = setInterval(function() {
            timeShow.innerText = 'Time:' + this.hou + ':' + this.min + ':' + this.sec;

            this.sec++;
            //时分秒换算
            if(this.sec == 60) {
                this.sec = '0' + 0;
                this.min++;
            }
            if(this.min == 60) {
                this.min = '0' + 0;
                this.hou++;
            }
            this.hou < 10 && this.hou.toString().length < 2 ? this.hou = '0' + this.hou : this.hou = this.hou;
            this.min < 10 && this.min.toString().length < 2 ? this.min = '0' + this.min : this.min = this.min;
            this.sec < 10 && this.sec.toString().length < 2 ? this.sec = '0' + this.sec : this.sec = this.sec;
        }.bind(this), 1000);
    },
    //停止计时方法
    stopTimer: function() {
        clearInterval(setTimer);
    }

}
  • 生成题目方法
function subHandle() {

    document.querySelector('#exer').innerHTML = '<tr><th>Question</th><th>Answer</th><th>Correct/Wrong</th></tr>';
    //保存题目数
    var n = document.querySelector('#ex_n').value;
    //保存范围数
    var r = document.querySelector('#ex_r').value;
    //存放四则运算符
    var ope = ['+', '-', '/', '*'];
    //判断用户输入信息是否符合要求
    if(parseInt(n) == n && parseInt(r) == r && n <= 10000 && r > 0 && n > 0) {
        document.getElementById('confirm').style.display = 'none';
        var current_n = 0;
        while(current_n < n) {
            //随机生成运算符数量1~3
            var ope_count = Math.round(Math.random() * 2) + 1;
            //根据运算符数量随机某生成括号的位置
            var bra_position = Math.round(Math.random() * ope_count);
            //存放算式的数
            var number = [];
            //根据运算符数量生成相应数量的数字并存入数组中
            for(var i = 0; i < ope_count + 1; i++) {
                number.push(Math.ceil(Math.random() * r))
            }
            //根据运算符数量不同,生成算式,或者带括号的算式
            if(ope_count == 1) {
                var equ = number.shift() + ope[Math.round(Math.random() * 3)] + number.shift();
            } else if(ope_count == 2) {
                switch(bra_position) {
                    case 0:
                        var equ = number.shift() + ope[Math.round(Math.random() * 3)] + number.shift() + ope[Math.round(Math.random() * 3)] + number.shift();
                        break;
                    case 1:
                        var equ = '(' + number.shift() + ope[Math.round(Math.random() * 3)] + number.shift() + ')' + ope[Math.round(Math.random() * 3)] + number.shift();
                        break;
                    case 2:
                        var equ = number.shift() + ope[Math.round(Math.random() * 3)] + '(' + number.shift() + ope[Math.round(Math.random() * 3)] + number.shift() + ')';
                        break
                }

            } else if(ope_count == 3) {
                switch(bra_position) {
                    case 0:
                        var equ = number.shift() + ope[Math.round(Math.random() * 3)] + number.shift() + ope[Math.round(Math.random() * 3)] + number.shift() + ope[Math.round(Math.random() * 3)] + number.shift();
                        break;
                    case 1:
                        var equ = '(' + number.shift() + ope[Math.round(Math.random() * 3)] + number.shift() + ')' + ope[Math.round(Math.random() * 3)] + number.shift() + ope[Math.round(Math.random() * 3)] + number.shift();
                        break;
                    case 2:
                        var equ = number.shift() + ope[Math.round(Math.random() * 3)] + '(' + number.shift() + ope[Math.round(Math.random() * 3)] + number.shift() + ')' + ope[Math.round(Math.random() * 3)] + number.shift();
                        break;
                    case 3:
                        var equ = number.shift() + ope[Math.round(Math.random() * 3)] + number.shift() + ope[Math.round(Math.random() * 3)] + '(' + number.shift() + ope[Math.round(Math.random() * 3)] + number.shift() + ')';
                        break;
                }

            }
            //将算式以及输入框等拼接插入标签中
            var strHtml = '<tr class="exercise"><td>Question-' + (current_n + 1) + '.&nbsp;<label>' + equ + '</label>=</td><td><input class="form-control" style="width:100px" type="text"/></td><td><span class="res right" style="color:green; display:none;">Correct</span><span class="res wrong" style="color:red; display:none;">Wrong</span></td></tr>'
            document.querySelector('#exer').innerHTML += strHtml;
            current_n++;
        }
        document.querySelector('#exer').innerHTML += '<button id="sub" class="btn btn-success" type="button" onclick="checkHandle()">Submit</button>';
        timer.startTimer();
    } else {
        alert('Please enter the question number and range of value correctly, the number of questions must be less than 10000, the range of value must be a natural number');
    }
}
  • 提交答案函数
function checkHandle() {
    document.getElementById('confirm').style.display = 'inline-block';
    timer.stopTimer();
    var exer = document.getElementsByClassName('exercise');
    for(i in exer) {
        if((typeof exer[i]) == 'object') {
            //获取答题框内容
            var ans = exer[i].getElementsByTagName('input')[0].value;
            if(ans.indexOf('\'') != -1 && ans.indexOf('/') != -1) {
                var arr = ans.split('\'');
                var result = parseInt(arr[0]) + eval(arr[1]);
                if(result == eval(exer[i].getElementsByTagName('label')[0].innerText)) {
                    rightFun(exer[i]);
                } else {
                    wrongFun(exer[i]);
                }

            } else if(ans.indexOf('/') != -1) {
                if(eval(ans) == eval(exer[i].getElementsByTagName('label')[0].innerText)) {
                    rightFun(exer[i]);
                } else {
                    wrongFun(exer[i]);
                }
            } else {
                if(ans == eval(exer[i].getElementsByTagName('label')[0].innerText)) {
                    rightFun(exer[i]);
                } else {
                    wrongFun(exer[i]);
                }
            }
        }
    }
    $('#right_num span').text($('span.right:visible').length);
    $('#wrong_num span').text($('span.wrong:visible').length);
}
  • 正误行为函数
//正确行为函数
function rightFun(ele) {
    ele.getElementsByClassName('right')[0].style.display = 'inline';
    ele.getElementsByClassName('wrong')[0].style.display = 'none';
    ele.style.borderColor = 'green';
}
//错误行为函数
function wrongFun(ele) {
    ele.getElementsByClassName('right')[0].style.display = 'none';
    ele.getElementsByClassName('wrong')[0].style.display = 'inline';
    ele.style.borderColor = 'red';
}

运行测试

  • 选择语言
    1212395-20171022163113349-527171333.jpg
    1212395-20171022163119709-868722184.jpg

  • 生成题目
    1212395-20171022163127177-715918332.jpg

  • 提交答案
    1212395-20171022163133927-1822076596.jpg


小结感受

先说结论:合作编程的效率是1+1<2。无论两个人在事前有着多么深入的讨论、规约,两个人的默契程度是不可能比得上单独一个人的,而且随着人数增多,这个效率会越来越低,比如1+1=1.9,那么1+1+1=2.8。人和人之间是必然存在分歧的,正所谓求同存异。所以我认为合作编程的意义在于1+1>1以及更多的想法,更多的可能性,只要比一个人做的时候效率高就好了。


伙伴评价

编程功底不是很强,但是在查阅资料和指导下还是写出了计时器的功能,不得不说很有学习天分。在要考研的严峻条件下还是能挤出事件完成这次的编程,真的很感人。


PSP

PSP2.1Personal Software Process StagesTime Senior Student(min)Time(min)
Planning计划55
Estimate估计这个任务需要多少时间11
Development开发180240
Analysis需求分析 (包括学习新技术)11
Design Spec生成设计文档21
Design Review设计复审52
Coding Standard代码规范11
Design具体设计1015
Coding具体编码120150
Code Review代码复审53
Test测试(自我测试,修改代码,提交修改)3060
Reporting报告3035
测试报告2025
计算工作量55
并提出过程改进计划55

转载于:https://www.cnblogs.com/esby/p/7710566.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值