metanohi-misc-subsites/subsites/projects/algo/pages/calc.htm

382 lines
11 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title></title>
<link href='pages.css' type='text/css' rel='stylesheet' />
<style type='text/css'>
* {
color: #ddd;
font: 12px 'dejavu sans mono';
}
body {
margin: 0;
padding: 0;
background-color: #000;
overflow: hidden;
}
div {
float: left;
}
div.result {
float: none;
width: 100%;
}
textarea {
float: left;
border: none;
background: none;
padding: 0;
overflow: hidden;
}
</style>
<script type='text/javascript' src='pages.js'></script>
<script type='text/javascript'>
/*
This program is free software. It comes without any warranty, to
the extent permitted by applicable law. You can redistribute it
and/or modify it under the terms of the Do What The Fuck You Want
To Public License, Version 2, as published by Sam Hocevar. See
http://sam.zoy.org/wtfpl/COPYING for more details.
*/
window.onload = function() {
var is_firefox
if (navigator.userAgent.indexOf('Firefox') != -1) is_firefox = true
else is_firefox = false
input_margin_top = 0
input_margin_right = 0
input_margin_bottom = 0
if (is_firefox) input_margin_left = -1
else input_margin_left = 0
ans = 0
curr_input_start = document.createElement('div')
curr_input_start.innerHTML = '>'
document.body.appendChild(curr_input_start)
input_start_width = curr_input_start.scrollWidth
input_start_height = curr_input_start.scrollHeight
curr_input = document.createElement('textarea')
curr_input.onkeydown = function() {
this.style.height = this.scrollHeight + 'px'
}
curr_input.onkeyup = function() {
this.style.height = this.scrollHeight + 'px'
}
curr_input.style.marginTop = input_margin_top + 'px'
curr_input.style.marginRight = input_margin_right + 'px'
curr_input.style.marginBottom = input_margin_bottom + 'px'
curr_input.style.marginLeft = input_margin_left + 'px'
curr_input.style.width = getw() - input_start_width - input_margin_left - input_margin_right + 'px'
curr_input.style.height = input_start_height + 'px'
document.body.appendChild(curr_input)
curr_input.focus()
continue_ok = true
window.onresize = function() {
curr_input.style.width = getw() - input_start_width - input_margin_left - input_margin_right + 'px'
setTimeout("curr_input.style.height = curr_input.scrollHeight + 'px'", 100)
}
document.onkeydown = function(event) {
if (!continue_ok) return
var code = getcode(event)
if (code == 13) {
var input_value, caltxt, len, oresult, result, result_elem, nl_elem, replace_elem
input_value = curr_input.value
caltxt = process_all(input_value)
if (caltxt != '') {
try {
oresult = eval(caltxt)
//parent.document.title = caltxt
if (typeof(oresult) == 'number') result = (oresult + '') //.replace(/\./g, ',')
else result = oresult
if (oresult == 'NaN') result = 'Error: Not a number'
else if (oresult == 'Infinity') result = 'Error: Infinity'
else ans = oresult
}
catch(e) {
result = 'Error'
}
if (result.substr(0, 5) == 'Error') result += '<br />Type "help" for help.'
}
else {
result = ''
}
nl_elem = document.createElement('br')
document.body.appendChild(nl_elem)
if (result != '' && result != 'undefined') {
result_elem = document.createElement('div')
result_elem.className = 'result'
result_elem.innerHTML = '>>>' + result
document.body.appendChild(result_elem)
}
replace_elem = document.createElement('div')
replace_elem.innerHTML = '>' + input_value
document.body.insertBefore(replace_elem, curr_input)
document.body.removeChild(curr_input_start)
document.body.removeChild(curr_input)
curr_input_start = document.createElement('div')
curr_input_start.innerHTML = '>'
document.body.appendChild(curr_input_start)
curr_input = document.createElement('textarea')
curr_input.onkeydown = function() {
this.style.height = this.scrollHeight + 'px'
}
curr_input.onkeyup = function() {
this.style.height = this.scrollHeight + 'px'
}
curr_input.style.marginTop = input_margin_top + 'px'
curr_input.style.marginRight = input_margin_right + 'px'
curr_input.style.marginBottom = input_margin_bottom + 'px'
curr_input.style.marginLeft = input_margin_left + 'px'
curr_input.style.width = getw() - input_start_width - input_margin_left - input_margin_right + 'px'
curr_input.style.height = input_start_height + 'px'
window.scrollBy(0, 100000)
}
continue_ok = false
}
document.onkeyup = function(event) {
var code = getcode(event)
if (code == 13) {
document.body.appendChild(curr_input)
curr_input.focus()
curr_input.value = ''
}
continue_ok = true
}
document.ondblclick = function() {
curr_input.focus()
}
}
function getcode(event) {
var e, code
if (window.event) e = window.event
else e = event
if (e.which) code = e.which
else if (e.charCode) code = e.charCode
else if (e.keyCode) code = e.keyCode
else code = 0
return code
}
function getw() {
var w
if (self.innerHeight)
w = self.innerWidth
else if (document.documentElement && document.documentElement.clientHeight)
w = document.documentElement.clientWidth
else if (document.body)
w = document.body.clientWidth
return w
}
function process_all(txt) {
var newtxt, txtspl, txtsta, replaces, len, i
newtxt = txt
if (newtxt.indexOf('=') != -1) {
txtspl = newtxt.split('=')
newtxt = txtspl[1]
txtsta = txtspl[0] + '='
}
else
txtsta = ''
replaces = [
[/\ /g, ''],
//[/\,/g, '.'], // Uncomment to allow "," as "." (used in some countries)
[/\'/g, ','],
[/\_/g, ','],
[/\*\*/g, '^'],
[/\&pi/gi, 'Math.PI'],
[/\&pi;/gi, 'Math.PI'],
[/\&e/gi, 'Math.E'],
[/log\(/gi, 'log('],
[/ln\(/gi, 'ln('],
[/sqrt\(/gi, 'sqrt('],
[/r\(/gi, 'sqrt('],
[/sin\(/gi, 'sin('],
[/sin\^\(-1\)\(/gi, 'asin('],
[/sin\^-1\(/gi, 'asin('],
[/cos\(/gi, 'cos('],
[/cos\^\(-1\)\(/gi, 'acos('],
[/cos\^-1\(/gi, 'acos('],
[/tan\(/gi, 'tan('],
[/tan\^\(-1\)\(/gi, 'atan('],
[/tan\^-1\(/gi, 'atan(']
]
len = replaces.length
for (i = 0; i < len; i++) {
newtxt = newtxt.replace(replaces[i][0], replaces[i][1])
}
if (newtxt == 'help')
return "'This is a simple calculator. It is able to put a number in the power of a number and supports functions like sin(), asin(), cos(), acos(), tan(), atan(), log() og ln(). To take the power of a number, write \"x^y\" or \"x**y\". To take the squareroot of a number, write \"r(x)\". To take another root of a number, write \"r(x,y)\". Type \"&pi\" for pi and \"&e\" for e. Trigonometric functions use degrees, not radians. The log() function uses 10 as its base, while the ln() function uses e. To use a previous result, type \"ans\".'"
else if (newtxt != '')
return txtsta + process_part(newtxt)
else
return txtsta + newtxt
}
function process_part(txt) {
var number_parts, operator_parts, bracket_count, len, i, curr, indexOfBracket, special_characters, newtxt, last, add_operator
number_parts = []
number_parts[0] = ''
operator_parts = []
bracket_count = 0
len = txt.length
for (i = 0; i < len; i++) {
curr = txt.substr(i, 1)
if (curr == '(') bracket_count++
else if (curr == ')') bracket_count--
if ((curr == '+' || curr == '-' || curr == '*' || curr == '/' || curr == '^') && bracket_count == 0) {
number_parts[number_parts.length] = ''
operator_parts[operator_parts.length] = txt.substr(i, 1)
}
else {
number_parts[number_parts.length - 1] += txt.substr(i, 1)
}
}
len = number_parts.length
if (number_parts[0] == '') number_parts[0] = '0'
if (number_parts[len - 1] == '') number_parts[len - 1] = '0'
for (i = 0; i < bracket_count; i++) {
number_parts[len - 1] += ')'
}
for (i = 0; i < len; i++) {
if (number_parts[i] != '') {
indexOfBracket = number_parts[i].indexOf('(')
if (indexOfBracket != -1) number_parts[i] = number_parts[i].substr(0, indexOfBracket) + '(' + process_part(number_parts[i].substr(indexOfBracket + 1, number_parts[i].length - indexOfBracket - 2)) + ')'
}
}
special_characters = '^'
newtxt = ''
for (i = 0; i < len; i++) {
last = (i == len - 1) ? true : false
add_operator = true
if (!last && special_characters.indexOf(operator_parts[i]) != -1) {
add_operator = false
if (operator_parts[i] == '^')
newtxt += 'pow(' + number_parts[i] + ',' + number_parts[i + 1] + ')'
}
else if (special_characters.indexOf(operator_parts[i - 1]) == -1) {
newtxt += number_parts[i]
}
if (!last && add_operator) newtxt += operator_parts[i]
}
return newtxt
}
function sin(num) {
return Math.sin(num * Math.PI / 180)
}
function cos(num) {
return Math.cos(num * Math.PI / 180)
}
function tan(num) {
return Math.tan(num * Math.PI / 180)
}
function asin(num) {
return (Math.asin(num) / Math.PI) * 180
}
function acos(num) {
return (Math.acos(num) / Math.PI) * 180
}
function atan(num) {
return (Math.atan(num) / Math.PI) * 180
}
function sqrt(num1, num2) {
if (num2 == undefined)
return Math.sqrt(num1)
else return pow(num1, 1 / num2)
}
function pow(num1, num2) {
return Math.pow(num1, num2)
}
function log(num) {
return Math.log(num) / Math.log(10)
}
function ln(num) {
return Math.log(num)
}
function max(num1, num2) {
return Math.max(num1, num2)
}
function min(num1, num2) {
return Math.min(num1, num2)
}
function round(num) {
return Math.round(num)
}
function floor(num) {
return Math.floor(num)
}
function ceil(num) {
return Math.ceil(num)
}
function rand() {
return Math.random()
}
function randint(num1, num2) {
return Math.floor(Math.random() * (num2 - num1 + 1)) + num1
}
</script>
</head>
<body></body>
</html>