亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#實現(xiàn)簡易計算器功能(附源碼)

 更新時間:2021年07月21日 12:16:21   作者:Just Do Its  
這篇文章主要為大家詳細介紹了C#實現(xiàn)簡易計算器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了C#實現(xiàn)簡易計算器功能的具體代碼,供大家參考,具體內(nèi)容如下

剖析:

1、先設計界面(按鈕、文本框(一個顯示算式,一個顯示結果))布局
2、單擊按鈕將其對應內(nèi)容顯示在文本框中
3、單擊符號(+、-、×、÷、%)時將第一次輸入的數(shù)儲存起來
4、單擊等號時將第二次輸入的數(shù)存儲起來并將第一次輸入的數(shù)與第二次輸入的數(shù)按照所單擊的符號進行運算將結果顯示在第一個文本框中
5、單擊C時將兩個文本框中的內(nèi)容清空

重點:

1、聲明一個bool類型的變量用于實現(xiàn)單擊符號再次輸入數(shù)字時第一次輸入的數(shù)字清空顯示第二次輸入的數(shù)字
2、聲明兩個double類型的變量用于裝第一次輸入的數(shù)和裝第二次輸入的數(shù)
3、聲明一個string類型的變量用于判斷運算符號

界面布局:

具體代碼如下:

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 Test_Calculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //聲明三個變量
        string type; //符號類型
        double x;//裝第一個數(shù)(按符號(+-×÷%)時textbox1中的數(shù)字)
        double y;//裝第二個數(shù)(按等號時textbox1中的數(shù)字)
        bool c=false;
        private void Form1_Load(object sender, EventArgs e)
        {
            this.CenterToScreen();//窗體居中顯示
            this.Text = "計算器";
            this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            textBox1.ReadOnly = true;//文本框只讀
            textBox2.TabIndex = 0;//光標焦點在textbox2中
        }

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

        private void button2_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += "2";
            textBox2.Text += "2";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += "3";
            textBox2.Text += "3";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += "4";
            textBox2.Text += "4";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += "5";
            textBox2.Text += "5";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += "6";
            textBox2.Text += "6";
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += "7";
            textBox2.Text += "7";
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += "8";
            textBox2.Text += "8";
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += "9";
            textBox2.Text += "9";
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += "0";
            textBox2.Text += "0";
        }

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

        private void button12_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
        }

        private void button13_Click(object sender, EventArgs e)
        {
            c = true;
            type = "+";
            textBox2.Text += "+";
            x = double.Parse(textBox1.Text);
        }

        private void button14_Click(object sender, EventArgs e)
        {
            c = true;
            type = "-";
            textBox2.Text += "-";
            x = double.Parse(textBox1.Text);
        }

        private void button15_Click(object sender, EventArgs e)
        {
            c = true;
            type = "×";
            textBox2.Text += "×";
            x = double.Parse(textBox1.Text);
        }

        private void button16_Click(object sender, EventArgs e)
        {
            c = true;
            type = "÷";
            textBox2.Text += "÷";
            x = double.Parse(textBox1.Text);
        }

        private void button18_Click(object sender, EventArgs e)
        {
            c = true;
            type = "%";
            textBox2.Text += "%";
            x = double.Parse(textBox1.Text);
        }

        private void button17_Click(object sender, EventArgs e)
        {
            y = double.Parse(textBox1.Text);
            //法一
            while (type=="+")
            {
                textBox1.Text = (x + y).ToString();
                textBox2.Text += "=" + textBox1.Text;
                return;
            }
            while (type == "-")
            {
                textBox1.Text = (x - y).ToString();
                textBox2.Text += "=" + textBox1.Text;
                return;
            } 
            while (type == "×")
            {
                textBox1.Text = (x * y).ToString();
                textBox2.Text += "=" + textBox1.Text;
                return;
            } 
            while (type == "÷")
            {
                if (y!=0)
                {
                    textBox1.Text = (x / y).ToString();
                    textBox2.Text += "=" + textBox1.Text;
                }
                else
                {
                    MessageBox.Show("請重新輸入","錯誤",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    textBox1.Text = "";
                    textBox2.Text = "";
                }
                return;
            }
            while (type == "%")
            {
                textBox1.Text = (x % y).ToString();
                textBox2.Text += "=" + textBox1.Text;
                return;
            }
            
            //法二:
            //if (type=="+")
            //{
            //    textBox1.Text=(x + y).ToString();
            //    textBox2.Text += "=" + textBox1.Text;
            //}
            //if (type=="-")
            //{
            //    textBox1.Text = (x - y).ToString();
            //    textBox2.Text += "=" + textBox1.Text;
            //}
            //if (type=="×")
            //{
            //    textBox1.Text = (x * y).ToString();
            //    textBox2.Text += "=" + textBox1.Text;
            //}
            //if (type=="÷")
            //{
            //    textBox1.Text = (x / y).ToString();
            //    textBox2.Text += "=" + textBox1.Text;
            //}
            //if (type=="%")
            //{
            //    textBox1.Text = (x % y).ToString();
            //    textBox2.Text += "=" + textBox1.Text;
            //}
        }

    }
}

