好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

C#实现简单的计算器功能完整实例

本文实例讲述了c#实现简单的计算器功能。分享给大家供大家参考,具体如下:

?

using system;

using system.collections.generic;

using system测试数据ponentmodel;

using system.data;

using system.drawing;

using system.linq;

using system.text;

using system.windows.forms;

using system.collections;

using system.threading;

using system.diagnostics;

namespace calculator

{

   public partial class form1 : form

   {

     public form1()

     {

       initializecomponent();

     }

     private string _sign;

     private double _first = 10241024;

     private double _second = 10241024;

     private string _number;

     private void button1_click( object sender, eventargs e)

     {

       _number += "1" ;

       this .textbox1.text += "1" ;

     }

     private void button2_click( object sender, eventargs e)

     {

       _number += "2" ;

       this .textbox1.text += "2" ;

     }

     private void button3_click( object sender, eventargs e)

     {

       _number += "3" ;

       this .textbox1.text += "3" ;

     }

     private void button4_click( object sender, eventargs e)

     {

       _number += "4" ;

       this .textbox1.text += "4" ;

     }

     private void button8_click( object sender, eventargs e)

     {

       _number += "5" ;

       this .textbox1.text += "5" ;

     }

     private void button7_click( object sender, eventargs e)

     {

       _number += "6" ;

       this .textbox1.text += "6" ;

     }

     private void button6_click( object sender, eventargs e)

     {

       _number += "7" ;

       this .textbox1.text += "7" ;

     }

     private void button5_click( object sender, eventargs e)

     {

       _number += "8" ;

       this .textbox1.text += "8" ;

     }

     private void button9_click( object sender, eventargs e)

     {

       _number += "9" ;

       this .textbox1.text += "9" ;

     }

     private void button10_click( object sender, eventargs e)

     {

       _number += "0" ;

       this .textbox1.text += "0" ;

     }

     private void button16_click( object sender, eventargs e)

     {

       //设置加号计算数

       setnumber(convert.todouble(_number));

       if (_sign == null || _second == 10241024)

       {

         _sign = "+" ;

         this .textbox1.text += "+" ;

         return ;

       }

       if (_second != 10241024)

       {

         //开始进行计算

         computer compro = new computer(_sign, _first, _second);

         this .textbox1.text = compro测试数据t( out _first).tostring();

         //验证除数为零的情况

         if (_first == 10241024.10241023)

         {

           this .textbox1.text = "0" ;

           messagebox.show( "除数不能为零!" );

           _number = null ;

           _first = 10241024;

           _second = 10241024;

           this .textbox1.text = null ;

           _sign = null ;

           this .textbox1.text = null ;

         }

         //开始初始化数据

         _sign = "+" ;

         this .textbox1.text += "+" ;

         _second = 10241024;

         _number = null ;

         return ;

       }

     }

     private void button15_click( object sender, eventargs e)

     {

       //设置减号计算数

       setnumber(convert.todouble(_number));

       if (_sign == null || _second == 10241024)

       {

         _sign = "-" ;

         this .textbox1.text += "-" ;

         return ;

       }

       if (_second != 10241024)

       {

         //开始进行计算

         computer compro = new computer(_sign, _first, _second);

         this .textbox1.text = compro测试数据t( out _first).tostring();

         //验证除数为零的情况

         if (_first == 10241024.10241023)

         {

           this .textbox1.text = "0" ;

           messagebox.show( "除数不能为零!" );

           _number = null ;

           _first = 10241024;

           _second = 10241024;

           this .textbox1.text = null ;

           _sign = null ;

           this .textbox1.text = null ;

         }

         //开始初始化数据

         _sign = "-" ;

         this .textbox1.text += "-" ;

         _second = 10241024;

         _number = null ;

         return ;

       }

     }

     private void button14_click( object sender, eventargs e)

