First Commit

This commit is contained in:
Mike Nolan 2024-04-12 22:41:59 -05:00
parent 3a77e4f5ef
commit 9267e4a38b
4 changed files with 180 additions and 0 deletions

View File

@ -1,2 +1,3 @@
# js-calc
[https://calc.site.tesses.net/](https://calc.site.tesses.net/)

1
beer.min.css vendored Normal file

File diff suppressed because one or more lines are too long

138
calc.js Normal file
View File

@ -0,0 +1,138 @@
const answer = document.getElementById('answer');
function cls()
{
answer.value = '';
}
function append(c)
{
answer.value += c;
}
function lex()
{
var lexed = [];
var builder = "";
var flush = ()=>{
if(builder.length > 0)
{
lexed.push(builder);
builder = "";
}
}
var val = answer.value;
for(i=0;i<val.length;i++)
{
switch(val.at(i))
{
case '.':
case '(':
case ')':
case '^':
case '-':
case '*':
case '/':
case '+':
{
flush();
lexed.push(val.at(i));
}
break;
default:
builder += val.at(i);
break;
}
}
flush();
return lexed;
}
function parse_value(d)
{
if(d.i < d.lexed.length && d.lexed[d.i] == '(')
{
d.i++;
var v= parse_add(d);
d.i++;
return v;
}
j = parseInt(d.lexed[d.i++]);
if(d.lexed[d.i] == '.')
{
d.i++;
j= parseFloat(`${j}.${d.lexed[d.i++]}`);
}
return j;
}
function parse_pow(d)
{
var r = parse_value(d);
while(d.i<d.lexed.length && d.lexed[d.i] == "^")
{
d.i++;
var l = parse_value(d);
r= Math.pow(r,l);
}
return r;
}
function parse_times(d)
{
var r = parse_pow(d);
while(d.i<d.lexed.length && (d.lexed[d.i] == "*" || d.lexed[d.i] == "/"))
{
if(d.i<d.lexed.length && d.lexed[d.i] == "*")
{
d.i++;
var l = parse_pow(d);
r *= l;
}
if(d.i<d.lexed.length && d.lexed[d.i] == "/")
{
d.i++;
var l = parse_pow(d);
r /= l;
}
}
return r;
}
function parse_add(d)
{
var r = parse_times(d);
while(d.i<d.lexed.length && (d.lexed[d.i] == "+" || d.lexed[d.i] == "-"))
{
if(d.i<d.lexed.length && d.lexed[d.i] == "+")
{
d.i++;
var l = parse_times(d);
r += l;
}
if(d.i<d.lexed.length && d.lexed[d.i] == "-")
{
d.i++;
var k = r;
var l = parse_times(d);
r -= l;
}
}
return r;
}
function calculate()
{
var lexed = lex();
var d = {
i: 0,
lexed: lexed
}
answer.value = parse_add(d).toString();
}

40
index.html Normal file
View File

@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator</title>
<link rel="stylesheet" href="./beer.min.css">
</head>
<body>
<div class="field border">
<input type="text" id="answer" style="text-align: right;">
</div>
<div class="grid">
<div class="s3"><button onclick="append('(')" class="responsive">(</button></div>
<div class="s3"><button onclick="append(')')" class="responsive">)</button></div>
<div class="s3"><button onclick="append('^')" class="responsive">pow</button></div>
<div class="s3"><button onclick="cls()" class="responsive">AC</button></div>
<div class="s3"><button onclick="append('7')" class="responsive">7</button></div>
<div class="s3"><button onclick="append('8')" class="responsive">8</button></div>
<div class="s3"><button onclick="append('9')" class="responsive">9</button></div>
<div class="s3"><button onclick="append('/')" class="responsive">/</button></div>
<div class="s3"><button onclick="append('4')" class="responsive">4</button></div>
<div class="s3"><button onclick="append('5')" class="responsive">5</button></div>
<div class="s3"><button onclick="append('6')" class="responsive">6</button></div>
<div class="s3"><button onclick="append('*')" class="responsive">*</button></div>
<div class="s3"><button onclick="append('1')" class="responsive">1</button></div>
<div class="s3"><button onclick="append('2')" class="responsive">2</button></div>
<div class="s3"><button onclick="append('3')" class="responsive">3</button></div>
<div class="s3"><button onclick="append('-')" class="responsive">-</button></div>
<div class="s3"><button onclick="append('0')" class="responsive">0</button></div>
<div class="s3"><button onclick="append('.')" class="responsive">.</button></div>
<div class="s3"><button onclick="calculate()" class="responsive">=</button></div>
<div class="s3"><button onclick="append('+')" class="responsive">+</button></div>
</div>
<script src="./calc.js"></script>
<a href="https://gitea.site.tesses.net/tesses50/js-calc"></a>
</body>
</html>