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