     {

       //设置乘号计算数

       setnumber(convert.todouble(_number));

       if (_sign == null || _second == 10241024)

       {

         _sign = "*" ;

         this .textbox1.text += "*" ;

         return ;

       }

       if (_second != 10241024)

       {

         //开始进行计算

         computer compro = new computer(_sign, _first, _second);

         this .textbox1.text = compro测试数据t( out _first).tostring();

         //验证除数为零的情况

         if (_first == 10241024.10241023)

         {

           this .textbox1.text = "0" ;

           messagebox.show( "除数不能为零!" );

           _number = null ;

           _first = 10241024;

           _second = 10241024;

           this .textbox1.text = null ;

           _sign = null ;

           this .textbox1.text = null ;

         }

         //开始初始化数据

         _sign = "*" ;

         this .textbox1.text += "*" ;

         _second = 10241024;

         _number = null ;

         return ;

       }

     }

       /// <summary>

       /// 设置除号计算数

       /// </summary>

       /// <param name="sender"></param>

       /// <param name="e"></param>

     private void button11_click( object sender, eventargs e)

     {

       setnumber(convert.todouble(_number));

       if (_sign == null || _second == 10241024)

       {

         _sign = "/" ;

         this .textbox1.text += "/" ;

         return ;

       }

       if (_second != 10241024)

       {

         //开始进行计算

         computer compro = new computer(_sign, _first, _second);

         this .textbox1.text = compro测试数据t( out _first).tostring();

         //验证除数为零的情况

         if (_first == 10241024.10241023)

         {

           this .textbox1.text = "0" ;

           this .textbox1.text = "除数不能为零!" ;

           _number = null ;

           _first = 10241024;

           _second = 10241024;

           this .textbox1.text = null ;

           _sign = null ;

           this .textbox1.text = null ;

         }

         //开始初始化数据

         _sign = "/" ;

         this .textbox1.text += "/" ;

         _second = 10241024;

         _number = null ;

         return ;

       }

     }

     /// <summary>

     /// 清空输入框

     /// </summary>

     /// <param name="sender"></param>

     /// <param name="e"></param>

     private void button12_click( object sender, eventargs e)

     {

       _number = null ;

       _first = 10241024;

       _second = 10241024;

       this .textbox1.text = null ;

       _sign = null ;

       this .textbox1.text = null ;

     }

     private void button13_click( object sender, eventargs e)

     {

       setnumber(convert.todouble(_number));

       //判断是否设置了_second的值

       if (_second == 10241024)

       {

         _second = convert.todouble(_number);

         _number = null ;

       }

       //判断是否设置了_first的值

       if (_first == 10241024)

       {

         _first = convert.todouble(_number);

         _number = null ;

       }

       //判断是否有_sign的值

       if (_sign == null )

       {

         _sign = "+" ;

       }

       //开始进行计算

       computer compro = new computer(_sign, _first, _second);

       this .textbox1.text = compro测试数据t( out _first).tostring();

       //验证除数为零的情况

       if (_first == 10241024.10241023)

       {

         this .textbox1.text = "0" ;

         messagebox.show( "除数不能为零!" );

         _number = null ;

         _first = 10241024;

         _second = 10241024;

         this .textbox1.text = null ;

         _sign = null ;

         this .textbox1.text = null ;

       }

       //开始初始化数据

       _sign = null ;

       _second = 10241024;

       _number = null ;

     }

     public class computer

     {

       private string _sign;

       private double _first;

       private double _second;

       public computer( string str, double first, double second)

       {

         _sign = str;

         _first = first;

         _second = second;

       }

       //选择相应的运算符进行运算

       public double comt( out double first)

       {

         if (_sign == "+" )

         {

           first = _first + _second;

           return _first + _second;

         }

         else if (_sign == "-" )

         {

           first = _first - _second;

           return _first - _second;

         }

         else if (_sign == "*" )

         {

           first = _first * _second;

           return _first * _second;

         }

         else if (_sign == "/" )

         {

           if (_second == 0)

           {

             first = 10241024.10241023;

             return 10241024.10241023;

           }

           else

           {

             first = _first / _second;

             return _first / _second;

           }

         }

         else

         {

           first = 10241024.10241024;

           return 10241024.10241024;

         }

       }

     }

