使用一个文本框创建计算器,用于接受数据和使用c#wpf
显示数据public partial class MainWindow : Window
{
int i,j;
double sum = 0.0;
Char sign;
public MainWindow()
{
InitializeComponent();
}
private void btn0_Click(object sender, RoutedEventArgs e)
{
tb1.Text = tb1.Text + System.Convert.ToString(0);
}
private void btn1_Click(object sender, RoutedEventArgs e)
{
tb1.Text = tb1.Text + System.Convert.ToString(1);
}
private void btn2_Click(object sender, RoutedEventArgs e)
{
tb1.Text = tb1.Text + System.Convert.ToString(2);
}
private void btn3_Click(object sender, RoutedEventArgs e)
{
tb1.Text = tb1.Text + System.Convert.ToString(3);
}
private void btn4_Click(object sender, RoutedEventArgs e)
{
tb1.Text = tb1.Text + System.Convert.ToString(4);
}
private void btn5_Click(object sender, RoutedEventArgs e)
{
tb1.Text = tb1.Text + System.Convert.ToString(5);
}
private void btn6_Click(object sender, RoutedEventArgs e)
{
tb1.Text = tb1.Text + System.Convert.ToString(6);
}
private void btn7_Click(object sender, RoutedEventArgs e)
{
tb1.Text = tb1.Text + System.Convert.ToString(7);
}
private void btn8_Click(object sender, RoutedEventArgs e)
{
tb1.Text = tb1.Text + System.Convert.ToString(8);
}
private void btn9_Click(object sender, RoutedEventArgs e)
{
tb1.Text = tb1.Text + System.Convert.ToString(9);
}
private void btn_eql_Click(object sender, RoutedEventArgs e)
{
j = System.Convert.ToInt32(tb1.Text);
switch (sign)
{
case 'p': sum = i + j;
tb1.Text = System.Convert.ToString(sum);
break;
case 's': sum = i - j;
tb1.Text = System.Convert.ToString(sum);
break;
case 'm': sum = i * j;
tb1.Text = System.Convert.ToString(sum);
break;
case 'd': sum = System.Convert.ToDouble(i) / System.Convert.ToDouble(j);
tb1.Text = System.Convert.ToString(sum);
break;
}
}
private void btnp_Click(object sender, RoutedEventArgs e)
{
tb1.Text = tb1.Text + System.Convert.ToString("+");
}
private void btns_Click(object sender, RoutedEventArgs e)
{
tb1.Text = tb1.Text + System.Convert.ToString("-");
}
private void btnm_Click(object sender, RoutedEventArgs e)
{
tb1.Text = tb1.Text + System.Convert.ToString("*");
}
private void btnd_Click(object sender, RoutedEventArgs e)
{
tb1.Text = tb1.Text + System.Convert.ToString("/");
}
private void btn_exit_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void btn_dot_Click(object sender, RoutedEventArgs e)
{
tb1.Text = tb1.Text + System.Convert.ToString(".");
}
private void btn_ob_Click(object sender, RoutedEventArgs e)
{
tb1.Text = tb1.Text + System.Convert.ToString("(");
}
private void btn_cb_Click(object sender, RoutedEventArgs e)
{
tb1.Text = tb1.Text + System.Convert.ToString(")");
}
private void btnc_Click(object sender, RoutedEventArgs e)
{
tb1.Text = String.Empty;
}
private void btnb_Click(object sender, RoutedEventArgs e)
{
tb1.Text = tb1.Text.Substring(0, tb1.Text.Length - 1);
}
答案 0 :(得分:0)
string s = "3+57";
MatchCollection matches = Regex.Matches( s, @"(\d+)\+(\d+)", RegexOptions.None );
int i = int.Parse( matches[0].Groups[1].Value );
int j = int.Parse( matches[0].Groups[2].Value );
int sum = i + j;
答案 1 :(得分:0)
string[] str=tb1.Text.Split('+');
int i=int.Parse(str[0]);
int j=int.Parse(str[1]);
答案 2 :(得分:0)
您可以使用regex
拆分文本并存储值。
string[] MyNum = Regex.Split(tb1.Text, "+");
int i = (int)(str[0]);
int j = (int)(str[1]);