宝宝的计算器1.0(C#编写)
时间:2011-06-17 来源:刘信含
宝宝的计算器1.0还在调试当中,存在按钮“清空”和键盘按键“="的冲突问题,存在小数不能退格问题。正在修复BUG当中。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication13
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string number, Temp, dengyu, number3;
public double number1, number2, result;
public int SymbolsJudgement = 0, lian = 0, dengyub = 0;
public bool xiaoshudian;
private void Form1_Load(object sender, EventArgs e)
{
TextBox.Text = "0"; //初始化为0
this.KeyDown += new KeyEventHandler(Form1_KeyDown); //添加键盘
}
void Form1_KeyDown(object sender, KeyEventArgs e) //设置键盘
{
string i;
switch (e.KeyCode)
{
case Keys.D1: i = "1"; this.method(i); break;
case Keys.D2: i = "2"; this.method(i); break;
case Keys.D3: i = "3"; this.method(i); break;
case Keys.D4: i = "4"; this.method(i); break;
case Keys.D5: i = "5"; this.method(i); break;
case Keys.D6: i = "6"; this.method(i); break;
case Keys.D7: i = "7"; this.method(i); break;
case Keys.D8: i = "8"; this.method(i); break;
case Keys.D9: i = "9"; this.method(i); break;
case Keys.D0: button10_Click(sender, e); break;
case Keys.Add: int j = 1; this.yunsuan(j); break;
case Keys.Subtract: j = 2; this.yunsuan(j); break;
case Keys.Multiply: j = 3; this.yunsuan(j); break;
case Keys.Divide: j = 4; this.yunsuan(j); break;
case Keys.NumPad1: i = "1"; this.method(i); break;
case Keys.NumPad2: i = "2"; this.method(i); break;
case Keys.NumPad3: i = "3"; this.method(i); break;
case Keys.NumPad4: i = "4"; this.method(i); break;
case Keys.NumPad5: i = "5"; this.method(i); break;
case Keys.NumPad6: i = "6"; this.method(i); break;
case Keys.NumPad7: i = "7"; this.method(i); break;
case Keys.NumPad8: i = "8"; this.method(i); break;
case Keys.NumPad9: i = "9"; this.method(i); break;
case Keys.NumPad0: button10_Click(sender,e); break;
case Keys.Back: Backspace_Click(sender,e); break;
case Keys.Enter: button15_Click(sender,e); break;
case Keys.Decimal: point_Click(sender, e); break;
case Keys.Delete: clear_Click(sender,e); break;
}
}
private void method(string i) //创建一个1-9数字的方法
{
if (TextBox.Text == "0")
{
xiaoshudian = false;
number = i;
TextBox.Text = Convert.ToString(number);
}
else
{
number = number + i;
TextBox.Text = number;
}
}
private void button_click(object sender, EventArgs e) //根据按钮最后一位的字符判断执行哪个按钮
{
if (dengyub == 1)
{
number = ""; TextBox.Text = "0"; result = 0.0; number1 = 0.0; lian = 0; number2 = 0.0; dengyub = 0;
}
string i;
int j;
Button btn = sender as Button;
string num = btn.Name.Substring(btn.Name.Length - 1);
switch (num)
{
case "1": i = "1"; this.method(i); break;
case "2": i = "2"; this.method(i); break;
case "3": i = "3"; this.method(i); break;
case "4": i = "4"; this.method(i); break;
case "5": i = "5"; this.method(i); break;
case "6": i = "6"; this.method(i); break;
case "7": i = "7"; this.method(i); break;
case "8": i = "8"; this.method(i); break;
case "9": i = "9"; this.method(i); break;
case "a": j = 1; this.yunsuan(j); break; //加法
case "s": j = 2; this.yunsuan(j); break; //减法
case "m": j = 3; this.yunsuan(j); break; //乘法
case "d": j = 4; this.yunsuan(j); break; //除法
}
}
private void button10_Click(object sender, EventArgs e) //数字0的按钮
{
if (dengyub == 1)
{
number = ""; TextBox.Text = "0"; result = 0.0; number1 = 0.0; lian = 0; number2 = 0.0; dengyub = 0;
}
if (TextBox.Text != "0")
{
number = number + "0";
TextBox.Text = number;
}
else
xiaoshudian = false;
}
private void yunsuan(int j)
{
dengyub = 0;
xiaoshudian = false;
if (lian == 0) //检查是否连续运算
{
SymbolsJudgement = j;
if (number == "")
number = "0";
number1 = Convert.ToDouble(number);
lian++;
number = "";
}
else //连续运算执行这里
{
if (SymbolsJudgement == 1)
{
if (number == "")
number = "0";
result = Convert.ToDouble(Convert.ToDouble(number1) + Convert.ToDouble(number));
number1 = result;
TextBox.Text = Convert.ToString(result);
number = "";
}
else if (SymbolsJudgement == 2)
{
if (number == "")
number = "0";
result = Convert.ToDouble(Convert.ToDouble(number1) - Convert.ToDouble(number));
number1 = result;
TextBox.Text = Convert.ToString(result);
number = "";
}
else if (SymbolsJudgement == 3)
{
if (number == "0")
number = "1";
if (number == "")
number = "0";
result = Convert.ToDouble(Convert.ToDouble(number1) * Convert.ToDouble(number));
number1 = result;
TextBox.Text = Convert.ToString(result);
number = "";
}
else if (SymbolsJudgement == 4)
{
if (number == "0")
number = "1";
if (number == "")
number = "1";
result = Convert.ToDouble(Convert.ToDouble(number1) / Convert.ToDouble(number));
number1 = result;
TextBox.Text = Convert.ToString(result);
number = "";
}
SymbolsJudgement = j;
}
}
private void button15_Click(object sender, EventArgs e) //等于按钮
{
xiaoshudian = false;
switch (SymbolsJudgement)
{
case 1:dengyu = Convert.ToString(number1 + Convert.ToDouble(number));
TextBox.Text = Convert.ToString(number1 + Convert.ToDouble(number));break;
case 2:dengyu = Convert.ToString(number1 - Convert.ToDouble(number));
TextBox.Text = Convert.ToString(number1 - Convert.ToDouble(number));break;
case 3:dengyu = Convert.ToString(number1 * Convert.ToDouble(number));
TextBox.Text = Convert.ToString(number1 * Convert.ToDouble(number));break;
case 4:
if (TextBox.Text == "0")
{
TextBox.Text = "除数不能为0";
break;
}
else
{
dengyu = Convert.ToString(number1 / Convert.ToDouble(number));
TextBox.Text = Convert.ToString(number1 / Convert.ToDouble(number));
break;
}
}
number = "0";
number1 = Convert.ToDouble(dengyu);
dengyub = 1;
}
private void clear_Click(object sender, EventArgs e) //清空按钮
{
number = ""; TextBox.Text = "0"; result = 0.0; number1 = 0.0; lian = 0; number2 = 0.0; dengyub = 0;
}
private void point_Click(object sender, EventArgs e) //小数点按钮
{
if (xiaoshudian == false)
{
if (TextBox.Text == "0")
number += "0.";
else
number += ".";
}
xiaoshudian = true;TextBox.Text = Convert.ToString(number);
}
private void Backspace_Click(object sender, EventArgs e) //退格按钮
{
if (dengyub != 1)
{
number2 = Convert.ToDouble(TextBox.Text);
if (number2 < 10)
{
TextBox.Text = "0";
return;
}
if (number2 != 0)
{
number3 = TextBox.Text;
TextBox.Text = number3.Substring(0, number3.Length - 1);
}
if (number2 == 0)
{
number = ""; TextBox.Text = "0"; result = 0.0; number1 = 0.0; lian = 0; number2 = 0.0; dengyub = 0;
}
}
}
private void negative_Click(object sender, EventArgs e) //正负转换按钮
{
if (dengyub == 0)
{
number = Convert.ToString(0 - Convert.ToDouble(TextBox.Text));
TextBox.Text = Convert.ToString(number);
}
else
{
number1 = Convert.ToDouble(0 - Convert.ToDouble(TextBox.Text));
TextBox.Text = Convert.ToString(number1);
}
}
private void sqrt_Click(object sender, EventArgs e) //根号按钮
{
if (Convert.ToDouble(TextBox.Text) < 0)
this.TextBox.Text = "负数不能平方根";
else
{
number2 = Convert.ToDouble(TextBox.Text);
TextBox.Text = Convert.ToString(System.Math.Sqrt(number2));
if (dengyub == 0)
number = TextBox.Text;
else
number1 = Convert.ToDouble(TextBox.Text);
}
}
private void 关于作者ToolStripMenuItem_Click(object sender, EventArgs e)
{
help helps = new help();
helps.Show();
}
private void 复制ToolStripMenuItem1_Click(object sender, EventArgs e)
{
Temp = TextBox.Text;
}
private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
{
TextBox.Text = Temp;
}
}
}