具有非重复操作jQuery的计算器



我想使用jQuery制作一个计算器,它不允许用户在输入两个操作之后输入,如--或++或**或//我希望计算器将值重置为"<--空,同时我也不希望用户首先输入操作,所以在操作之前必须有一个数字。

这是我的代码,请帮我

注意:我是一个初学者,请原谅我这个原始的问题。

<html>
    <head>
        <script>
        var expr="";
        function keypressed(pressedKey){
            expr+=pressedKey;
            document.getElementById("output").value = expr;
        }
        function printresult(){
            var res = eval(expr);
            expr +="=";
            expr +=res;
            document.getElementById("output").value = expr;
        }
        </script>
    </head>
<body>
<table border="1">
    <form>
        <tr>
            <td colspan="4" align="center">
                <input type="text" id="output" size="15">
            </td>
        </tr>
        <tr>
            <td><input type="button" value="1" onclick='keypressed("1");
                '></td>
        <td><input type="button" value="2" onclick='keypressed("2");
            '></td>
            <td><input type="button" value="3" onclick='keypressed("3");
                '></td>
            <td><input type="reset" value="C" onclick='expr="";
                '></td>
        </tr>
        <tr>
            <td><input type="button" value="4" onclick='keypressed("4");
                '></td>
            <td><input type="button" value="5" onclick='keypressed("5");
                '></td>
            <td><input type="button" value="6" onclick='keypressed("6");
            '></td>
            <td><input type="button" value="*" onclick='keypressed("*");
                '></td>
        </tr>
        <tr>
            <td><input type="button" value="7" onclick='keypressed("7");
                '></td>
            <td><input type="button" value="8" onclick='keypressed("8");
                '></td>
            <td><input type="button" value="9" onclick='keypressed("9");
                '></td>
            <td><input type="button" value="-" onclick='keypressed("-");
                '></td>
        </tr>
        <tr>
            <td><input type="button" value="." onclick='keypressed(".");
                '></td>
            <td><input type="button" value="0" onclick='keypressed("0");
                '></td>
            <td><input type="button" value="/" onclick='keypressed("/");
                '></td>
            <td><input type="button" value="+" onclick='keypressed("+");
                '></td>
        </tr>
        <tr>
            <td colspan="4" align="center">
                <input type="button" value="         =         "          onclick='printresult();'>
            </td>
        </tr>
    </form>
</table>
</body>
</html> 

我做了一把小小提琴,也许它有帮助:https://jsfiddle.net/y21ghazh/

我添加了两个新功能:

function isNumeric(n) {
  var matches = n.match(/d+/g);
  if (matches != null) {
      return true;
  }
  return false; //No number
}
function checkDuplicateOperator(expr, nextChar)
{
    if(expr.length <= 0)
        return true;
    //Check if the last char of the expression is a non numeric(+,-,/ etc..) value AND
    //If the next char is a non numeric value => This would result in ++ or +- or +/ ... abort!
    if(!isNumeric(expr[expr.length-1]) && !isNumeric(nextChar))
    return false;
  return true;
}

第一个检查字符串中是否有数字字符。需要检查表达式的第一个字符是否是数字,以及检查表达式的最后一个字符是否为数字。

第二个函数检查最后一个charcated是否是像+-/*这样的运算符。如果是,则返回false并且没有


让我解释一下:您的第一个字符必须是数字,并且您不希望使用双运算符:因此,您必须丢弃keypressed()函数中的密钥:

伪代码:

//expr is empty and first char is NOT NUMERIC => ABORT!
if(expr.length == 0 AND pressedKey != NUMERIC)
 return; //Abort
else
{
    //pressed key is an operator and the last char of expr is an operator? ==> ABORT!
    if(pressedKey is OPERATOR AND expr.LastKey is OPERATOR)
        return; //Abort
    else
    {
        expr += pressedKey;
    }
}

最新更新