using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace wdjisuanqi
{
public partial class Form1 : Form
{
protected double iNum1;
protected char cOperator;
protected bool bNumBegins;
protected bool bDot;
protected int curBase;
public Form1()
{
InitializeComponent();
InitMembers();
}
private void InitMembers()
{
iNum1 = 0;
curBase = 10;
cOperator = '=';
bNumBegins = true;
bDot = false;
}
private void Numbers_Click(long i)
{
if (textBox1.Text == "Error")
{
textBox1.Text = "0";
}
try
{
if (bNumBegins)
{
textBox1.Text = i.ToString();
bNumBegins = false;
}
else
{
textBox1.Text += i.ToString();
}
double iCurrent = double.Parse(textBox1.Text);
}
catch
{//捕捉异常
textBox1.Text = "Error";
InitMembers();
return;
}
}
private void Dot_Click()
{
//不允许在一个数中输入两个或两个以上的小数点
if (bDot)
{
textBox1.Text = "Error";
InitMembers();
return;
}
else if (bNumBegins)
{
textBox1.Text = "0.";
bNumBegins = false;
bDot = true;
}
else
textBox1.Text += ".";
bDot = true;
}
private void Operators_Click(char op)
{
double iCurrent;
try
{
iCurrent = double.Parse(textBox1.Text);
}
catch
{
textBox1.Text = "Error";
InitMembers();
return;
}
//计算结果
double result;
try
{
switch (cOperator)
{
case '+':
checked { result = iNum1 + iCurrent; }
break;
case '-':
checked { result = iNum1 - iCurrent; }
break;
case '*':
checked { result = iNum1 * iCurrent; }
break;
case '/':
checked { result = iNum1 / iCurrent; }
break;
default:
result = iCurrent;
break;
}
}
catch
{
//如果在计算中发生溢出,则显示"Error"
textBox1.Text = "Error";
InitMembers();
return;
}
//输出结果,并把当前结果保存起来
textBox1.Text = result.ToString();
iNum1 = result;
//准备接受下一个数
bNumBegins = true;
bDot = false;
//保存符号
cOperator = op;
}
private void Fun_Click(char op)
{
double iCurrent;
try
{
iCurrent = double.Parse(textBox1.Text);
iCurrent = Math.PI * iCurrent / 180.0;//把弧度转化为度数
}
catch
{
textBox1.Text = "Error";
InitMembers();
return;
}
//计算结果
double result;
try
{
switch (op)
{
case 's':
checked { result = Math.Sin(iCurrent); }
break;
case 'c':
checked { result = Math.Cos(iCurrent); }
break;
default:
result = Math.Tan(iCurrent);
break;
}
}
catch
{
//如果在计算中发生溢出,则显示"Error"
textBox1.Text = "Error";
InitMembers();
return;
}
//输出结果,并把当前结果保存起来
textBox1.Text = result.ToString();
iNum1 = result;
//准备接受下一个数
bNumBegins = true;
bDot = false;
}
private void btnClear_Click(object sender, EventArgs e)
{
textBox1.Text = "0";
InitMembers();
return;
}
private void button1_Click(object sender, EventArgs e)
{
Numbers_Click(1);
}
private void button2_Click(object sender, EventArgs e)
{
Numbers_Click(2);
}
private void button3_Click(object sender, EventArgs e)
{
Numbers_Click(3);
}
private void button4_Click(object sender, EventArgs e)
{
Numbers_Click(4);
}
private void button5_Click(object sender, EventArgs e)
{
Numbers_Click(5);
}
private void button6_Click(object sender, EventArgs e)
{
Numbers_Click(6);
}
private void button7_Click(object sender, EventArgs e)
{
Numbers_Click(7);
}
private void button8_Click(object sender, EventArgs e)
{
Numbers_Click(8);
}
private void button9_Click(object sender, EventArgs e)
{
Numbers_Click(9);
}
private void button0_Click(object sender, EventArgs e)
{
Numbers_Click(0);
}
private void btnAdd_Click(object sender, EventArgs e)
{
Operators_Click('+');
}
private void btnSubstract_Click(object sender, EventArgs e)
{
Operators_Click('-');
}
private void btnMultiply_Click(object sender, EventArgs e)
{
Operators_Click('*');
}
private void btnDivide_Click(object sender, EventArgs e)
{
Operators_Click('/');
}
private void btnDot_Click(object sender, EventArgs e)
{
Dot_Click();
}
private void btnEquals_Click(object sender, EventArgs e)
{
Operators_Click('=');
}