using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private static double value1 = 0, value2=0, value3=1, cal=0, calold=0, valuecal;
private int deci = 0, decicount=10, operat=1, second=0, sign=0;
private void keyenable(bool x)
{
button1.Enabled = x;button6.Enabled=x; button10.Enabled=x; button11.Enabled=x; button12.Enabled=x;
button13.Enabled = x; button14.Enabled = x; button15.Enabled = x; button16.Enabled = x; button17.Enabled = x; button3.Enabled = x; button18.Enabled = x;
}
private void key(int x)
{
switch (second)
{
case 0:
if (deci == 0) { value1 = value1 * 10 + x; textBox1.Text = value1.ToString(); }
else if (deci == 1) { value1 = value1 + (double)x / decicount; textBox1.Text = value1.ToString(); decicount *= 10; }
calold = value1;
break;
case 1:
if (deci == 0) { value2 = value2 * 10 + x; textBox2.Text = value2.ToString(); }
else if (deci == 1) { value2 = value2 + (double)x / decicount; textBox2.Text = value2.ToString(); decicount *= 10; }
value3 = value2;
break;
default:
break;
}
}
private void calculate(int operat)
{
second = 1; value1 = calold; deci = 0; decicount = 10; sign = 0;
textBox2.Text = ""; textBox4.Text = "";
switch (operat)
{
case 1:
cal = value1 + value2;
break;
case 2:
cal = value1 - value2;
break;
case 3:
cal = value1 * value3;
break;
case 4:
cal = value1 / value3;
break;
case 5:
cal = Math.Pow(value1,value3);
break;
case 6:
cal = Math.Sin(value1/180*Math.PI);
break;
case 7:
cal = Math.Log(value1,value2);
break;
default:
break;
}
calold = cal; valuecal = value2; value3 = 1; value2 = 0;
}
//1
private void button1_Click(object sender, EventArgs e)
{
if (sign == 0) { key(1); } else if (sign == 1) { key(-1); }
}
//.
private void button3_Click(object sender, EventArgs e)
{
switch (second)
{
case 0:
{
if (deci == 1) { return; }
else if (deci == 0) { deci = 1; textBox1.Text += "."; }
break;
}
case 1:
{
if (deci == 1) { return; }
else if (deci == 0) { deci = 1; textBox2.Text += "."; }
break;
}
default:
break;
}
}
//+
private void button2_Click(object sender, EventArgs e)
{
keyenable(true);
textBox3.Text = "+";
calculate(operat);
operat = 1;
textBox1.Text = cal.ToString();
}
//clear
private void button4_Click(object sender, EventArgs e)
{
textBox2.Text = ""; textBox4.Text = ""; textBox1.Text = ""; textBox3.Text = "";
value1 = 0; value2=0; value3=1; cal=0; calold=0;
deci = 0; decicount = 10; operat = 1; second = 0; sign = 0; keyenable(true);
}
//=
private void button5_Click(object sender, EventArgs e)
{
keyenable(true); calculate(operat);
textBox4.Text = cal.ToString();
second = 0;
}
//2
private void button6_Click(object sender, EventArgs e)
{
if (sign == 0) { key(2); } else if (sign == 1) { key(-2); }
}
//-
private void button7_Click(object sender, EventArgs e)
{
keyenable(true);
textBox3.Text = "-";
calculate(operat);
operat = 2;
textBox1.Text = cal.ToString();
}
//*
private void button8_Click(object sender, EventArgs e)
{
keyenable(true);
textBox3.Text = "*";
calculate(operat);
operat = 3;
textBox1.Text = cal.ToString();
}
///
private void button9_Click(object sender, EventArgs e)
{
keyenable(true);
textBox3.Text = "/";
calculate(operat);
operat = 4;
textBox1.Text = cal.ToString();
}
//3
private void button10_Click(object sender, EventArgs e)
{
if (sign == 0) { key(3); } else if (sign == 1) { key(-3); }
}
private void button11_Click(object sender, EventArgs e)
{
if (sign == 0) { key(4); } else if (sign == 1) { key(-4); }
}
private void button12_Click(object sender, EventArgs e)
{
if (sign == 0) { key(5); } else if (sign == 1) { key(-5); }
}
private void button13_Click(object sender, EventArgs e)
{
if (sign == 0) { key(6); } else if (sign == 1) { key(-6); }
}
private void button14_Click(object sender, EventArgs e)
{
if (sign == 0) { key(7); } else if (sign == 1) { key(-7); }
}
private void button15_Click(object sender, EventArgs e)
{
if (sign == 0) { key(8); } else if (sign == 1) { key(-8); }
}
private void button16_Click(object sender, EventArgs e)
{
if (sign == 0) { key(9); } else if (sign == 1) { key(-9); }
}
private void button17_Click(object sender, EventArgs e)
{
key(0);
}
//+/-
private void button18_Click(object sender, EventArgs e)
{
if (sign == 0) { sign = 1; }
else if (sign == 1) { sign = 0; }
if (second == 0)
{
value1 = -value1; calold = -calold; textBox1.Text = calold.ToString();
}
else if (second == 1)
{
value2 = -value2; value3 = -value3; textBox2.Text = value2.ToString();
}
}
//次方
private void button19_Click(object sender, EventArgs e)
{
keyenable(true);
textBox3.Text = "次方";
calculate(operat);
operat = 5;
textBox1.Text = cal.ToString();
}
private void button20_Click(object sender, EventArgs e)
{
keyenable(false);
textBox3.Text = "sin";
calculate(operat);
operat = 6;
textBox1.Text = cal.ToString();
}
private void button21_Click(object sender, EventArgs e)
{
keyenable(true);
textBox3.Text = "log";
calculate(operat);
operat = 7;
textBox1.Text = cal.ToString();
}
private void button22_Click(object sender, EventArgs e)
{
keyenable(false);
switch (second)
{
case 0:
value1 = Math.Exp(1); textBox1.Text = value1.ToString(); calold = value1;
break;
case 1:
value2 = Math.Exp(1); textBox2.Text = value2.ToString(); value3 = value2;
break;
default:
break;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
No comments:
Post a Comment