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.因为不能进行连续运算,所以也没有涉及到运算优先级的问题,难度明显降低。

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

  • 7
    点赞
  • 59
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
计算器是一种常见的工具,用于进行各种数学运算。而Visual Studio是一种集成开发环境(IDE),可以用于编写各种不同类型的程序,包括简单的计算器。 要使用Visual Studio写一个计算器,通常需要以下步骤: 1. 创建一个新的项目:在Visual Studio中,选择新建项目,并选择一个适合的项目模板,例如Windows窗体应用程序。 2. 添加用户界面:在创建的项目中,通过拖放或手动编写代码来添加用户界面元素,例如按钮、文本框和标签。这些界面元素将用于输入和显示计算器的结果。 3. 实现计算逻辑:使用编程语言,例如C#,来实现计算器的计算逻辑。这可以通过给按钮添加事件处理程序,然后在处理程序中编写计算逻辑来完成。 4. 处理用户输入和输出:通过编写代码来处理用户输入和输出。例如,当用户点击某个按钮时,可以将其文本添加到输入框中,并进行相应的计算操作。然后,将计算结果显示在输出文本框中。 5. 测试和调试:完成计算器的编写后,可以使用Visual Studio提供的调试功能对代码进行测试和调试。这可以帮助我们找到并解决潜在的bug和错误。 6. 生成和运行:最后,通过在Visual Studio中选择生成选项,将代码编译为可执行文件。然后,可以运行生成的应用程序,测试计算器的功能。 通过以上步骤,我们可以使用Visual Studio轻松地创建一个简单的计算器,并实现基本的计算功能。当然,如果需要更复杂的功能,可以继续在代码中添加其他算法和逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值