您的位置:首页 > 编程语言 > C#

C#基础之计算器的简单实现

2016-09-06 09:49 288 查看
自学C#已经两周了,之前写了一个简单的秒表,感觉不过瘾,觉得是不是可以试着做个功能更复杂的小程序来练练手。

于是我想到了做计算器。但是尝试之后发现做好一个计算器不简单,你需要考虑运算符的优先级,需要考虑中间结果的保存,需要考虑除零的情况,需要考虑浮点情况等等。

当然,我没有时间去做一个功能很复杂的科学计算器,也没有精力去做一个运算速度极佳的并行计算器。我的目的只是通过简单的项目提升对C#的掌握程度。

下面开始正题。。

首先设计一个简单的计算器界面。

需要17个button,一个label,一个textbox(显示结果),界面设计结果如下:



想显得高端一点,我给按钮都加了背景图片,但是貌似我图没抠好,导致效果很差,将就着看吧,哈哈。

需要说明的是,我只做了二目运算,即输入进去的操作符只支持一个运算符,然后必须点击“=”,进行运算。不支持2+3x5这种运算符的运算。

计算器的功能想必大家都懂,我不在此处写出。不废话了,直接贴代码:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace Calculator

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            Button btn = (Button)sender;

            textBox1.Text += btn.Text;

        }

        private void button2_Click(object sender, EventArgs e)

        {

            Button btn = (Button)sender;

            textBox1.Text += btn.Text;

        }

        private void button3_Click(object sender, EventArgs e)

        {

            Button btn = (Button)sender;

            textBox1.Text += btn.Text;

        }

        private void button4_Click(object sender, EventArgs e)

        {

            Button btn = (Button)sender;

            textBox1.Text += btn.Text;

        }

        private void button5_Click(object sender, EventArgs e)

        {

            Button btn = (Button)sender;

            textBox1.Text += btn.Text;

        }

        private void button6_Click(object sender, EventArgs e)

        {

            Button btn = (Button)sender;

            textBox1.Text += btn.Text;

        }

        private void button7_Click(object sender, EventArgs e)

        {

            Button btn = (Button)sender;

            textBox1.Text += btn.Text;

        }

        private void button8_Click(object sender, EventArgs e)

        {

            Button btn = (Button)sender;

            textBox1.Text += btn.Text;

        }

        private void button9_Click(object sender, EventArgs e)

        {

            Button btn = (Button)sender;

            textBox1.Text += btn.Text;

        }

        private void button10_Click(object sender, EventArgs e)

        {

            Button btn = (Button)sender;

            textBox1.Text += btn.Text;

        }

        private void button12_Click(object sender, EventArgs e)

        {

            Button btn = (Button)sender;

            textBox1.Text = textBox1.Text + " " + btn.Text + " ";//空格用于分隔数字各运算符

        }

        private void button13_Click(object sender, EventArgs e)

        {

            Button btn = (Button)sender;

            textBox1.Text = textBox1.Text + " " + btn.Text + " ";//空格用于分隔数字各运算符

        }

        private void button14_Click(object sender, EventArgs e)

        {

            Button btn = (Button)sender;

            textBox1.Text = textBox1.Text + " " + btn.Text + " ";//空格用于分隔数字各运算符

        }

        private void button15_Click(object sender, EventArgs e)

        {

            Button btn = (Button)sender;

            textBox1.Text = textBox1.Text + " " + btn.Text + " ";//空格用于分隔数字各运算符

        }

        private void button16_Click(object sender, EventArgs e)

        {

            textBox1.Text = "";

        }

        private void button17_Click(object sender, EventArgs e)

        {

            Button btn = (Button)sender;

            textBox1.Text += btn.Text;

        }

        private void button11_Click(object sender, EventArgs e)

        {

            Single r;                   //用于保存计算结果

            string t = textBox1.Text;  //t用于保存文本框中的算术表达式

            int space = t.IndexOf(' ');//用于搜索空格位置

            string s1 = t.Substring(0, space);//s1用于保存第一个运算数

            char op = Convert.ToChar(t.Substring(space + 1, 1));//op用于保存运算符

            string s2 = t.Substring(space + 3);//s2用于保存第二个运算数

            Single arg1 = Convert.ToSingle(s1);//将运算数从string转换为Single

            Single arg2 = Convert.ToSingle(s2);

            switch (op)

            {

                case '+':

                    r = arg1 + arg2;

                    break;

                case '-':

                    r = arg1 - arg2;

                    break;

                case '*':

                    r = arg1 * arg2;

                    break;

                case '/':

                if (arg2 == 0)

                {

                    MessageBox.Show("除数不能为0!");

                    textBox1.Text = "Error!";

                    return;

                }

                else

                {

                    r = arg1 / arg2;

                    break;

                 }

             default:

                 throw new ApplicationException();

            }

                //将计算结果显示在文本框中

              textBox1.Text = r.ToString();

         }

        private void Form1_Load(object sender, EventArgs e)

        {

        }

    }

}