     //设置数字函数

     public void setnumber( double number)

     {

       if (_first == 10241024 && _number != null )

       {

         _first = convert.todouble(_number);

         _number = null ;

       }

       else if (_second == 10241024 && _number != null )

       {

         _second = convert.todouble(_number);

         _number = null ;

       }

     }

     private void timer1_tick( object sender, eventargs e)

     {

       this .label2.text = datetime.now.tostring();

     }

     private void button24_click( object sender, eventargs e)

     {

       if ( this .textbox1.text == " " )

       {

         messagebox.show( "请输入数据" );

         this .textbox1.text = convert.toint64(textbox1.text.tostring(), 2).tostring();

       }

       else

       {

         this .textbox1.text = convert.toint64(textbox1.text.tostring(), 2).tostring();

       }

     }

     private void button22_click( object sender, eventargs e)

     {

       if ( this .textbox1.text == " " )

       {

         messagebox.show( "请输入数据" );

         int64 int64 = convert.toint64(textbox1.text.tostring(), 2);

         this .textbox1.text = convert.tostring(int64,16);

       }

       else

       {

         int64 int64 = convert.toint64(textbox1.text.tostring(), 2);

         this .textbox1.text = convert.tostring(int64, 16);

       }

     }

     private void button23_click( object sender, eventargs e)

     {

       if ( this .textbox1.text == " " )

       {

         messagebox.show( "请输入数据" );

         int64 int64 = convert.toint64(textbox1.text.tostring(), 2);

         this .textbox1.text = convert.tostring(int64, 8);

       }

       else

       {

         int64 int64 = convert.toint64(textbox1.text.tostring(), 2);

         this .textbox1.text = convert.tostring(int64, 8);

       }

     }

     private void button21_click( object sender, eventargs e)

     {

       if ( this .textbox1.text == " " )

       {

         messagebox.show( "请输入数据" );

         int64 int64 = convert.toint64(textbox1.text);

         this .textbox1.text = convert.tostring(int64, 2);

       }

       else

       {

         int64 int64 = convert.toint64(textbox1.text);

         this .textbox1.text = convert.tostring(int64, 2);

       }

     }

     private void button27_click( object sender, eventargs e)

     {

       double buffer1= convert.todouble(textbox1.text);

         buffer1 = 1 / buffer1;

       this .textbox1.text = buffer1.tostring();

     }

     private void button17_click( object sender, eventargs e)

     {

       _number += "." ;

       this .textbox1.text += "." ;

     }

     private void button25_click( object sender, eventargs e)

     {

       double buffer1 = convert.todouble(textbox1.text);

       buffer1 = buffer1 * buffer1;

       this .textbox1.text = buffer1.tostring();

     }

     private void button26_click( object sender, eventargs e)

     {

       if ( this .textbox1.text == " " )

       {

         messagebox.show( "请输入数据" );

         int64 int64 = convert.toint64( this .textbox1.text);

         int64 temp1 = 1;

         while (int64 > 1)

         {

           temp1 = temp1*int64 ;

           int64--;

         }

         this .textbox1.text = temp1.tostring();

       }

       else

       {

         int64 int64 = convert.toint64( this .textbox1.text);

         int64 temp1 = 1;

         while (int64 > 1)

         {

           temp1 = temp1 * int64;

           int64--;

         }

         this .textbox1.text = temp1.tostring();

       }

     }

     private void button28_click( object sender, eventargs e)

     {

       //int64 int64 = convert.toint64(this.textbox1.text);

       //double temp=math.log10(int64);

       //this.textbox1.text = temp.tostring();

       double temp1, temp2;

       string [] strarry = new string [2];

       strarry = this .textbox1.text.split( '.' );

       temp1 = convert.todouble(strarry[0]);

       temp2 = convert.todouble(strarry[1]);

       // double temp1 = convert.todouble(this.textbox1.text);

       temp2 = math.log(temp2,temp1 );

       this .textbox1.text = temp2.tostring();

     }

