Javascript 联机计算器错误检查
我制作了一个在线计算器程序,但遇到了一些错误检查问题。这就是我想让我的程序检查的内容: 1.不应连续添加两个运算符。 2.方程式不应从除负之外的运算符开始。 3.一个数字中的小数点不应超过一个 任何帮助完成这项任务的人都将不胜感激 下面是我的JavaScript代码部分。如果有人需要我的HTML部分,请询问。不过,我认为现在没有必要这么做Javascript 联机计算器错误检查,javascript,Javascript,我制作了一个在线计算器程序,但遇到了一些错误检查问题。这就是我想让我的程序检查的内容: 1.不应连续添加两个运算符。 2.方程式不应从除负之外的运算符开始。 3.一个数字中的小数点不应超过一个 任何帮助完成这项任务的人都将不胜感激 下面是我的JavaScript代码部分。如果有人需要我的HTML部分,请询问。不过,我认为现在没有必要这么做 <script type="text/javascript"> // Addings operators var operatorEl
<script type="text/javascript">
// Addings operators
var operatorEl = document.getElementsByClassName('operator');
// Adding btnElements
var btnElements = document.getElementsByClassName('addme');
//var to store user input and button input
var inputVal = '';
var buttonVal = '';
var display = document.getElementById('display');
var errorMsg = document.getElementById('error-msg');
function clearContent(){
display.value = '';
inputVal = '';
buttonVal = '';
errorMsg.innerHTML = '';
}
operatorEl[0].onclick = clearContent;
function backBtn() {
var dvLength = buttonVal.length;
buttonVal = buttonVal.substring(0,dvLength - 1);
display.value = buttonVal;
}
operatorEl[1].onclick = backBtn;
function calcCos(){
inputVal = display.value;
var newVal = Math.cos(inputVal);
display.value = newVal;
}
operatorEl[2].onclick = calcCos;
function calcSin() {
inputVal = display.value;
var newVal = Math.sin(inputVal);
display.value = newVal;
}
operatorEl[3].onclick = calcSin;
function calcSqrt(){
inputVal = display.value;
if(inputVal < 0){
errorMsg.innerHTML = 'Cannot calculate the square root of a negative number.';
display.value = '';
//return false;
}
var newVal = Math.sqrt(inputVal);
display.value = newVal;
}
operatorEl[4].onclick = calcSqrt;
function calcLog(){
inputVal = display.value;
if(inputVal < 1){
errorMsg.innerHTML = 'Cannot calculate the log of a non-positive number.';
display.value = '';
//return false;
}
var newVal = Math.log(inputVal);
display.value = newVal;
}
operatorEl[5].onclick = calcLog;
//When clicked the button's value will be added to the buttonVal and put into display
for(var i = 0; i < btnElements.length; i++){
btnElements[i].onclick = function (){
buttonVal += this.value;
display.value = buttonVal;
}
}
function evaluate(){
var evaluatedOutput = eval(buttonVal);
display.value = evaluatedOutput;
}
operatorEl[6].onclick = evaluate;
</script>
//加法运算符
var operatorEl=document.getElementsByClassName('operator');
//添加元素
var btnElements=document.getElementsByClassName('addme');
//var来存储用户输入和按钮输入
var inputVal='';
var buttonVal='';
var display=document.getElementById('display');
var errorMsg=document.getElementById('error-msg');
函数clearContent(){
display.value='';
inputVal='';
按钮瓦尔=“”;
errorMsg.innerHTML='';
}
operatorEl[0]。onclick=clearContent;
函数backBtn(){
var dvLength=按钮瓦尔长度;
buttonVal=buttonVal.子字符串(0,dvLength-1);
display.value=buttonVal;
}
运算符rel[1]。onclick=backBtn;
函数calcCos(){
inputVal=display.value;
var newVal=Math.cos(inputVal);
display.value=newVal;
}
operatorEl[2]。onclick=calcCos;
函数calcSin(){
inputVal=display.value;
var newVal=Math.sin(inputVal);
display.value=newVal;
}
operatorEl[3]。onclick=calcSin;
函数calcSqrt(){
inputVal=display.value;
如果(输入值<0){
errorMsg.innerHTML='无法计算负数的平方根';
display.value='';
//返回false;
}
var newVal=Math.sqrt(inputVal);
display.value=newVal;
}
operatorEl[4]。onclick=calcSqrt;
函数calcLog(){
inputVal=display.value;
如果(输入值<1){
errorMsg.innerHTML='无法计算非正数的日志';
display.value='';
//返回false;
}
var newVal=Math.log(inputVal);
display.value=newVal;
}
operatorEl[5]。onclick=calcLog;
//单击按钮时,按钮的值将添加到buttonVal并显示
对于(变量i=0;i
我的假设是,检查这3个条件的代码将出现在这里的某个地方:
//When clicked the button's value will be added to the buttonVal and put into display
for(var i = 0; i < btnElements.length; i++){
btnElements[i].onclick = function (){
buttonVal += this.value;
display.value = buttonVal;
}
}
//单击按钮时,按钮的值将添加到buttonVal并显示出来
对于(变量i=0;i
然而,如何在我的程序中真正清楚地表达这一点让我不知所措。提前感谢大家。您应该使用标志来实现这些目的,这是最简单的方法
- 使用一个在输入第一个字符后设置为false,在等号后再次设置为true的标志
- 输入一个运算符后,使用一个设置为false的标志,如果下一个字符不是运算符,则再次设置为true。问题:
23+-3
- 在第一个小数点处使用一个设置为false的标志,在最后一个数字(运算符或等号)后再次使用一个设置为true的标志