2012-11-30 47 views
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); 
     } 

    } 
} 

​​3210

+0

請添加樣品輸入 – Habib

+0

第一if語句可以被縮短爲'如果( 「= + - * /」。的indexOf(inputarray [I])== -1)'這也更具有可讀性。可選:var operators = new [] {'=','+',' - ','*','/'};如果(!operators.Contains(inputarray [i]))' – jgauffin

+0

所有其他的if語句都可以用'IOperatorExecutor'接口和'Dictionary ' – jgauffin

回答

2

在你的do循環的每次迭代之後,你在做什麼來增加i?我試過你的代碼,看起來好像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); 
     } 
    } 
} 
+0

+1良好的觀察。我現在在最後一個'if else'語句之後添加了'i ++',但它返回了不尋常的結果 – techAddict82

+1

您的減號,除法和乘法運算符是錯誤的(可能會複製粘貼問題),並且'txtout'應該以'TxtInputBox.Text' ,而不是'TxtInputBox' – John

+0

@John你是對的! minus操作符正在添加,並且輸出框顯示了'System.Windows.Forms.TextBox,Text:5 6 - = 6',而不是預期的輸出:'5 6 - = -1' –

3

拆分命令,不帶參數,被分裂的空間和其他空白的字符串。

在 - =之間的輸入中沒有空格,因此它被視爲與if語句中的測試不匹配的一個標記。

原始答案不正確地提示沒有參數的Split會分裂爲單個字符。

+1

前者不是事實。.Split()與空參數隱式分隔空格。 http://msdn.microsoft.com/en-us/library/ms131448.aspx – John

+0

@John - 我將編輯答案。 –

+0

+1用於更正您的答案。你能幫我解決我的代碼中的分裂問題嗎? – techAddict82