C#编程科学计算器
using System;
using System.Collections.Generic;
using https://www.360docs.net/doc/149667558.html,ponentModel;
using System.Data;
using System.Drawing;
using System.T ext;
using System.Windows.Forms;
namespace Calculator
{
public partial class Counter : Form
{
public Counter()
{
InitializeComponent();
}
double num1, num2;
double result=0;
int op;
int flag=0;//开始标志
int sp = 0;//判断除数为零时移除监听
int max = 0;//范围标识
int eq=0;
#region功能键
private void on_Click(object sender, EventArgs e)//开始
{
flag = 1;
sp = 0;
max = 0;
this.numInput.T ext = "0";
this.txtInput.Text = "";
}
private void off_Click(object sender, EventArgs e)//关闭
{
flag = 0;
sp = 0;
max = 0;
this.numInput.T ext = "";
this.txtInput.Text = "";
}
private void button9_Click(object sender, EventArgs e)//删除
{
max = 0;
if (flag == 1&&sp==0)
{
string numstring = this.numInput.T ext;
if (numstring.Length > 0)
{
string temp = numstring.Substring(0, numstring.Length - 1);
this.numInput.T ext = temp.ToString();
}
else
{
this.numInput.T ext = "0";
}
}
}
private void clear_Click(object sender, EventArgs e)//c清零
{
max = 0;
if (flag == 1)
{
sp = 0;
this.numInput.T ext = "0";
this.txtInput.T ext = "";
}
}
private void delete_Click(object sender, EventArgs e)//清零
{
max = 0;
if (flag == 1)
{
sp = 0;
this.numInput.T ext = "0";
}
}
#endregion
#region运算符
private void equal_Click(object sender, EventArgs e)//等号
{
eq = 1;
if (flag == 1 && sp == 0)
{
max = 0;
num2 = Convert.T oDouble(this.numInput.T ext);
this.txtInput.T ext += num2.T oString();
this.txtInput.T ext += "=";
txtInput.Text = null;
switch (op)
{
case 1: result = num1 + num2;
break;
case 2: result = num1 - num2;
break;
case 3: result = num1 * num2;
break;
case 4:
result = num1 / num2;
break;
case 5: result = Math.Pow(num1, num2);
break;
case 6: result = Mod(num1, num2);
break;
default: return;
}
if (op == 4&&num2 == 0)
{
this.numInput.Text = "除数不能为0";
sp = 1;
}else
//this.txtInput.T ext += result.ToString();
this.numInput.T ext = result.T oString();
}
}
private void sub_Click(object sender, EventArgs e)//减法{
max = 0;
if (flag == 1 && sp == 0)
{
num1 = Convert.T oDouble(this.numInput.T ext);
this.txtInput.T ext += num1.T oString();
numInput.T ext = "";
this.txtInput.T ext += "-";
op = 2;
}
}
private void mul_Click(object sender, EventArgs e)//乘法{
max = 0;
if (flag == 1 && sp == 0)
{
num1 = Convert.T oDouble(this.numInput.T ext);
this.txtInput.T ext += num1.T oString();
this.numInput.T ext = "";
this.txtInput.T ext += "*";
op = 3;
}
}
private void divide_Click(object sender, EventArgs e)//除法{
max = 0;
if (flag == 1 && sp == 0)
{
num1 = Convert.T oDouble(this.numInput.T ext);
this.txtInput.T ext += num1.T oString();
this.numInput.T ext = "";
this.txtInput.T ext += "/";
op = 4;
}
}
private void plus_Click(object sender, EventArgs e)//加法{
max = 0;
if (flag == 1 && sp == 0)
{
num1 = Convert.T oDouble(this.numInput.T ext);
this.txtInput.T ext += num1.T oString();
numInput.T ext = "";
this.txtInput.T ext += "+";
op = 1;
}
}
public double Mod(double x, double y)//取模运算
{
max = 0;
int temp = Convert.T oInt32(x / y);
return (x - y * temp);
}
private void power_Click(object sender, EventArgs e)//幂运算{
max = 0;
if (flag == 1 && sp == 0)
{
num1 = Convert.T oDouble(numInput.T ext);
this.txtInput.T ext += num1.T oString();
this.numInput.T ext = "";
this.txtInput.T ext += "^";
op = 5;
}
}
private void recip_Click(object sender, EventArgs e)//倒数
{
max = 0;
if (flag == 1 && sp == 0)
{
num1 = Convert.T oDouble(this.numInput.T ext);
double temp = 0;
temp = 1 / num1;
this.txtInput.T ext = "1/" + num1.T oString() + "=";
this.numInput.T ext = temp.T oString();
}
}
private void percent_Click(object sender, EventArgs e)
{
max = 0;
if (flag == 1 && sp == 0)
{
//double temp = Convert.T oDouble(this.numInput.T ext);
//this.txtInput.T ext += temp.T oString();
//temp = temp / 100;
//this.numInput.T ext = temp.T oString();
//this.txtInput.T ext += "%=";
num1 = Convert.T oDouble(this.numInput.T ext);
this.txtInput.T ext += num1.T oString();
this.numInput.T ext = "";
this.txtInput.T ext += "%";
op = 6;
}
}
#endregion
#region数字
private void one_Click(object sender, EventArgs e)//1 {
if (flag == 1 && sp == 0 && max == 0)
{
if (this.numInput.T ext == "0")
{
this.numInput.T ext = "";
}
if (eq == 1)
{
this.numInput.T ext = "1";
eq = 0;
}else
this.numInput.T ext += "1";
}
}
private void two_Click(object sender, EventArgs e)//2 {
if (flag == 1 && sp == 0 && max == 0)
{
if (this.numInput.T ext == "0")
{
this.numInput.T ext = "";
}
if (eq == 1)
{
this.numInput.T ext = "2";
eq = 0;
}
else
this.numInput.T ext += "2";
//this.txtInput.T ext += "2";
}
}
private void three_Click(object sender, EventArgs e)//3
{
if (flag == 1 && sp == 0 && max == 0)
{
if (this.numInput.T ext == "0")
{
this.numInput.T ext = "";
}
if (eq == 1)
{
this.numInput.T ext = "3";
eq = 0;
}
else
this.numInput.T ext += "3";
//this.txtInput.T ext += "3";
}
}
private void four_Click(object sender, EventArgs e)//4 {
if (flag == 1 && sp == 0 && max == 0)
{
if (this.numInput.T ext == "0")
{
this.numInput.T ext = "";
}
if (eq == 1)
{
this.numInput.T ext = "4";
eq = 0;
}
else
this.numInput.T ext += "4";
// this.txtInput.T ext += "4";
}
}
private void five_Click(object sender, EventArgs e)//5 {
if (flag == 1 && sp == 0 && max == 0)
{
if (this.numInput.T ext == "0")
{
this.numInput.T ext = "";
if (eq == 1)
{
this.numInput.T ext = "5";
eq = 0;
}
else
this.numInput.T ext += "5";
//this.txtInput.T ext += "5";
}
}
private void six_Click(object sender, EventArgs e)//6 {
if (flag == 1 && sp == 0 && max == 0)
{
if (this.numInput.T ext == "0")
{
this.numInput.T ext = "";
}
if (eq == 1)
{
this.numInput.T ext = "6";
eq = 0;
}
else
this.numInput.T ext += "6";
//this.txtInput.T ext += "6";
}
}
private void seven_Click(object sender, EventArgs e)//7 {
if (flag == 1 && sp == 0 && max == 0)
{
if (this.numInput.T ext == "0")
{
this.numInput.T ext = "";
}
if (eq == 1)
{
this.numInput.T ext = "7";
eq = 0;
}
this.numInput.T ext += "7";
//this.txtInput.T ext += "7";
}
}
private void eight_Click(object sender, EventArgs e)//8 {
if (flag == 1 && sp == 0 && max == 0)
{
if (this.numInput.T ext == "0")
{
this.numInput.T ext = "";
}
if (eq == 1)
{
this.numInput.T ext = "8";
eq = 0;
}
else
this.numInput.T ext += "8";
//this.txtInput.T ext += "8";
}
}
private void night_Click(object sender, EventArgs e)//9 {
if (flag == 1 && sp == 0 && max == 0)
{
if (this.numInput.T ext == "0")
{
this.numInput.T ext = "";
}
if (eq == 1)
{
this.numInput.T ext = "9";
eq = 0;
}
else
this.numInput.T ext += "9";
//this.txtInput.T ext += "9";
}
}
private void zero_Click(object sender, EventArgs e)//0
{
if (flag == 1 && sp == 0 && max == 0)
{
if (this.numInput.T ext == "0")
{
this.numInput.T ext = "";
}
if (eq == 1)
{
this.numInput.T ext = "0";
eq = 0;
}
else
this.numInput.T ext += "0";
//this.txtInput.T ext += "0";
}
}
private void dian_Click(object sender, EventArgs e)//小数点{
if (flag == 1 && sp == 0 && max == 0)
{
if (eq == 1)
{
this.numInput.T ext = ".";
eq = 0;
}
else
this.numInput.T ext += ".";
//this.txtInput.T ext += ".";
}
}
#endregion
#region三角函数
private void sin_Click(object sender, EventArgs e)//sin
{
if (flag == 1 && sp == 0)
{
num1 = Convert.T oDouble(this.numInput.T ext);
double temp = Math.PI * (num1 / 180);
this.txtInput.T ext += "sin(" + num1 + ")=";
num1 = Math.Sin(temp);
this.numInput.T ext = num1.ToString();
//this.txtInput.T ext += num1.ToString();
}
}
private void cos_Click(object sender, EventArgs e)//cos
{
if (flag == 1 && sp == 0)
{
num1 = Convert.T oDouble(this.numInput.T ext);
double temp = Math.PI * (num1 / 180);
this.txtInput.T ext += "cos(" + num1 + ")=";
num1 = Math.Cos(temp);
this.numInput.T ext = num1.ToString();
}
}
private void tan_Click(object sender, EventArgs e)//tan
{
if (flag == 1 && sp == 0)
{
num1 = Convert.T oDouble(this.numInput.T ext);
double temp = Math.PI * (num1 / 180);
this.txtInput.T ext += "tan(" + num1 + ")=";
num1 = Math.T an(temp);
this.numInput.T ext = num1.ToString();
}
}
#endregion
#region帮助
private void关于产品T oolStripMenuItem_Click(object sender, EventArgs e) {
MenuAbout about = new MenuAbout();
about.Show();
}
private void change_Click(object sender, EventArgs e)
{
max = 0;
if (flag == 1&&sp == 0)
{
num1 = Convert.T oDouble(this.numInput.T ext);
num1 = -num1;
this.numInput.T ext = num1.ToString();
}
}
private void使用手册T oolStripMenuItem_Click(object sender, EventArgs e) {
MenuUse use = new MenuUse();
use.Show();
}
#endregion
#region皮肤
private void皮肤1T oolStripMenuItem_Click(object sender, EventArgs e) {
skinEngine1.SkinFile = Application.StartupPath + "\Calmness.ssk";
}
private void皮肤2T oolStripMenuItem_Click(object sender, EventArgs e) {
skinEngine1.SkinFile = Application.StartupPath + "\DeepCyan.ssk";
}
private void皮肤3T oolStripMenuItem_Click(object sender, EventArgs e) {
skinEngine1.SkinFile = Application.StartupPath + "\Eighteen.ssk";
}
private void皮肤4T oolStripMenuItem_Click(object sender, EventArgs e) {
skinEngine1.SkinFile = Application.StartupPath + "\GlassGreen.ssk";
}
private void皮肤5T oolStripMenuItem_Click(object sender, EventArgs e) {
skinEngine1.SkinFile = Application.StartupPath + "\Silver.ssk";
}
#endregion
#region复制粘贴
private void复制T oolStripMenuItem_Click(object sender, EventArgs e) {
if (numInput.SelectionLength > 0)
{
numInput.Copy();
}
}
private void粘贴T oolStripMenuItem_Click(object sender, EventArgs e)
{
numInput.Paste();
}
#endregion
private void Counter_Load(object sender, EventArgs e)
{
numInput.SelectAll();
Rectangle ScreenArea = System.Windows.Forms.Screen.GetBounds(this);
int width = ScreenArea.Width;
int heigth = ScreenArea.Height;
this.Location = new Point((width / 2) - (this.Width / 2), (heigth / 2) - (this.Height / 2));
}
private void numInput_TextChanged(object sender, EventArgs e)//numtext
{
string numstring = this.numInput.T ext;
if (numstring.Length > 14)
{
max = 1;
}
}
}
}