나는 프로젝트의 일종으로 계산기를 만들려고 노력 중이다. 기본적으로 배열의 모든 사용자 클릭 수 (+와 - 같은 숫자와 연산자 일 수 있음)를 저장하고 사용자가 "="을 클릭하면 다른 함수에 배열을 건네줍니다. 이 자바 스크립트를 사용하고 있습니다. 기본적인 수학에 대한계산기 변형을위한 자바 스크립트
<button class="num" onclick="getval(0)">0</button>
<button class="num" onclick="getval(1)">1</button>
<button class="num" onclick="getval(2)" >2</button>
<button class="num" onclick="getval(3)" >3</button>
......................... and so on
이 코드는 잘 작동하고 문제가되지 않습니다 :이 같은 버튼이 있습니다 HTML을
var arr=[]; //array of every input from the user interface stored
var i=0; //number of input clicks from the user
var opPos=[]; //position of the operator as given by the user
var operAnd=[];//operands as given by the user
var disp='';
function clearval() {
// This function clears all the values stored in related array when C or CE is pressed
document.getElementById("op").value=0;
arr=[];
i=0;
opPos=[];
operAnd=[];
disp='';
}
//This Function get the value and supplies the array to calculating function
function getval(inp){
if(inp!="=")
{
arr[i]=inp;
disp=disp+inp;//for display in the screen of the output screen
document.getElementById("op").value=disp;
if (typeof inp!="number"){
opPos.push(i);
operAnd.push(inp);
}
i++;
}
else
{
var newInput=assembler(arr,opPos,operAnd);
clearval();
getval(parseFloat(newInput,10));
}
}
//<!------This Function calculates based on array, operator position and operands------!>
function assembler(num_array,opPos,operAnd){
var num='';
var numCollect=[];
var posCount=0;
for(var j=0;j<num_array.length;j++){
if (j==opPos[posCount]) {
numCollect.push(parseFloat(num,10));
num='';
j++;
posCount++;
}
else if (j>posCount) {
}
num=num+num_array[j];
}
num=parseFloat(num,10);
numCollect.push(num);
//document.getElementById("op").value= numCollect;
var newInput=calculator(numCollect,operAnd);
return newInput;
}
function calculator(target_num,operAnd) {
// Not the nice solution but straightforward nonetheless
var result='';
for (var l=0;l<operAnd.length;l++) {
result=result+target_num[l]+operAnd[l];
}
result=result+target_num[l];
document.getElementById("op").value=result + '=' + eval(result) ;
return eval(result);
}
: 아래는 내 코드입니다. 그러나 여기에 구현하는 방법을 생각하는 데 어려움을 겪고있는 곳에서 내 문제가 있습니다. 예를 들어, 다음과 같은 버튼이 있습니다.
<button class='extra' onclick="Regression()"> Find Regression </button>
지금, 입력 회귀 유형을 사용자에게 요청한다 회귀 함수를 가질 것이다 (2 차 등등 1 선형 ... 이것은 단지 일례이다).
function regression(){
clearval();
document.getElementById("op").value=" Enter the degree of regression:";
기본적으로 사용자가 번호를 입력하고 '='을 클릭하여 프로그램에 입장하도록 요청합니다. 이제 내 딜레마를 볼 수 있습니다. 사용자가 입력 한 내용은 먼저 getval()에 의해 처리됩니다.이 getval()은 사용자가 '='을 클릭하면 배열을 다른 함수로 전달합니다.이 경우에는 내가 원하지 않습니다. 분명히 말하자면, 이런 종류의 경우에 나는 std와 같은 많은 것을 가질 것입니다. dev 또는 이런 종류의 함수를 사용하고 싶다면 키패드가 일반 키패드처럼 작동하고 정상적인 계산기 작업을 수행하지 않고 다른 기능에 값을 전달해야합니다.
나는이 똑바로 생각하고 있니? 어떤 제안?
이것은 실제로 볼 때까지 좋은 방법입니다. 하지만 다시 내 무지를 용서해, 배열에 좋은 버튼을 생각할 수 없다 ... var 버튼 ... 어떻게 할 수 있습니까? –
이 말은 sth를 의미합니까? var buttons = document.getElementsByTagName ('button'); –
예, 그런 것 같습니다. 그러나 나의 예를 들어 보면, 다음과 같이 될 것입니다.'var buttons = document.getElementsByTagName ('input');'그런 다음 안에있는 버튼을 찾습니다. – DontVoteMeDown