2015-05-17 34 views
-1

我正在製作一個簡單的計算器。到目前爲止,我已經成功地在我的計算器中實現了一些基本功能。看看代碼在C中使用括號()計算器#

public double num1 { get; set; } 
     public double num2 { get; set; } 
     public string op { get; set; } 
     // public bool checker { get; set; } 

     private void ButtonBase_OnClick1(object sender, RoutedEventArgs e) 
     { 
      CalculateTextBlock.Text += Button1.Content.ToString(); 
      ShowTextBlock.Text += Button1.Content.ToString(); 
     } 

     private void ButtonBase_OnClick2(object sender, RoutedEventArgs e) 
     { 
      CalculateTextBlock.Text += Button2.Content.ToString(); 
      ShowTextBlock.Text += Button2.Content.ToString(); 
     } 

     private void ButtonBase_OnClick3(object sender, RoutedEventArgs e) 
     { 
      CalculateTextBlock.Text += Button3.Content.ToString(); 
      ShowTextBlock.Text += Button3.Content.ToString(); 
     } 

     private void ButtonBase_OnClick4(object sender, RoutedEventArgs e) 
     { 
      CalculateTextBlock.Text += Button4.Content.ToString(); 
      ShowTextBlock.Text += Button4.Content.ToString(); 
     } 

     private void ButtonBase_OnClick5(object sender, RoutedEventArgs e) 
     { 
      CalculateTextBlock.Text += Button5.Content.ToString(); 
      ShowTextBlock.Text += Button5.Content.ToString(); 
     } 

     private void ButtonBase_OnClick6(object sender, RoutedEventArgs e) 
     { 
      CalculateTextBlock.Text += Button6.Content.ToString(); 
      ShowTextBlock.Text += Button6.Content.ToString(); 
     } 

     private void ButtonBase_OnClick7(object sender, RoutedEventArgs e) 
     { 
      CalculateTextBlock.Text += Button7.Content.ToString(); 
      ShowTextBlock.Text += Button7.Content.ToString(); 
     } 

     private void ButtonBase_OnClick8(object sender, RoutedEventArgs e) 
     { 
      CalculateTextBlock.Text += Button8.Content.ToString(); 
      ShowTextBlock.Text += Button8.Content.ToString(); 
     } 

     private void ButtonBase_OnClick9(object sender, RoutedEventArgs e) 
     { 
      CalculateTextBlock.Text += Button9.Content.ToString(); 
      ShowTextBlock.Text += Button9.Content.ToString(); 
     } 

     private void ButtonBase_OnClick0(object sender, RoutedEventArgs e) 
     { 
      CalculateTextBlock.Text += Button0.Content.ToString(); 
      ShowTextBlock.Text += Button0.Content.ToString(); 
     } 

     private void ButtonBase_OnClickdot(object sender, RoutedEventArgs e) 
     { 
      CalculateTextBlock.Text += Buttondot.Content.ToString(); 
      ShowTextBlock.Text += Buttondot.Content.ToString(); 
     } 

     private void ButtonBase_OnClickobrac(object sender, RoutedEventArgs e) 
     { 
      CalculateTextBlock.Text += Buttonobrac.Content; 
      ShowTextBlock.Text += Buttonobrac.Content.ToString(); 
     } 

     private void ButtonBase_OnClickcbrac(object sender, RoutedEventArgs e) 
     { 
      CalculateTextBlock.Text += Buttoncbrac.Content; 
      ShowTextBlock.Text += Buttoncbrac.Content.ToString(); 
     } 



     //private void ButtonBase_OnClickinf(object sender, RoutedEventArgs e) 
     //{ 
     // CalculateTextBlock.Text += ("0/0").ToString(); 
     // ShowTextBlock.Text += "Inf."; 
     //} 

     private void ButtonBase_OnClickplus(object sender, RoutedEventArgs e) 
     { 
      //num1 += double.Parse(CalculateTextBlock.Text); 
      num1 = Convert.ToDouble(CalculateTextBlock.Text); 
      op = "plus"; 
      CalculateTextBlock.Text = ""; 
      ShowTextBlock.Text += "+"; 
     } 

     private void ButtonBase_OnClickminus(object sender, RoutedEventArgs e) 
     { 
      num1 += double.Parse(CalculateTextBlock.Text); 
      op = "sub"; 
      CalculateTextBlock.Text = ""; 
      ShowTextBlock.Text += "-"; 
     } 

     private void ButtonBase_OnClickmul(object sender, RoutedEventArgs e) 
     { 
      num1 += double.Parse(CalculateTextBlock.Text); 
      op = "mul"; 
      CalculateTextBlock.Text = ""; 
      ShowTextBlock.Text += "*"; 
     } 

     private void ButtonBase_OnClickdiv(object sender, RoutedEventArgs e) 
     { 
      num1 += double.Parse(CalculateTextBlock.Text); 
      op = "div"; 
      CalculateTextBlock.Text = ""; 
      ShowTextBlock.Text += "/"; 
     } 

     private void ButtonBase_OnClickequal(object sender, RoutedEventArgs e) 
     { 
      switch (op) 
      { 
       case "plus" : 
        //num2 = num1 + double.Parse(CalculateTextBlock.Text); 
        num2 = num1 + Convert.ToDouble(CalculateTextBlock.Text); 
        break; 

       case "sub": 
        num2 = num1 - double.Parse(CalculateTextBlock.Text); 
        break; 

       case "mul": 
        num2 = num1*double.Parse(CalculateTextBlock.Text); 
        break; 

       case "div": 
        num2 = num1/double.Parse(CalculateTextBlock.Text); 
        break; 
      } 
      CalculateTextBlock.Text = num2.ToString(); 
      num1 = 0; 
      ShowTextBlock.Text = ""; 

     } 

     private void ButtonBase_OnClickclear(object sender, RoutedEventArgs e) 
     { 
      num1 = 0; 
      num2 = 0; 
      ShowTextBlock.Text = ""; 
      CalculateTextBlock.Text = ""; 
     } 