本来想贴几个功能测试的图的,但是好麻烦,就算了吧。。2目运算都能算,支持浮点。。。

--------------------------------------------------------------------------------------------------------------------------------

最后按照惯例,把界面自动生成的代码贴出来,给一些不知道怎么做界面的同学一些参考。

  #region Windows 窗体设计器生成的代码

        /// <summary>

        /// 设计器支持所需的方法 - 不要

        /// 使用代码编辑器修改此方法的内容。

        /// </summary>

        private void InitializeComponent()

        {

            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));

            this.button1 = new System.Windows.Forms.Button();

            this.button2 = new System.Windows.Forms.Button();

            this.button3 = new System.Windows.Forms.Button();

            this.button4 = new System.Windows.Forms.Button();

            this.button5 = new System.Windows.Forms.Button();

            this.button6 = new System.Windows.Forms.Button();

            this.button7 = new System.Windows.Forms.Button();

            this.button8 = new System.Windows.Forms.Button();

            this.button9 = new System.Windows.Forms.Button();

            this.button10 = new System.Windows.Fo
e852
rms.Button();

            this.button11 = new System.Windows.Forms.Button();

            this.button12 = new System.Windows.Forms.Button();

            this.button13 = new System.Windows.Forms.Button();

            this.button14 = new System.Windows.Forms.Button();

            this.button15 = new System.Windows.Forms.Button();

            this.label1 = new System.Windows.Forms.Label();

            this.button16 = new System.Windows.Forms.Button();

            this.textBox1 = new System.Windows.Forms.TextBox();

            this.button17 = new System.Windows.Forms.Button();

            this.SuspendLayout();

            // 

            // button1

            // 

            this.button1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button1.BackgroundImage")));

            this.button1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;

            this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

            this.button1.Location = new System.Drawing.Point(36, 71);

            this.button1.Name = "button1";

            this.button1.Size = new System.Drawing.Size(40, 40);

            this.button1.TabIndex = 0;

            this.button1.Text = "1";

            this.button1.UseVisualStyleBackColor = true;

            this.button1.Click += new System.EventHandler(this.button1_Click);

            // 

            // button2

            // 

            this.button2.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button2.BackgroundImage")));

            this.button2.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;

            this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

            this.button2.Location = new System.Drawing.Point(83, 71);

            this.button2.Name = "button2";

            this.button2.Size = new System.Drawing.Size(41, 40);

            this.button2.TabIndex = 1;

            this.button2.Text = "2";

            this.button2.UseVisualStyleBackColor = true;

            this.button2.Click += new System.EventHandler(this.button2_Click);

            // 

            // button3

            // 

            this.button3.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button3.BackgroundImage")));

            this.button3.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;

            this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

            this.button3.Location = new System.Drawing.Point(129, 71);

            this.button3.Name = "button3";

            this.button3.Size = new System.Drawing.Size(41, 40);

            this.button3.TabIndex = 2;

            this.button3.Text = "3";

            this.button3.UseVisualStyleBackColor = true;

            this.button3.Click += new System.EventHandler(this.button3_Click);

            // 

            // button4

            // 

            this.button4.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button4.BackgroundImage")));

            this.button4.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;

            this.button4.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

            this.button4.Location = new System.Drawing.Point(36, 117);

            this.button4.Name = "button4";

            this.button4.Size = new System.Drawing.Size(41, 42);

            this.button4.TabIndex = 3;

            this.button4.Text = "4";

            this.button4.UseVisualStyleBackColor = true;

            this.button4.Click += new System.EventHandler(this.button4_Click);

            // 

            // button5

            // 

            this.button5.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button5.BackgroundImage")));

            this.button5.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;

            this.button5.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

            this.button5.Location = new System.Drawing.Point(83, 117);

            this.button5.Name = "button5";

            this.button5.Size = new System.Drawing.Size(41, 42);

            this.button5.TabIndex = 4;

            this.button5.Text = "5";

            this.button5.UseVisualStyleBackColor = true;

            this.button5.Click += new System.EventHandler(this.button5_Click);

            // 

            // button6

            // 

            this.button6.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button6.BackgroundImage")));

            this.button6.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;

            this.button6.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

            this.button6.Location = new System.Drawing.Point(130, 117);

            this.button6.Name = "button6";

            this.button6.Size = new System.Drawing.Size(40, 42);

            this.button6.TabIndex = 5;

            this.button6.Text = "6";

            this.button6.UseVisualStyleBackColor = true;

            this.button6.Click += new System.EventHandler(this.button6_Click);

            // 

            // button7

            // 

            this.button7.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button7.BackgroundImage")));

            this.button7.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;

            this.button7.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

            this.button7.Location = new System.Drawing.Point(36, 165);

            this.button7.Name = "button7";

            this.button7.Size = new System.Drawing.Size(41, 37);

            this.button7.TabIndex = 6;

            this.button7.Text = "7";

            this.button7.UseVisualStyleBackColor = true;

            this.button7.Click += new System.EventHandler(this.button7_Click);

            // 

            // button8

            // 

            this.button8.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button8.BackgroundImage")));

            this.button8.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;

            this.button8.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

            this.button8.Location = new System.Drawing.Point(83, 165);

            this.button8.Name = "button8";

            this.button8.Size = new System.Drawing.Size(41, 37);

            this.button8.TabIndex = 7;

            this.button8.Text = "8";

            this.button8.UseVisualStyleBackColor = true;

            this.button8.Click += new System.EventHandler(this.button8_Click);

            // 

            // button9

            // 

            this.button9.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button9.BackgroundImage")));

            this.button9.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;

            this.button9.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

            this.button9.Location = new System.Drawing.Point(129, 165);

            this.button9.Name = "button9";

            this.button9.Size = new System.Drawing.Size(41, 37);

            this.button9.TabIndex = 8;

            this.button9.Text = "9";

            this.button9.UseVisualStyleBackColor = true;

            this.button9.Click += new System.EventHandler(this.button9_Click);

            // 

            // button10

            // 

            this.button10.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button10.BackgroundImage")));

            this.button10.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;

            this.button10.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

            this.button10.Location = new System.Drawing.Point(83, 208);

            this.button10.Name = "button10";

            this.button10.Size = new System.Drawing.Size(41, 40);

            this.button10.TabIndex = 9;

            this.button10.Text = "0";

            this.button10.UseVisualStyleBackColor = true;

            this.button10.Click += new System.EventHandler(this.button10_Click);

            // 

            // button11

            // 

            this.button11.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button11.BackgroundImage")));

            this.button11.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;

            this.button11.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

            this.button11.Location = new System.Drawing.Point(129, 208);

            this.button11.Name = "button11";

            this.button11.Size = new System.Drawing.Size(40, 40);

            this.button11.TabIndex = 10;

            this.button11.Text = "+";

            this.button11.UseVisualStyleBackColor = true;

            this.button11.Click += new System.EventHandler(this.button11_Click);

            // 

            // button12

            // 

            this.button12.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button12.BackgroundImage")));

            this.button12.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;

            this.button12.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

            this.button12.Location = new System.Drawing.Point(174, 71);

            this.button12.Name = "button12";

            this.button12.Size = new System.Drawing.Size(42, 40);

            this.button12.TabIndex = 11;

            this.button12.Text = "+";

            this.button12.UseVisualStyleBackColor = true;

            this.button12.Click += new System.EventHandler(this.button12_Click);

            // 

            // button13

            // 

            this.button13.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button13.BackgroundImage")));

            this.button13.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;

            this.button13.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

            this.button13.Location = new System.Drawing.Point(176, 117);

            this.button13.Name = "button13";

            this.button13.Size = new System.Drawing.Size(40, 42);

            this.button13.TabIndex = 12;

            this.button13.Text = "-";

            this.button13.UseVisualStyleBackColor = true;

            this.button13.Click += new System.EventHandler(this.button13_Click);

            // 

            // button14

            // 

            this.button14.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button14.BackgroundImage")));

            this.button14.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;

            this.button14.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

            this.button14.Location = new System.Drawing.Point(176, 165);

            this.button14.Name = "button14";

            this.button14.Size = new System.Drawing.Size(42, 37);

            this.button14.TabIndex = 13;

            this.button14.Text = "*";

            this.button14.UseVisualStyleBackColor = true;

            this.button14.Click += new System.EventHandler(this.button14_Click);

            // 

            // button15

            // 

            this.button15.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button15.BackgroundImage")));

            this.button15.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;

            this.button15.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

            this.button15.Location = new System.Drawing.Point(175, 208);

            this.button15.Name = "button15";

            this.button15.Size = new System.Drawing.Size(41, 40);

            this.button15.TabIndex = 14;

            this.button15.Text = "/";

            this.button15.UseVisualStyleBackColor = true;

            this.button15.Click += new System.EventHandler(this.button15_Click);

            // 

            // label1

            // 

            this.label1.AutoSize = true;

            this.label1.BackColor = System.Drawing.SystemColors.ActiveCaption;

            this.label1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

            this.label1.Location = new System.Drawing.Point(4, 21);

            this.label1.Name = "label1";

            this.label1.Size = new System.Drawing.Size(40, 16);

            this.label1.TabIndex = 15;

            this.label1.Text = "结果";

            // 

            // button16

            // 

            this.button16.BackColor = System.Drawing.SystemColors.ActiveCaption;

            this.button16.Location = new System.Drawing.Point(177, 14);

            this.button16.Name = "button16";

            this.button16.Size = new System.Drawing.Size(61, 34);

            this.button16.TabIndex = 16;

            this.button16.Text = "清空";

            this.button16.UseVisualStyleBackColor = false;

            this.button16.Click += new System.EventHandler(this.button16_Click);

            // 

            // textBox1

            // 

            this.textBox1.Location = new System.Drawing.Point(50, 21);

            this.textBox1.Name = "textBox1";

            this.textBox1.Size = new System.Drawing.Size(121, 21);

            this.textBox1.TabIndex = 17;

            // 

            // button17

            // 

            this.button17.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button17.BackgroundImage")));

            this.button17.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;

            this.button17.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

            this.button17.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

            this.button17.Location = new System.Drawing.Point(36, 208);

            this.button17.Name = "button17";

            this.button17.Size = new System.Drawing.Size(41, 40);

            this.button17.TabIndex = 18;

            this.button17.Text = ".";

            this.button17.TextAlign = System.Drawing.ContentAlignment.TopCenter;

            this.button17.UseVisualStyleBackColor = true;

            this.button17.Click += new System.EventHandler(this.button17_Click);

            // 

            // Form1

            // 

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);

            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

            this.ClientSize = new System.Drawing.Size(243, 267);

            this.Controls.Add(this.button17);

            this.Controls.Add(this.textBox1);

            this.Controls.Add(this.button16);

            this.Controls.Add(this.label1);

            this.Controls.Add(this.button15);

            this.Controls.Add(this.button14);

            this.Controls.Add(this.button13);

            this.Controls.Add(this.button12);

            this.Controls.Add(this.button11);

            this.Controls.Add(this.button10);

            this.Controls.Add(this.button9);

            this.Controls.Add(this.button8);

            this.Controls.Add(this.button7);

            this.Controls.Add(this.button6);

            this.Controls.Add(this.button5);

            this.Controls.Add(this.button4);

            this.Controls.Add(this.button3);

            this.Controls.Add(this.button2);

            this.Controls.Add(this.button1);

            this.Name = "Form1";

            this.Text = "简易计算器";

            this.Load += new System.EventHandler(this.Form1_Load);

            this.ResumeLayout(false);

            this.PerformLayout();

        }

        #endregion
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息