欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#编写的windows计算器的实例代码

程序员文章站 2023-12-20 13:15:34
复制代码 代码如下: using system; using system.drawing; using system.windows; using system....

复制代码 代码如下:

using system;
using system.drawing;
using system.windows;
using system.windows.forms;
using system.collections;
using system.componentmodel;
using system.data;
namespace comput
{
    /// <summary>
    /// 这是一个计算器的简单实现。
    /// </summary>
    public class form1 : system.windows.forms.form
    {
        #region 控件声明
        private system.windows.forms.textbox txtshow;
        private system.windows.forms.groupbox groupbox1;
        private system.windows.forms.button btn_rev;
        private system.windows.forms.button btn_dot;
        private system.windows.forms.button btn_add;
        private system.windows.forms.button btn_equ;
        private system.windows.forms.button btn_sign;
        private system.windows.forms.button btn_sub;
        private system.windows.forms.button btn_mul;
        private system.windows.forms.button btn_0;
        private system.windows.forms.button btn_3;
        private system.windows.forms.button btn_2;
        private system.windows.forms.button btn_1;
        private system.windows.forms.button btn_6;
        private system.windows.forms.button btn_5;
        private system.windows.forms.button btn_4;
        private system.windows.forms.button btn_sqrt;
        private system.windows.forms.button btn_div;
        private system.windows.forms.button btn_7;
        private system.windows.forms.button btn_8;
        private system.windows.forms.button btn_9;
        private system.windows.forms.mainmenu mainmenu1;
        private system.windows.forms.menuitem menuitem1;
        private system.windows.forms.button btn_sqr;
        private system.windows.forms.menuitem menuitem2;
        private system.windows.forms.menuitem menuitem3;
        private system.windows.forms.menuitem menuitem4;
        private system.windows.forms.menuitem menuitem5;
        private system.windows.forms.menuitem menuitem6;
        private system.windows.forms.button c;
        private system.windows.forms.button ce;
        private system.windows.forms.menuitem menuitem8;
        private system.windows.forms.menuitem menuitem9;
        private system.windows.forms.menuitem menuitem10;
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private system.componentmodel.container components = null;
        public form1()
        {
            //
            // windows 窗体设计器支持所必需的
            //
            initializecomponent();
            //
            // todo: 在 initializecomponent 调用后添加任何构造函数代码
            //
        }
        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        protected override void dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.dispose();
                }
            }
            base.dispose( disposing );
        }
       #endregion
        #region 各控件的属性的方法windows form designer generated code
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void initializecomponent()
        {
            system.resources.resourcemanager resources = new system.resources.resourcemanager(typeof(form1));
            this.txtshow = new system.windows.forms.textbox();
            this.groupbox1 = new system.windows.forms.groupbox();
            this.c = new system.windows.forms.button();
            this.ce = new system.windows.forms.button();
            this.btn_rev = new system.windows.forms.button();
            this.btn_dot = new system.windows.forms.button();
            this.btn_add = new system.windows.forms.button();
            this.btn_equ = new system.windows.forms.button();
            this.btn_sign = new system.windows.forms.button();
            this.btn_sub = new system.windows.forms.button();
            this.btn_mul = new system.windows.forms.button();
            this.btn_0 = new system.windows.forms.button();
            this.btn_3 = new system.windows.forms.button();
            this.btn_2 = new system.windows.forms.button();
            this.btn_1 = new system.windows.forms.button();
            this.btn_6 = new system.windows.forms.button();
            this.btn_5 = new system.windows.forms.button();
            this.btn_4 = new system.windows.forms.button();
            this.btn_sqrt = new system.windows.forms.button();
            this.btn_div = new system.windows.forms.button();
            this.btn_7 = new system.windows.forms.button();
            this.btn_8 = new system.windows.forms.button();
            this.btn_9 = new system.windows.forms.button();
            this.btn_sqr = new system.windows.forms.button();
            this.mainmenu1 = new system.windows.forms.mainmenu();
            this.menuitem1 = new system.windows.forms.menuitem();
            this.menuitem2 = new system.windows.forms.menuitem();
            this.menuitem3 = new system.windows.forms.menuitem();
            this.menuitem4 = new system.windows.forms.menuitem();
            this.menuitem5 = new system.windows.forms.menuitem();
            this.menuitem6 = new system.windows.forms.menuitem();
            this.menuitem8 = new system.windows.forms.menuitem();
            this.menuitem9 = new system.windows.forms.menuitem();
            this.menuitem10 = new system.windows.forms.menuitem();
            this.groupbox1.suspendlayout();
            this.suspendlayout();
            //
            // txtshow
            //
            this.txtshow.backcolor = system.drawing.color.fromargb(((system.byte)(192)), ((system.byte)(192)), ((system.byte)(255)));
            this.txtshow.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.txtshow.forecolor = system.drawing.color.purple;
            this.txtshow.location = new system.drawing.point(25, 8);
            this.txtshow.name = "txtshow";
            this.txtshow.size = new system.drawing.size(228, 23);
            this.txtshow.tabindex = 1;
            this.txtshow.text = "0.";
            this.txtshow.textalign = system.windows.forms.horizontalalignment.right;
            //
            // groupbox1
            //
            this.groupbox1.controls.add(this.c);
            this.groupbox1.controls.add(this.ce);
            this.groupbox1.controls.add(this.btn_rev);
            this.groupbox1.controls.add(this.btn_dot);
            this.groupbox1.controls.add(this.btn_add);
            this.groupbox1.controls.add(this.btn_equ);
            this.groupbox1.controls.add(this.btn_sign);
            this.groupbox1.controls.add(this.btn_sub);
            this.groupbox1.controls.add(this.btn_mul);
            this.groupbox1.controls.add(this.btn_0);
            this.groupbox1.controls.add(this.btn_3);
            this.groupbox1.controls.add(this.btn_2);
            this.groupbox1.controls.add(this.btn_1);
            this.groupbox1.controls.add(this.btn_6);
            this.groupbox1.controls.add(this.btn_5);
            this.groupbox1.controls.add(this.btn_4);
            this.groupbox1.controls.add(this.btn_sqrt);
            this.groupbox1.controls.add(this.btn_div);
            this.groupbox1.controls.add(this.btn_7);
            this.groupbox1.controls.add(this.btn_8);
            this.groupbox1.controls.add(this.btn_9);
            this.groupbox1.controls.add(this.btn_sqr);
            this.groupbox1.location = new system.drawing.point(24, 40);
            this.groupbox1.name = "groupbox1";
            this.groupbox1.size = new system.drawing.size(232, 184);
            this.groupbox1.tabindex = 21;
            this.groupbox1.tabstop = false;
            this.groupbox1.text = "计算机区";
            //
            // c
            //
            this.c.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.c.forecolor = system.drawing.color.red;
            this.c.location = new system.drawing.point(178, 48);
            this.c.name = "c";
            this.c.size = new system.drawing.size(36, 61);
            this.c.tabindex = 41;
            this.c.text = "c";
            this.c.click += new system.eventhandler(this.btn_oper);
            //
            // ce
            //
            this.ce.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.ce.forecolor = system.drawing.color.red;
            this.ce.location = new system.drawing.point(138, 16);
            this.ce.name = "ce";
            this.ce.size = new system.drawing.size(76, 29);
            this.ce.tabindex = 40;
            this.ce.text = "ce";
            this.ce.click += new system.eventhandler(this.btn_oper);
            //
            // btn_rev
            //
            this.btn_rev.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.btn_rev.forecolor = system.drawing.color.blue;
            this.btn_rev.location = new system.drawing.point(178, 112);
            this.btn_rev.name = "btn_rev";
            this.btn_rev.size = new system.drawing.size(36, 29);
            this.btn_rev.tabindex = 39;
            this.btn_rev.text = "1/x";
            this.btn_rev.click += new system.eventhandler(this.btn_oper);
            //
            // btn_dot
            //
            this.btn_dot.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.btn_dot.location = new system.drawing.point(98, 144);
            this.btn_dot.name = "btn_dot";
            this.btn_dot.size = new system.drawing.size(36, 29);
            this.btn_dot.tabindex = 38;
            this.btn_dot.tag = "0";
            this.btn_dot.text = ".";
            this.btn_dot.click += new system.eventhandler(this.btn_oper);
            //
            // btn_add
            //
            this.btn_add.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.btn_add.forecolor = system.drawing.color.red;
            this.btn_add.location = new system.drawing.point(138, 144);
            this.btn_add.name = "btn_add";
            this.btn_add.size = new system.drawing.size(36, 29);
            this.btn_add.tabindex = 37;
            this.btn_add.text = "+";
            this.btn_add.click += new system.eventhandler(this.btn_oper);
            //
            // btn_equ
            //
            this.btn_equ.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.btn_equ.forecolor = system.drawing.color.red;
            this.btn_equ.location = new system.drawing.point(178, 144);
            this.btn_equ.name = "btn_equ";
            this.btn_equ.size = new system.drawing.size(36, 29);
            this.btn_equ.tabindex = 36;
            this.btn_equ.text = "=";
            this.btn_equ.click += new system.eventhandler(this.btn_equ_click);
            //
            // btn_sign
            //
            this.btn_sign.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.btn_sign.forecolor = system.drawing.color.blue;
            this.btn_sign.location = new system.drawing.point(58, 144);
            this.btn_sign.name = "btn_sign";
            this.btn_sign.size = new system.drawing.size(36, 29);
            this.btn_sign.tabindex = 35;
            this.btn_sign.text = "+/-";
            this.btn_sign.click += new system.eventhandler(this.btn_oper);
            //
            // btn_sub
            //
            this.btn_sub.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.btn_sub.forecolor = system.drawing.color.red;
            this.btn_sub.location = new system.drawing.point(138, 112);
            this.btn_sub.name = "btn_sub";
            this.btn_sub.size = new system.drawing.size(36, 29);
            this.btn_sub.tabindex = 34;
            this.btn_sub.text = "-";
            this.btn_sub.click += new system.eventhandler(this.btn_oper);
            //
            // btn_mul
            //
            this.btn_mul.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.btn_mul.forecolor = system.drawing.color.red;
            this.btn_mul.location = new system.drawing.point(138, 80);
            this.btn_mul.name = "btn_mul";
            this.btn_mul.size = new system.drawing.size(36, 29);
            this.btn_mul.tabindex = 33;
            this.btn_mul.text = "*";
            this.btn_mul.click += new system.eventhandler(this.btn_oper);
            //
            // btn_0
            //
            this.btn_0.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.btn_0.forecolor = system.drawing.color.blue;
            this.btn_0.location = new system.drawing.point(18, 144);
            this.btn_0.name = "btn_0";
            this.btn_0.size = new system.drawing.size(36, 29);
            this.btn_0.tabindex = 32;
            this.btn_0.tag = "0";
            this.btn_0.text = "0";
            this.btn_0.click += new system.eventhandler(this.btn_0_click);
            //
            // btn_3
            //
            this.btn_3.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.btn_3.forecolor = system.drawing.color.blue;
            this.btn_3.location = new system.drawing.point(98, 112);
            this.btn_3.name = "btn_3";
            this.btn_3.size = new system.drawing.size(36, 29);
            this.btn_3.tabindex = 31;
            this.btn_3.tag = "3";
            this.btn_3.text = "3";
            this.btn_3.click += new system.eventhandler(this.btn_0_click);
            //
            // btn_2
            //
            this.btn_2.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.btn_2.forecolor = system.drawing.color.blue;
            this.btn_2.location = new system.drawing.point(58, 112);
            this.btn_2.name = "btn_2";
            this.btn_2.size = new system.drawing.size(36, 29);
            this.btn_2.tabindex = 30;
            this.btn_2.tag = "2";
            this.btn_2.text = "2";
            this.btn_2.click += new system.eventhandler(this.btn_0_click);
            //
            // btn_1
            //
            this.btn_1.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.btn_1.forecolor = system.drawing.color.blue;
            this.btn_1.location = new system.drawing.point(18, 112);
            this.btn_1.name = "btn_1";
            this.btn_1.size = new system.drawing.size(36, 29);
            this.btn_1.tabindex = 29;
            this.btn_1.tag = "1";
            this.btn_1.text = "1";
            this.btn_1.click += new system.eventhandler(this.btn_0_click);
            //
            // btn_6
            //
            this.btn_6.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.btn_6.forecolor = system.drawing.color.blue;
            this.btn_6.location = new system.drawing.point(98, 80);
            this.btn_6.name = "btn_6";
            this.btn_6.size = new system.drawing.size(36, 29);
            this.btn_6.tabindex = 28;
            this.btn_6.tag = "6";
            this.btn_6.text = "6";
            this.btn_6.click += new system.eventhandler(this.btn_0_click);
            //
            // btn_5
            //
            this.btn_5.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.btn_5.forecolor = system.drawing.color.blue;
            this.btn_5.location = new system.drawing.point(58, 80);
            this.btn_5.name = "btn_5";
            this.btn_5.size = new system.drawing.size(36, 29);
            this.btn_5.tabindex = 27;
            this.btn_5.tag = "5";
            this.btn_5.text = "5";
            this.btn_5.click += new system.eventhandler(this.btn_0_click);
            //
            // btn_4
            //
            this.btn_4.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.btn_4.forecolor = system.drawing.color.blue;
            this.btn_4.location = new system.drawing.point(18, 80);
            this.btn_4.name = "btn_4";
            this.btn_4.size = new system.drawing.size(36, 29);
            this.btn_4.tabindex = 26;
            this.btn_4.tag = "4";
            this.btn_4.text = "4";
            this.btn_4.click += new system.eventhandler(this.btn_0_click);
            //
            // btn_sqrt
            //
            this.btn_sqrt.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.btn_sqrt.forecolor = system.drawing.color.blue;
            this.btn_sqrt.location = new system.drawing.point(18, 16);
            this.btn_sqrt.name = "btn_sqrt";
            this.btn_sqrt.size = new system.drawing.size(76, 29);
            this.btn_sqrt.tabindex = 25;
            this.btn_sqrt.text = "sqrt";
            this.btn_sqrt.click += new system.eventhandler(this.btn_oper);
            //
            // btn_div
            //
            this.btn_div.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.btn_div.forecolor = system.drawing.color.red;
            this.btn_div.location = new system.drawing.point(138, 48);
            this.btn_div.name = "btn_div";
            this.btn_div.size = new system.drawing.size(36, 29);
            this.btn_div.tabindex = 24;
            this.btn_div.text = "/";
            this.btn_div.click += new system.eventhandler(this.btn_oper);
            //
            // btn_7
            //
            this.btn_7.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.btn_7.forecolor = system.drawing.color.blue;
            this.btn_7.location = new system.drawing.point(18, 48);
            this.btn_7.name = "btn_7";
            this.btn_7.size = new system.drawing.size(36, 29);
            this.btn_7.tabindex = 23;
            this.btn_7.tag = "7";
            this.btn_7.text = "7";
            this.btn_7.click += new system.eventhandler(this.btn_0_click);
            //
            // btn_8
            //
            this.btn_8.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.btn_8.forecolor = system.drawing.color.blue;
            this.btn_8.location = new system.drawing.point(58, 48);
            this.btn_8.name = "btn_8";
            this.btn_8.size = new system.drawing.size(36, 29);
            this.btn_8.tabindex = 22;
            this.btn_8.tag = "8";
            this.btn_8.text = "8";
            this.btn_8.click += new system.eventhandler(this.btn_0_click);
            //
            // btn_9
            //
            this.btn_9.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.btn_9.forecolor = system.drawing.color.blue;
            this.btn_9.location = new system.drawing.point(98, 48);
            this.btn_9.name = "btn_9";
            this.btn_9.size = new system.drawing.size(36, 29);
            this.btn_9.tabindex = 21;
            this.btn_9.tag = "9";
            this.btn_9.text = "9";
            this.btn_9.click += new system.eventhandler(this.btn_0_click);
            //
            // btn_sqr
            //
            this.btn_sqr.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.btn_sqr.forecolor = system.drawing.color.blue;
            this.btn_sqr.location = new system.drawing.point(98, 16);
            this.btn_sqr.name = "btn_sqr";
            this.btn_sqr.size = new system.drawing.size(36, 29);
            this.btn_sqr.tabindex = 19;
            this.btn_sqr.text = "sqr";
            this.btn_sqr.click += new system.eventhandler(this.btn_oper);
            //
            // mainmenu1
            //
            this.mainmenu1.menuitems.addrange(new system.windows.forms.menuitem[] {
                                                                                      this.menuitem8,
                                                                                      this.menuitem1,
                                                                                      this.menuitem4});
            //
            // menuitem1
            //
            this.menuitem1.index = 1;
            this.menuitem1.menuitems.addrange(new system.windows.forms.menuitem[] {
                                                                                      this.menuitem2,
                                                                                      this.menuitem3});
            this.menuitem1.text = "编辑(&e)";
            //
            // menuitem2
            //
            this.menuitem2.index = 0;
            this.menuitem2.text = "复制(&c)";
            this.menuitem2.click += new system.eventhandler(this.menuitem2_click);
            //
            // menuitem3
            //
            this.menuitem3.index = 1;
            this.menuitem3.text = "粘贴(&p)";
            this.menuitem3.click += new system.eventhandler(this.menuitem3_click);
            //
            // menuitem4
            //
            this.menuitem4.index = 2;
            this.menuitem4.menuitems.addrange(new system.windows.forms.menuitem[] {
                                                                                      this.menuitem5,
                                                                                      this.menuitem6});
            this.menuitem4.text = "帮助(&h)";
            //
            // menuitem5
            //
            this.menuitem5.index = 0;
            this.menuitem5.text = "帮助主题(&h)";
            this.menuitem5.click += new system.eventhandler(this.menuitem5_click);
            //
            // menuitem6
            //
            this.menuitem6.index = 1;
            this.menuitem6.text = "关于(&a)";
            this.menuitem6.click += new system.eventhandler(this.menuitem6_click);
            //
            // menuitem8
            //
            this.menuitem8.index = 0;
            this.menuitem8.menuitems.addrange(new system.windows.forms.menuitem[] {
                                                                                      this.menuitem9,
                                                                                      this.menuitem10});
            this.menuitem8.text = "文件(&f)";
            //
            // menuitem9
            //
            this.menuitem9.index = 0;
            this.menuitem9.text = "打开windows计算器(&o)";
            this.menuitem9.click += new system.eventhandler(this.menuitem9_click);
            //
            // menuitem10
            //
            this.menuitem10.index = 1;
            this.menuitem10.text = "退出(&q)";
            this.menuitem10.click += new system.eventhandler(this.menuitem10_click);
            //
            // form1
            //
            this.autoscalebasesize = new system.drawing.size(6, 14);
            this.clientsize = new system.drawing.size(278, 239);
            this.controls.add(this.groupbox1);
            this.controls.add(this.txtshow);
            this.formborderstyle = system.windows.forms.formborderstyle.fixed3d;
            this.icon = ((system.drawing.icon)(resources.getobject("$this.icon")));
            this.maximizebox = false;
            this.menu = this.mainmenu1;
            this.name = "form1";
            this.startposition = system.windows.forms.formstartposition.centerscreen;
            this.text = "计算器";
            this.groupbox1.resumelayout(false);
            this.resumelayout(false);
        }
        #endregion
        #region 各变量和常数的声明
        public const int null = 0;      // 定义操作码
        public const int add = 1;     //表示加
        public const int sub = 2;     //减
        public const int mul = 3;     //乘
        public const int div = 4;    //除
        public const int sqr = 5;    //求平方
        public const int sqrt = 6;   //求平方根
        public const int nodot  = 0;     // 定义是否点击了小数点,0 为没点
        public const int hasdot = 1;
        private double res = 0;         // 记录结果数
        private double tmp = 0;         // 当前输入的操作数
        private int opt = null;         // 记录操作码
        private int dot = nodot;    // 记录是否点击了小数点,0为没有点
        private int num = 0;        // 记录输入操作数的个数
        private int dotnum = 0;        // 记录小数点部分的个数       
        string stroper;            //获取操作符
        #endregion
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [stathread]
        static void main()
        {
            application.run(new form1());
        }
        /// <summary>
        #region 获取操作数事件
        //获取操作数事件
        private void btn_0_click(object sender, system.eventargs e)
        {
            system.windows.forms.button btntmp;
            double i;
            btntmp = sender as system.windows.forms.button;           
            if (btntmp != null)
            {
                if (dot == nodot)
                {
                    // 没有点击小数点
                    i = double.parse(btntmp.tag.tostring()); //取用户自定义的控件关联数,并转换成double型
                    tmp = tmp * 10 + i;
                    txtshow.text = tmp.tostring();   //将其放入文本显示屏啊
                }
                else   // 点击了小数点
                {                   
                    dotnum++;    //记录小数点部分的个数
                                                       // 生成小数部分的新的数值
                    i = double.parse(btntmp.tag.tostring()) / system.math.pow(10,dotnum);
                    tmp = tmp + i;  //将小数点后的值加到当前操做数
                    txtshow.text = tmp.tostring();
                }
            }
        }
        #endregion
        #region 等于事件和等于运算方法
        //等于事件
        private void btn_equ_click(object sender, system.eventargs e)
        {
            calc();
        }
        //等于运算方法
        private void calc()
        {
            // 生成结果
            if (num == 0 )    //是否有操作数,没有就返回0
            {
                res = 0;
                tmp = 0;
                txtshow.text = res.tostring();
                return;
            }
            switch (opt)  //找到对应的运算符进行计算
            {
                    // 加法
                case add:
                    res = res + tmp;                   
                    break;
                    // 减法
                case sub:
                    res = res - tmp;                   
                    break;
                    // 乘法
                case mul:
                    res = res * tmp;                   
                    break;
                    // 除法
                case div:
                    res = res / tmp;                   
                    break;
                    // 平方
                case sqr:
                    res = tmp * tmp;                   
                    break;
                    // 平方根
                case sqrt:
                    res = system.math.sqrt(tmp);                   
                    break;
                default:
                    return;
            }
            txtshow.text = res.tostring();   //结果输出到文本显示屏
            opt = null;  //运算符清空
            tmp=0;   
            dot = nodot;
                    //res = 0;
                    //num = 0;
        }
        #endregion
        #region 获取操作符运事件
        //获取操作符运事件
        private void btn_oper(object obj,eventargs ea)
        {
            button tmp1=(button)obj;
            stroper=tmp1.text;
            switch (stroper)
            {
                case "/":       //除法运算
                    if(opt!=null&&opt!=div)
                    {
                        calc();
                    }
                    opt = div;
                    if (num != 0)  //判断操作数的个数,如果两个就做二元运算
                    {
                        if (tmp != 0)
                            res = res / tmp;
                    }
                    else
                        res = tmp;
                    num++;                     
                    tmp = 0;
                    txtshow.text = res.tostring();
                    dot = nodot;
                    break;
                case "*":
                    // 乘法运算
                    if(opt!=null&&opt!=mul)
                    {
                        calc();
                    }
                    opt = mul;
                    if (num!= 0 )     //判断操作数的个数,如果两个就做二元运算   
                    {
                            if(tmp!=0)
                         res = res * tmp;
                    }
                    else
                        res = tmp;
                    num++;                                     
                    tmp = 0;
                    txtshow.text = res.tostring();
                    dot = nodot;
                    break;
                case "+":            //加法运算
                    if(opt!=null&&opt!=add)
                    {
                        calc();
                    }
                    opt = add;
                    if (num != 0)    //判断操作数的个数,如果两个就做二元运算
                        res = res + tmp;
                    else
                        res = tmp;
                    num++;                   
                    tmp = 0;
                    txtshow.text = res.tostring();
                    dot = nodot;
                    break;
                case "-":        //减法运算
                    if(opt!=null&&opt!=sub)
                    {
                        calc();
                    }
                    /*if(opt==add)
                    {
                        res=res+tmp;
                        tmp=0;
                    }*/
                    opt = sub;
                    if (num != 0)    //判断操作数的个数,如果两个就做二元运算
                        res = res - tmp;
                    else
                        res = tmp;
                    num++;                    
                    tmp = 0;
                    txtshow.text = res.tostring();
                    dot = nodot;
                    break;
                case "sqrt":     //平方根运算
                    if(opt!=null)
                    {
                        calc();
                    }
                    //opt=sqrt;
                    if (tmp > 0)  //要求操作数大于0
                    {
                        res = math.sqrt(tmp);
                        //res=tmp;
                    }
                    else if(res>0)
                        res= math.sqrt(res);
                        txtshow.text =res.tostring();
                    num++;   
                    tmp=0;
                    dot = nodot;
                    break;
                case "sqr":
                    // 平方运算
                    if(opt!=null)
                    {
                        calc();
                    }
                    //opt=sqr;
                    if (tmp != 0)
                    {
                        res = tmp * tmp;
                        //res=tmp;
                    }
                    else
                        res=res*res;
                    txtshow.text = res.tostring();
                    num++;   
                    tmp=0;
                    dot = nodot;
                    break;
                case "1/x":  //倒数运算
                    if(opt!=null)
                    {
                        calc();
                    }
                    if (tmp != 0)
                    {
                        res = 1 / tmp;
                        //res=tmp;
                    }
                    else                   
                    res=1/res;
                    txtshow.text = res.tostring();
                    tmp=0;
                    dot = nodot;
                    break;
                case ".":
                            // 点击了小数点
                    if(dot==hasdot)
                        return;
                    else
                    {
                        dot = hasdot;
                        dotnum = 0;
                    }                   
                    break;
                case "+/-":
                                // 点击了符号运算
                    if(tmp!=0)
                    {
                        tmp = -tmp;
                        txtshow.text = tmp.tostring();
                    }
                    else
                    {
                        res=-res;
                        //res=tmp;
                        txtshow.text = res.tostring();   
                    }
                    dot = nodot;
                    break;   
                case "ce":     //清除运算
                    res = 0;         // 记录结果数
                    tmp = 0;         // 当前输入的操作数
                    opt = null;         // 记录操作码
                    dot = nodot;    // 记录是否点击了小数点
                    num = 0;        // 记录输入操作数的个数
                    dotnum = 0;        // 记录小数点部分的个数
                    txtshow.text="";                   
                    break;
                case "c":         //清除运算
                    res = 0;         // 记录结果数
                    tmp = 0;         // 当前输入的操作数
                    opt = null;         // 记录操作码
                    dot = nodot;    // 记录是否点击了小数点
                    num = 0;        // 记录输入操作数的个数
                    dotnum = 0;        // 记录小数点部分的个数
                    txtshow.text="";
                    break;
            }
        }
        #endregion
        #region     主菜单事件
        //打开关于主题---调用windows xp中计算器的帮助
        private void menuitem5_click(object sender, system.eventargs e)
        {
            help.showhelp(this,"c:\\windows\\help\\calc.chm");       
        }
        //打开于我们
        private void menuitem6_click(object sender, system.eventargs e)
        {
            form2 fm=new form2();
            fm.show();
        }
        //复制
        private void menuitem2_click(object sender, system.eventargs e)
        {
            if(txtshow.selectionlength>0)
            {
                txtshow.copy();
            }
        }
        //
       //粘贴
        private void menuitem3_click(object sender, system.eventargs e)
        {
            txtshow.paste();
        }
          //调用windows xp中的计算器
        private void menuitem9_click(object sender, system.eventargs e)
        {
             help.showhelp(this,"c:\\windows\\system32\\calc.exe");
        }
        //退出
        private void menuitem10_click(object sender, system.eventargs e)
        {
            application.exit();
        }
        #endregion
    }
}

ps:这里再为大家推荐两款本站的在线计算器,都是采用js实现,相信对大家有一定借鉴作用:

在线标准计算器:

在线科学计算器:

上一篇:

下一篇: