What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » [?] Pow... err, triggering some function on certain convo events?

[?] Pow... err, triggering some function on certain convo events?
Author: Message:
Shondoit
Full Member
***

Avatar
Hmm, Just Me...

Posts: 227
Reputation: 15
35 / Male / Flag
Joined: Jul 2006
RE: [?] Pow... err, triggering some function on certain convo events?
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?)

This post was edited on 09-17-2006 at 06:56 PM by Shondoit.
My scripts:                            [Image: shondoit.gif]
+ Timezone
+ Camelo
+ Multisearch
09-17-2006 03:30 PM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
[?] Pow... err, triggering some function on certain convo events? - by Matti on 09-16-2006 at 11:41 AM
RE: [?] Powers - by markee on 09-16-2006 at 11:51 AM
RE: [?] Powers - by Matti on 09-16-2006 at 12:06 PM
RE: [?] Powers - by markee on 09-16-2006 at 12:23 PM
RE: [?] Powers - by CookieRevised on 09-16-2006 at 01:27 PM
RE: [?] Powers - by Matti on 09-16-2006 at 02:02 PM
RE: [?] Powers - by CookieRevised on 09-16-2006 at 02:11 PM
RE: [?] Pow... err, triggering some function on certain convo events? - by Matti on 09-16-2006 at 02:25 PM
RE: [?] Pow... err, triggering some function on certain convo events? - by deAd on 09-16-2006 at 08:00 PM
RE: [?] Pow... err, triggering some function on certain convo events? - by Matti on 09-17-2006 at 09:19 AM
RE: [?] Pow... err, triggering some function on certain convo events? - by phalanxii on 09-17-2006 at 10:18 AM
RE: [?] Pow... err, triggering some function on certain convo events? - by Plik on 09-17-2006 at 12:30 PM
RE: [?] Pow... err, triggering some function on certain convo events? - by Matti on 09-17-2006 at 12:38 PM
RE: [?] Pow... err, triggering some function on certain convo events? - by markee on 09-17-2006 at 12:46 PM
RE: [?] Pow... err, triggering some function on certain convo events? - by Matti on 09-17-2006 at 01:04 PM
RE: [?] Pow... err, triggering some function on certain convo events? - by Shondoit on 09-17-2006 at 03:30 PM
RE: RE: [?] Pow... err, triggering some function on certain convo events? - by CookieRevised on 09-17-2006 at 11:03 PM
RE: RE: RE: [?] Pow... err, triggering some function on certain convo events? - by deAd on 09-17-2006 at 11:18 PM
RE: [?] Pow... err, triggering some function on certain convo events? - by CookieRevised on 09-17-2006 at 11:42 PM
RE: [?] Pow... err, triggering some function on certain convo events? - by phalanxii on 09-18-2006 at 03:54 AM
RE: [?] Pow... err, triggering some function on certain convo events? - by Shondoit on 09-18-2006 at 05:57 AM
RE: [?] Pow... err, triggering some function on certain convo events? - by phalanxii on 09-18-2006 at 08:02 AM
RE: [?] Pow... err, triggering some function on certain convo events? - by Shondoit on 09-18-2006 at 11:09 AM
RE: [?] Pow... err, triggering some function on certain convo events? - by Matti on 09-18-2006 at 06:35 PM
RE: [?] Pow... err, triggering some function on certain convo events? - by Shondoit on 09-18-2006 at 08:07 PM
RE: [?] Pow... err, triggering some function on certain convo events? - by phalanxii on 09-18-2006 at 11:15 PM
RE: RE: [?] Pow... err, triggering some function on certain convo events? - by CookieRevised on 09-19-2006 at 06:34 AM
RE: RE: RE: [?] Pow... err, triggering some function on certain convo events? - by phalanxii on 09-19-2006 at 06:48 AM
RE: [?] Pow... err, triggering some function on certain convo events? - by Shondoit on 09-19-2006 at 05:20 AM
RE: [?] Pow... err, triggering some function on certain convo events? - by CookieRevised on 09-19-2006 at 10:50 AM
RE: [?] Pow... err, triggering some function on certain convo events? - by phalanxii on 09-19-2006 at 12:16 PM
RE: [?] Pow... err, triggering some function on certain convo events? - by Matti on 09-19-2006 at 06:33 PM
RE: [?] Pow... err, triggering some function on certain convo events? - by Shondoit on 09-19-2006 at 08:53 PM
RE: [?] Pow... err, triggering some function on certain convo events? - by phalanxii on 09-20-2006 at 12:31 AM
RE: [?] Pow... err, triggering some function on certain convo events? - by Shondoit on 09-20-2006 at 05:06 AM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On