第二个作品(宝宝的计算器1.0)
时间:2011-06-15 来源:刘信含
代码还在测试当中,请大家多多发现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;
public double number1, number2, result;
public int SymbolsJudgement=0,lian = 0;
public bool xiaoshudian;
private void Form1_Load(object sender, EventArgs e)
{
TextBox.Text = "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: button11_Click(sender, e); break;
case Keys.Subtract: button12_Click(sender, e); break;
case Keys.Multiply: button13_Click(sender, e); break;
case Keys.Divide: button14_Click(sender, e); 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)
{
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)
{
string i;
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;
}
}
private void button10_Click(object sender, EventArgs e)
{
if (TextBox.Text != "0")
{
number = number + "0";
TextBox.Text = number;
}
else
{
xiaoshudian = false;
}
}
private void yunsuan(int j)
{
xiaoshudian = false;
if (lian == 0)
{
SymbolsJudgement = j;
if (number == "")
{
number = "0";
}
number1 = Convert.ToDouble(number);
lian++;
number = "";
}
else
{
number = "0";
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 button11_Click(object sender, EventArgs e)
{
int j = 1;this.yunsuan(j);
}
private void button12_Click(object sender, EventArgs e)
{
int j = 2;this.yunsuan(j);
}
private void button13_Click(object sender, EventArgs e)
{
int j = 3;this.yunsuan(j);
}
private void button14_Click(object sender, EventArgs e)
{
int j = 4;this.yunsuan(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;
}
}
number1 = Convert.ToDouble(dengyu);
}
private void clear_Click(object sender, EventArgs e)
{
number = ""; TextBox.Text = "0"; result = 0.0; number1 = 0.0; lian = 0; number2 = 0.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 (TextBox.Text != "0")
{
number2 = Convert.ToDouble(TextBox.Text);
number = Convert.ToString((number2 - number2 % 10) / 10);
TextBox.Text = Convert.ToString(number);
}
if (TextBox.Text == "0")
{
number = "";TextBox.Text = "0";result = 0.0;number1 = 0.0;lian = 0;number2 = 0.0;
}
}
private void negative_Click(object sender, EventArgs e)
{
number = Convert.ToString(0 - Convert.ToDouble(TextBox.Text));
TextBox.Text = Convert.ToString(number);
}
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));
number = TextBox.Text;
xiaoshudian = true;
}
}
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;
}
}
}