C#编写仿照windows计算器- Achely's Blog

  //===================================作者:宇智波涛==================================
  //
//=================================时间:2010年4月30号================================
  //
//===========================编程新手 爱好编程 希望结交各种高手==========================
  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 计算器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
   private void Form1_Load(object sender, EventArgs e)
{
   }
   string text = "0", sign;//sign用来标记加减乘除的运算的;
double number1, number2 = 0, number3 = 0, number = 0;//number1表示前台数 number2,number3表示后台数
  //======================================数字=========================================
   private void Number0_Click(object sender, EventArgs e)
{
if (text == "0")
{
textBox1.Text = "0";
text = textBox1.Text;
}
else
{
text = textBox1.Text;
textBox1.Text = text + "0";
}
number1 = Convert.ToDouble(textBox1.Text);
}
   private void Number1_Click(object sender, EventArgs e)
{
if (text == "0")
{
textBox1.Text = "1";
text = textBox1.Text;
}
else
{
text = textBox1.Text;
textBox1.Text = text + "1";
}
number1 = Convert.ToDouble(textBox1.Text);
}
   private void Number2_Click(object sender, EventArgs e)
{
if (text == "0")
{
textBox1.Text = "2";
text = textBox1.Text;
}
else
{
text = textBox1.Text;
textBox1.Text = text + "2";
}
number1 = Convert.ToDouble(textBox1.Text);
}
   private void Number3_Click(object sender, EventArgs e)
{
if (text == "0")
{
textBox1.Text = "3";
text = textBox1.Text;
}
else
{
text = textBox1.Text;
textBox1.Text = text + "3";
}
number1 = Convert.ToDouble(textBox1.Text);
}
   private void Number4_Click(object sender, EventArgs e)
{
if (text == "0")
{
textBox1.Text = "4";
text = textBox1.Text;
}
else
{
text = textBox1.Text;
textBox1.Text = text + "4";
}
number1 = Convert.ToDouble(textBox1.Text);
}
   private void Number5_Click(object sender, EventArgs e)
{
if (text == "0")
{
textBox1.Text = "5";
text = textBox1.Text;
}
else
{
text = textBox1.Text;
textBox1.Text = text + "5";
}
number1 = Convert.ToDouble(textBox1.Text);
}
   private void Number6_Click(object sender, EventArgs e)
{
if (text == "0")
{
textBox1.Text = "6";
text = textBox1.Text;
}
else
{
text = textBox1.Text;
textBox1.Text = text + "6";
}
number1 = Convert.ToDouble(textBox1.Text);
}
   private void Number7_Click(object sender, EventArgs e)
{
if (text == "0")
{
textBox1.Text = "7";
text = textBox1.Text;
}
else
{
text = textBox1.Text;
textBox1.Text = text + "7";
}
number1 = Convert.ToDouble(textBox1.Text);
}
   private void Number8_Click(object sender, EventArgs e)
{
if (text == "0")
{
textBox1.Text = "8";
text = textBox1.Text;
}
else
{
text = textBox1.Text;
textBox1.Text = text + "8";
}
number1 = Convert.ToDouble(textBox1.Text);
}
   private void Number9_Click(object sender, EventArgs e)
{
if (text == "0")
{
textBox1.Text = "9";
text = textBox1.Text;
}
else
{
text = textBox1.Text;
textBox1.Text = text + "9";
}
number1 = Convert.ToDouble(textBox1.Text);
}
   private void buttonPoint_Click(object sender, EventArgs e)
{
try
{
textBox1.Text += ".";
text = textBox1.Text ;
number1 = Convert.ToDouble(textBox1.Text);
}
catch
{
textBox1.Text = text.Substring(0, text.Length - 1);
}
}
   private void buttonUnsign_Click(object sender, EventArgs e)
{
   number1 = 0 - Convert.ToDouble(textBox1.Text);
textBox1.Text = Convert.ToString(number1);
   }

   //=======================================运算符====================================
   private void buttonAddition_Click(object sender, EventArgs e)
{
if (number3 == 0)
{
number3 = number2 += number1;
}
else
{
switch (sign)
{
case "+": number3 = number2 += number1; break;
case "-": number3 = number2 -= number1; break;
case "*": number3 = number2 *= number1; break;
case "/": if (number1 == 0) textBox1.Text = "除数不能为零"; else number3 = number2 /= number1; break;
}
text = Convert.ToString(number3);
textBox1.Text = text;
}
number1 = 0;
sign = "+";
text = "0";
}
   private void buttonSubtraction_Click(object sender, EventArgs e)
{
if (number3 == 0)
{
number3 = number2 += number1;
}
else
{
switch (sign)
{
case "+": number3 = number2 += number1; break;
case "-": number3 = number2 -= number1; break;
case "*": number3 = number2 *= number1; break;
case "/": if (number1 == 0) textBox1.Text = "除数不能为零"; else number3 = number2 /= number1; break;
}
text = Convert.ToString(number3);
textBox1.Text = text;
}
number1 = 0;
sign = "-";
text = "0";
}
   private void buttonMultiplication_Click(object sender, EventArgs e)
{
if (number3 == 0)
{
number3 = number2 += number1;
}
else
{
switch (sign)
{
case "+": number3 = number2 += number1; break;
case "-": number3 = number2 -= number1; break;
case "*": number3 = number2 *= number1; break;
case "/": if (number1 == 0) textBox1.Text = "除数不能为零"; else number3 = number2 /= number1; break;
}
text = Convert.ToString(number3);
textBox1.Text = text;
}
number1 = 1;
sign = "*";
text = "0";
}
   private void buttonDivision_Click(object sender, EventArgs e)
{
if (number3 == 0)
{
number3 = number2 += number1;
}
else
{
switch (sign)
{
case "+": number3 = number2 += number1; break;
case "-": number3 = number2 -= number1; break;
case "*": number3 = number2 *= number1; break;
case "/": if (number1 == 0) textBox1.Text = "除数不能为零"; else number3 = number2 /= number1; break;
}
text = Convert.ToString(number3);
textBox1.Text = text;
}
number1 = 1;
sign = "/";
text = "0";
}
   private void buttonRadical_Click(object sender, EventArgs e)
{
textBox1.Text = Convert.ToString(System.Math.Sqrt(Convert.ToDouble(textBox1.Text)));
number2 = Convert.ToDouble(textBox1.Text);
number1 = 0;
text = "0";
}
   private void buttonPercent_Click(object sender, EventArgs e)
{
textBox1.Text = Convert.ToString(Convert.ToDouble(textBox1.Text) * 0.01);
number2 = Convert.ToDouble(textBox1.Text);
number1 = 0;
text = "0";
}
   private void buttonReciprocal_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
{
textBox1.Text = "除数不能为零";
}
else
{
textBox1.Text = Convert.ToString(1 / Convert.ToDouble(textBox1.Text));
number2 = Convert.ToDouble(textBox1.Text);
number1 = 0;
text = "0";
}
}
   private void buttonResult_Click(object sender, EventArgs e)
{
number3 = 0;
switch (sign)
{
case "+": number2 += number1; textBox1.Text = Convert.ToString(number2); break;
case "-": number2 -= number1; textBox1.Text = Convert.ToString(number2); break;
case "*": number2 *= number1; textBox1.Text = Convert.ToString(number2); break;
case "/": if (number1 == 0) textBox1.Text = "除数不能为零"; else { number2 /= number1; textBox1.Text = Convert.ToString(number2); } break;
}
text = "0";
//number1 = 0;
}
   //========================================记忆M系列================================
   private void button_MC_Click(object sender, EventArgs e)
{
number = 0; textBox2.Text = "";
}
   private void button_MR_Click(object sender, EventArgs e)
{
textBox1.Text = Convert.ToString(number);
}
   private void button_MS_Click(object sender, EventArgs e)
{
number = Convert.ToDouble(textBox1.Text);
textBox2.Text = "M";
}
   private void button_MAdd_Click(object sender, EventArgs e)
{
if (textBox2.Text == "")
{
textBox2.Text = "M";
number = Convert.ToDouble(textBox1.Text);
}
else
{
number += Convert.ToDouble(textBox1.Text);
}
}
   //===================================清空 删除 等键=================================
   private void button_Backpace_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length > 1)
{
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
}
else
{
textBox1.Text = "0";
}
number1 = Convert.ToDouble(textBox1.Text);
}
   private void button_Clear_textBox_Click(object sender, EventArgs e)
{
textBox1.Text = "0";
}
   private void button_Clear_Click(object sender, EventArgs e)
{
number1 = number2 = number3 = 0;
text = "0";
textBox1.Text = "0";
sign = "";
}
   //=====================================菜单栏等键====================================
   private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
{
sign = textBox1.Text;
}
   private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Text = sign;
number1 = Convert.ToDouble(textBox1.Text);
}
   private void 帮助主题ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("作者:宇智波涛 QQ1095344800", "帮助");
}
   private void 关于计算器ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("这是我写的仿照windows计算器的计算器功能还不是很完善 欢迎指导", "计算器");
}
   }
}
  程序中还有些小问题没有解决 例如点击 一个数字点减号 再点正负号 再点小数点 再点等号 就是错的 呵呵 没改过来 这样的问题还有很多 慢慢改进 !!!
  我是新手 欢迎各位提宝贵意见!!!

/文章来自:
/引用通告地址:
/Tags:
郑重声明:资讯 【C#编写仿照windows计算器- Achely's Blog】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——