• 3月8日 第一个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
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private bool isok = true;      //用来判断是否刚刚点过运算符
            private string biaodashi;     //表达式
            private double sum = 0;      //记录运算的结果
            private string Preyunsuanfu;//记录上一个运算符
    
            private string btnow = "";//记录刚输入的数字
    
            #region
            //数字输入
            private void no0_Click(object sender, EventArgs e)
            {
                Button btn = (Button)sender;
                btnow = btn.Text;
    
                if (shuru.Text != "0")
                {
                    if (isok)
                    {
                        shuru.Text = btn.Text.ToString();
                        isok = false;
                    }
                    else
                    {
                        shuru.Text += btn.Text.ToString();
                    }
                }
                else
                {
                    if (isok)
                    {
                        shuru.Text = "";
                        shuru.Text = btn.Text;
                        isok = false;
                    }
                    else
                    {
                        shuru.Text = "";
                        shuru.Text += btn.Text;
                    }
                }
            }
            #endregion
            #region
            //运算符
            private void btjia_Click(object sender, EventArgs e)
            {
                Button btn = (Button)sender;
                string yunsuanfu = btn.Text;
    
                if (Preyunsuanfu == null)
                {
                    biaodashi += shuru.Text;
                    sum = double.Parse(shuru.Text);
                }
                else
                {
                    if (!isok)
                    {
                        if (Preyunsuanfu == "+")
                        {
                            sum += double.Parse(shuru.Text);
                        }
                        if (Preyunsuanfu == "-")
                        {
                            sum -= double.Parse(shuru.Text);
                        }
                        if (Preyunsuanfu == "*")
                        {
                            sum *= double.Parse(shuru.Text);
                        }
                        if (Preyunsuanfu == "/")
                        {
                            sum /= double.Parse(shuru.Text);
                        }
                        if (Preyunsuanfu == "%")
                        {
                            sum %= double.Parse(shuru.Text);
                        }
                        biaodashi = biaodashi + Preyunsuanfu + shuru.Text;
                        shuru.Text = sum.ToString();
                    }
                }
                shuchu.Text = biaodashi + yunsuanfu;
                Preyunsuanfu = yunsuanfu;
                isok = true;
            }
            #endregion
            #region
            //退格
            private void tuige_Click(object sender, EventArgs e)
            {      
               int geshu= shuru.Text.ToString().Length;
                string str = shuru.Text.ToString();
                if (geshu > 1)
                {
                    str = str.Remove(geshu - 1, 1);
                    shuru.Text = str;
                }
                else
                {
                    shuru.Text = "0";
                }
            }
            #endregion
            #region
            //清空
            private void clear_Click(object sender, EventArgs e)
            {
                shuru.Text = "0";
                shuchu.Text = "";
                sum = 0;
                biaodashi = "";
                Preyunsuanfu = "";
            }
            #endregion
            #region
            //等于
            private void equal_Click(object sender, EventArgs e)
            {
                shuchu.Text = "";
                if (Preyunsuanfu == "+")
                {
                    shuru.Text = (sum + double.Parse(btnow)).ToString();
                }
                if (Preyunsuanfu == "-")
                {
                    shuru.Text = (sum - double.Parse(btnow)).ToString();
                }
                if (Preyunsuanfu == "*")
                {
                    shuru.Text = (sum * double.Parse(btnow)).ToString();
                }
                if (Preyunsuanfu == "/")
                {
                    shuru.Text = (sum / double.Parse(btnow)).ToString();
                }
                if (Preyunsuanfu == "%")
                {
                    shuru.Text = (sum % double.Parse(btnow)).ToString();
                }
            }
            #endregion
            #region
            //小数点
            //private string dian = "";
            private void point_Click(object sender, EventArgs e)
            {
                Button btn = (Button)sender;
                string predian = btn.Text;
    
                shuru.Text += btn.Text;
    
            }
            #endregion
        }
    }
    

      

  • 相关阅读:
    【零基础】量子纠缠图像问世,简单解读实验原理
    【零基础】一文读懂CPU(从二极管到超大规模集成电路)
    【零基础】搞定LAMP(linux、apache、mysql、php)环境安装图文教程(基于centos7)
    【零基础】Selenium:Webdriver图文入门教程java篇(附相关包下载)
    【零基础】快速入门爬虫框架HtmlUnit
    【零基础】speech driven animation中文安装使用指南
    【零基础】斯坦福四足机器人DIY指引
    【零基础】为什么Facebook发币就不一样
    【零基础】彻底搞懂51单片机各种型号(ATMEL系列)
    【零基础】简单说说一键果体APP的AI
  • 原文地址:https://www.cnblogs.com/tzq9308/p/4321588.html
Copyright © 2020-2023  润新知