用VS的C#.NET语言设计一个计算器,求源代码......窗体的,最好带有注释!

哪位大哥会,本人VS菜鸟
2025-01-05 12:56:47
推荐回答(2个)
回答(1):

你在界面中拉三个textbox和四个buttoo就可以了

//加法
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("请输入两个数字");
}
else
{
double FstNum = Convert.ToDouble(textBox1.Text);
double SndNum = Convert.ToDouble(textBox2.Text);
double Sum = FstNum + SndNum;
textBox3.Text = Convert.ToString(Sum);
}
}
//减法
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("请输入两个数字");
}
else
{
double FstNum = Convert.ToDouble(textBox1.Text);
double SndNum = Convert.ToDouble(textBox2.Text);
double Sum = FstNum - SndNum;
textBox3.Text = Convert.ToString(Sum);
}
}
//乘法
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("请输入两个数字");
}
else
{
double FstNum = Convert.ToDouble(textBox1.Text);
double SndNum = Convert.ToDouble(textBox2.Text);
double Sum = FstNum * SndNum;
textBox3.Text = Convert.ToString(Sum);
}
}
//除法
private void button4_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("请输入两个数字");
}
if (Convert.ToDouble(textBox1.Text) == 0 || Convert.ToDouble(textBox2.Text) == 0)
{
MessageBox.Show("两个数字均不能为0");
}
else
{
double FstNum = Convert.ToDouble(textBox1.Text);
double SndNum = Convert.ToDouble(textBox2.Text);
double Sum = FstNum / SndNum;
textBox3.Text = Convert.ToString(Sum);
}
}

回答(2):

楼上的回答只是做了基本功能。。。并不能连加,连减
所以需要在方法内加上,一个判断
比如:
string firstNum = textbox1.text.trim();
if(firstNum!="" || firstNum!=null)
{
//楼上的加法代码..此处省略
}

其他的就类推吧..