AQA Computer Science A Level

Adding operators
By Admin - Oct. 3, 2024, 12:31 p.m.
Last Edit - Oct. 3, 2024, 2:08 p.m.

Add Power Operator

Step 1

Find the regular expression in the CheckIfUserInputValid method. You need to add your operator to the REGEX:

@"^([0-9]+[\+\-\*\/\^])+[0-9]+$")

I have added \^ to the REGEX.

Step 2

Find the EvaluateRPN method. The while loop uses this !"+-*/^" it essentially means not an operator. I have added the ^ to this string.

Step 3

Add a new case for the new operator. For power use Math.Pow(num1,num2) for the calculation.

Step 4

Find the ConvertToRPN method. You need to add the new operators into the precedence dictionary.

 Dictionary Precedence = new Dictionary
            {
                { "+", 2 }, { "-", 2 }, { "*", 4 }, { "/", 4 }, { "^", 6 }
            };