Search code examples
c#postfix-notation

Reverse Polish Notation: Calculating exponents


I am currently working with Reverse Polish Notation. I have able to perform all operation in exception of exponential math. I am aware that C# sharps performs exponential math with Math.Pow but using that in my code is giving me an error 'System.Collections.Generic.Stack<T>.Push(T)' is a 'method', which is not valid in the given context. You can find the specific issue in the last if else statement. Any idea how I can properly correct or create a way to perform exponential math?

Code

private string inputValue = "";

private void RPNCalc(string rpnValue)
{
Stack<int> stackCreated = new Stack<int>();
stackCreated.Clear();
string[] inputArray = rpnValue.Split();
int end = inputArray.Length - 1;
int numInput;
int i = 0;

do
{
    if ("=+-*/%^".IndexOf(inputArray[i]) == -1)
    {
        try
        {
            numInput = Convert.ToInt32(inputArray[i]);
            stackCreated.Push(numInput);
        }
        catch
        {
            MessageBox.Show("Please check the input");
        }
    }

        else if (inputArray[i]== "+")
        {
            try
            {
                int store1 = stackCreated.Pop();
                int store2 = stackCreated.Pop();
                stackCreated.Push(store2 + store1);
            }
            catch
            {
            }
        }

        else if (inputArray[i]== "-")
        {
            try
            {
                int store1 = stackCreated.Pop();
                int store2 = stackCreated.Pop();
                stackCreated.Push(store2 - store1);
            }
            catch
            {
            }
        }

        else if (inputArray[i]== "%")
        {
            try
            {
                int store1 = stackCreated.Pop();
                int store2 = stackCreated.Pop();
                stackCreated.Push(store2 % store1);
            }
            catch
            {
            }
        }

        else if (inputArray[i]== "*")
        {
            try
            {
                int store1 = stackCreated.Pop();
                int store2 = stackCreated.Pop();
                stackCreated.Push(store2 * store1);
            }
            catch
            {
            }
        }

        else if (inputArray[i]== "/")
        {
            try
            {
                int store1 = stackCreated.Pop();
                int store2 = stackCreated.Pop();
                stackCreated.Push(store2 / store1);
            }
            catch
            {
            }
        }

        else if (inputArray[i] == "^")
        {
            try
            {
                int store1 = stackCreated.Pop();
                int store2 = stackCreated.Pop();
                stackCreated.Push.Math.Pow(store1, store2);
            }
            catch
            {
            }
        }

}
while(i++ < end && inputArray[i]!= "=" && stackCreated.Count != 0);
string result = inputValue + " " + stackCreated.Pop().ToString() + Environment.NewLine;
TxtOutputBox.AppendText(result);
TxtInputBox.Clear();

}

Solution

  • Should be:

    stackCreated.Push((int)Math.Pow(store1, store2));
    

    This will perform the power operation, and then push the result onto the stack.

    Its like doing:

    int tmp = (int)Math.Pow(store1, store2);
    stackCreated.Push(tmp);
    

    Also note that Math.Pow works on doubles. Your ints (store1 and store2) will be automatically converted up to a double, but you need to tell the compiler to cast the result back down to an int.