回复 2楼 邓士林
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double num1;
double num2;
double resutl;//操作数和运行结果
bool XiaoShu = false;//判断是否是小数
string op;
//操作数的类型
public void numClick(int Mynum)
{
if (XiaoShu)
{
textBox1.Text = textBox1.Text + Mynum;
}
else
{
if (textBox1.Text == "0.")
{
textBox1.Text = Mynum + ".";
}
else
{
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1) + Mynum + ".";
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "0";
textBox1.TextAlign = HorizontalAlignment.Right;//文本框向右对齐
}
private void button17_Click(object sender, EventArgs e)
{
num2 = Convert.ToDouble(textBox1.Text);
switch (op)
{
case "Add":
resutl = num1 + num2;
textBox1.Text = resutl.ToString();//加的情况
break;
case "Jian":
resutl = num1 - num2;
textBox1.Text = resutl.ToString();//减的情况
break;
case "Cheng":
resutl = num1 * num2;
textBox1.Text = resutl.ToString();//乘的情况
break;
case "Chu":
if (num2 == 0)
{
textBox1.Text = "被除数为0,无法整除";
}
else
{
resutl = num1 / num2;
textBox1.Text = resutl.ToString();//除的情况
}
break;
default:
resutl = Math.Sqrt(num1);
textBox1.Text = resutl.ToString();//开方的情况
break;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button16_Click(object sender, EventArgs e)
{
XiaoShu = true;
}
private void btnC_Click(object sender, EventArgs e)
{
num1 = 0;
num2 = 0;
textBox1.Text = "0";
XiaoShu = false;
}
private void button12_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
numClick(1);
}
private void button2_Click(object sender, EventArgs e)
{
numClick(2);
}
private void button3_Click(object sender, EventArgs e)
{
numClick(3);
}
private void button4_Click(object sender, EventArgs e)
{
numClick(4);
}
private void button5_Click(object sender, EventArgs e)
{
numClick(5);
}
private void button6_Click(object sender, EventArgs e)
{
numClick(6);
}
private void button7_Click(object sender, EventArgs e)
{
numClick(7);
}
private void button8_Click(object sender, EventArgs e)
{
numClick(8);
}
private void button9_Click(object sender, EventArgs e)
{
numClick(9);
}
private void button10_Click(object sender, EventArgs e)
{
numClick(0);
}
private void button14_Click(object sender, EventArgs e)
{
op = "Cheng";
num1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = "*";
XiaoShu = false;
}
private void button11_Click(object sender, EventArgs e)
{
op = "Add";
num1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = "+";
XiaoShu = false;
}
private void button13_Click(object sender, EventArgs e)
{
op = "Jian";
num1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = "-";
XiaoShu = false;
}
private void button15_Click(object sender, EventArgs e)
{
op = "Chu";
num1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = "/";
XiaoShu = false;
}
private void button18_Click(object sender, EventArgs e)
{
op = "PF";
num1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = "sqrt";
XiaoShu = false;
}
}
}