zoukankan      html  css  js  c++  java
  • 简易计算器制作

    简易的制作了一下计算器,基本功能都实现了,待后续完善

    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 WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            //存储上次点击了什么按钮,0代表什么都没点击,1代表点击了数字按钮,2代表点击了运算符
            private int prev = 0;
            //存储计算的中间结果
            private decimal zj = 0;
            //记录上次按的什么运算符
            private string prevysf = "+";
            public Form1()
            {
                InitializeComponent();
            }
            //数字键按钮
            private void button8_Click(object sender, EventArgs e)
            {
                //将事件源转换为按钮
                Button btn = sender as Button;
                //替换(如果下面文本框内容为0或者上次点击了运算符)
                if (prev == 2 || txtbottom.Text == "0")
                {
                    txtbottom.Text = btn.Text;
                }
                //追加(如果下面文本框内容不为0并且上次没有点击运算符)
                else
                {
                    txtbottom.Text += btn.Text;
                }
                //点击了数字按钮
                prev = 1;
            }
            //运算符按钮
            private void button17_Click(object sender, EventArgs e)
            {
                Button btn = sender as Button;
                //上次按了数字
                if (prev == 1)
                {
                    txttop.Text += txtbottom.Text + btn.Text;
    
                    switch (prevysf)
                    {
                        case "+":
                            zj = zj + Convert.ToDecimal(txtbottom.Text);
                            break;
                        case "-":
                            zj = zj - Convert.ToDecimal(txtbottom.Text);
                            break;
                        case "*":
                            zj = zj * Convert.ToDecimal(txtbottom.Text);
                            break;
                        case "/":
                            zj = zj / Convert.ToDecimal(txtbottom.Text);
                            break;
                    }
    
                    txtbottom.Text = zj.ToString(); 
                }
                //上次按了运算符
                else
                {
                    string s = txttop.Text;
                    s = s.Substring(0, s.Length - 1);
                    s = s + btn.Text;
                    txttop.Text = s;
                }
                //点击了运算符
                prev = 2;
                //记录下运算符
                prevysf = btn.Text;
              
            }
            //清零按钮
            private void button19_Click(object sender, EventArgs e)
            {
                txttop.Text = "";
                txtbottom.Text = "0";
                prev = 0;
                zj = 0;
                prevysf = "+";
            }
    
            private void button20_Click(object sender, EventArgs e)
            {
                txtbottom.Text = "0";
            }
            //等号按钮
            private void button4_Click(object sender, EventArgs e)
            {
                Button btn = sender as Button;
                txttop.Text += txtbottom.Text + btn.Text;
    
                switch (prevysf)
                {
                    case "+":
                        zj = zj + Convert.ToDecimal(txtbottom.Text);
                        break;
                    case "-":
                        zj = zj - Convert.ToDecimal(txtbottom.Text);
                        break;
                    case "*":
                        zj = zj * Convert.ToDecimal(txtbottom.Text);
                        break;
                    case "/":
                        zj = zj / Convert.ToDecimal(txtbottom.Text);
                        break;
                }
    
                txtbottom.Text = zj.ToString();
                txttop.Text = "";
                
                zj = 0;
                
            }
            //
            private void button3_Click(object sender, EventArgs e)
            {
                txtbottom.Text += ".";
            }
        }
    }
    代码区
  • 相关阅读:
    LeetCode 1275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game
    LeetCode 307. 区域和检索
    LeetCode 1271 十六进制魔术数字 Hexspeak
    秋实大哥与花 线段树模板
    AcWing 835. Trie字符串统计
    Leetcode 216. 组合总和 III
    Mybatis 示例之 复杂(complex)属性(property)
    Mybatis 示例之 复杂(complex)属性(property)
    Mybatis 高级结果映射 ResultMap Association Collection
    Mybatis 高级结果映射 ResultMap Association Collection
  • 原文地址:https://www.cnblogs.com/jiuban2391/p/6159512.html
Copyright © 2011-2022 走看看