This is the fixed code... It supports different angle types (Angle = "degree" or Angle = "radians" and even supports "grad")
It supports Mixed case functions (Only if it is a function of the Math object) and I fixed the space problem...
But Mattike, please go on, you can desgin the GUI, the scriptcommands, ...etc. I don't really have the time for it, if you need any help, add me to WLM (I speak Dutch (y))
code:
String.prototype.ToPostfix = function () {
var Stack = ""
var Token = ""
var Result = ""
for (var i = 0; i < this.length; i++) {
Token = this.charAt(i)
switch (Token) {
case "(":
Stack += "("
break
case ")":
while (Stack.length > 0 && Stack.charAt(Stack.length - 1) != "(") {
Result += Stack.charAt(Stack.length - 1)
Stack = Stack.substring(0, Stack.length - 1)
}
Stack = Stack.substring(0, Stack.length - 1)
break
case "^": case "*": case "/": case "+": case "-":
PopStack()
break
default:
while (/[^()\^*/+-]/.test(this.charAt(i + 1)) && i < this.length) {
Token += this.charAt(i++ + 1)
}
if (/\+|-/.test(this.charAt(i + 1))) {
if (/e/i.test(this.charAt(i)) && i > 0 && /\d/.test(this.charAt(i - 1))) {
Token += this.charAt(i++ + 1)
while (/[^()\^*/+-]/.test(this.charAt(i + 1)) && i < this.length) {
Token += this.charAt(i++ + 1)
}
}
}
if (this.charAt(i + 1) == "(") {
try {
if (typeof(Math[Token.toLowerCase()])=="function" || eval("typeof("+Token+")=='function'")) {
if (eval("typeof("+Token+")!='function'")) {
Token = "Math." + Token.toLowerCase()
}
Token += this.charAt(i++ + 1)
var ParamToken = ""
var Parentheses = 0
while ((/[^)]/.test(this.charAt(i + 1)) || Parentheses > 0) && i < this.length) {
if (this.charAt(i + 1) == "(") Parentheses++; else if (this.charAt(i + 1) == ")") Parentheses--
ParamToken += this.charAt(i++ + 1)
}
var Params = ParamToken.split(/\s*,\s*/)
for (Index in Params) {
Params[Index] = Params[Index].Calculate()
}
Token += Params.join(",")
Token += this.charAt(i++ + 1)
if (/^Math\.(sin|cos|tan)\((.+)\)$/.test(Token)) {
var Func = "Math." + RegExp.$1 + "(" + RegExp.$2 + "*Math.PI"
if (/^degrees?$/i.test(Angle)){
Token = Func + "/180)"
} else if (/^grad$/i.test(Angle)) {
Token = Func + "/200)"
}
}
if (/^Math\.(a(?:sin|cos|tan2?))\((.+)\)$/.test(Token)) {
var Func = "Math." + RegExp.$1 + "(" + RegExp.$2 + ")/Math.PI"
if (/^degrees?$/i.test(Angle)){
Token = Func + "*180"
} else if (/^grad$/i.test(Angle)) {
Token = Func + "*200"
}
Token = Token.Calculate()
}
} else {
PopStack("*")
}
} catch (e) {
return undefined
}
}
Result += "\x01" + Token + "\x01"
break
}
}
while (Stack.length > 0) {
Result += Stack.charAt(Stack.length - 1)
Stack = Stack.substring(0, Stack.length-1)
}
return Result.replace(/\s/g, "")
function PopStack (Operator) {
if (Operator == undefined) Operator = Token
while (Stack.length > 0 && ((/[\^*/+-]/.test(Operator) && /[\^]/.test(Stack.charAt(Stack.length - 1))) || (/[*/+-]/.test(Operator) && /[*/]/.test(Stack.charAt(Stack.length - 1))) || (/[+-]/.test(Operator) && /[+-]/.test(Stack.charAt(Stack.length - 1))))) {
Result += Stack.charAt(Stack.length - 1)
Stack = Stack.substring(0, Stack.length - 1)
}
Stack += Operator
}
}
String.prototype.CalculatePostfix = function () {
var Stack = new Array()
var Token = ""
for (var i = 0; i < this.length; i++) {
Token = this.charAt(i)
if (/[^\^*/+-]/.test(Token)) {
while (/[^\x01]/.test(this.charAt(i + 1)) && i < this.length) {
Token += this.charAt(i++ + 1)
}
Token += this.charAt(i++ + 1)
if (/\+|-/.test(this.charAt(i + 1))) {
if (/e/i.test(this.charAt(i)) && i > 0 && /\d/.test(this.charAt(i - 1))) {
Token += this.charAt(i++ + 1)
while (/[^()\^*/+-]/.test(this.charAt(i + 1)) && i < this.length) {
Token += this.charAt(i++ + 1)
}
}
}
if (/\x01(.+)\x01/.test(Token)) {
var Operands = RegExp.$1.split(/\x01\x01/)
for (Index in Operands) {
if(eval("typeof("+Operands[Index]+")=='undefined'")) {
if (typeof(Math[Operands[Index].toUpperCase()]) != "undefined" && typeof(Math[Operands[Index]]) != "function") {
Operands[Index] = Math[Operands[Index].toUpperCase()]
}
}
}
Stack = Stack.concat(Operands)
} else {
return undefined
}
} else {
if (Stack.length >= 2) {
if (Token == "^") {
Stack[Stack.length - 2] = Math.pow(eval(Stack[Stack.length - 2]), eval(Stack[Stack.length - 1]))
} else {
Stack[Stack.length - 2] = eval(eval(Stack[Stack.length - 2]) + Token + eval(Stack[Stack.length - 1]))
}
Stack.pop()
}
}
}
try {
return eval(Stack[0])
} catch (e) {
return undefined
}
}
String.prototype.Calculate = function () {
var Infix = this.replace(/\s/, "")
var Postfix = Infix.ToPostfix()
if (Postfix != undefined) return Postfix.CalculatePostfix()
}