     private void button18_click( object sender, eventargs e)

     {

       double temp1 = convert.todouble( this .textbox1.text);

       double temp2 = math.sin(temp1);

       this .textbox1.text = temp2.tostring();

     }

     private void button19_click( object sender, eventargs e)

     {

       double temp1 = convert.todouble( this .textbox1.text);

       double temp2 = math.cos(temp1);

       this .textbox1.text = temp2.tostring();

     }

     private void button20_click( object sender, eventargs e)

     {

       double temp1 = convert.todouble( this .textbox1.text);

       double temp2 = math.tan(temp1);

       this .textbox1.text = temp2.tostring();

     }

     private void button33_click( object sender, eventargs e)

     {

       double temp1, temp2;

       string [] strarry= new string [2];

       strarry= this .textbox1.text.split( '.' );

       temp1 = convert.todouble(strarry[0]);

       temp2 = convert.todouble(strarry[1]);

       // double temp1 = convert.todouble(this.textbox1.text);

       temp2 = math.pow(temp1,temp2);

       this .textbox1.text = temp2.tostring();

     }

     /// <summary>

     /// 计算e的temp1次幂

     /// </summary>

     /// <param name="sender"></param>

     /// <param name="e"></param>

     private void button31_click( object sender, eventargs e)

     {

       double temp1 = convert.todouble( this .textbox1.text);

       double temp2 = math.exp(temp1);

       this .textbox1.text = temp2.tostring();

     }

     private void button34_click( object sender, eventargs e)

     {

        double temp1 = convert.todouble( this .textbox1.text);

       double temp2 = math.sqrt(temp1);

       this .textbox1.text = temp2.tostring();

     }

     private void button32_click( object sender, eventargs e)

     {

       double temp1, temp2;

       string [] strarry = new string [2];

       strarry = this .textbox1.text.split( '.' );

       temp1 = convert.todouble(strarry[0]);

       temp2 = convert.todouble(strarry[1]);

       // double temp1 = convert.todouble(this.textbox1.text);

       temp2 = math.ieeeremainder(temp1, temp2);

       this .textbox1.text = temp2.tostring();

     }

     /// <summary>

     /// 圆的面积

     /// </summary>

     /// <param name="sender"></param>

     /// <param name="e"></param>

     private void button30_click( object sender, eventargs e)

     {

       double buffer1 = convert.todouble(textbox1.text);

       buffer1 =3.1415926*buffer1*buffer1;

       this .textbox1.text = buffer1.tostring();

     }

     /// <summary>

     /// 三角形面积

     /// </summary>

     /// <param name="sender"></param>

     /// <param name="e"></param>

     private void button29_click( object sender, eventargs e)

     {

       double temp1, temp2,temp3,p,area;

       string [] strarry = new string [3];

       strarry = this .textbox1.text.split( '.' );

       temp1 = convert.todouble(strarry[0]);

       temp2 = convert.todouble(strarry[1]);

       temp3 = convert.todouble(strarry[2]);

       // double temp1 = convert.todouble(this.textbox1.text);

       p = (temp1 + temp2 + temp3) / 2;

       area= p * (p - temp1) * (p - temp2) * (p - temp3);

       area = math.sqrt(area);

       this .textbox1.text = area.tostring();

     }

     private void linklabel1_linkclicked( object sender, linklabellinkclickedeventargs e)

     {

       string str = e.link.tostring();

       process.start(str);

     }

     private void form1_load( object sender, eventargs e)

     {

     }

   }

}

运行效果:

希望本文所述对大家c#程序设计有所帮助。

原文链接:http://blog.csdn.net/microzone/article/details/16904057

dy("nrwz");

查看更多关于C#实现简单的计算器功能完整实例的详细内容...

  阅读:42次