数据库作业2

用C#制作一个简易计算器

目录:

1界面展示
2界面设置
3按键功能设置
4程序完整代码部分

一 界面:

首先来看一看要制作的计算器的简单界面:
在这里插入图片描述
(呃,,,是有些简陋,但不影响功能)
接下来我简单介绍部分按键的功能:
1.【Del】:撤回键
2.【AC】:清空键
3为什么两个文本框呢,是因为我还不会再一个文本框里记录展示运算过程和结果(有点小菜);第一个文本框是之前输入的数据的显示,第二个自然就是剩下的数据啦。就是如下图这个作用:
在这里插入图片描述

二 界面设置

在这里呢介绍一下上图的界面是如何”面世“的:

  1. 首先在visual studio上新建一个c#的项目:(如图所示)在右边的属性里可以改变它的样貌
    在这里插入图片描述
  2. 左边有一个工具箱,其包含多个组件,先选择【button】拖出来,多整几个再进行排列就ok了,我的文本框选择的是【TextBox】。这些拖出来后,单击它右边也会出现它的属性,按自己的喜欢修改就好了。

三 各按键的具体处理(附带少部分异常处理)

1.首先来看数字0-9:
以”1“为例,其他的数都是相似的

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

2.运算符号键:
a. ”➕“:
在直接按加号时加号前的数默认为0,我这样做是因为为了防止误触+号时报错。

 private void button10_Click(object sender, EventArgs e)//加法
        {
            if (textBox1.Text == "")
            {
                LeftNum = 0;
            }
            else
            {
                LeftNum = Convert.ToDouble(textBox1.Text);
            }      
            Flag = "+";
            textBox1.Text = "";
        }

➖、✖和➗与加号类似。
b. 【Del】:
这个按键相当于回车键,就是将Text Box中的字符串去掉末位。

textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);

c. 【AC】:
清空键,直接等于空。

 textBox1.Text = "";

d. 【.】:
这里为了防止每次点击其时都出现一个小数点,即类似”1…“的情况,对输入小数点时做了一个判断。

bool result = textBox1.Text.Contains(".");
                if (result == false)
                {
                    textBox1.Text += ".";
                }

直接点小数点时显示”0. “。
e. 【=】:
在这一部分需要计算数据的值并显示出来,在做除法时记得处理一下除数不能为0的情况。

private void button17_Click(object sender, EventArgs e)//等于,计算结果
        {
            if (Flag == "+")
            { 
                Result = LeftNum + RightNum;
                textBox1.Text = Result.ToString();
            }
            else if(Flag=="-")
            {
                Result = LeftNum - RightNum;
                textBox1.Text = Result.ToString();
            }
            else if(Flag=="*")
            {
                Result = LeftNum * RightNum;
                textBox1.Text = Result.ToString();
            }
            else if(Flag=="/")
            {
                if(RightNum==0)//除数不能为0
                {
                    textBox1.Text = "除数不能为0";
                }
                else
                {
                    Result = LeftNum / RightNum;
                    textBox1.Text = Result.ToString();
                }   
            }
            else//处理没有输入加减乘除时的情况
            {
                textBox1.Text = RightNum.ToString();
            }
        }

四 完整代码:

这里面对一些常现的错误操作做了简单的处理,可能有点乱,但耐心还能看下去。

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 Calculater
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Double LeftNum, RightNum, Result;//左操作数,右操作数,运算结果
        String Flag="@";//操作符
        private void button1_Click(object sender, EventArgs e)//按键1
        {
            textBox1.Text += "1";
        }

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

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

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

        private void button5_Click(object sender, EventArgs e)//按键5
        {
            textBox1.Text += "5";
        }

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

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

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

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

        private void button10_Click(object sender, EventArgs e)//加法
        {
            if (textBox1.Text == "")//在按加法前未输入操作数
            {
                LeftNum = 0;
            }
            else
            {
                LeftNum = Convert.ToDouble(textBox1.Text);
            }      
            Flag = "+";       
            if (textBox2.Text!= "")
                textBox2.Text= "";
            textBox2.Text += LeftNum.ToString();//记录之前输入的操作数
            textBox2.Text += "+";
            textBox1.Text = "";
        }

        private void button11_Click(object sender, EventArgs e)//减法
        {
            if(textBox1.Text=="")
            {
                LeftNum = 0;
            }
            else
            {
                LeftNum = Convert.ToDouble(textBox1.Text);
            }
            Flag = "-";
            if (textBox2.Text != "")
                textBox2.Text = "";
            textBox2.Text += LeftNum.ToString();
            textBox2.Text += "-";
            textBox1.Text = "";
        }

        private void button12_Click(object sender, EventArgs e)//乘法
        {
            if (textBox1.Text == "")
            {
                LeftNum = 0;
            }
            else
            {
                LeftNum = Convert.ToDouble(textBox1.Text);
            }
            Flag = "*";
            if (textBox2.Text != "")
                textBox2.Text = "";
            textBox2.Text += LeftNum.ToString();
            textBox2.Text += "*";
            textBox1.Text = "";
        }

        private void button13_Click(object sender, EventArgs e)//除法
        {
            if (textBox1.Text == "")
            {
                LeftNum = 0;
            }
            else
            {
                LeftNum = Convert.ToDouble(textBox1.Text);
            }
            Flag = "/";
            if (textBox2.Text != "")
                textBox2.Text = "";
            textBox2.Text += LeftNum.ToString();
            textBox2.Text += "/";
            textBox1.Text = "";
        }

        private void button17_Click(object sender, EventArgs e)//等于,计算结果
        {
            if (textBox1.Text == "")//未输入右操作数
            {
                RightNum = 0;
            }
            else
            {
                RightNum = Convert.ToDouble(textBox1.Text);
            }
            textBox2.Text += RightNum.ToString();
            //运算处理
            if (Flag == "+")
            { 
                Result = LeftNum + RightNum;
                textBox1.Text = Result.ToString();
            }
            else if(Flag=="-")
            {
                Result = LeftNum - RightNum;
                textBox1.Text = Result.ToString();
            }
            else if(Flag=="*")
            {
                Result = LeftNum * RightNum;
                textBox1.Text = Result.ToString();
            }
            else if(Flag=="/")
            {
                if(RightNum==0)//除数不能为0
                {
                    textBox1.Text = "除数不能为0";
                }
                else
                {
                    Result = LeftNum / RightNum;
                    textBox1.Text = Result.ToString();
                }   
            }
            else//处理没有输入加减乘除时的情况
            {
                textBox1.Text = RightNum.ToString();
            }
        }

        private void button16_Click(object sender, EventArgs e)//清除键
        {
            textBox1.Text = "";
            textBox2.Text = "";
        }

        private void button18_Click(object sender, EventArgs e)//撤回键
        {
            if (textBox1.Text == "")
            {
                if(textBox2.Text!="")
                {
                    textBox2.Text = textBox2.Text.Substring(0, textBox2.Text.Length - 1);
                }
                textBox1.Text = "";
            }
            else
            {
                textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
            }
        }

        private void button14_Click(object sender, EventArgs e)//0键
        {
            textBox1.Text += "0";
        }

        private void button15_Click(object sender, EventArgs e)//小数点
        {
            if (textBox1.Text == "")//直接点小数点时
            {
                textBox1.Text += "0.";
            }
            else
            {
                bool result = textBox1.Text.Contains(".");
                if (result == false)
                {
                    textBox1.Text += ".";
                }
            }           
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值