2012-11-30 31 views
0

我是新手C#編碼人員,目前正在研究反向波蘭符號計算器。我創建了一個方法來處理計算,但是在我的代碼中有三行導致錯誤。在每個=之後執行操作然後顯示。我試圖從TxtInputBox中獲取字符串並將其轉換爲整數,但下面這行是我得到紅色波浪線的地方:string[] inputarray = TxtBoxInputbox.Text.Split();。第二次調用函數RPNCalc從按鈕單擊Btn_Calc並分配文本框時,我會收到另一條紅色波浪線:RPNCalc(TxtInputBox, TxtOutputBox);我不知道如何處理這兩個使我的表單無法工作的錯誤。請幫助使錯誤免費。反向波蘭語表示法:從輸入文本框中獲取值以執行計算

代碼

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

     private void RPNCalc(TxtBoxInputbox, 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

+0

Squiggly行很好,但實際*錯誤*是什麼? (如果您使用VS,請查看錯誤列表。)閱讀此內容(通常)會有所幫助。 –

回答

2

RPNCalc方法簽名缺少類型的參數。您的意思是:

private void RPNCalc(TextBox TxtBoxInputbox, TextBox TxtBoxOutputbox) 

側面說明,作爲RPNCalc是形式的成員函數,它已經獲得會員this.TxtBoxInputboxthis.TxtBoxOutputbox。所以你根本不需要傳遞任何參數。當我們談論這個主題時,如果它是我的代碼,我可能會重寫整個方法而沒有參數,要返回一個int(即private int RPNCalc()),它將採取this.TxtBoxInputbox中的任何內容,進行計算並返回結果。調用方法然後可以做它想要的結果(在這種情況下,將其添加到輸出)。但是,這超出了原始問題的範圍。

+0

+1謝謝你的回答和建議。雖然現在我沒有得到任何顯示。所以我必須在其他地方發生另一個我不確定的錯誤:(。 – techAddict82