zoukankan      html  css  js  c++  java
  • 练习:WinForm 计算器

    界面设计:

    代码:

    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
        {
            //存储上次点击的按钮(0:没点击,1:数字,2:运算符)
            private int prev = 0;
    
            //存储计算的中间结果
            private decimal zhong = 0;
    
            //记录上次点击的运算符
            private string prevYSF = "+";
    
            public Form1()
            {
                InitializeComponent();
            }
    
            //数字键的操作
            private void button27_Click(object sender, EventArgs e)
            {
                //将事件源转换成按钮
                Button btn = sender as Button;
    
                //替换(如果下面文本框为0或者上次点击了运算符)
                if (txtBottom.Text == "0" || prev == 2)
                {
                    txtBottom.Text = btn.Text;
                }
    
                //追加
                else
                {
                    txtBottom.Text += btn.Text;
                }
    
                //点击了数字
                prev = 1;
            }
    
            //运算符的操作
            private void button26_Click(object sender, EventArgs e)
            {
                //将事件源转换为按钮
                Button btn = sender as Button;
    
                //上次按了数字
                if (prev == 1)
                {
                    txtTop.Text += txtBottom.Text + btn.Text;
                    switch (prevYSF)
                    {
                        case "+":
                            zhong = zhong + Convert.ToDecimal(txtBottom.Text);
                            txtBottom.Text = zhong.ToString();
                            break;
                        case "-":
                            zhong = zhong - Convert.ToDecimal(txtBottom.Text);
                            txtBottom.Text = zhong.ToString();
                            break;
                        case "*":
                            zhong = zhong * Convert.ToDecimal(txtBottom.Text);
                            txtBottom.Text = zhong.ToString();
                            break;
                        case "/":
                            if (txtBottom.Text == "0")
                            {
                                txtBottom.Text = "除数不能为0";
                            }
                            else
                            {
                                zhong = zhong / Convert.ToDecimal(txtBottom.Text);
                                txtBottom.Text = zhong.ToString();
                            }
                            break;
                    } 
                }
    
    
                //上次按了运算符
                else
                {
                    if (txtTop.Text == "")
                    {
                        txtTop.Text = txtBottom.Text + btn.Text;
                    }
                    else
                    {
                        txtTop.Text = txtTop.Text.Substring(0, txtTop.Text.Length - 1) + btn.Text;
                    }
                }
    
                //点击运算符
                prev = 2;
    
                //记录运算符
                prevYSF = btn.Text;
            }
    
            //清空键的操作
            private void button8_Click(object sender, EventArgs e)
            {
                txtTop.Text = "";
                txtBottom.Text = "0";
                prev = 0;
                zhong = 0;
                prevYSF = "+";
            }
    
            //等号操作
            private void button28_Click(object sender, EventArgs e)
            {
                //将事件源转换为按钮
                Button btn = sender as Button;
    
                //上次按了数字
                if (prev == 1)
                {
                    txtTop.Text += txtBottom.Text + btn.Text;
                    switch (prevYSF)
                    {
                        case "+":
                            zhong = zhong + Convert.ToDecimal(txtBottom.Text);
                            txtBottom.Text = zhong.ToString();
                            break;
                        case "-":
                            zhong = zhong - Convert.ToDecimal(txtBottom.Text);
                            txtBottom.Text = zhong.ToString();
                            break;
                        case "*":
                            zhong = zhong * Convert.ToDecimal(txtBottom.Text);
                            txtBottom.Text = zhong.ToString();
                            break;
                        case "/":
                             if (txtBottom.Text == "0")
                            {
                                txtBottom.Text = "除数不能为0";
                            }
                            else
                            {
                                zhong = zhong / Convert.ToDecimal(txtBottom.Text);
                                txtBottom.Text = zhong.ToString();
                            }
                            break;
                    } 
                }
                //上次按了运算符
                else
                {
                    txtTop.Text = txtTop.Text.Substring(0, txtTop.Text.Length - 1) + btn.Text;
                }
    
                //点击运算符
                prev = 2;
    
                //记录运算符
                prevYSF = btn.Text;
    
                txtTop.Text = "";
            }
    
        }
    }

  • 相关阅读:
    MySQL命令行基本操作
    MYSQL多表查询笔记
    MYSQL事务笔记
    Linux测试环境部署相关命令和步骤
    Jmeter录制脚本
    办公软件通讯录排序相关测试点
    Burp Suite抓包App
    安全测试 Burp Suite抓包工具
    及时通信办公软件,验证码登录,获取验证码失败原因分析和规避方法
    redis内存溢出问题分析和后续规避方法
  • 原文地址:https://www.cnblogs.com/xiao55/p/5805423.html
Copyright © 2011-2022 走看看