Or perhaps you could use the Postfix method Cookie brought up
I already worked out the parsing of infix to postfix...
Now I'm working on calculating the postfix expression
So hang-on
-edit- Almost finished, done within an hour, I hope (I have to eat :P)
-edit2- It took a little longer than I expected, becuase the function weren't called correctly... But here it is anyway
It's very easy to use... Just call StringName.Calculate()
i.e.:
var Expression = Message.substr(0,4)
Result = Expression.Calculate()
It supports presedence, exponents, parenthese multiplication ( 3(4+1) = 15) and Math object functions and constants, for example, you can calculate a function like this:
Result = "3(1 + sin(PI))^2".Calculate()
or
var x = -4
Result = "-sqrt(abs(x))".Calculate()
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])=="function" || eval("typeof("+Token+")=='function'")) {
if (eval("typeof("+Token+")!='function'")) {
Token = "Math." + Token
}
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)
} 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]]) != "undefined" && typeof(Math[Operands[Index]]) != "function") {
Operands[Index] = Math[Operands[Index]]
}
}
}
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 Postfix = this.ToPostfix()
if (Postfix != undefined) return Postfix.CalculatePostfix()
}
Cookie, I'm not sure if I missed something... Could you have a look?
I think I took care off all the basic operations..., thx (y)
(Why is [noparse] not working for smilies?)