效果圖:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • C#直線的最小二乘法線性回歸運算實例

    C#直線的最小二乘法線性回歸運算實例

    這篇文章主要介紹了C#直線的最小二乘法線性回歸運算方法,實例分析了給定一組點,用最小二乘法進行線性回歸運算的實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-08-08
  • C#實現(xiàn)向數(shù)組指定索引位置插入新的元素值

    C#實現(xiàn)向數(shù)組指定索引位置插入新的元素值

    這篇文章給大家介紹了利用C#實現(xiàn)向數(shù)組指定索引位置插入新的元素值,首先需要定義一個一維數(shù)組,然后修改數(shù)組的長度,從而在其中增加一個元素,需要的朋友可以參考下
    2024-02-02
  • C#使用遠程服務調(diào)用框架Apache Thrift

    C#使用遠程服務調(diào)用框架Apache Thrift

    這篇文章介紹了C#使用遠程服務調(diào)用框架Apache Thrift的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • C#實現(xiàn)TCP和UDP通信的示例詳解

    C#實現(xiàn)TCP和UDP通信的示例詳解

    這篇文章主要為大家詳細介紹了C#實現(xiàn)TCP和UDP通信的相關知識,文中的示例代碼講解詳細,具有一定的學習價值,感興趣的小伙伴可以了解一下
    2023-03-03
  • c#計算某段代碼的執(zhí)行時間實例方法

    c#計算某段代碼的執(zhí)行時間實例方法

    在本篇文章里我們給大家整理了關于c#計算某段代碼的執(zhí)行時間的方法和經(jīng)驗,有興趣的朋友們學習下。
    2019-02-02
  • C#中的yield關鍵字的使用方法介紹

    C#中的yield關鍵字的使用方法介紹

    yield這個關鍵字是和迭代器掛鉤的,而且是與return一起以yield return的形式合用的,用來返回迭代器中的條目。
    2013-04-04
  • c# EnumHelper枚舉常用操作類

    c# EnumHelper枚舉常用操作類

    在項目中需要把枚舉填充到下拉框中,所以使用統(tǒng)一的方法實現(xiàn),測試代碼如下,需要的朋友可以參考下
    2016-11-11
  • C# 常用日期時間函數(shù)(老用不熟)

    C# 常用日期時間函數(shù)(老用不熟)

    C# 常用日期時間函數(shù)(老用不熟) ,需要的朋友可以參考下。
    2009-09-09
  • C#實現(xiàn)過濾html標簽并保留a標簽的方法

    C#實現(xiàn)過濾html標簽并保留a標簽的方法

    這篇文章主要介紹了C#實現(xiàn)過濾html標簽并保留a標簽的方法,文中的自定義函數(shù)采用正則過濾實現(xiàn)了該功能,是非常實用的技巧,需要的朋友可以參考下
    2014-09-09
  • C# 線程安全詳解

    C# 線程安全詳解

    這篇文章主要介紹了c# 線程安全的用法原理及使用示例,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下,希望能夠給你帶來幫助
    2021-09-09

最新評論