0

我目前正在使用反向波兰表示法。我能够执行除指数数学之外的所有操作。我知道 C# sharps 执行指数数学运算,Math.Pow但在我的代码中使用它会给我一个错误'System.Collections.Generic.Stack<T>.Push(T)' is a 'method', which is not valid in the given contextif else您可以在最后一条语句中找到具体问题。知道如何正确纠正或创建执行指数数学的方法吗?

代码

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();

}
4

1 回答 1

3

应该:

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

这将执行幂运算,然后将结果压入堆栈。

就像这样做:

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

另请注意,它Math.Pow适用于doubles。您的 ints ( store1and store2) 将自动转换为 a double,但您需要告诉编译器将结果转换回 a int

于 2012-11-30T16:30:12.097 回答