Javascript用户界面计算器
我正在尝试制作一个带有HTML和Javascript用户界面的计算器。我已经修复了很多错误,但仍然存在一些问题,例如:如果第二个运算符超过一位数,则答案是错误的,除法不起作用。我是一个新手,我知道我的代码效率很低,300行太长,但有人能幽默我并解释我的错误吗Javascript用户界面计算器,javascript,html,css,syntax,calculator,Javascript,Html,Css,Syntax,Calculator,我正在尝试制作一个带有HTML和Javascript用户界面的计算器。我已经修复了很多错误,但仍然存在一些问题,例如:如果第二个运算符超过一位数,则答案是错误的,除法不起作用。我是一个新手,我知道我的代码效率很低,300行太长,但有人能幽默我并解释我的错误吗 var resultline_str=”“; var多位数=0 变量空间=0 var操作=[]; 函数类型1(){ 结果行_str+=“1”; document.getElementById('result').innerHTML=res
var resultline_str=”“;
var多位数=0
变量空间=0
var操作=[];
函数类型1(){
结果行_str+=“1”;
document.getElementById('result').innerHTML=resultline\u str;
如果(多位数===0){
操作推送(1);
多位数=1;
}否则{
如果(空格===0){
var n=运行[0]
n=正在运行[0]。toString();
n+='1';
n=parseInt(n);
操作[0]=n;
}否则{
var n=运行[2]
n=操作[2]。toString();
n+='1';
n=parseInt(n);
操作[2]=n;
};
};
};
函数类型2(){
结果行_str+=“2”;
document.getElementById('result').innerHTML=resultline\u str;
如果(多位数===0){
操作推送(2);
多位数=1;
}否则{
如果(空格===0){
var n=运行[0]
n=正在运行[0]。toString();
n+='2';
n=parseInt(n);
操作[0]=n;
}否则{
var n=运行[2]
n=操作[2]。toString();
n+='2';
n=parseInt(n);
操作[2]=n;
};
};
};
函数类型3(){
结果行_str+=“3”;
document.getElementById('result').innerHTML=resultline\u str;
如果(多位数===0){
操作推送(3);
多位数=1;
}否则{
如果(空格===0){
var n=运行[0]
n=正在运行[0]。toString();
n+='3';
n=parseInt(n);
操作[0]=n;
}否则{
var n=运行[2]
n=操作[2]。toString();
n+='3';
n=parseInt(n);
操作[2]=n;
};
};
};
函数类型4(){
结果行_str+=“4”;
document.getElementById('result').innerHTML=resultline\u str;
如果(多位数===0){
操作推送(4);
多位数=1;
}否则{
如果(空格===0){
var n=运行[0]
n=正在运行[0]。toString();
n+='4';
n=parseInt(n);
操作[0]=n;
}否则{
var n=运行[2]
n=操作[2]。toString();
n+='4';
n=parseInt(n);
操作[2]=n;
};
};
};
函数类型5(){
结果行_str+=“5”;
document.getElementById('result').innerHTML=resultline\u str;
如果(多位数===0){
操作推送(5);
多位数=1;
}否则{
如果(空格===0){
var n=运行[0]
n=正在运行[0]。toString();
n+='5';
n=parseInt(n);
操作[0]=n;
}否则{
var n=运行[2]
n=操作[2]。toString();
n+='5';
n=parseInt(n);
操作[2]=n;
};
};
};
函数类型6(){
结果行_str+=“6”;
document.getElementById('result').innerHTML=resultline\u str;
如果(多位数===0){
操作推送(6);
多位数=1;
}否则{
如果(空格===0){
var n=运行[0]
n=正在运行[0]。toString();
n+='6';
n=parseInt(n);
操作[0]=n;
}否则{
var n=运行[2]
n=操作[2]。toString();
n+='6';
n=parseInt(n);
操作[2]=n;
};
};
};
函数类型7(){
结果行_str+=“7”;
document.getElementById('result').innerHTML=resultline\u str;
如果(多位数===0){
操作推送(7);
多位数=1;
}否则{
如果(空格===0){
var n=运行[0]
n=正在运行[0]。toString();
n+='7';
n=parseInt(n);
操作[0]=n;
}否则{
var n=运行[2]
n=操作[2]。toString();
n+='7';
n=parseInt(n);
操作[2]=n;
};
};
};
函数类型8(){
结果行_str+=“8”;
document.getElementById('result').innerHTML=resultline\u str;
如果(多位数===0){
操作推送(8);
多位数=1;
}否则{
如果(空格===0){
var n=运行[0]
n=正在运行[0]。toString();
n+='8';
n=parseInt(n);
操作[0]=n;
}否则{
var n=运行[2]
n=操作[2]。toString();
n+='8';
n=parseInt(n);
操作[2]=n;
};
};
};
函数类型9(){
结果行_str+=“9”;
document.getElementById('result').innerHTML=resultline\u str;
如果(多位数===0){
操作推送(9);
多位数=1;
}否则{
如果(空格===0){
var n=运行[0]
n=正在运行[0]。toString();
n+='9';
n=parseInt(n);
操作[0]=n;
}否则{
var n=运行[2]
n=操作[2]。toString();
n+='9';
n=parseInt(n);
操作[2]=n;
};
};
};
函数类型_plus(){
结果line_str+=“+”;
document.getElementById('result').innerHTML=resultline\u str;
操作推送(+);
多位数=0;
地点=2;
};
函数类型_减号(){
结果行_str+=“-”;
document.getElementById('result').innerHTML=resultline\u str;
操作推送('-');
多位数=0;
地点=2;
};
函数等于(){
如果(工作长度!=3){
document.getElementById('result')。innerHTML='Error';
多位数=0;
地点=0;
返回;
}否则{
如果(操作[1]=='+'){
resultline_str=resultline_str+'
'+(parseInt(操作[0])+parseInt(操作[2])+'
';
}else if(操作[1]='-'){
resultline_str=resultline_str+'
'+(parseInt(操作[0])-parseInt(操作[2])+'
';
}else if(操作[1]=='*'){
resultline_str=resultline_str+'
'+(parseInt(操作[0])*pa
<div id="head">
<h1>Calculator
<h1>
</div>
<div id="grid">
<!-- calculator buttons -->
<table>
<tr>
<td id="1" onclick='typeNumber(1)'>1</td>
<td id="2" onclick='typeNumber(2)'>2</td>
<td id="3" onclick='typeNumber(3)'>3</td>
</tr>
<tr>
<td id="4" onclick='typeNumber(4)'>4</td>
<td id="5" onclick='typeNumber(5)'>5</td>
<td id="6" onclick='typeNumber(6)'>6</td>
</tr>
<tr>
<td id="7" onclick='typeNumber(7)'>7</td>
<td id="8" onclick='typeNumber(8)'>8</td>
<td id="9" onclick='typeNumber(9)'>9</td>
</tr>
<tr>
<td id="=" onclick='equals()'>=</td>
<td id="+" onclick='type_plus()'>+</td>
<td id="-" onclick='type_minus()'>-</td>
</tr>
<tr>
<td id="times" onclick='times()'>X</td>
<td id="clear" onclick='clear_line()'>C</td>
<td id="divide" onclick='divide()'>%</td>
</tr>
</table>
</div>
<div id="result">
<b></b>
</div>
</div>
function typeNumber(num) {
resultline_str += " " + num;
document.getElementById('result').innerHTML = resultline_str;
if (multidigit === 0) {
operating.push(num);
multidigit = 1;
} else {
if (space === 0) {
var n = operating[0]
n = operating[0].toString();
n += num;
n = parseInt(n);
operating[0] = n;
} else {
var n = operating[2]
n = operating[2].toString();
n += num;
n = parseInt(n);
operating[2] = n;
};
};
};
function divide() {
resultline_str += " /";
document.getElementById('result').innerHTML = resultline_str;
operating.push('/');
multidigit = 0;
place = 2;
};
<td id="1" onclick='getInput(this)'>1</td>
function getInput(el){
var value = el.id;
var num, op;
num = parseInt(value);
if(!isNaN(value)){
// it is a number
// handle numbers
} else {
// it is an operator
op = value;
// handle operators
}
}
function type9() {
// we got a digit 9 as a string, concatenate it to the resultstring
resultline_str += " 9";
// and "print" it
document.getElementById('result').innerHTML = resultline_str;
// the first digit doesn't get any special treatment, just
// shoved on the stack
if (multidigit === 0) {
operating.push(9);
// flag that we already have one digit on the stack
multidigit = 1;
} else {
// space is alwys 0, it never gets changed anywhere
// so only the first branch runs, the "else" is never reached
if (space === 0) {
// put the value of the zeroth element of "operating" into n
var n = operating[0];
// overwrite(!) that with the zeroth element of "operating"
// cast to a string
n = operating[0].toString();
// concatenate the digit 9 as a string to the end of "n"
n += '9';
// make an integer Number out of it
n = parseInt(n);
// put that number back to the zeroth place in "operators
operating[0] = n;
}
// there is no "else" hence no second operator, everything
// get shoved into the first one
else {
var n = operating[2]
n = operating[2].toString();
n += '9';
n = parseInt(n);
operating[2] = n;
}
}
}
function type_plus() {
// print the operator
resultline_str += " +";
document.getElementById('result').innerHTML = resultline_str;
// add the operator to the end of "operating" as a string
operating.push('+');
// reset the flag that counts the digits
multidigit = 0;
// set "place" to the value 2 as an integer
place = 2;
}
function equals() {
// check for errors: always a good idea!
if (operating.length != 3) {
document.getElementById('result').innerHTML = 'Error';
// reset everything
multidigit = 0;
place = 0;
return;
} else {
// iterate over the possible operators
if (operating[1] === '+') {
resultline_str = resultline_str + '<br/>'
// do the actual addition
// put it in parenthesis to avoid automatic conversion
// to a string
+ (parseInt(operating[0]) + parseInt(operating[2]))
+ '<br/>';
} else if {
// let me skip the rest, ok?
}
}
// print the result
document.getElementById('result').innerHTML = resultline_str;
// reset all variables
operating = [];
multidigit = 0;
place = 0;
}