using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
double a;
double b;
string ysf;
bool ahavenumber;
bool isdeng;
public Form1()
{
InitializeComponent();
}
private void AddNum(string s)
{
if (isdeng)//加入点击了等号 则把内容清空
{
a = 0;
this.textBox1.Text = "0";
ahavenumber = false;
isdeng = false;
}
else
{
if (ysf != "" && !ahavenumber) //要是没还没点击运算符 或者文本框还没有值
{
a = Convert.ToDouble(this.textBox1.Text);
this.textBox1.Text = "0";
ahavenumber = true;
}
}
if (this.textBox1.Text== "0") //文本框的内容为0
{
if (s != ".")//要是没有小数点 则是顺着写整数
this.textBox1.Text = s;
else//要是加了小数点 则开始写小数部分
this.textBox1.Text = this.textBox1.Text + s;
return;
}
switch (s)
{
case"."://要是输入的是小数点 而且文本中还没有出现过小数点 则在后面加个小数点
if (this.textBox1.Text.IndexOf(".") == -1)
{
this.textBox1.Text = this.textBox1.Text + ".";
}
break;
case "": break;
default: this.textBox1.Text = this.textBox1.Text + s;
break;
}
}
private void Form1_Load(object sender, EventArgs e)
{
ysf = "";
isdeng = false;
}
private void button20_Click(object sender, EventArgs e)
{//如果文本框没有负号 则在前面加个负号
if (this.textBox1.Text.IndexOf("-") == -1)
this.textBox1.Text = "-" + this.textBox1.Text;
else//否则就把第一位给去掉(也就是把最前面的负号去掉)
this.textBox1.Text = this.textBox1.Text.Substring(1);
}
private void button19_Click(object sender, EventArgs e)
{
AddNum(".");
}
private void button21_Click(object sender, EventArgs e)
{
AddNum("0");
}
private void button16_Click(object sender, EventArgs e)
{
AddNum("1");
}
private void button15_Click(object sender, EventArgs e)
{
AddNum("2");
}
private void button14_Click(object sender, EventArgs e)
{
AddNum("3");
}
private void button11_Click(object sender, EventArgs e)
{
AddNum("4");
}
private void button10_Click(object sender, EventArgs e)
{
AddNum("5");
}
private void button9_Click(object sender, EventArgs e)
{
AddNum("6");
}
private void button2_Click(object sender, EventArgs e)
{
AddNum("7");
}
private void button3_Click(object sender, EventArgs e)
{
AddNum("8");
}
private void button4_Click(object sender, EventArgs e)
{
AddNum("9");
}
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
}
private double cal(double a, double b, string c)
{
switch (c)
{
case "+":return a + b;
case "-":return a - b;
case "*":return a * b;
case "/":return a / b;
case "%":return a % b;
default: return 0;
}
}
private double kf(double i)
{
return Math.Sqrt(i);
}
private double ds(double i)
{
return (1 / i);
}
private void button17_Click(object sender, EventArgs e)
{
b = Convert.ToDouble(this.textBox1.Text);
this.textBox1.Text = cal(a, b, ysf).ToString();
ysf = "";
isdeng = true;
}
private void button12_Click(object sender, EventArgs e)
{
ysf = "%";
}
private void button18_Click(object sender, EventArgs e)
{
ysf = "+";
}
private void button13_Click(object sender, EventArgs e)
{
ysf = "-";
}
private void button8_Click(object sender, EventArgs e)
{
ysf = "*";
}
private void button5_Click(object sender, EventArgs e)
{
ysf = "/";
}
private void button6_Click(object sender, EventArgs e)
{
a = Convert.ToDouble(this.textBox1.Text);
this.textBox1.Text = ds(a).ToString();
ysf = "";
isdeng = true;
}
private void button7_Click(object sender, EventArgs e)
{
a = Convert.ToDouble(this.textBox1.Text);
this.textBox1.Text = kf(a).ToString();
ysf = "";
isdeng = true;
}
}
}