2014-12-08 36 views
-4

我在c#中很新!任何人都可以幫助我如何在已經在WPF中創建的計算器中添加一個sqrt按鈕。我粘貼在這裏的代碼工作正常,但我必須添加另一個應該是平方根的按鈕,但我不知道該怎麼做。請誰能幫助我,我真的很感激如何在WPF計算器中添加sqrt c#

using System; 
using System.Windows; 
using System.Windows.Controls; 

namespace MyCalculatorv1 
{ 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     // all button 0 to 9 go here. 
     private void Button_Click_1(object sender, RoutedEventArgs e) 
     { 
      Button b = (Button) sender; 
      tb.Text += b.Content.ToString(); 
     } 

     // equal button goes here 
     private void Result_click(object sender, RoutedEventArgs e) 
     { 
      try 
      { 
       result(); 
      } 
      catch (Exception err) 
      { 
       tb.Text = "Error!"; 
      } 
     } 

     private void result() 
     { 
      String op; 
      int iOp = 0; 
      if (tb.Text.Contains("+")) 
      { 
       iOp = tb.Text.IndexOf("+"); 
      } 
      else if (tb.Text.Contains("-")) 
      { 
       iOp = tb.Text.IndexOf("-"); 
      } 
      else if (tb.Text.Contains("*")) 
      { 
       iOp = tb.Text.IndexOf("*"); 
      } 
      else if (tb.Text.Contains("/")) 
      { 
       iOp = tb.Text.IndexOf("/"); 
      } 
      else 
      { 
       //error 
      } 

      op = tb.Text.Substring(iOp, 1); 
      double op1 = Convert.ToDouble(tb.Text.Substring(0, iOp)); 
      double op2 = Convert.ToDouble(tb.Text.Substring(iOp + 1, tb.Text.Length - iOp - 1)); 

      if (op == "+") 
      { 
       tb.Text += "=" + (op1 + op2); 
      } 
      else if (op == "-") 
      { 
       tb.Text += "=" + (op1 - op2); 
      } 
      else if (op == "*") 
      { 
       tb.Text += "=" + (op1 * op2); 
      } 
      else 
      { 
       tb.Text += "=" + (op1/op2); 
      } 
     } 

     private void Off_Click_1(object sender, RoutedEventArgs e) 
     { 
      Application.Current.Shutdown(); 
     } 

     private void Del_Click(object sender, RoutedEventArgs e) 
     { 
      tb.Text = ""; 
     } 

     private void R_Click(object sender, RoutedEventArgs e) 
     { 
      if (tb.Text.Length > 0) 
      { 
       tb.Text = tb.Text.Substring(0, tb.Text.Length - 1); 
      } 
     } 

     private void about(object sender, RoutedEventArgs e) 
     { 
      MessageBox.Show("simple calculator"); 
     } 
    } 
} 
+0

哪一部分代碼拋出一個錯誤?如果沒有人爲您提供快速解答,請參閱http://stackoverflow.com/help/mcve以獲取關於將此編輯爲更好形狀的指導。 – bmike 2014-12-08 01:29:19

回答

1

Math.Sqrt

XAML:

<Grid> 
    <Button Content="&#8730;" HorizontalAlignment="Left" Margin="145,10,0,0" VerticalAlignment="Top" Width="75" 
      Click="OnSquareRootClick"/> 
    <TextBox x:Name="txtNumber" HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" Text="" VerticalAlignment="Top" Width="120"/> 
    <TextBox x:Name="txtResult" HorizontalAlignment="Left" Height="23" Margin="240,10,0,0" Text="" VerticalAlignment="Top" Width="120"/> 
</Grid> 

代碼背後:

private void OnSquareRootClick(object sender, RoutedEventArgs e) 
    { 
     double number; 
     var isDouble = double.TryParse(this.txtNumber.Text, out number); 
     if (isDouble) 
     { 
      this.txtResult.Text = 
       string.Format(
        "{0}{1} = {2}", 
        "\u221A", 
        this.txtNumber.Text, 
        Math.Round(Math.Sqrt(number), 2)); 
     } 
    } 
+0

謝謝你的幫助。它只適用於數字9,4和1,當我點擊其他數字時,它只顯示=符號,它不顯示任何結果!除了這些命令之外,我還應該做些什麼嗎?請我需要進一步的幫助,謝謝 – danny 2014-12-08 04:09:30

+0

你將需要調試,確保tb.Text是數字,因爲如果它不是數字我的代碼將不會做任何事情。我更新了包括一輪 – 2014-12-08 06:49:28

+0

更新我的答案,以清楚說明,在樣本項目上進行測試。你需要根據你的需求來改變它 – 2014-12-08 07:00:33