我是初学者 做了个计算器 可是计算有问题麻烦各位前辈帮下忙 看看错误在哪要怎么改 谢谢
using System;using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
static double x1, x2;
double x;
string c;
public Form1()
{
InitializeComponent();
}
private void sz_button(int n)
{
if (c == null) x1 = x1 * 10 + n;
else x2 = x2 * 10 + n;
textBox1.Text += n;
}
private void ysf_button(string m)
{
c = m;
textBox1.Text += m;
}
private void button11_Click(object sender, EventArgs e)
{
sz_button(1);
}
private void button12_Click(object sender, EventArgs e)
{
sz_button(2);
}
private void button13_Click(object sender, EventArgs e)
{
sz_button(3);
}
private void button6_Click(object sender, EventArgs e)
{
sz_button(4);
}
private void button7_Click(object sender, EventArgs e)
{
sz_button(5);
}
private void button8_Click(object sender, EventArgs e)
{
sz_button(6);
}
private void button21_Click(object sender, EventArgs e)
{
sz_button(7);
}
private void button22_Click(object sender, EventArgs e)
{
sz_button(8);
}
private void button3_Click(object sender, EventArgs e)
{
sz_button(9);
}
private void button16_Click(object sender, EventArgs e)
{
sz_button(0);
}
private void car()//运算
{ textBox1.Text += "=";
switch (c)
{
case "+": x = x1 + x2; break;
case "-": x = x1 - x2; break;
case "*": x = x1 * x2; break;
case "/": x = x1 / x2; break;
case "%": x = x1 % x2; break;
}
textBox1.Text =x.ToString();
}
private void button19_Click(object sender, EventArgs e)
{
ysf_button("+");
}
private void button14_Click(object sender, EventArgs e)
{
ysf_button("-");
}
private void button9_Click(object sender, EventArgs e)
{
ysf_button("*");
}
private void button4_Click(object sender, EventArgs e)
{
ysf_button("%");
}
private void button17_Click(object sender, EventArgs e)
{
x1 = Convert.ToDouble(textBox1.Text);
if (x1 != 0)
x1 = -x1;
textBox1.Text = x1.ToString();
}
private void button18_Click(object sender, EventArgs e)
{
if (textBox1.Text.IndexOf(".") == -1)
{
textBox1.Text += ".";
}
else
{
textBox1.Text += "";
}
}
private void button15_Click(object sender, EventArgs e)
{
x1 = Convert.ToDouble(textBox1.Text);
x = 1 / x1;
textBox1.Text = x.ToString();
}
private void button10_Click(object sender, EventArgs e)
{
ysf_button("%");
}
private void button5_Click(object sender, EventArgs e)
{
x1 = Convert.ToDouble(textBox1.Text);
x = Math.Sqrt(x1);
textBox1.Text = x.ToString();
}
private void button20_Click(object sender, EventArgs e)
{
car();//调用方法
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = "0";
x1 = 0;
x2 = 0;
x = 0;
c = "";
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
textBox1.Text = "0";
}
}
}