將括號插入calculatetextbox時會出現問題。它在解析時拋出一個FormatException。有什麼方法可以在這裏實施BIDMAS嗎?

注:我曾經嘗試都解析和轉換方法{int.parse和轉換}

任何幫助表示讚賞。由於

+0

是否所有這些代碼真的有必要嗎?我認爲你應該只顯示那些需要得到很好答案的部分。 – Mohayemin

+0

@Mohayemin他們只是計算器的計算部分。對於任何第一次看到此代碼的人,我都會很容易理解我想要做的事情。只有數字按鈕部分不是必需的。 – Prajjwal

+4

BIDMAS需要更復雜的代碼。你可能需要做的是解析你的字符串來計算括號對,從最外部到最內部。然後,如果找到括號,則評估其內容(可能包含更多的括號),遞歸地重複此操作直到找不到括號,然後返回評估結果。 –

回答

0

好像它只是缺少的ToString您Bottonobrac.Content()的轉換中,你必須在你的Buttondot.Content

private void ButtonBase_OnClickdot(object sender, RoutedEventArgs e) 
    { 
     CalculateTextBlock.Text += Buttondot.Content.ToString(); //You have it here 
     ShowTextBlock.Text += Buttondot.Content.ToString(); 
    } 

    private void ButtonBase_OnClickobrac(object sender, RoutedEventArgs e) 
    { 
     CalculateTextBlock.Text += Buttonobrac.Content; //Add .ToString() 
     ShowTextBlock.Text += Buttonobrac.Content.ToString(); 
    } 

這應該解決您的問題。同時,您可以使用compile and execute c# online來測試代碼中缺少的內容。

+0

我以前試過tostring方法。它只是拋出FormatException。這就是爲什麼我嘗試刪除「ToString」方法。 – Prajjwal

0

試試這個: -

