反向波兰表示法 - 逻辑和结果显示

时间:2012-11-30 05:52:35

标签: c#

我正在研究反向抛光记谱计算器。我创建了一个将负责计算的方法,但我的代码中有三行导致错误。执行每个=操作然后显示。我试图从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);
        }

    }
}

enter image description here

2 个答案:

答案 0 :(得分:3)

Split命令,没有参数,在空格和其他空格上拆分字符串。

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

原始答案错误地提示没有参数的Split分裂为单个字符。

答案 1 :(得分: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);
        }
    }
}