Author Topic: Getting parenthesis to parse both ways (probably stupid mistake)  (Read 1102 times)

If I input ( 5 x 2 ) x 2, it gives me 20. But... if I input 2 x ( 5 x 2 ), it gives me 10.
Code:
Code: [Select]
function Calculator_MathBackwards(%a)
{
   for(%b=0;%b<strLen(%a);%b++)
      %c = getSubStr(%a, %b, 1) @ %c;
   return %c;
}
function Calculator_Parse(%string)
{
   %string = stripChars(%string, " ");
   %string = strReplace(%string, "+", " + ");
   %string = strReplace(%string, "x", " x ");
   %string = strReplace(%string, "*", " * ");
   %string = strReplace(%string, "/", " / ");
   %string = strReplace(%string, "-", " - ");
   %string = strReplace(%string, "^", " ^ ");
   if(strPos(%string, "(") > -1 && strPos(%string, ")") > 0)
   {
      %f = Calculator_MathBackwards(%string);
      %start = strPos(%string, "(");
      %close = mAbs(strPos(%f, ")") - strLen(%string));
      %close2 = mAbs(%close - strLen(%string));
      %string = Calculator_Parse(stripTrailingSpaces(getSubStr(%string, %start + 1, %close - 2))) @ getSubStr(%string, strLen(%string) - %close2, strLen(%string) - (strLen(%string) - %close2));
   }
   for(%a=0;%a<getWordCount(%string);%a++)
   {
      if(getWord(%string, %a) $= "^" && %a != 0)
      {
         %f = getWord(%string, %a-1);
         %l = getWord(%string, %a+1);
         %string = setWord(%string, %a, mPow(%f, %l));
         %string = removeWord(removeWord(%string, %a+1), %a-1);
         %a = %a - 2;
      }
   }
   for(%a=0;%a<getWordCount(%string);%a++)
   {
      if(getWord(%string, %a) $= "x" || getWord(%string, %a) $= "*" || getWord(%string, %a) $= "/" && %a != 0)
      {
         %f = getWord(%string, %a - 1);
         %l = getWord(%string, %a + 1);
         %o = getWord(%string, %a);
         switch$(%o)
         {
            case "x":
               %string = setWord(%string, %a, %f * %l);
               %string = removeWord(%string, %a + 1);
               %string = removeWord(%string, %a - 1);
               %a = %a - 2;
            case "*":
               %string = setWord(%string, %a, %f * %l);
               %string = removeWord(%string, %a + 1);
               %string = removeWord(%string, %a - 1);
               %a = %a - 2;
            case "/":
               %string = setWord(%string, %a, %f / %l);
               %string = removeWord(%string, %a + 1);
               %string = removeWord(%string, %a - 1);
               %a = %a - 2;
         }
      }
   }
   for(%a=0;%a<getWordCount(%string);%a++)
   {
      if(getWord(%string, %a) $= "+" || getWord(%string, %a) $= "-" && %a != 0)
      {
         %f = getWord(%string, %a - 1);
         %l = getWord(%string, %a + 1);
         %o = getWord(%string, %a);
         switch$(%o)
         {
            case "+":
               %string = setWord(%string, %a, %f + %l);
               %string = removeWord(%string, %a + 1);
               %string = removeWord(%string, %a - 1);
               %a = %a - 2;
            case "-":
               %string = setWord(%string, %a, %f - %l);
               %string = removeWord(%string, %a + 1);
               %string = removeWord(%string, %a - 1);
               %a = %a - 2;
         }
      }
   }
   if(%string $= "1.#INF")
   {
      %string = "Error";
   }
   $Calculator_String = %string;
   Calculator_Screen.setValue("\c7" @ $Calculator_String);
   return %string;
}

just do
Code: [Select]
function Calculator_Parse(%string)
{
    for(%i = 0; %i < strLen(%string); %i++)
        if(strPos("012345667789 ()*/+-^", getSubStr(%string,%i,1)) == -1)
            %fail = 1;
    if(!%fail)
        return eval("return " @ %string @ ";");
    else
        return "";
}
???

just do
Code: [Select]
function Calculator_Parse(%string)
{
    for(%i = 0; %i < strLen(%string); %i++)
        if(strPos("012345667789 ()*/+-^", getSubStr(%string,%i,1)) == -1)
            %fail = 1;
    if(!%fail)
        return eval("return " @ %string @ ";");
    else
        return "";
}
???
Wow, i'm such a moron.
Thanks again Zeblote.

Wow, i'm such a moron.
Thanks again Zeblote.
Note that it doesn't prevent handicaps from entering 1 1) or similar syntax errors

Note that it doesn't prevent handicaps from entering 1 1) or similar syntax errors

It also has two sixes, two sevens, and no exponential capability.

It also has two sixes, two sevens, and no exponential capability.
6677??what

Oh yeah I forgot the ^ operator was for xor encryption



6677??what

Oh yeah I forgot the ^ operator was for xor encryption

What? It isn't encryption.

^ is performing exclusive OR (XOR), for instance:
6 is 110 in binary, 3 is 011 in binary, and 6 ^ 3, meaning 110 XOR 011 gives 101 (5).

  110   since 0 ^ 0 => 0
  011         0 ^ 1 => 1
  ---         1 ^ 0 => 1
  101         1 ^ 1 => 0
« Last Edit: July 07, 2013, 10:00:05 AM by Port »

What? It isn't encryption.
computermix said it is, I actually have no idea what the ^ operator does

computermix said it is, I actually have no idea what the ^ operator does

Port is right and Computermix is wrong; ^ is binary XOR. USED for many encryption schemes, including the one-time pad (the only known theoretically uncrackable scheme1), but it is not in and of itself encryption.

Likewise, | is binary OR, & is binary AND, and ~ is binary negation.

1Real-world logistics like true randomness and distribution of the pads make it much less secure.

Computermix was talking about DSO string tables, they're obfuscated using XOR along with the length of the string table as the key. He just misunderstood.

Computermix was talking about DSO string tables, they're obfuscated using XOR along with the length of the string table as the key. He just misunderstood.
No, he meant the ^ operator in torquescript and when I tried it it outputted garbage so I thought it was some kind of encryption

No, he meant the ^ operator in torquescript and when I tried it it outputted garbage so I thought it was some kind of encryption
I wasn't even aware Computermix knew Torquescript. Everything he's ever done for Blockland has been written in C++.