<script type="text/javascript"> 
     function Calculate() { 

      var expression = document.getElementById('<%=this.TextBox1.ClientID%>').value; 
      var result = eval(expression); 
      var hidden = document.getElementById('<%=this.hdfResult.ClientID%>'); 
      hidden.value = result; 

     } 
    </script> 

和按鈕單擊事件寫這OnClientClick="Calculate();即調用腳本。 欲瞭解更多信息,請通過這個Link

OR

試試這個

<form id="Calc" runat="server"> 
    <div> 

    <table border= "4"> 
    <tr> 
    <td> 
    <input type="text" name="Input" size="16" \> 
    <br /> 
    </td> 
    </tr> 
    <tr> 
    <td> 
    <input type="button" name="one" value=" 1 " onclick ="Calc.Input.value += '1'" \> 
    <input type="button" name="two" value=" 2 " onclick="Calc.Input.value += '2'" \> 
    <input type="button" name="three" value=" 3 " onclick="Calc.Input.value += '3'" \> 
    <input type="button" name="plus" value=" + " onclick="Calc.Input.value += ' + '" \> 
    <br /> 
    <input type="button" name="four" value=" 4 " onclick="Calc.Input.value += '4'" \> 
    <input type="button" name="five" value=" 5 " onclick="Calc.Input.value += '5'" \> 
    <input type="button" name="six" value=" 6 " onclick="Calc.Input.value += '6'" \> 
    <input type="button" name="minus" value=" - " onclick="Calc.Input.value += ' - '" \> 
    <br /> 
    <input type="button" name="seven" value=" 7 " onclick="Calc.Input.value += '7'" \> 
    <input type="button" name="eight" value=" 8 " onclick="Calc.Input.value += '8'" \> 
    <input type="button" name="nine" value=" 9 " onclick="Calc.Input.value += '9'" \> 
    <input type="button" name="times" value=" x " onclick="Calc.Input.value += ' * '" \> 
    <br /> 
    <input type="button" name="clear" value=" c " onclick="Calc.Input.value = ''" \> 
    <input type="button" name="zero" value=" 0 " onclick="Calc.Input.value += '0'" \> 
    <input type="button" name="DoIt" value=" = " onclick="Calc.Input.value = eval(Calc.Input.value)" \> 
    <input type="button" name="div" value="/" onclick="Calc.Input.value += '/'" \> 
    <br /> 
    </td> 
    </tr> 
    </table> </div> 
     </form> 
+0

...爲什麼你用JavaScript回答C#問題? –

+0

不能在C#中使用JavaScript# – Prajjwal

+0

Okkk但第二種方法正在工作.. –

0

@Prajjwal我也做了一個簡單的計算器上的Microsoft Visual Studio Express的2013年網絡。這是我的代碼。看一看!希望它可以幫助:

public partial class Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    protected void addButton_Click(object sender, EventArgs e) 
    { 
     double firstValue = double.Parse(firstTextBox.Text); 
     double secondValue = double.Parse(secondTextBox.Text); 
     double addValue = firstValue + secondValue; 
     resultLabel.Text = addValue.ToString(); 
    } 

    protected void subtractButton_Click(object sender, EventArgs e) 
    { 
     double firstValue = double.Parse(firstTextBox.Text); 
     double secondValue = double.Parse(secondTextBox.Text); 
     double addValue = firstValue - secondValue; 
     resultLabel.Text = addValue.ToString(); 
    } 

    protected void multiplicationButton_Click(object sender, EventArgs e) 
    { 
     double firstValue = double.Parse(firstTextBox.Text); 
     double secondValue = double.Parse(secondTextBox.Text); 
     double addValue = firstValue * secondValue; 
     resultLabel.Text = addValue.ToString(); 
    } 

    protected void divideButton_Click(object sender, EventArgs e) 
    { 
     double firstValue = double.Parse(firstTextBox.Text); 
     double secondValue = double.Parse(secondTextBox.Text); 
     double addValue = firstValue/secondValue; 
     resultLabel.Text = addValue.ToString(); 
    } 
} 
+0

感謝代碼,但它不能解決括號問題。 – Prajjwal