C# Visual studio 简易计算器

前几天老师讲的,今天才想起来完善一下。
发现Visual Studio有那么点小好用~

Visual Studio的简易用法

1.添加组件:直接将工具箱中的组件直接到框架中
2.更改名称文本等:单击组件,右下角的属性中进行更改
在这里插入图片描述3.添加动作事件:双击组件,进入代码页面,输入相应代码。

简易计算器

我是根据我手机上的小米计算器制作的页面:
在这里插入图片描述
全部代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 计算器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Text = "";
            textBox1.Text += "5";
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Text = "";
            textBox1.Text += "1";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Text = "";
            textBox1.Text += "2";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Text = "";
            textBox1.Text += "3";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Text = "";
            textBox1.Text += "4";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Text = "";
            textBox1.Text += "6";
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Text = "";
            textBox1.Text += "7";
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Text = "";
            textBox1.Text += "8";
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Text = "";
            textBox1.Text += "9";
        }

        private void button0_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Text = "";
            textBox1.Text += "0";
        }

        private void button_spot_Click(object sender, EventArgs e)
        {
            textBox1.Text += ".";
        }

        private void buttonAC_Click(object sender, EventArgs e)
        {
            textBox1.Text = "0";
            history.Text = "";
        }

        private void button_back_Click(object sender, EventArgs e)
        {
            int length = textBox1.TextLength; //取字符串的长度
            if(length > 0)
               textBox1.Text = textBox1.Text.Substring(0, length - 1);//取子串,去掉最后一个字符
        }
        double i, j, k;
        char flag; //标志变量,表示运算
        private void button_div_Click(object sender, EventArgs e)
        {
            i = Convert.ToDouble(textBox1.Text);
            textBox1.Text = "";//置空
            history.Text = i.ToString() + '/';
            flag = '/';
        }

        private void button_multiply_Click(object sender, EventArgs e)
        {
            i = Convert.ToDouble(textBox1.Text);
            textBox1.Text = "";//置空
            history.Text = i.ToString() + 'x';
            flag = 'x';
        }

        private void button_subtract_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Text = "-";
            else
            {
                i = Convert.ToDouble(textBox1.Text);
                textBox1.Text = "";//置空
                history.Text = i.ToString() + '-';
                flag = '-';
            }
        }

        private void button_plus_Click(object sender, EventArgs e)
        {
            i = Convert.ToDouble(textBox1.Text);
            textBox1.Text = "";//置空
            history.Text = i.ToString() + '+';
            flag = '+';
        }

        private void history_TextChanged(object sender, EventArgs e)
        {

        }

        private void button_equal_Click(object sender, EventArgs e)
        {
            j = Convert.ToDouble(textBox1.Text);
            history.Text += j.ToString();
            switch (flag)
            {
                case '+':
                    {
                        k = i + j;
                        break;
                    }
                case '-':
                    {
                        k = i - j;
                        break;
                    }
                case 'x':
                    {
                        k = i * j;
                        break;
                    }
                case '/':
                    {
                        if(j == 0)
                        {
                            textBox1.Text = "∞";
                            break;
                        }
                        else
                        {
                            k = i / j;
                            break;
                        }
                    }
                case '%':
                    {
                        if (i - (int)i != 0 || j - (int)j != 0 || j == 0)
                        {
                            textBox1.Text = "Error"; //整数才能进行求余运算
                            break;
                        }
                        k = i % j;
                        break;
                    }
            }
            if (textBox1.Text != "∞" && textBox1.Text != "Error")
            {
                history.Text += "=" + k.ToString();
                textBox1.Text = "0";
            }
            else
            {
                history.Text += "=" + textBox1.Text;
                textBox1.Text = "0";
            }
        }

        private void button_surplus_Click(object sender, EventArgs e)
        {
            i = Convert.ToDouble(textBox1.Text);
            textBox1.Text = "";//置空
            history.Text = i.ToString() + '%';
            flag = '%';
        }
    }
}

部分代码分析:
①数字键的处理

 private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Text = "";
            textBox1.Text += "1";
        }

②运算键
i,j 分别存储两个运算数;flag 字符型 存储运算符。
当按下 = 键的时候,根据flag,对i,j做运算(采用switch…case…语句)。

private void button_equal_Click(object sender, EventArgs e)
        {
            j = Convert.ToDouble(textBox1.Text);
            history.Text += j.ToString();
            switch (flag)
            {
                case '+':
                    {
                        k = i + j;
                        break;
                    }
                case '-':
                    {
                        k = i - j;
                        break;
                    }
                case 'x':
                    {
                        k = i * j;
                        break;
                    }
                case '/':
                    {
                        if(j == 0)
                        {
                            textBox1.Text = "∞";
                            break;
                        }
                        else
                        {
                            k = i / j;
                            break;
                        }
                    }
                case '%':
                    {
                        if (i - (int)i != 0 || j - (int)j != 0 || j == 0)
                        {
                            textBox1.Text = "Error"; //整数才能进行求余运算
                            break;
                        }
                        k = i % j;
                        break;
                    }
            }
            if (textBox1.Text != "∞" && textBox1.Text != "Error")
            {
                history.Text += "=" + k.ToString();
                textBox1.Text = "0";
            }
            else
            {
                history.Text += "=" + textBox1.Text;
                textBox1.Text = "0";
            }
        }

注:
1.当没有输入数的时候,减号可以做负号
2.做除法时,除数不能为零
3.求余运算必须是整数操作数

功能实现:
①AC 全部清除键
在这里插入图片描述
②← 倒退键
在这里插入图片描述
③加法运算
在这里插入图片描述(输入过程均如上,以下运算省略)
④减法运算
在这里插入图片描述
⑤乘法运算
在这里插入图片描述
⑥除法运算
在这里插入图片描述
⑦求余运算(小数不能求余)
在这里插入图片描述负数求余
在这里插入图片描述
完美结束~
缺陷:
1.不能进行连续运算
2.只能进行简易运算,平方,开方,指数等未涉及
3.因为不能进行连续运算,所以也没有涉及到运算优先级的问题,难度明显降低。

用时一小时,但我依旧比较满意,起码它看起来像是那么回事~~

原创文章 23 获赞 29 访问量 7282

猜你喜欢

转载自blog.csdn.net/fu_GAGA/article/details/105893963
今日推荐