C#计算器

啊的的 贡献于2011-12-20

作者 yucihan  创建于2011-10-28 12:03:00   修改者yucihan  修改于2011-10-28 12:06:00字数18440

文档摘要:C#计算器实例代码
关键词:

 第一部份.程序界面   1,以下控件表 控件类型 Name Text form calcForm 计算器 button button1 0   .....     button10 9   bDot .(小数点) 小数点按钮   bPlus +(加号) 加号按钮   bSub -(减号) 减号按钮   bMul *(乘号) 乘号按钮   bDiv /(除号) 除号按钮   bEqu =(等号) 等号按钮   bClr AC 清除按钮   textBox txtCalc (空值) 用来显示输入及输出结果   第二部份,程序结构   1,定义以下变量 Double dblAcc; //运算数A Double dblSec; //运算数B bool blnClear,blnFrstOpen;//布尔类型用来判断清除与否,以及第一个显示字符 String strOper;//通过获取strOper的值来决定运算+,-,*,/,=   2,用以下方法来实现按钮的动作   例: bDot.click+=net EventHandler(btn_clk);//EventHandler类是事件代表类,用来注册事件的处理方法.   //第一个参数是object类型,指向发出事件的对象;   //第二个参数是EventArgs类型,包含了关于这个事件的数据   3,用以下方法来判断运算以及运算操作 private void calc(){ switch(strOper){  case "+":   dblAcc+=dblSec;//加法运算   break;  case "-":   dblAcc-=dblSec;//减法运算   break;  case "*":   dblAcc*=dblSec;//乘法运算   break;  case "/":   dblAcc/=dblSec;//除法运算   break; } strOper="=";//等号运算 blnFrstOpen=true; txtCalc.Text=Convert.ToString(dblAcc);//将运算结果转换成字符型,并输出结果 dblSec=dblAcc; }   4,小数点运算 //先判断是否已经按了小数点按钮,如果按了,最0.x来代替运算变量,并且将转换成Double数型数值 private void btn_clk(object obj,EventArgs ea){  if(blnClear)   txtCalc.Text="";   Button b3=(Button)obj;   txtCalc.Text+=b3.Text;  if(txtCalc.Text==".")   txtCalc.Text="0.";   dblSec=Convert.ToDouble(txtCalc.Text); 二、开发过程   下面跟我们一步步学习吧.   先启动vs.net,建一个空的C#项目,如下(pic1.jpg)   名称用wincalc,位置就自己设定好了。确定后,从vs.net开发环境中右边的工具箱中加入控件表的控件,,注意修改txtCalc的如属性ReadOnly=True,RightTOLeft=Yes(从右至左输入),最后画面如何(pic2) 添加代码如源程序所示:   最后大功告成,按Ctrl+F5吧,看看结果. 三、源程序 //基本的计算器 //蚕蛹 2001.11.26 //Using C# //E-mail:sillnet@hotmail.com using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; namespace wincalc { /// /// Summary description for calcForm. /// public class calcForm : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button4; private System.Windows.Forms.Button button5; private System.Windows.Forms.Button button6; private System.Windows.Forms.Button button7; private System.Windows.Forms.Button button8; private System.Windows.Forms.Button button9; private System.Windows.Forms.Button button10; private System.Windows.Forms.Button bClr; private System.Windows.Forms.Button bDot; private System.Windows.Forms.Button bPlus; private System.Windows.Forms.Button bSub; private System.Windows.Forms.Button bMul; private System.Windows.Forms.Button bDiv; private System.Windows.Forms.Button bEqu; private System.Windows.Forms.TextBox txtCalc; //以下是要添加的代码 //定义变量 Double dblAcc; Double dblSec; bool blnClear,blnFrstOpen; String strOper; //以上是添加的代码 /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public calcForm() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // //以下是要添加的代码 //初始化设量 dblAcc=0; dblSec=0; blnFrstOpen=true; blnClear=true; strOper=new string('=',1); //以上是添加的代码 } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.bPlus = new System.Windows.Forms.Button(); this.bMul = new System.Windows.Forms.Button(); this.bDot = new System.Windows.Forms.Button(); this.txtCalc = new System.Windows.Forms.TextBox(); this.bClr = new System.Windows.Forms.Button(); this.bDiv = new System.Windows.Forms.Button(); this.bSub = new System.Windows.Forms.Button(); this.button8 = new System.Windows.Forms.Button(); this.button9 = new System.Windows.Forms.Button(); this.bEqu = new System.Windows.Forms.Button(); this.button10 = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); this.button5 = new System.Windows.Forms.Button(); this.button6 = new System.Windows.Forms.Button(); this.button7 = new System.Windows.Forms.Button(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // bPlus // this.bPlus.BackColor = System.Drawing.SystemColors.Control; this.bPlus.ForeColor = System.Drawing.SystemColors.ControlText; this.bPlus.Location = new System.Drawing.Point(208, 112); this.bPlus.Name = "bPlus"; this.bPlus.Size = new System.Drawing.Size(32, 80); this.bPlus.TabIndex = 1; this.bPlus.Text = "+"; //以下是要添加的代码 bPlus.Click += new System.EventHandler(this.btn_Oper); //以上是添加的代码 // // bMul // this.bMul.Location = new System.Drawing.Point(160, 112); this.bMul.Name = "bMul"; this.bMul.Size = new System.Drawing.Size(32, 32); this.bMul.TabIndex = 1; this.bMul.Text = "*"; //以下是要添加的代码 bMul.Click += new System.EventHandler(this.btn_Oper); //以上是添加的代码 // // bDot // this.bDot.ForeColor = System.Drawing.Color.Black; this.bDot.Location = new System.Drawing.Point(112, 208); this.bDot.Name = "bDot"; this.bDot.Size = new System.Drawing.Size(32, 32); this.bDot.TabIndex = 0; this.bDot.Text = "."; //以下是要添加的代码 bDot.Click += new System.EventHandler(this.btn_clk); //以上是添加的代码 // // txtCalc // this.txtCalc.Location = new System.Drawing.Point(16, 24); this.txtCalc.Name = "txtCalc"; this.txtCalc.ReadOnly = true; this.txtCalc.RightToLeft = System.Windows.Forms.RightToLeft.Yes; this.txtCalc.Size = new System.Drawing.Size(224, 21); this.txtCalc.TabIndex = 2; this.txtCalc.Text = ""; // // bClr // this.bClr.BackColor = System.Drawing.SystemColors.Control; this.bClr.ForeColor = System.Drawing.SystemColors.ControlText; this.bClr.Location = new System.Drawing.Point(208, 64); this.bClr.Name = "bClr"; this.bClr.Size = new System.Drawing.Size(32, 32); this.bClr.TabIndex = 0; this.bClr.Text = "AC"; //以下是要添加的代码 bClr.Click += new System.EventHandler(this.btn_clr); //以上是添加的代码 // // bDiv // this.bDiv.Location = new System.Drawing.Point(160, 160); this.bDiv.Name = "bDiv"; this.bDiv.Size = new System.Drawing.Size(32, 32); this.bDiv.TabIndex = 1; this.bDiv.Text = "/"; //以下是要添加的代码 bDiv.Click += new System.EventHandler(this.btn_Oper); //以上是添加的代码 // // bSub // this.bSub.Location = new System.Drawing.Point(160, 64); this.bSub.Name = "bSub"; this.bSub.Size = new System.Drawing.Size(32, 32); this.bSub.TabIndex = 1; this.bSub.Text = "-"; //以下是要添加的代码 bSub.Click += new System.EventHandler(this.btn_Oper); //以上是添加的代码 // // button8 // this.button8.Location = new System.Drawing.Point(16, 64); this.button8.Name = "button8"; this.button8.Size = new System.Drawing.Size(32, 32); this.button8.TabIndex = 0; this.button8.Text = "7"; //以下是要添加的代码 button8.Click += new System.EventHandler(this.btn_clk); //以上是添加的代码 // // button9 // this.button9.Location = new System.Drawing.Point(64, 64); this.button9.Name = "button9"; this.button9.Size = new System.Drawing.Size(32, 32); this.button9.TabIndex = 0; this.button9.Text = "8"; //以下是要添加的代码 button9.Click += new System.EventHandler(this.btn_clk); //以上是添加的代码 // // bEqu // this.bEqu.BackColor = System.Drawing.SystemColors.Control; this.bEqu.ForeColor = System.Drawing.SystemColors.ControlText; this.bEqu.Location = new System.Drawing.Point(160, 208); this.bEqu.Name = "bEqu"; this.bEqu.Size = new System.Drawing.Size(80, 32); this.bEqu.TabIndex = 1; this.bEqu.Text = "="; //以下是要添加的代码 bEqu.Click += new System.EventHandler(this.btn_equ); //以上是添加的代码 // // button10 // this.button10.Location = new System.Drawing.Point(112, 64); this.button10.Name = "button10"; this.button10.Size = new System.Drawing.Size(32, 32); this.button10.TabIndex = 0; this.button10.Text = "9"; //以下是要添加的代码 button10.Click += new System.EventHandler(this.btn_clk); //以上是添加的代码 // // button4 // this.button4.Location = new System.Drawing.Point(112, 160); this.button4.Name = "button4"; this.button4.Size = new System.Drawing.Size(32, 32); this.button4.TabIndex = 0; this.button4.Text = "3"; //以下是要添加的代码 button4.Click += new System.EventHandler(this.btn_clk); //以上是添加的代码 // // button5 // this.button5.Location = new System.Drawing.Point(16, 112); this.button5.Name = "button5"; this.button5.Size = new System.Drawing.Size(32, 32); this.button5.TabIndex = 0; this.button5.Text = "4"; //以下是要添加的代码 button5.Click += new System.EventHandler(this.btn_clk); //以上是添加的代码 // // button6 // this.button6.Location = new System.Drawing.Point(64, 112); this.button6.Name = "button6"; this.button6.Size = new System.Drawing.Size(32, 32); this.button6.TabIndex = 0; this.button6.Text = "5"; //以下是要添加的代码 button6.Click += new System.EventHandler(this.btn_clk); //以上是添加的代码 // // button7 // this.button7.Location = new System.Drawing.Point(112, 112); this.button7.Name = "button7"; this.button7.Size = new System.Drawing.Size(32, 32); this.button7.TabIndex = 0; this.button7.Text = "6"; //以下是要添加的代码 button7.Click += new System.EventHandler(this.btn_clk); //以上是添加的代码 // // button1 // this.button1.BackColor = System.Drawing.SystemColors.Control; this.button1.ForeColor = System.Drawing.Color.Black; this.button1.Location = new System.Drawing.Point(16, 208); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(80, 32); this.button1.TabIndex = 0; this.button1.Text = "0"; //以下是要添加的代码 button1.Click += new System.EventHandler(this.btn_clk); //以上是添加的代码 // // button2 // this.button2.Location = new System.Drawing.Point(16, 160); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(32, 32); this.button2.TabIndex = 0; this.button2.Text = "1"; //以下是要添加的代码 button2.Click += new System.EventHandler(this.btn_clk); //以上是添加的代码 // // button3 // this.button3.Location = new System.Drawing.Point(64, 160); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(32, 32); this.button3.TabIndex = 0; this.button3.Text = "2"; //以下是要添加的代码 button3.Click += new System.EventHandler(this.btn_clk); //以上是添加的代码 // // calcForm // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(256, 261); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.txtCalc, this.bEqu, this.bDiv, this.bMul, this.bSub, this.bPlus, this.bDot, this.bClr, this.button10, this.button9, this.button8, this.button7, this.button6, this.button5, this.button4, this.button3, th is.button2, this.button1}); this.Name = "calcForm"; this.Text = "计算器"; this.ResumeLayout(false); } #endregion //以下是要添加的代码 //小数点的操作 private void btn_clk(object obj,EventArgs ea){ if(blnClear) txtCalc.Text=""; Button b3=(Button)obj; txtCalc.Text+=b3.Text; if(txtCalc.Text==".") txtCalc.Text="0."; dblSec=Convert.ToDouble(txtCalc.Text); blnClear=false; } // 程序开始点 private static void Main(){ Application.Run(new calcForm()); } private void btn_Oper(object obj,EventArgs ea){ Button tmp=(Button)obj; strOper=tmp.Text; if(blnFrstOpen) dblAcc=dblSec; else calc(); blnFrstOpen=false; blnClear=true; } //等号运算 private void btn_equ(object obj,EventArgs ea){ calc(); } //所有运算操作 private void calc(){ switch(strOper){ case "+": dblAcc+=dblSec; //加号运算 break; case "-": dblAcc-=dblSec; //减号运算 break; case "*": dblAcc*=dblSec; //乘号运算 break; case "/": dblAcc/=dblSec; //除号运算 break; } strOper="="; //等号运算 blnFrstOpen=true; txtCalc.Text=Convert.ToString(dblAcc);//将运算结果转换成字符类型,并输出 dblSec=dblAcc;//将运算数A的值放入运算数B中,以便后面运算 } //清除按钮 private void btn_clr(object obj,EventArgs ea){ clear(); } //清除按钮的操作 private void clear(){ dblAcc=0; dblSec=0; blnFrstOpen=true; txtCalc.Text=""; txtCalc.Focus();//设置焦点为txtCalc } //以上是添加的代码 } }   以上只是一个简单的用C#开发的Windows Form程序,在vs.nt bate2+windows 2000专业版编译通过.向正在学习vs.net网友们抛砖引玉,其功能上还有很多不完善的地方,欢迎网友们将其完善。 **********************************************************************     小妹是C#初学者,这个计算器是我按照google计算器的模型来做的。分为传统计算器和个性计算器两个部分。传统计算器中实现了基本的四则运算,但是MC、MR、MS还未实现;在个性计算器中,实现了两个操作数的基本四则运算,即可以在输入框中直接输入4+5,然后点击运算即可得到答案了!这个程序在改进之中,希望大家关注! 运行程序截图:                      下面写出部分代码,希望大家给提点意见! 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; using System.Text.Regularexpression_r_r_r_r_rs; namespace MickeyCalculator {     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }                 private void clear_Click(object sender, EventArgs e)         {             textBox1.Text = "0.0";             textBox3.Text = "";         }                 private void back_Click(object sender, EventArgs e)         {             if ((textBox1.Text != "") && (textBox1.Text != "0.0"))             {                 textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);                 textBox3.Text = textBox1.Text;                 // 如果字符串的长度为0时,此时默认值为0                 if (textBox1.Text.Length == 0)                 {                     textBox1.Text = "0";                     textBox3.Text = "0";                 }             }         }         // 这个成员变量用来保存用户输入的操作信息         private string operand1 = "";         private string operand2 = "";         // 这个成员变量用来存储+、-、*、/等操作符         private string action = "";         // 这个成员变量用于标识是否进行过“=”运算,默认true为没有进行过此运算         private bool equal_flag = true;                 private void numberInputCheck(Button button)         {             // 如果输入框中是“0.0”或者“0”,说明这个数字是第一个操作数                        if (textBox1.Text.Equals("0.0") || textBox1.Text.Equals("0"))             {                 textBox3.Text = "";                 textBox1.Text = button.Text;                 operand1 = textBox1.Text;                 textBox3.Text = button.Text;             }             // 如果输入框中不是上面的情况、长度不为0并且操作符已经输入,此时应该做的是第一个操作数拼接             else if ((action.Equals("")) && (textBox1.Text.Length != 0) && (!textBox1.Text.Equals("0.0")) && (!textBox1.Text.Equals("0")))             {                 textBox1.Text += button.Text;                 operand1 = textBox1.Text;                 textBox3.Text += button.Text;             }             // 如果输入框中不是上面的情况、长度不为0并且操作符已经输入,此时应该做的是第二个操作数拼接             else if ((!action.Equals("")) && (textBox1.Text.Length != 0) && (!textBox1.Text.Equals("0.0")) && (!textBox1.Text.Equals("0")))             {                 textBox1.Text += button.Text;                 operand2 = textBox1.Text;                 textBox3.Text += button.Text;             }             // 输入操作符后会将输入框中的字符串清零,所以此时应该是第二个操作数的输入             else if (textBox1.Text.Equals("") && (!action.Equals("")))             {                 textBox1.Text += button.Text;                 textBox3.Text += button.Text;                 operand2 = textBox1.Text;             }         }           private void zero_Click(object sender, EventArgs e)         {             // 没有点击过等号             if (equal_flag)             {                 numberInputCheck(zero);             }             else             {                 textBox1.Text = "";                 numberInputCheck(zero);                 equal_flag = true;             }         }         private void dot_Click(object sender, EventArgs e)         {             // 没有点击过等号             if (equal_flag)             {                 if (textBox1.Text.Equals("0") || textBox1.Text.Equals("0.0"))                 {                     textBox1.Text = "0" + dot.Text;                     textBox3.Text = textBox1.Text;                 }                 else                 {                     textBox1.Text += dot.Text;                     textBox3.Text = textBox1.Text;                 }             }         }         private void number_one_Click(object sender, EventArgs e)         {             if (equal_flag)             {                 numberInputCheck(number_one);             }             else             {                 textBox1.Text = "";                 numberInputCheck(number_one);                 equal_flag = true;             }         }         private void number_two_Click(object sender, EventArgs e)         {             if (equal_flag)             {                 numberInputCheck(number_two);             }             else             {                 textBox1.Text = "";                 numberInputCheck(number_two);                 equal_flag = true;             }         }         private void number_three_Click(object sender, EventArgs e)         {             if (equal_flag)             {                 numberInputCheck(number_three);             }             else             {                 textBox1.Text = "";                 numberInputCheck(number_three);                 equal_flag = true;             }         }         private void number_four_Click(object sender, EventArgs e)         {             if (equal_flag)             {                 numberInputCheck(number_four);             }             else             {                 textBox1.Text = "";                 numberInputCheck(number_four);                 equal_flag = true;             }         }         private void number_five_Click(object sender, EventArgs e)         {             if (equal_flag)             {                 numberInputCheck(number_five);             }             else             {                 textBox1.Text = "";                 numberInputCheck(number_five);                 equal_flag = true;             }         }         private void number_six_Click(object sender, EventArgs e)         {             if (equal_flag)             {                 numberInputCheck(number_six);             }             else             {                 textBox1.Text = "";                 numberInputCheck(number_six);                 equal_flag = true;             }         }         private void number_seven_Click(object sender, EventArgs e)         {             if (equal_flag)             {                 numberInputCheck(number_seven);             }             else             {                 textBox1.Text = "";                 numberInputCheck(number_seven);                 equal_flag = true;             }         }         private void number_eight_Click(object sender, EventArgs e)         {             if (equal_flag)             {                 numberInputCheck(number_eight);             }             else             {                 textBox1.Text = "";                 numberInputCheck(number_eight);                 equal_flag = true;             }         }         private void number_nine_Click(object sender, EventArgs e)         {             if (equal_flag)             {                 numberInputCheck(number_nine);             }             else             {                 textBox1.Text = "";                 numberInputCheck(number_nine);                 equal_flag = true;             }         }         private string calculte(string operand1, string operand2, string action)         {             switch (action)             {                 case "+":                     //MessageBox.Show(operand1,"oper1");                     //MessageBox.Show(operand2, "oper2");                     operand1 = (double.Parse(operand1) + double.Parse(operand2)).ToString();                     //MessageBox.Show(operand1,"calculte");                     return operand1;                 // MessageBox.Show(operand1);                 case "-":                     operand1 = (double.Parse(operand1) - double.Parse(operand2)).ToString();                     return operand1;                 case "×":                     operand1 = (double.Parse(operand1) * double.Parse(operand2)).ToString();                     return operand1;                 case "÷":                     operand1 = (double.Parse(operand1) / double.Parse(operand2)).ToString();                     return operand1;                     // 以下的两个case是为tabPage2中服务的                 case "*":                     operand1 = (double.Parse(operand1) * double.Parse(operand2)).ToString();                     return operand1;                 case "/":                     operand1 = (double.Parse(operand1) / double.Parse(operand2)).ToString();                     return operand1;                 default:                     MessageBox.Show("您的操作有误!", "提示");                     return "wrong";             }         }                 private void operator_check(Button button)         {             // 第二个操作数还没有输入,说明这是用户这个运算中的第一个操作符             if ((!operand1.Equals("")) && (operand2.Equals("")))             {                 action = button.Text;                 textBox3.Text += button.Text;                 textBox1.Text = "";             }             // 两个操作数均不为空,说明这是第二个操作符了,这时应该将前两个运算数进行运算             else if ((!operand1.Equals("")) && (!operand2.Equals("")) && (!action.Equals("")) && equal_flag)             {                 textBox1.Text = "";                 operand1 = calculte(operand1, operand2, action);                 textBox3.Text += action;             }             // 直接点击操作符时提示出错!             else if (operand1.Equals(""))             {                 MessageBox.Show("请输入您要进行运算的数字!", "提示");                 textBox3.Text = "";             }             // 完成前一个运算后直接点击运算符,此时应该将上一次的结果作为第二次运算的第一个操作数             else if (!equal_flag)             {                 action = button.Text;                 operand1 = textBox1.Text;                 textBox1.Text = "";                 textBox3.Text = operand1 + button.Text;             }             else             {                 MessageBox.Show("wrong");             }         }         private void add_Click(object sender, EventArgs e)         {             operator_check(add);         }         private void sub_Click(object sender, EventArgs e)         {             operator_check(sub);         }         private void mul_Click(object sender, EventArgs e)         {             operator_check(mul);         }         private void div_Click(object sender, EventArgs e)         {             operator_check(div);         }         private void sqrt_button_Click(object sender, EventArgs e)         {             //只输入了一个操作数并且点击了这个按钮之后触发的事件             if (!operand1.Equals(""))             {                 sqrt_calculate(sqrt_button);             }             else             {                 MessageBox.Show("您的操作有误!", "提示");             }         }         private void sqrt_calculate(Button button)         {             textBox3.Text = "sqrt(" + operand1 + ")";             operand1 = Math.Sqrt(double.Parse(operand1)).ToString();             textBox1.Text = operand1;         }                 private void countor_Click(object sender, EventArgs e)         {             if (equal_flag)             {                 if ((!operand1.Equals("")) && (!operand2.Equals("")) && (!action.Equals("")))                 {                     textBox1.Text = "";                     textBox3.Text = "";                     operand1 = calculte(operand1, operand2, action);                     textBox1.Text = operand1;                     textBox3.Text = "";                     // 点击过“=”后,标志位就变为false                     equal_flag = false;                 }                 else                 {                     MessageBox.Show("您的输入有误!", "提示");                 }             }             else             {                 textBox3.Text = "";                 textBox3.Text = operand1 + action + operand2;                 operand1 = calculte(operand1, operand2, action);                 textBox1.Text = operand1;                 equal_flag = false;             }         }                 private bool check(string inputInfo)         {             // 即用户的输入必须是以下格式的:3+3-7*5......             //string matchInfo = "^[0-9]([+-*/][0-9])+$";             //Regex regex = new Regex(matchInfo);             //return regex.IsMatch(matchInfo,inputInfo);             return true;         }                 private void myCalculate_Click(object sender, EventArgs e)         {             operand1 = "";             operand2 = "";             // 用来存放用户的输入             string inputInfo = textBox2.Text;             if (textBox2.Text.Equals("作者的信息"))             {                 showInfo.Text = "作者的基本信息:\r\n" + "姓名:狗狗Mickey\r\n" + "国籍:中国\r\n" + "英名:Zoe\r\n" + "爱好:c#\r\n";                 for (int i = 0; i < 255; i++)                 {                     showInfo.BackColor = Color.FromArgb(255, 255 - i, i);                     System.Threading.Thread.Sleep(10);                     Application.DoEvents();                 }                 for (int i = 255; i > 0; i--)                 {                     showInfo.BackColor = Color.FromArgb(255, 255 - i, i);                     System.Threading.Thread.Sleep(10);                     Application.DoEvents();                 }             }             else                 // 用户的输入符合标准                 if (check(inputInfo))                 {                     showInfo.BackColor = Color.Transparent;                     operation(inputInfo);                 }                 else                 {                     MessageBox.Show("您的输入有误!", "狗狗温馨提示");                 }         }                 private void operation(string inputInfo)         {             if (inputInfo.Contains("+"))             {                 chooseOperation(inputInfo, '+');             }             else if (inputInfo.Contains("-"))             {                 chooseOperation(inputInfo,'-');             }             else if (inputInfo.Contains("*"))             {                 chooseOperation(inputInfo, '*');             }             else if (inputInfo.Contains("/"))             {                 chooseOperation(inputInfo, '/');             }         }                 private void chooseOperation(string inputInfo,char oper) {             // 找出运算符的位置             int index = inputInfo.IndexOf(oper);             // 将两个操作数提取出来             string[] operands = inputInfo.Split(oper);             operand1 = operands[0];             operand2 = operands[1];             //MessageBox.Show(operand1,operand2);             showInfo.Text = operand1 + oper + operand2 + "=" + calculte(operand1, operand2, oper.ToString());         }     } }

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 8 金币 [ 分享文档获得金币 ] 2 人已下载

下载文档