1
\$\begingroup\$

I'm trying to find a way to make this code more efficient than using a lot of if statements because there are a lot of combinations. The final result variable is an equation made from randomly chosen arithmetic signs and the operands are random numbers between 1 and 10 to create an equation. But after 2 operands there are a lot of combinations so I need help finding a different way so I don't have to manually put in all the possible combinations in if statements. The equations can either have two operators at the least to 4 operators and up to to 5 operands per equation so each equation will be randomly created.

public class MathEngine : MonoBehaviour## Heading ##
{
public Text equationText;
public bool setFixedNumOfOperands;

public bool sumOnly;
public bool subtractionOnly;
public bool multiplyOnly;
public bool divideOnly;

private List<string> randomSigns;
private List<int> operandList;


[Range(2,5)] 
public int numberOFOperands;

public int operandRange;

public int operandRangeMinimum = 1;
public int operandRangeMaximum= 10;

string randomsigns;


private float finalResult;

private float operand1;
private float operand2;
private float operand3;
private float operand4;
private float operand5;


// Use this for initialization
private void Start()
{
    operandRange = Random.Range(operandRangeMinimum, operandRangeMaximum + 1);

    operand1 = Random.Range(operandRangeMinimum, operandRangeMaximum + 1);
    operand2 = Random.Range(operandRangeMinimum, operandRangeMaximum + 1);
    operand3 = Random.Range(operandRangeMinimum, operandRangeMaximum + 1);
    operand4 = Random.Range(operandRangeMinimum, operandRangeMaximum + 1);
    operand5 = Random.Range(operandRangeMinimum, operandRangeMaximum + 1);

    operandList = new List<int>();
    randomSigns = new List<string>();

    randomSigns.Add("+");
    randomSigns.Add("-");
    randomSigns.Add("*");
    randomSigns.Add("/");

    string sign1 = randomSigns[Random.Range(0, 4)];
    string sign2 = randomSigns[Random.Range(0,4)];
    string sign3 = randomSigns[Random.Range(0, 4)];
    string sign4 = randomSigns[Random.Range(0, 4)];
    string sign5 = randomSigns[Random.Range(0, 4)];

    string sign =  randomSigns[Random.Range(0, 4)];

    if (setFixedNumOfOperands == false)
    {
        numberOFOperands = Random.Range(2, 6);


        for (int i = 0; i < numberOFOperands; i++)
        {
            //randomsigns = randomSigns[Random.Range(0, 4)];
            operandList.Add(i);
        }
        Debug.Log("random: " + numberOFOperands);
        Debug.Log("number of operands in list: " + operandList.Count);

     }
    else
    {
        for (int i = 0; i < numberOFOperands; i++)
        {
            operandList.Add(i);
        }
        Debug.Log("number of operands in list: " + operandList.Count);

    }


    //----------2 sign combinations 3 operands--------
    if (numberOFOperands == 3 && setFixedNumOfOperands)
    {
        //----If all signs are the same-----
        if (sign1 == "+" && sign2 == "+")
        {
            finalResult = operand1 + operand2 + operand3;
        }
        if (sign1 == "-" && sign2 == "-")
        {
            finalResult = operand1 - operand2 - operand3;
        }
        if (sign1 == "*" && sign2 == "*")
        {
            finalResult = operand1 * operand2 * operand3;
        }

        if (sign1 == "/" && sign2 == "/")
        {
            finalResult = operand1 / operand2 / operand3;
        }

        //---------Addition Combinations---------
        if (sign1 == "+" && sign2 == "/")
        {
            finalResult = (operand1 + operand2) / operand3;
        }
        if (sign1 == "+" && sign2 == "-")
        {
            finalResult = operand1 + operand2 - operand3;
        }
        if (sign1 == "+" && sign2 == "*")
        {
            finalResult = operand1 + (operand2 * operand3);
        }

        //---------Subtraction Combinations---------
        if (sign1 == "-" && sign2 == "/")
        {
            finalResult = (operand1 - operand2) / operand3;
        }
        if (sign1 == "-" && sign2 == "+")
        {
            finalResult = operand1 - operand2 + operand3;
        }
        if (sign1 == "-" && sign2 == "*")
        {
            finalResult = operand1 - (operand2 * operand3);
        }
        //---------Multiplication Combinations---------
        if (sign1 == "*" && sign2 == "/")
        {
            finalResult = (operand1 * operand2) / operand3;
        }
        if (sign1 == "*" && sign2 == "+")
        {
            finalResult = (operand1 * operand2) + operand3;
        }
        if (sign1 == "*" && sign2 == "-")
        {
            finalResult = (operand1 * operand2) - operand3;
        }
        //---------Division Combinations---------
        if (sign1 == "/" && sign2 == "*")
        {
            finalResult = (operand1 / operand2) * operand3;
        }
        if (sign1 == "/" && sign2 == "+")
        {
            finalResult = (operand1 / operand2) + operand3;
        }
        if (sign1 == "/" && sign2 == "-")
        {
            finalResult = (operand1 / operand2) - operand3;
        }

        //--------if only 2 signs and 3 operands -------------
        equationText.text = operand1 + " " + sign1 + " " + operand2 + " " + sign2 + " " + operand3 + " " + "= " + finalResult;
    }


    //----------3 sign combinations 4 operands--------
    if (numberOFOperands == 4 && setFixedNumOfOperands)
    {
        //----If all signs are the same-----
        if (sign1 == "+" && sign2 == "+" && sign3 == "+")
        {
            finalResult = operand1 + operand2 + operand3 + operand4;
        }
        if (sign1 == "-" && sign2 == "-" && sign3 == "-")
        {
            finalResult = operand1 - operand2 - operand3 - operand4;
        }
        if (sign1 == "*" && sign2 == "*" && sign3 == "*")
        {
            finalResult = operand1 * operand2 * operand3 * operand4;
        }

        if (sign1 == "/" && sign2 == "/" && sign3 == "/")
        {
            finalResult = operand1 / operand2 / operand3 / operand4;
        }

        //---------Addition Combinations---------
        if (sign1 == "+" && sign2 == "/" && sign3 == "/")
        {
            finalResult = (operand1 + operand2) / operand3 / operand4;
        }
        if (sign1 == "+" && sign2 == "-" && sign3 == "-")
        {
            finalResult = operand1 + operand2 - operand3 - operand4;
        }
        if (sign1 == "+" && sign2 == "*" && sign3 == "*" )
        {
            finalResult = operand1 + (operand2 * operand3) * operand4;
        }
        //---------
        if (sign1 == "+" && sign2 == "+" && sign3 == "*")
        {
            finalResult = operand1 + operand2 + (operand3 * operand4);
        }
        if (sign1 == "+" && sign2 == "+" && sign3 == "/")
        {
            finalResult = operand1 + operand2 + (operand3 / operand4);
        }
        if (sign1 == "+" && sign2 == "+" && sign3 == "-")
        {
            finalResult = operand1 + operand2 + operand3 - operand4;
        }
        //-----------

        if (sign1 == "+" && sign2 == "*" && sign3 == "+")
        {
            finalResult = operand1 + (operand2 * operand3) + operand4;
        }
        if (sign1 == "+" && sign2 == "/" && sign3 == "+")
        {
            finalResult = operand1 + (operand2 / operand3) + operand4;
        }
        if (sign1 == "+" && sign2 == "-" && sign3 == "+")
        {
            finalResult = operand1 + operand2 - operand3 + operand4;
        }


        //---------Subtraction Combinations---------
        if (sign1 == "-" && sign2 == "/" && sign3 == "/")
        {
            finalResult = (operand1 - operand2) / (operand3 / operand4);
        }
        if (sign1 == "-" && sign2 == "+" && sign3 == "+")
        {
            finalResult = operand1 - operand2 + operand3 + operand4;
        }
        if (sign1 == "-" && sign2 == "*" && sign3 == "*")
        {
            finalResult = operand1 - (operand2 * operand3) * operand4;
        }
        //---------
        if (sign1 == "-" && sign2 == "-" && sign3 == "*")
        {
            finalResult = operand1 - operand2 - (operand3 * operand4);
        }
        if (sign1 == "-" && sign2 == "-" && sign3 == "/")
        {
            finalResult = operand1 - operand2 - (operand3 / operand4);
        }
        if (sign1 == "-" && sign2 == "-" && sign3 == "+")
        {
            finalResult = operand1 - operand2 - operand3 + operand4;
        }
        //-----------

        if (sign1 == "-" && sign2 == "*" && sign3 == "-")
        {
            finalResult = operand1 - (operand2 * operand3) - operand4;
        }
        if (sign1 == "-" && sign2 == "/" && sign3 == "-")
        {
            finalResult = operand1 - (operand2 / operand3) - operand4;
        }
        if (sign1 == "-" && sign2 == "+" && sign3 == "-")
        {
            finalResult = operand1 - operand2 + operand3 - operand4;
        }

        //---------Multiplication Combinations---------
        if (sign1 == "*" && sign2 == "/" && sign3 == "/")
        {
            finalResult = (operand1 * operand2) / operand3 / operand4;
        }
        if (sign1 == "*" && sign2 == "+" && sign3 == "+")
        {
            finalResult = (operand1 * operand2) + operand3 + operand4;
        }
        if (sign1 == "*" && sign2 == "-" && sign3 == "-")
        {
            finalResult = (operand1 * operand2) - operand3 - operand4;
        }
        //---------
        if (sign1 == "*" && sign2 == "*" && sign3 == "-")
        {
            finalResult = (operand1 * operand2) * operand3 - operand4;
        }
        if (sign1 == "*" && sign2 == "*" && sign3 == "/")
        {
            finalResult = (operand1 * operand2) * (operand3 / operand4);
        }
        if (sign1 == "*" && sign2 == "*" && sign3 == "+")
        {
            finalResult = (operand1 * operand2) *  (operand3 + operand4);
        }
        //-----------

        if (sign1 == "*" && sign2 == "-" && sign3 == "*")
        {
            finalResult = (operand1 * operand2) - (operand3 * operand4);
        }
        if (sign1 == "*" && sign2 == "/" && sign3 == "*")
        {
            finalResult = (operand1 * operand2) / (operand3 * operand4);
        }
        if (sign1 == "*" && sign2 == "+" && sign3 == "*")
        {
            finalResult = (operand1 * operand2) + (operand3 * operand4);
        }

        //---------Division Combinations---------
        if (sign1 == "/" && sign2 == "*" && sign3 == "*")
        {
            finalResult = (operand1 / operand2) * operand3 * operand4;
        }
        if (sign1 == "/" && sign2 == "+" && sign3 == "+")
        {
            finalResult = (operand1 / operand2) + operand3 + operand4;
        }
        if (sign1 == "/" && sign2 == "-" && sign3 == "-")
        {
            finalResult = (operand1 / operand2) - operand3 - operand4;
        }

        //---------
        if (sign1 == "/" && sign2 == "/" && sign3 == "-")
        {
            finalResult = (operand1 / operand2) / (operand3 - operand4);
        }
        if (sign1 == "/" && sign2 == "/" && sign3 == "*")
        {
            finalResult = (operand1 / operand2) / (operand3 * operand4);
        }
        if (sign1 == "/" && sign2 == "/" && sign3 == "+")
        {
            finalResult = (operand1 / operand2) / (operand3 + operand4);
        }
        //-----------

        if (sign1 == "/" && sign2 == "-" && sign3 == "/")
        {
            finalResult = (operand1 / operand2) - (operand3 / operand4);
        }
        if (sign1 == "/" && sign2 == "*" && sign3 == "/")
        {
            finalResult = (operand1 / operand2) * (operand3 / operand4);
        }
        if (sign1 == "/" && sign2 == "+" && sign3 == "/")
        {
            finalResult = (operand1 / operand2) + (operand3 / operand4);
        }

        //--------if only 3 signs and 4 operands -------------
        equationText.text = operand1 + " " + sign1 + " " + operand2 + " " + sign2 + " " + operand3 +" " + sign3 + " " + operand4 + " " + "= " + finalResult;
    }


}
\$\endgroup\$
11
  • \$\begingroup\$ What is Random.Range? Is this something you wrote yourself or is this a unity API? \$\endgroup\$
    – t3chb0t
    Commented Jul 1, 2017 at 17:59
  • \$\begingroup\$ Random.range is unity API \$\endgroup\$
    – BigDuke6
    Commented Jul 1, 2017 at 19:31
  • \$\begingroup\$ I think the algorithm is simply bad. Make two stacks, one of n operands and one and one of n-1 random operators. pop operands and operators, apply the math, push the result back on the operand stack. continue until the operator stack is empty. This is a 20 line program. \$\endgroup\$
    – Tony Ennis
    Commented Jul 1, 2017 at 22:10
  • \$\begingroup\$ Whenever you see that much repetition, there's something wrong. \$\endgroup\$
    – Stephen S
    Commented Jul 1, 2017 at 22:47
  • \$\begingroup\$ @TonyEnnis Would you care to post your 20 line solution? \$\endgroup\$
    – paparazzo
    Commented Jul 3, 2017 at 20:02

2 Answers 2

3
\$\begingroup\$

What is the purpose of operandList? All you use is the count and that is numberOFOperands.

Just take two passes and do / * in the first pass.

Use a List rather than operand1 - operand5

Use a List rather than sign1 - sign5

I'm not sure this is correct but something like this.

public static float Maths2(out string equation)
{
    Random rand = new Random();
    float answer = 0;
    List<char> operators = new List<char>() { '+', '-', '/', '*' };
    List<int> operatorsThis = new List<int>();
    List<float> operandsThis = new List<float>();
    int operatorsCount = 4;
    int operandsMin = 1;
    int operandsMax = 10;
    StringBuilder sb = new StringBuilder();
    float f;
    List<int> operatorsThisPlusMinus = new List<int>();
    List<float> operandsPlusMinus = new List<float>();

    for (int i = 0; i < operatorsCount; i++)
    {
        f = rand.Next(operandsMin, operandsMax + 1);
        operandsThis.Add(f);
        sb.Append(f.ToString() + " ");

        int r = rand.Next(4);
        operatorsThis.Add(r);
        sb.Append(operators[r] + " ");
    }
    f = rand.Next(operandsMin, operandsMax + 1);
    operandsThis.Add(f);
    sb.Append(f.ToString() + " ");
    equation = sb.ToString();
    Debug.WriteLine(equation);
    f = operandsThis[0];
    for (int i = 0; i < operatorsThis.Count; i++)
    {
        if (operatorsThis[i] < 2)
        {
            operatorsThisPlusMinus.Add(operatorsThis[i]);
            operandsPlusMinus.Add(f);
            f = operandsThis[i + 1];
        }
        else if (operatorsThis[i] == 2)
        {
            f /= operandsThis[i + 1];
        }
        else if (operatorsThis[i] == 3)
        {
            f *= operandsThis[i + 1];
        }
    }
    operandsPlusMinus.Add(f);
    answer = operandsPlusMinus[0];
    for (int i = 0; i < operatorsThisPlusMinus.Count; i++)
    {
        if (operatorsThisPlusMinus[i] == 0)
        {
            answer += operandsPlusMinus[i + 1];
        }
        else if (operatorsThisPlusMinus[i] == 1)
        {
            answer -= operandsPlusMinus[i + 1];
        }
    }
    Debug.WriteLine(answer);
    return answer;
}
\$\endgroup\$
0
-2
\$\begingroup\$

Ok here I got it to multiply and divide with one operator but the result is still wrong when there are more than one operators. I'm still at a loss here I appreciate the help but I just don't know how to correct the order of operations still.

public class Example : MonoBehaviour {

// Use this for initialization
void Start ()
{
    Maths();

}

// Update is called once per frame
void Update () {

}
public static float Maths()
{
    System.Random rand = new System.Random();
    float answer = 0;
    List<char> operators = new List<char>() { '+', '-', '/', '*' };
    List<int> operatorsThis = new List<int>();
    List<float> operands = new List<float>();
    int operatorsCount = 2;
    int operandsMin = 1;
    int operandsMax = 10;
    StringBuilder sb = new StringBuilder();
    float f;
    int r = -1;


    for (int i = 0; i < operatorsCount; i++)
    {
        f = rand.Next(operandsMin, operandsMax + 1);
        operands.Add(f);
        sb.Append(f.ToString() + " ");

         r = rand.Next(4);
         char c = operators[r];
        operatorsThis.Add(r);
        sb.Append(c + " ");



    }
    f = rand.Next(operandsMin, operandsMax + 1);
    operands.Add(f);
    sb.Append(f.ToString() + " ");
    Debug.Log(sb.ToString());
    f = operands[0];
    answer = f;
    int iLast = 0;

    for (int i = 0; i < operatorsThis.Count; i++)
    {
        if (operatorsThis[i] >= 2)
        {
            iLast = i;
            f = operands[i + 1];

            if (operatorsThis[i] == 2)
            {
                answer /= f;

            }
            if (operatorsThis[i] == 3)
            {

                answer *= f;
            }
        }

        if (operatorsThis[i] < 2)
        {
            iLast = i;
            f = operands[i + 1];


            if (operatorsThis[i] == 0)
            {
                answer += f;
            }
            else if (operatorsThis[i] == 1)
            {
                answer -= f;
            }
        }
        else
        {
            if (operatorsThis[i] == 2)
            {
                f /= operands[i + 1];
            }
            else if (operatorsThis[i] == 3)
            {
                f *= operands[i + 1];
            }
            if (i == operatorsThis.Count - 1)
            {
                if (operatorsThis[iLast] == 0)
                {
                    answer += f;
                }
                else if (operatorsThis[iLast] == 1)
                {
                    answer -= f;
                }
            }
        }
    }
    Debug.Log("the Answer is: " + answer);
    return answer;
}

}

\$\endgroup\$
1
  • \$\begingroup\$ or is there a way i can evaluate the string? \$\endgroup\$
    – BigDuke6
    Commented Jul 1, 2017 at 23:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.