0

我能够创建一个执行逆波兰表示法的函数。该方法的结构很好,我遇到的两个问题是如何获取用户输入的公式textBox1并在textBox2. 我已分配给textBox1变量rpnValue,但它给出了错误消息A field initializer cannot reference the non-static field, method, or property 'modified_rpn.Form1.textBox1'。那么我如何再次获取用户输入的公式textBox1并在多行`textBox2上显示答案(公式=答案)?

代码

namespace rpn
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string rpnValue = textBox1.Text; 

        private void RPNCalc(rpnValue)
        {
            Stack<int> stackCreated = new Stack<int>();
            try
            {
                var tokens = rpnValue.Replace("(", " ").Replace(")", " ")
                                     .Split().Where(s => !String.IsNullOrWhiteSpace(s));
                foreach (var t in tokens)
                {
                    try
                    {
                        stackCreated.Push(Convert.ToInt32(t));
                    }
                    catch
                    {
                        int store1 = stackCreated.Pop();
                        int store2 = stackCreated.Pop();
                        switch (t)
                        {
                            case "+": store2 += store1; break;
                            case "-": store2 -= store1; break;
                            case "*": store2 *= store1; break;
                            case "/": store2 /= store1; break;
                            case "%": store2 %= store1; break;
                            case "^": store2 = (int)Math.Pow(store1, store2); break; 
                            default: throw new Exception();
                        }
                        stackCreated.Push(store2);
                    }
                }

                if (stackCreated.Count != 1)
                    MessageBox.Show("Please check the input");
                else
                    textBox1.Text = stackCreated.Pop().ToString();

            }
            catch
            {
                MessageBox.Show("Please check the input");
            }

            textBox2.AppendText(rpnValue);
            textBox1.Clear();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            RPNCalc(textBox1, textBox2);
        }
    }
}

在此处输入图像描述

4

2 回答 2

1

three problems with your code :

first is, its illogical to access value of a textbox's text at line: string rpnValue = textBox1.text and secondly,

private void button1_Click(object sender, EventArgs e)
        {
            RPNCalc(textBox1, textBox2);
        }

here, you can see that you're supplying 2 arguments to RPNCalc() when its actually only expecting one. You need to seriously understand what you're trying to do here. Also you donot specify the "type" of value supplied to RPNCalc() during the definition of that method.

Re-read your C# book :-)

于 2012-12-01T18:19:03.340 回答
0

您需要移动此行:

    string rpnValue = textBox1.Text; 

在方法或函数内部。你在方法或函数之外拥有它,你不能这样做。

于 2012-12-01T18:13:16.200 回答