最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

剖析:

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

重點(diǎn):

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

界面布局:

具體代碼如下:

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 = "計(jì)算器";
            this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            textBox1.ReadOnly = true;//文本框只讀
            textBox2.TabIndex = 0;//光標(biāo)焦點(diǎn)在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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#實(shí)現(xiàn)飛行棋(Winform)

    C#實(shí)現(xiàn)飛行棋(Winform)

    這篇文章主要為大家詳細(xì)介紹了基于Winform框架的飛行棋,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • C#?try?catch?使用實(shí)例詳解

    C#?try?catch?使用實(shí)例詳解

    在編程中,?try-catch-throw?是一種常見的錯誤處理模式,這三個關(guān)鍵字通常一起使用,以捕獲異常、處理異常和重新拋出異常,這篇文章主要介紹了C#?try?catch?使用,需要的朋友可以參考下
    2023-09-09
  • 基于c# 類、接口、結(jié)構(gòu)的聯(lián)系與區(qū)別詳解

    基于c# 類、接口、結(jié)構(gòu)的聯(lián)系與區(qū)別詳解

    本篇文章是對c#中類與接口以及結(jié)構(gòu)的聯(lián)系與區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • C#如何使用Bogus創(chuàng)建模擬數(shù)據(jù)示例代碼

    C#如何使用Bogus創(chuàng)建模擬數(shù)據(jù)示例代碼

    這篇文章主要給大家介紹了關(guān)于C#如何使用Bogus創(chuàng)建模擬數(shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • C#使用二維數(shù)組模擬斗地主

    C#使用二維數(shù)組模擬斗地主

    這篇文章主要介紹了C#使用二維數(shù)組模擬斗地主的方法,通過C#的二維數(shù)組簡單實(shí)現(xiàn)撲克隨機(jī)發(fā)牌的功能,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • C#調(diào)用DeepSeek?API的兩種實(shí)現(xiàn)方案

    C#調(diào)用DeepSeek?API的兩種實(shí)現(xiàn)方案

    DeepSeek(深度求索)?最近可謂火爆的一塌糊涂,具體的介紹這里不再贅述,您可以各種搜索其信息,即使您不搜索,只要您拿起手機(jī),各種關(guān)于?DeepSeek?的新聞、資訊也會撲面而來的推送到您面前,本文給大家介紹了C#調(diào)用DeepSeek?API的兩種實(shí)現(xiàn)方案,需要的朋友可以參考下
    2025-02-02
  • C# wpf Brush轉(zhuǎn)Hex字符串的實(shí)例代碼

    C# wpf Brush轉(zhuǎn)Hex字符串的實(shí)例代碼

    這篇文章主要介紹了C# wpf Brush轉(zhuǎn)Hex字符串的實(shí)例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • C#動態(tài)生成PictureBox并指定圖片的方法

    C#動態(tài)生成PictureBox并指定圖片的方法

    這篇文章主要介紹了C#動態(tài)生成PictureBox并指定圖片的方法,實(shí)例分析了C#圖形控件的動態(tài)生成及使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • C#的File類實(shí)現(xiàn)文件操作實(shí)例詳解

    C#的File類實(shí)現(xiàn)文件操作實(shí)例詳解

    這篇文章主要介紹了C#的File類實(shí)現(xiàn)文件操作的方法,非常實(shí)用,需要的朋友可以參考下
    2014-07-07
  • C#畫筆Pen繪制曲線的方法

    C#畫筆Pen繪制曲線的方法

    這篇文章主要介紹了C#畫筆Pen繪制曲線的方法,主要涉及C#畫筆中DrawCurve方法的使用技巧,需要的朋友可以參考下
    2015-06-06

最新評論

昆山市| 兴隆县| 洪泽县| 新晃| 巴彦县| 克拉玛依市| 黄浦区| 青州市| 青冈县| 安义县| 红安县| 利津县| 宜兴市| 翁源县| 天津市| 张家界市| 闸北区| 东源县| 买车| 平山县| 陕西省| 固阳县| 红河县| 东明县| 都匀市| 平武县| 永善县| 武功县| 明水县| 上思县| 毕节市| 江城| 天峨县| 会理县| 深圳市| 罗田县| 新宾| 滦平县| 陵川县| 荣昌县| 江津市|