Hello,
I am making a calculator for school and so far I have written a code that allows user to combine and input. Calling it a formula would be wrong I think. It's like User pushesh buttons and comes up with an input like this for example : 11+4+9-5. Now that is just a string. What I need is to split the string where the operators occur and then choose a function based on the operand. Basically I need to convert that string to working forula and output the answer. This is my code so far.
<html>
<head>
<title>Kalkulaator</title>
<style>
.button
{
float:left;
}
.left
{
float:left;
}
.clear
{
clear:both;
}
</style>
<script>
function Arvuti(kiht){
ekraan = document.getElementById("ekraan");
this.registerInput = function(vaartus){
ekraan.value = ekraan.value + vaartus;
}
this.arvuta = function(){
ekraan.value
}
}
var a1;
function init(){
a1 = new Arvuti("kiht1");
}
window.onload = init;
</script>
</head>
<body>
<div id="kalkulaator1">
<input id="ekraan" type="text" width="100" disabled />
<div class="clear"></div>
<table class="nupud left">
<tr>
<td><input type='button' value='1' onclick='a1.registerInput(this.value);' class="button" /></td>
<td><input type='button' value='2' onclick='a1.registerInput(this.value);' class="button" /></td>
<td><input type='button' value='3' onclick='a1.registerInput(this.value);' class="button" /></td>
</tr>
<tr>
<td><input type='button' value='4' onclick='a1.registerInput(this.value);' class="button" /></td>
<td><input type='button' value='5' onclick='a1.registerInput(this.value);' class="button" /></td>
<td><input type='button' value='6' onclick='a1.registerInput(this.value);' class="button" /></td>
</tr>
<tr>
<td><input type='button' value='7' onclick='a1.registerInput(this.value);' class="button" /></td>
<td><input type='button' value='8' onclick='a1.registerInput(this.value);' class="button" /></td>
<td><input type='button' value='9' onclick='a1.registerInput(this.value);' class="button" /></td>
</tr>
</table>
<table class="operaatorid left">
<tr>
<td><input type="button" value="+" onclick='a1.registerInput(this.value);'/></td>
</tr>
<tr>
<td><input type="button" value="-" onclick='a1.registerInput(this.value);'/></td>
</tr>
</table>
<input type="button" value="Arvuta" onclick="a1.arvuta();" class="left clear" />
</div>
</body>
</html>
The argument for Arvuta() is not beeing used atm but it will be used to output the calculator in the div that is named what the argument holds.