回 帖 发 新 帖 刷新版面

主题:[原创]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
{
    /// <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
            //

回复列表 (共20个回复)

11 楼

有些地方看不懂哦
这个  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;   //求平方根
还有
System.Windows.Forms.Button btnTmp;
            double i;

            btnTmp = sender as System.Windows.Forms.Button;  
能解释下么

12 楼

鼓励原创~

13 楼

有些不懂耶,
首先:对于0,1,2...9这些数,你是怎样接受输入的,
另外:这个不懂诶

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();
                }
            }
        }


尤其是:      System.Windows.Forms.Button btnTmp;

            btnTmp = sender as System.Windows.Forms.Button;  
能解释下么

14 楼

[color=#880088][size=7]太棒了!![/size][/color]

15 楼

好东西大家分享,楼主辛苦了!!!

16 楼

仁兄的编的计算器思路不是很清晰哦~况且大动干戈没必要吧。
真想编计算器练手的话,我想你应该编个 表达式计算器~

17 楼

不错不错.

18 楼

不是吧,在我在上面怎么出错了?说Drawing和Windows在System不存在,是不是缺少程序集引用?

19 楼

不错不错.加油呀,.

20 楼

收藏下了,慢慢读,表示感谢。

我来回复

您尚未登录,请登录后再回复。点此登录或注册