(原创)C#编写的windows计算器----源代码

C#编写的windows计算器-源代码 C#编写的windows计算器-源代码 [ 繁體中文 ] | 文章类别:C#应用 | 文章等级: | 发表日期:2005-6-18  星期六网站计数器 | 魔法表情申请 | 精彩博客 var arrBaiduCproConfig=new Array();arrBaiduCproConfig[uid] = 2214;arrBaiduCproConfig[n] = sayyes 阅读详情
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

        
    }
}
C#窗体程序—计算器(能实现四则混合运算) VC#_Windows窗体应用程序的使用(新建项目等操作),在上一篇博客中有详细介绍,在此就不赘述。 四则运算先乘除后加减, 如下 1 + 14 / 7 x (7 - 3) 我们可以很简单的计算 7-3=4 , 14/7=2 , 2*4=8 , 1+8=9 得到最终答案9. 该运算在程序中 是以字符形式存储在 数组中的:char_temp[ ]={1,+,1,4,/,7,x,(,7,-,3,)} ... 阅读详情

相关推荐

报告(二)用Visual Studio制作简易计算器

(一)用Visual Studio创建一个新项目 首先打开VS点击右下角创建新项目—>选择C#语言、Windows平台—>选择下方的Windows窗体应用—>配置路径—>目标框架使用默认的—>创建完成 (二)界面规划 在左边工具箱中将Button和TextBox拖入的中间的空白界面,Button即为计算器的按键TextBox为计算器的输入输出框 将Button复制或者从工具箱再拖过来,数量为你想要制作的计算器的按键数量,然后将界面布置一下 点击选中一个键后可以再右边出现的属

MHCddt的博客 8196

c# 字体格式设定

Font txtfont; //粗体 txtfont = new System.Drawing.Font("宋体", float.Parse("12"), (FontStyle)Enum.Parse(typeof(FontStyle), "Bold"), System.Drawing.GraphicsUnit.Point, ((byte)(134))); //粗体+斜体 txtfont = new System.Drawing.Font("宋体", float.Parse("12"), (Font

weixin_46051941的博客 1628

C#课程设计[简单计算器的设计与实现]【附带项目报告】

课程名称: C#课程设计 设计课题: 简单计算器的设计与实现 二O二三年 十二 月十一日 C#课程设计设计要求 (1)分组后,每组抽取一个题目,每人完成该题目 的一个功能模块的设计; (2)winform应用程序实现(代码须锯齿型书写格式); (3)必须上机调试通过; (4)每人单独完成一份完整的课程设计报

L152690的博客 2626

vb.net 控件使用外部字体自动选择

vb.net将vb一族带到了一个新的shidai

weidunet的专栏 1146

C# Windows计算器项目源代码解析

C#(读作“看”)是一种优雅且类型安全的面向对象的编程语言,由微软公司设计。它既可用于开发传统的桌面应用程序,也可用于开发更为现代的网络服务、游戏以及移动应用。C#广泛应用于.NET框架中,其设计哲学深受Java的影响,但同时也引入了包括委托、事件和泛型等在内的多种创新特性。WinForm是Microsoft推出的一种用于创建Windows桌面应用程序的框架,它伴随着.NET框架的发布而诞生,提供了丰富的控件集以及快速开发的界面设计能力。

weixin_42593549的博客 849

C#多功能计算器源代码深入分析

本文还有配套的精品资源,点击获取 简介:这份资源提供了由C#编写的多功能计算器源代码,适合初学者和有经验的开发者作为参考和学习材料。文章深入探讨了C#语言的基本特性,包括面向对象编程、泛型和异常处理,以及如何在实际项目中应用这些概念。源代码展示了C#基础语法和数学运算的使用,包括控制流语句、循环结构、数据结构的运用,以及函数封装。还讨论了计算器用户界面的设计,使用了Wind...

weixin_42592399的博客 1213

C# RichTextBox显示不同格式的字体

//定义四个文本部分,便于分别应用不同的文本格式             string firstString = "大山的骄傲\r\n ";             string secondString = "——eheartcool其人\r\n\r ";             string thirdString = "\t七十年代末的一个夜晚,没有星星,也没有月亮," + "中国湖北恩施...

qq_1206588007的博客 1188

C#语言实现多功能计算器的全面指南

C#(发音为 “C sharp”)是一种由微软开发的面向对象的编程语言。作为.NET框架的核心编程语言之一,C#在2000年首次被引入,并随着每个新版本的.NET框架不断进化。它起源于C和C++语言,因此继承了C++的强大和灵活性,同时为开发者提供了简化的语法和自动内存管理(垃圾收集)特性。C#语言强调安全、现代、组件化和类型安全的设计。

weixin_29363791的博客 1087

C#中如何取得系统字体

HTML>    HEAD>        title>systemfonttitle>        meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">        meta name="CODE_LANGUAGE" Content="C#">        meta name="vs_defaultClientS

整理的参考资料 1060

windows计算器源代码

学习C#过程中仿制的windows计算器,功能稍差,代码不够简练,但经本人调试未发现BUG ,希望大家提出宝贵意见!

李沐之数值稳定性+模型初始化和激活函数

层记为t,这里的y不是预测,还包括了损失函数,y是优化函数,不是预测的结果y。第t层是输出。所有的h都是一些向量,向量关于向量的导数是一个矩阵,所以用了很多次的矩阵乘法,出问题就在这里。

m0_51133942的博客 1532

【语言-c#】常用类型转化

