2

我正在研究一个反向波兰符号计算器。我创建了一个方法来处理计算,但我的代码中有三行导致错误。每=执行一次操作,然后显示。我试图从中获取字符串TxtInputBox并转换为整数,但它总是显示捕获消息Please check the input。然后没有任何东西被计算或显示。我确信我的第一个 if 语句将检查实际整数并避免字符。我的最终目标是输入 rpn 格式的公式,并将结果显示在多行文本框中。

样本输入 5 6 -=

代码

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

        private void RPNCalc(TextBox TxtBoxInputbox, TextBox TxtBoxOutputbox)
        {
            Stack<int> stackone = new Stack<int>();
            stackone.Clear();
            string[] inputarray = TxtBoxInputbox.Text.Split();
            int end = inputarray.Length - 1;
            int numinput;
            int i = 0;

            do
            {
                if(inputarray[i] != "=" && inputarray[i] != "+" && inputarray[i] != "-" && inputarray[i] != "*" && inputarray[i] != "/")  
                {
                    try
                    {
                        numinput = Convert.ToInt32(inputarray[i]);
                        stackone.Push(numinput);
                    }
                    catch
                    {
                        MessageBox.Show("Please check the input");
                    }
                }

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

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

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

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

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

            }
            while(i < end && inputarray[i]!= "=" && stackone.Count != 0);
            string txtout = TxtInputBox + " " + stackone.Pop().ToString() + Environment.NewLine;
            TxtOutputBox.AppendText(txtout);
            TxtInputBox.Clear();

        }

        private void Btn_Calc_Click(object sender, EventArgs e)
        {
            RPNCalc(TxtInputBox, TxtOutputBox);
        }

    }
}

在此处输入图像描述

4

2 回答 2

3

不带参数的拆分命令将字符串拆分为空格和其他空格。

-= 之间的输入中没有空格,因此它被视为与 if 语句中的测试不匹配的一个标记。

原始答案错误地表明没有参数的拆分正在拆分为单个字符。

于 2012-11-30T06:04:54.407 回答
2

i在循环的每次迭代之后,你在做什么来增加do?我尝试了您的代码,似乎i永远不会增加。此外,当你赶上并跑

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

您也许可以将其更改为:

catch (Exception e) 
{
    MessageBox.Show(e.ToString());
}

所以你可以确定你正在捕捉什么,以及为什么。

编辑:

这是我的代码版本,现在可以正常工作:

  • i在每次迭代中递增
  • 修复了减号、乘法和除法运算符中的拼写错误,导致它们改为进行加法运算
  • 删除了多余的加法运算符
namespace rpncalc {
    public partial class Form1 : Form {
        public Form1 () {
            InitializeComponent();
        }

        private void RPNCalc (TextBox TxtBoxInputbox, TextBox TxtBoxOutputbox) {
            Stack<int> stackone = new Stack<int>();
            stackone.Clear();
            string[] inputarray = TxtBoxInputbox.Text.Split();
            int end = inputarray.Length - 1;
            int numinput;
            int i = 0;

            do {
                if (inputarray[i] != "=" && inputarray[i] != "+" && inputarray[i] != "-" && inputarray[i] != "*" && inputarray[i] != "/") {
                    try {
                        numinput = Convert.ToInt32(inputarray[i]);
                        stackone.Push(numinput);
                    } catch (Exception e) {
                        MessageBox.Show(e.ToString());
                    }
                } else if (inputarray[i] == "+") {
                    try {
                        int store1 = stackone.Pop();
                        int store2 = stackone.Pop();
                        stackone.Push(store2 + store1);
                    } catch {
                    }
                } else if (inputarray[i] == "-") {
                    try {
                        int store1 = stackone.Pop();
                        int store2 = stackone.Pop();
                        stackone.Push(store2 - store1);
                    } catch {
                    }
                } else if (inputarray[i] == "*") {
                    try {
                        int store1 = stackone.Pop();
                        int store2 = stackone.Pop();
                        stackone.Push(store2 * store1);
                    } catch {
                    }
                } else if (inputarray[i] == "/") {
                    try {
                        int store1 = stackone.Pop();
                        int store2 = stackone.Pop();
                        stackone.Push(store2 / store1);
                    } catch {
                    }
                }
            }
            while (i++ < end && inputarray[i] != "=" && stackone.Count != 0);
            string txtout = TxtInputBox.Text + " " + stackone.Pop().ToString() + Environment.NewLine;
            TxtOutputBox.AppendText(txtout);
            TxtInputBox.Clear();

        }

        private void Btn_Calc_Click (object sender, EventArgs e) {
            RPNCalc(TxtInputBox, TxtOutputBox);
        }
    }
}
于 2012-11-30T06:28:40.857 回答