Jump to content
Sign in to follow this  
themax90

Smarter Scripting?

Recommended Posts

Okay. As it seems for various values and parameters I find myself writing out every possible type in a param combination/type.

For example Math(a, b, type)

As represented in C++:

#include <iostream>

using namespace std;

float Math(float a, float b, float c)
{
      /*
      Math function.  a (param c) b.
      1 = Add
      2 = Subtract
      3 = Multiply
      4 = Divide
      */
    float d;
    if (c == 1)
    {
          d = a + b;
    }
    else if (c == 2)
    {
         d = a - b;
    }
    else if (c == 3)
    { 
         d = a * b;
    }
    else if (c == 4)
    {
         d = a / b;
    }
    else
    {
        return -1;
    }
    return(d);
}

int main(void)
{
     cout << "2 + 2 : ";
     cout << Math(2, 2, 1);
     return 0;
}

As represented in AutoIt:

$Type = InputBox("Type...", "1 = add, 2 = subtract, 3 = multiply, 4 = divide")
$Result = Math(2, 2, $Type)
If @Error Then Exit MsgBox(16, "Error", "Invalid type.")
MsgBox(0, "Result", $Result)
Func Math($a, $b, $c)
    If $c = 1 Then Return ($a + $B)
    If $c = 2 Then Return ($a - $B)
    If $c = 3 Then Return ($a * $B)
    If $c = 4 Then Return ($a / $B)
    If $c <= 0 Or $c >= 5 Then SetError(-1)
EndFunc

My question is this :

Is there a simpler way of coding this without all the hassle of writing out each type?

Thanks, just wondering.

AutoIt Smith

Share this post


Link to post
Share on other sites

How about:

$Type = '*'
;~ $Type = InputBox("Type...", "+ = add, - = subtract, * = multiply, / = divide")
$Result = Math(2, 2, $Type)
If @Error Then Exit MsgBox(16, "Error", "Invalid type.")
MsgBox(0, "Result", $Result)
Func Math($a, $b, $c)
    Return Execute($a & $c & $B)
EndFunc

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Is there a way to do it in C++?

Huh? I thought you wanted an easier way in AutoIt :whistle:

I would look up to see if the Execute function is released in the source if I were you.


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

float Math(float a, float b, float c)
{
      /*
      Math function.  a (param c) b.
      1 = Add
      2 = Subtract
      3 = Multiply
      4 = Divide
      */
    float d;
    if (c < 1 || c > 4) return -1;
    if (c < 3){
        if (c == 2) b *= -1;
        return a + b;
    } 
    if (c > 2){
        if (c == 4) b = 1 / b;
        return a * b;
    }
}

#)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...