1、 转化说明 对象 与System.String互转方法名称 字符串格式 优缺点 System.Drawing.Rectangle xzString xzRectangle X, Y, Width, Height System.Drawing.Point xzString xzPoint X, Y System.Drawing.Color xzString x...

少莫千华 657

ADS2023实战:2.4GHz超外差接收机从设计到链路预算全流程解析

本文以ADS 2023软件为平台,详细解析了2.4GHz超外差接收机从核心指标定义、架构原理到完整链路搭建与仿真的全流程。通过实战演练,重点阐述了如何利用ADS进行谐波平衡仿真和链路预算分析,以量化评估系统增益、噪声系数等关键性能,为射频工程师提供了一套从理论到仿真的系统级设计方法。

herb5的博客 830

C#编写的windows计算器----源代码

using System;using System.Drawing;using System.Windows;using System.Windows.Forms;using System.Collections;using System.ComponentModel;using System.Data;namespace comput{    ///     /// 这是一个计算器的简单实现。 

记得忘记@博客 2066

从安装Eclipse 开始 开发第一个 CDS View

一篇超级简单快速的 创建CDS VIEW 入门步骤介绍文章。

IT小职员 4270

Unity游戏开发实战:用柏林噪声打造3D随机地形(附完整C#代码)

本文详细介绍了如何在Unity游戏开发中使用柏林噪声算法生成3D随机地形,包含完整的C#代码实现。从基础柏林噪声原理到高级参数控制,再到性能优化和常见问题解决,帮助开发者掌握程序化地形生成技术,打造丰富多变的游戏世界。

weixin_29281941的博客 106

C#编写的计算器程序(仿windows自带的计算器)

C#初学者,水平肯定是比较差的,欢迎大家批评指正!(开发环境visual studio2010,源代码下载地址:http://pan.baidu.com/s/1skOZGhZ) 废话不多数,上图上代码! (一)界面设计 “科学型”模式界面如下: 只实现了“标准型”和“科学型”两种模式。 切换到“标准型”时效果如下: 设计时,对于如何实现模式切换时界面的变化花了点功夫,最

yuren_xia的博客 1万+

黑马JavaWeb开发笔记01——前端介绍,HTML实现标题:排版

本篇文章是2023年最新黑马JavaWeb企业级开发(知识清单)01:前端介绍、html实现标题格式内容的总结,帮助需要学习Web开发的朋友温故而知新。

apple_68589597的博客 1759

用C#语言编写计算器

 模仿windows操作系统附件中的计算器样式进行设计,要设计基本的菜单项(快捷键、热键、和分割线等)              功能要求:实现如下图所示的功能键   设置热键方法如下:   先用窗体项目做出如图窗体 右击下拉菜单项   点击编辑项设置热键   点击DropdownItems集合 在这里设置热键,以此类推;      

dream_18的博客 1844

AI人工智能代理工作流 AI Agent WorkFlow:在股票市场预测中的应用

AI人工智能代理工作流 AI Agent WorkFlow:在股票市场预测中的应用 作者:禅与计算机程序设计艺术 / Zen and the Art of Computer Programming 关键词: AI Agent, W

AI天才研究院 864

Codex 自定义模型操作指南:CLI 与桌面客户端双端实操

检查版本 codex --version # 预期输出类似:codex-cli 0.133.0-alpha.1 # 如未安装 npm install -g @openai/codex假设你要接入DeepSeek,在# 临时设置(当前终端会话有效)export DEEPSEEK_API_KEY="sk-你的 DeepSeek API Key"# 永久设置(推荐,写入 shell 配置)

篮球界最会写bug的人;程序猿中打球最菜的人。 478

小程序制作:C# - Windows窗体应用制作加法计算器

说明 本章所讲述的所有黑客技术,均不得用于非法目的。 如果你觉得文章含有侵权部分,可以联系CSDN私聊,我会适当修改。 未经允许,不得转载,如需转载,请CSDN私聊。 大佬勿喷 这些小程序都是简单的小程序,所以大佬勿喷啊! 如果你喜欢这个教程的话,请点个赞吧,谢谢! 大概思路 我们要用C#做一个加法计算器,然后我们可以使用Visual Studio 2019的WIndows窗体应用来完成GUI的制作和设计。计算器的核心算法非常简单。 准备 安装Visual Studio 2019 最新版的VS IDE是非

to_run_with_wind的博客 7884

汽车UDS诊断之诊断会话控制服务(0x10)深度剖析

DiagnosticSessionControl就是诊断会话控制。 请求消息定义 A_Data类型 参数名称 Cvt 字节值 助记符 #1 诊断会话控制请求SID M 0x10 DSC #2 子功能=[诊断会话类型] M 0x00-0xFF LEV_DS_ ...

心骗小白话的博客 7427

GitHub日收7000星,Windows计算器项目开源即爆红!

说起此番微软开源 Windows 计算器,有道是“春风得意马蹄疾,一日‘摘星’ 7000+”…… 整理 | 仲培艺 来源 | CSDN(ID:CSDNnews) 别惊讶!人工智能时代即将到来! https://edu.csdn.net/topic/ai30?utm_source=cxrs_bw 微软又来给自己拥抱开源的决心送”证明素材“了! 昨日,微软官宣在 M...

AI科技大本营 2611

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

上一篇: (原创)C#获取本地计算机名,IP,MAC地址
下一篇: 利用C#重启远程计算机
CSPRO
博客等级 码龄22年 1粉丝 2原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值