欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#使用分部類設(shè)計實現(xiàn)一個計算器

 更新時間:2024年02月20日 08:20:32   作者:wenchm  
分部類是C#4.5中的一個新特性,它的出現(xiàn)使得程序的結(jié)構(gòu)更加合理,代碼組織更加緊密,本文將使用分部類設(shè)計實現(xiàn)一個簡單的計算器,感興趣的小伙伴可以了解下

在開發(fā)一些大型項目或者特殊部署時,可能需要把一個類、結(jié)構(gòu)或接口放在幾個文件中來分別進行處理。等到編譯時,再自動把它們整合起來,這時就用到了分部類。 

分部類相當(dāng)于將一個會計部門(類、結(jié)構(gòu)或接口)分成兩個部門,這兩個部門可以單獨對公司各部門的賬目進行審核,而在繁忙時期,兩個部門也可以相互調(diào)動人員(這里的人員相當(dāng)于類中的方法、屬性、變量等),或是合成一個整體進行工作 

一、涉及到的知識點

1.分部類

分部類是C#4.5中的一個新特性,它的出現(xiàn)使得程序的結(jié)構(gòu)更加合理,代碼組織更加緊密。開發(fā)人員可以將類、結(jié)構(gòu)或接口的定義拆分到兩個或多個源文件中,每個源文件包含類定義的一部分,編譯應(yīng)用程序時,VS會把所有部分組合起來,這樣的類被稱為分部類。

定義分部類時需要使用partial關(guān)鍵字,分部類的每個部分都必須包含一個partial關(guān)鍵字,并且其聲明必須與其他部分位于同一命名空間。使用分部類時,要成為同一類型的各個部分的所有分部類型定義都必須在同一程序集和同一模塊(.exe或.dll文件)中進行定義,分部類定義不能跨越多個模塊。使用分部類時,各個部分必須具有相同的可訪問性,如public、private等。

2.分部類主要應(yīng)用在以下兩個方面

當(dāng)項目比較龐大時,使用分部類可以拆分一個類至幾個文件中,這樣可以使不同的開發(fā)人員同時進行工作,提高了工作效率。
使用自動生成的文件源時,無須重新創(chuàng)建源文件即可將代碼添加到類中。VS在創(chuàng)建Windows窗體和Web服務(wù)包裝代碼等時都使用此方法。

3.合理使用分部類分割類

C#編碼規(guī)范中規(guī)定,一個類中的代碼最好不要超過500行,但是,如果實際開發(fā)中,確實需要在一個類中包含超過500行以上的代碼時,該怎么辦呢?這時可以使用分部類將該類分成幾部分,其中每個部分包含一部分實現(xiàn)代碼。

例如,本例中,把一個public class Account類,用partial關(guān)鍵字,分割成兩個public partial class Account類。

//分部類
//分割類的用法
namespace _124
{
    public partial class Account
    {
        #region 執(zhí)行兩個數(shù)的加法運算
        /// <summary>
        /// 執(zhí)行兩個數(shù)的加法運算
        /// </summary>
        /// <param name="Former">加數(shù)</param>
        /// <param name="After">被加數(shù)</param>
        /// <returns>返回相加后的結(jié)果</returns>
        public static double Addition(double Former, double After)
        {
            return Former + After;
        }
        #endregion
 
        #region 執(zhí)行兩個數(shù)的減法運算
        /// <summary>
        /// 執(zhí)行兩個數(shù)的減法運算
        /// </summary>
        /// <param name="Former">減數(shù)</param>
        /// <param name="After">被減數(shù)</param>
        /// <returns>返回相減后的結(jié)果</returns>
        public static double Subtration(double Former, double After)
        {
            return Former - After;
        }
        #endregion
 
        #region 執(zhí)行兩個數(shù)的乘法運算
        /// <summary>
        /// 執(zhí)行兩個數(shù)的乘法運算
        /// </summary>
        /// <param name="Former">乘數(shù)</param>
        /// <param name="After">被乘數(shù)</param>
        /// <returns>返回相乘后的結(jié)果</returns>
        public static double Multiplication(double Former, double After)
        {
            return Former * After;
        }
        #endregion
 
        #region 執(zhí)行兩個數(shù)的除法運算
        /// <summary>
        /// 執(zhí)行兩個數(shù)的除法運算
        /// </summary>
        /// <param name="Former">除數(shù)</param>
        /// <param name="After">被除數(shù)</param>
        /// <returns>返回相除后的結(jié)果</returns>
        public static double Division(double Former, double After)
        {
            if (After == 0)
            {
                MessageBox.Show("被除數(shù)不能為0。");
                return 0;
            }
            return Former / After;
        }
        #endregion
    }
 
 
    public partial class Account
    {
        /// <summary>
        /// 計算一個數(shù)的倒數(shù)
        /// </summary>
        /// <param name="num">數(shù)據(jù)</param>
        /// <returns>返回倒數(shù)值</returns>
        public static double Reciprocal(double num)
        {
            if (num == 0)
            {
                MessageBox.Show("分母不能為0。");
                return 0;
            }
            return 1 / num;
        }
 
        /// <summary>
        /// 計算一個數(shù)的開方
        /// </summary>
        /// <param name="num">數(shù)據(jù)</param>
        /// <returns>返回開方后的值</returns>
        public static double SquareRoot(double num)
        {
            if (num <= 0)
            {
                MessageBox.Show("負數(shù)不能開方。");
                return 0;
            }
            return Math.Sqrt(num);
        }
 
        /// <summary>
        /// 求一個數(shù)的相反數(shù)
        /// </summary>
        /// <param name="num">數(shù)據(jù)</param>
        /// <returns>相反數(shù)</returns>
        public static double Opposition(double num)
        {
            return -num;
        }
 
        /// <summary>
        /// 一個數(shù)的百分比
        /// </summary>
        /// <param name="num">數(shù)據(jù)</param>
        /// <returns>返回百分比</returns>
        public static double Percentage(double num)
        {
            return num / 100;
        }
    }
}

4.事件處理程序

在C#編程中,事件處理程序是一個方法,它用于響應(yīng)特定事件的發(fā)生。例如,當(dāng)一個按鈕被單擊時,可以使用事件處理程序來響應(yīng)這個事件并執(zhí)行相應(yīng)的代碼。例如:

this.pictureBox22.Click += new System.EventHandler(this.pictureBox21_Click);

在這個例子中,pictureBox22控件的Click事件被添加了一個事件處理程序,即pictureBox21_Click方法。當(dāng)pictureBox22控件被單擊時,pictureBox21_Click方法將被執(zhí)行。這樣就可以在pictureBox22控件被單擊時,響應(yīng)這個事件并執(zhí)行相應(yīng)的代碼。

這樣的設(shè)計,可以集中處理功能類似的事件,極大地減少代碼量,并且代碼更易讀、更易維護。

5.Math.Ceiling方法

Math.Ceiling是 C# 中的一個方法,用于將一個浮點數(shù)向上取整到最接近的整數(shù)。如果浮點數(shù)已經(jīng)是整數(shù),則該方法不會對其做任何修改。

例如,Math.Ceiling(3.14)的結(jié)果是4,因為 3.14 最接近的整數(shù)是4。Math.Ceiling(5.0)的結(jié)果是5,因為 5.0 已經(jīng)是一個整數(shù)。

需要注意的是,如果浮點數(shù)是負數(shù),則Math.Ceiling方法會將其向上取整到最接近的負整數(shù)。例如,Math.Ceiling(-3.14)的結(jié)果是-3,因為-3.14最接近的負整數(shù)是-3。   

6.Text.Contains()     

Text.Contains()是 C# 中的一個方法,用于檢查一個字符串是否包含另一個字符串。它返回一個布爾值,表示指定的子字符串是否存在于字符串中。

string str = "Hello World";
bool result = str.Contains("World"); //result 變量的值為 true

Text.Contains()方法接受一個字符串參數(shù),用于指定要查找的子字符串。它還接受一個可選的StringComparison枚舉值參數(shù),用于指定字符串比較的類型。如果不指定這個參數(shù),則默認使用StringComparison.CurrentCulture,它使用當(dāng)前文化來比較字符串。

if (!textBox1!.Text.Contains('.', StringComparison.CurrentCulture))
//等價語句
if (!textBox1!.Text.Contains('.'))

7.pictureBox.Tag屬性

設(shè)置pictureBox的Tag屬性的字符串內(nèi)容=該按鍵的功能,比如,設(shè)置按鍵CE的Tag屬性為"CE"。其它按鍵屬性的設(shè)置以此類推。

// 
// pictureBox22
// 
pictureBox22 = new PictureBox
{
    BackgroundImage = Properties.Resources.ce,
    Location = new Point(91, 72),
    Name = "pictureBox22",
    Size = new Size(66, 24),
    TabIndex = 21,
    TabStop = false,
    Tag = "CE"
};
//pictureBox22.Click += PictureBox22_Click;
pictureBox22.Click += new EventHandler(PictureBox21_Click);
((System.ComponentModel.ISupportInitialize)pictureBox22).BeginInit();

設(shè)置pictureBox的Tag屬性的用途是當(dāng)觸發(fā)按鈕事件時,把該按鈕的Tag屬性作為Switch表達式的參數(shù)列表,從而響應(yīng)按鈕事件。例如:

private void PictureBox21_Click(object? sender, EventArgs e)
{
    ButtonTag = (sender as PictureBox)!.Tag!.ToString()!;//獲取當(dāng)前按鈕的標(biāo)識
    switch (ButtonTag)
    {
        //點擊到數(shù)字鍵
        case "0": ReadNumber(ButtonTag); break;
        case "1": ReadNumber(ButtonTag); break;
        case "2": ReadNumber(ButtonTag); break;
        case "3": ReadNumber(ButtonTag); break;
        case "4": ReadNumber(ButtonTag); break;
        case "5": ReadNumber(ButtonTag); break;
        case "6": ReadNumber(ButtonTag); break;
        case "7": ReadNumber(ButtonTag); break;
        case "8": ReadNumber(ButtonTag); break;
        case "9": ReadNumber(ButtonTag); break;
        //點擊到運算符
        case "+": OperatorType = ButtonTag; isnum = true; textBox1!.Text = "0"; break;
        case "-": OperatorType = ButtonTag; isnum = true; textBox1!.Text = "0"; break;
        case "*": OperatorType = ButtonTag; isnum = true; textBox1!.Text = "0"; break;
        case "/": OperatorType = ButtonTag; isnum = true; textBox1!.Text = "0"; break;
        case "%": AuxiliaryCalculation(ButtonTag); break;
        case "1/X": AuxiliaryCalculation(ButtonTag); break;
        case "+-": AuxiliaryCalculation(ButtonTag); break;
        case "Sqrt": AuxiliaryCalculation(ButtonTag); break;
        case ".": DecimalPoint(); break;
        
        case "=": Calculation(ButtonTag); isnum = false; break;    //計算結(jié)果
        //C 則是 "Clear" 的簡稱,它用于清除計算器中的所有數(shù)據(jù),包括存儲器和寄存器的內(nèi)容。
        case "C":
            {
                Value_1 = "";
                Value_2 = "";
                OperatorType = "";
                textBox1!.Text = "0";
                break;
            }
        //CE 是 "Clear Entry" 的縮寫,它的主要作用是清除當(dāng)前正在輸入的數(shù)字或者運算符。
        case "CE": textBox1!.Text = "0"; Value_1 = ""; break;
        default: Backspace(); break;     /*"Back":*/
    }
}

二、實例 

本實例將使用分部類制作一個計算器,其中主要是用分部類來分別記錄計算器的計算方法,如將實現(xiàn)加、減、乘和除的方法放在一個分部類中,而將實現(xiàn)正負、開方、百分比和倒數(shù)的方法放在另一個分部類中。

1.源碼

// 使用分部類實現(xiàn)多種計算方法
namespace _124
{
    public partial class Form1 : Form
    {
        private Panel? panel1;
        private PictureBox? pictureBox5;
        private PictureBox? pictureBox4;
        private PictureBox? pictureBox3;
        private PictureBox? pictureBox2;
        private PictureBox? pictureBox1;
        private PictureBox? pictureBox10;
        private PictureBox? pictureBox9;
        private PictureBox? pictureBox8;
        private PictureBox? pictureBox7;
        private PictureBox? pictureBox6;
        private PictureBox? pictureBox20;
        private PictureBox? pictureBox19;
        private PictureBox? pictureBox18;
        private PictureBox? pictureBox17;
        private PictureBox? pictureBox16;
        private PictureBox? pictureBox15;
        private PictureBox? pictureBox14;
        private PictureBox? pictureBox13;
        private PictureBox? pictureBox12;
        private PictureBox? pictureBox11;
        private PictureBox? pictureBox23;
        private PictureBox? pictureBox22;
        private PictureBox? pictureBox21;
        private PictureBox? pictureBox24;
        private TextBox? textBox1;
 
        public string Value_1 = "";       //操作數(shù)1
        public string Value_2 = "";       //操作數(shù)2
        public string OperatorType = "";  //運算符種類
        string ButtonTag = "";            //記錄當(dāng)前輸入的鍵值
        bool isnum = false;               //判斷輸入的是計算的那個值,他就是一個開關(guān),按下運算符號時翻轉(zhuǎn),按下等號后,要復(fù)原
        bool IsNotDecimalPoint = false;   //是否包含小數(shù)點
 
        public Form1()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
            Load += Form1_Load;
        }
 
        private void Form1_Load(object? sender, EventArgs e)
        {
            // 
            // pictureBox24
            // 
            pictureBox24 = new PictureBox
            {
                BackgroundImage = Properties.Resources.Close,
                Location = new Point(226, 0),
                Name = "pictureBox24",
                Size = new Size(11, 11),
                TabIndex = 23,
                TabStop = false
            };
            pictureBox24.Click += PictureBox24_Click;
            ((System.ComponentModel.ISupportInitialize)pictureBox24).BeginInit();
            // 
            // pictureBox23
            // 
            pictureBox23 = new PictureBox
            {
                BackgroundImage = Properties.Resources.c,
                Location = new Point(161, 72),
                Name = "pictureBox23",
                Size = new Size(66, 24),
                TabIndex = 22,
                TabStop = false,
                Tag = "C"
            };
            pictureBox23.Click += new EventHandler(PictureBox21_Click);
            ((System.ComponentModel.ISupportInitialize)pictureBox23).BeginInit();
            // 
            // pictureBox22
            // 
            pictureBox22 = new PictureBox
            {
                BackgroundImage = Properties.Resources.ce,
                Location = new Point(91, 72),
                Name = "pictureBox22",
                Size = new Size(66, 24),
                TabIndex = 21,
                TabStop = false,
                Tag = "CE"
            };
            //pictureBox22.Click += PictureBox22_Click;
            pictureBox22.Click += new EventHandler(PictureBox21_Click);
            ((System.ComponentModel.ISupportInitialize)pictureBox22).BeginInit();
            // 
            // pictureBox21
            // 
            pictureBox21 = new PictureBox
            {
                BackgroundImage = Properties.Resources.back,
                Location = new Point(12, 72),
                Name = "pictureBox21",
                Size = new Size(75, 24),
                TabIndex = 20,
                TabStop = false,
                Tag = "Back"
            };
            pictureBox21.Click += PictureBox21_Click;
            ((System.ComponentModel.ISupportInitialize)pictureBox21).BeginInit();
            // 
            // pictureBox20
            // 
            pictureBox20 = new PictureBox
            {
                BackgroundImage = Properties.Resources.Amound,
                Location = new Point(188, 192),
                Name = "pictureBox20",
                Size = new Size(40, 24),
                TabIndex = 19,
                TabStop = false,
                Tag = "="
            };
            pictureBox20.Click += new EventHandler(PictureBox21_Click);
            ((System.ComponentModel.ISupportInitialize)pictureBox20).BeginInit();
            // 
            // pictureBox19
            // 
            pictureBox19 = new PictureBox
            {
                BackgroundImage = Properties.Resources.Add,
                Location = new Point(144, 192),
                Name = "pictureBox19",
                Size = new Size(40, 24),
                TabIndex = 18,
                TabStop = false,
                Tag = "+"
            };
            pictureBox19.Click += new EventHandler(PictureBox21_Click);
            ((System.ComponentModel.ISupportInitialize)pictureBox19).BeginInit();
            // 
            // pictureBox18
            // 
            pictureBox18 = new PictureBox
            {
                BackgroundImage = Properties.Resources.Dot,
                Location = new Point(100, 192),
                Name = "pictureBox18",
                Size = new Size(40, 24),
                TabIndex = 17,
                TabStop = false,
                Tag = "."
            };
            pictureBox18.Click += new EventHandler(PictureBox21_Click);
            ((System.ComponentModel.ISupportInitialize)pictureBox18).BeginInit();
            // 
            // pictureBox17
            // 
            pictureBox17 = new PictureBox
            {
                BackgroundImage = Properties.Resources.Bear,
                Location = new Point(56, 192),
                Name = "pictureBox17",
                Size = new Size(40, 24),
                TabIndex = 16,
                TabStop = false,
                Tag = "+-"
            };
            pictureBox17.Click += new EventHandler(PictureBox21_Click);
            ((System.ComponentModel.ISupportInitialize)pictureBox17).BeginInit();
            // 
            // pictureBox16
            // 
            pictureBox16 = new PictureBox
            {
                BackgroundImage = Properties.Resources._0,
                Location = new Point(12, 192),
                Name = "pictureBox16",
                Size = new Size(40, 24),
                TabIndex = 15,
                TabStop = false,
                Tag = "0"
            };
            pictureBox16.Click += new EventHandler(PictureBox21_Click);
            ((System.ComponentModel.ISupportInitialize)pictureBox16).BeginInit();
            // 
            // pictureBox15
            // 
            pictureBox15 = new PictureBox
            {
                BackgroundImage = Properties.Resources.Deno,
                Location = new Point(188, 162),
                Name = "pictureBox15",
                Size = new Size(40, 24),
                TabIndex = 14,
                TabStop = false,
                Tag = "1/X"
            };
            pictureBox15.Click += new EventHandler(PictureBox21_Click);
            ((System.ComponentModel.ISupportInitialize)pictureBox15).BeginInit();
            // 
            // pictureBox14
            //
            pictureBox14 = new PictureBox
            {
                BackgroundImage = Properties.Resources.Decr,
                Location = new Point(144, 162),
                Name = "pictureBox14",
                Size = new Size(40, 24),
                TabIndex = 13,
                TabStop = false,
                Tag = "-"
            };
            pictureBox14.Click += new EventHandler(PictureBox21_Click);
            ((System.ComponentModel.ISupportInitialize)pictureBox14).BeginInit();
            // 
            // pictureBox13
            // 
            pictureBox13 = new PictureBox
            {
                BackgroundImage = Properties.Resources._3,
                Location = new Point(100, 162),
                Name = "pictureBox13",
                Size = new Size(40, 24),
                TabIndex = 12,
                TabStop = false,
                Tag = "3"
            };
            pictureBox13.Click += new EventHandler(PictureBox21_Click);
            ((System.ComponentModel.ISupportInitialize)pictureBox13).BeginInit();
            // 
            // pictureBox12
            // 
            pictureBox12 = new PictureBox
            {
                BackgroundImage = Properties.Resources._2,
                Location = new Point(56, 162),
                Name = "pictureBox12",
                Size = new Size(40, 24),
                TabIndex = 11,
                TabStop = false,
                Tag = "2"
            };
            pictureBox12.Click += new EventHandler(PictureBox21_Click);
            ((System.ComponentModel.ISupportInitialize)pictureBox12).BeginInit();
            // 
            // pictureBox11
            // 
            pictureBox11 = new PictureBox
            {
                BackgroundImage = Properties.Resources._1,
                Location = new Point(12, 162),
                Name = "pictureBox11",
                Size = new Size(40, 24),
                TabIndex = 10,
                TabStop = false,
                Tag = "1"
            };
            pictureBox11.Click += new EventHandler(PictureBox21_Click);
            ((System.ComponentModel.ISupportInitialize)pictureBox11).BeginInit();
            // 
            // pictureBox10
            // 
            pictureBox10 = new PictureBox
            {
                BackgroundImage = Properties.Resources.Hund,
                Location = new Point(188, 132),
                Name = "pictureBox10",
                Size = new Size(40, 24),
                TabIndex = 9,
                TabStop = false,
                Tag = "%"
            };
            pictureBox10.Click += new EventHandler(PictureBox21_Click);
            ((System.ComponentModel.ISupportInitialize)pictureBox10).BeginInit();
            // 
            // pictureBox9
            // 
            pictureBox9 = new PictureBox
            {
                BackgroundImage = Properties.Resources.Ride,
                Location = new Point(144, 132),
                Name = "pictureBox9",
                Size = new Size(40, 24),
                TabIndex = 8,
                TabStop = false,
                Tag = "*"
            };
            pictureBox9.Click += new EventHandler(PictureBox21_Click);
            ((System.ComponentModel.ISupportInitialize)pictureBox9).BeginInit();
            // 
            // pictureBox8
            // 
            pictureBox8 = new PictureBox
            {
                BackgroundImage = Properties.Resources._6,
                Location = new Point(100, 132),
                Name = "pictureBox8",
                Size = new Size(40, 24),
                TabIndex = 7,
                TabStop = false,
                Tag = "6"
            };
            pictureBox8.Click += new EventHandler(PictureBox21_Click);
            ((System.ComponentModel.ISupportInitialize)pictureBox8).BeginInit();
            // 
            // pictureBox7
            // 
            pictureBox7 = new PictureBox
            {
                BackgroundImage = Properties.Resources._5,
                Location = new Point(56, 132),
                Name = "pictureBox7",
                Size = new Size(40, 24),
                TabIndex = 6,
                TabStop = false,
                Tag = "5"
            };
            pictureBox7.Click += new EventHandler(PictureBox21_Click);
            ((System.ComponentModel.ISupportInitialize)pictureBox7).BeginInit();
            // 
            // pictureBox6
            // 
            pictureBox6 = new PictureBox
            {
                BackgroundImage = Properties.Resources._4,
                Location = new Point(12, 132),
                Name = "pictureBox6",
                Size = new Size(40, 24),
                TabIndex = 5,
                TabStop = false,
                Tag = "4"
            };
            pictureBox6.Click += new EventHandler(PictureBox21_Click);
            ((System.ComponentModel.ISupportInitialize)pictureBox6).BeginInit();
            // 
            // pictureBox5
            // 
            pictureBox5 = new PictureBox
            {
                BackgroundImage = Properties.Resources.sqrt,
                Location = new Point(188, 102),
                Name = "pictureBox5",
                Size = new Size(40, 24),
                TabIndex = 4,
                TabStop = false,
                Tag = "Sqrt"
            };
            pictureBox5.Click += new EventHandler(PictureBox21_Click);
            ((System.ComponentModel.ISupportInitialize)pictureBox5).BeginInit();
            // 
            // pictureBox4
            // 
            pictureBox4 = new PictureBox
            {
                BackgroundImage = Properties.Resources.Remove,
                Location = new Point(144, 102),
                Name = "pictureBox4",
                Size = new Size(40, 24),
                TabIndex = 3,
                TabStop = false,
                Tag = "/"
            };
            pictureBox4.Click += new EventHandler(PictureBox21_Click);
            ((System.ComponentModel.ISupportInitialize)pictureBox4).BeginInit();
            // 
            // pictureBox3
            //
            pictureBox3 = new PictureBox
            {
                BackgroundImage = Properties.Resources._9,
                Location = new Point(100, 102),
                Name = "pictureBox3",
                Size = new Size(40, 24),
                TabIndex = 2,
                TabStop = false,
                Tag = "9"
            };
            pictureBox3.Click += new EventHandler(PictureBox21_Click);
            ((System.ComponentModel.ISupportInitialize)pictureBox3).BeginInit();
            // 
            // pictureBox2
            // 
            pictureBox2 = new PictureBox
            {
                BackgroundImage = Properties.Resources._8,
                Location = new Point(56, 102),
                Name = "pictureBox2",
                Size = new Size(40, 24),
                TabIndex = 1,
                TabStop = false,
                Tag = "8"
            };
            pictureBox2.Click += new EventHandler(PictureBox21_Click);
            ((System.ComponentModel.ISupportInitialize)pictureBox2).BeginInit();
            // 
            // pictureBox1
            // 
            pictureBox1 = new PictureBox
            {
                BackgroundImage = Properties.Resources._7,
                Location = new Point(12, 102),
                Name = "pictureBox1",
                Size = new Size(40, 24),
                TabIndex = 0,
                TabStop = false,
                Tag = "7"
            };
            pictureBox1.Click += new EventHandler(PictureBox21_Click);
            ((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit();
            // 
            // textBox1
            // 
            textBox1 = new TextBox
            {
                Location = new Point(12, 33),
                Name = "textBox1",
                Size = new Size(215, 23),
                TabIndex = 24
            };
            // 
            // panel1
            // 
            panel1 = new Panel
            {
                BackgroundImage = Properties.Resources.bg,
                Dock = DockStyle.Fill,
                Location = new Point(0, 0),
                Name = "panel1",
                Size = new Size(240, 230),
                TabIndex = 0
            };
            panel1.Controls.Add(textBox1);
            panel1.Controls.Add(pictureBox24);
            panel1.Controls.Add(pictureBox23);
            panel1.Controls.Add(pictureBox22);
            panel1.Controls.Add(pictureBox21);
            panel1.Controls.Add(pictureBox20);
            panel1.Controls.Add(pictureBox19);
            panel1.Controls.Add(pictureBox18);
            panel1.Controls.Add(pictureBox17);
            panel1.Controls.Add(pictureBox16);
            panel1.Controls.Add(pictureBox15);
            panel1.Controls.Add(pictureBox14);
            panel1.Controls.Add(pictureBox13);
            panel1.Controls.Add(pictureBox12);
            panel1.Controls.Add(pictureBox11);
            panel1.Controls.Add(pictureBox10);
            panel1.Controls.Add(pictureBox9);
            panel1.Controls.Add(pictureBox8);
            panel1.Controls.Add(pictureBox7);
            panel1.Controls.Add(pictureBox6);
            panel1.Controls.Add(pictureBox5);
            panel1.Controls.Add(pictureBox4);
            panel1.Controls.Add(pictureBox3);
            panel1.Controls.Add(pictureBox2);
            panel1.Controls.Add(pictureBox1);
            panel1.SuspendLayout();
            
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(240, 230);
            Controls.Add(panel1);
            FormBorderStyle = FormBorderStyle.None;//隱藏窗體的邊框
            Name = "Form1";
            Text = "Form1";
            panel1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)pictureBox24).EndInit();
            ((System.ComponentModel.ISupportInitialize)pictureBox23).EndInit();
            ((System.ComponentModel.ISupportInitialize)pictureBox22).EndInit();
            ((System.ComponentModel.ISupportInitialize)pictureBox21).EndInit();
            ((System.ComponentModel.ISupportInitialize)pictureBox20).EndInit();
            ((System.ComponentModel.ISupportInitialize)pictureBox19).EndInit();
            ((System.ComponentModel.ISupportInitialize)pictureBox18).EndInit();
            ((System.ComponentModel.ISupportInitialize)pictureBox17).EndInit();
            ((System.ComponentModel.ISupportInitialize)pictureBox16).EndInit();
            ((System.ComponentModel.ISupportInitialize)pictureBox15).EndInit();
            ((System.ComponentModel.ISupportInitialize)pictureBox14).EndInit();
            ((System.ComponentModel.ISupportInitialize)pictureBox13).EndInit();
            ((System.ComponentModel.ISupportInitialize)pictureBox12).EndInit();
            ((System.ComponentModel.ISupportInitialize)pictureBox11).EndInit();
            ((System.ComponentModel.ISupportInitialize)pictureBox10).EndInit();
            ((System.ComponentModel.ISupportInitialize)pictureBox9).EndInit();
            ((System.ComponentModel.ISupportInitialize)pictureBox8).EndInit();
            ((System.ComponentModel.ISupportInitialize)pictureBox7).EndInit();
            ((System.ComponentModel.ISupportInitialize)pictureBox6).EndInit();
            ((System.ComponentModel.ISupportInitialize)pictureBox5).EndInit();
            ((System.ComponentModel.ISupportInitialize)pictureBox4).EndInit();
            ((System.ComponentModel.ISupportInitialize)pictureBox3).EndInit();
            ((System.ComponentModel.ISupportInitialize)pictureBox2).EndInit();
            ((System.ComponentModel.ISupportInitialize)pictureBox1).EndInit();
        }
        
        /// <summary>
        /// Backspace
        /// 根據(jù)鍵值觸發(fā)相應(yīng)的功能
        /// </summary>
        private void PictureBox21_Click(object? sender, EventArgs e)
        {
            ButtonTag = (sender as PictureBox)!.Tag!.ToString()!;//獲取當(dāng)前按鈕的標(biāo)識
            switch (ButtonTag)
            {
                //點擊到數(shù)字鍵
                case "0": ReadNumber(ButtonTag); break;
                case "1": ReadNumber(ButtonTag); break;
                case "2": ReadNumber(ButtonTag); break;
                case "3": ReadNumber(ButtonTag); break;
                case "4": ReadNumber(ButtonTag); break;
                case "5": ReadNumber(ButtonTag); break;
                case "6": ReadNumber(ButtonTag); break;
                case "7": ReadNumber(ButtonTag); break;
                case "8": ReadNumber(ButtonTag); break;
                case "9": ReadNumber(ButtonTag); break;
                //點擊到運算符
                case "+": OperatorType = ButtonTag; isnum = true; textBox1!.Text = "0"; break;
                case "-": OperatorType = ButtonTag; isnum = true; textBox1!.Text = "0"; break;
                case "*": OperatorType = ButtonTag; isnum = true; textBox1!.Text = "0"; break;
                case "/": OperatorType = ButtonTag; isnum = true; textBox1!.Text = "0"; break;
                case "%": AuxiliaryCalculation(ButtonTag); break;
                case "1/X": AuxiliaryCalculation(ButtonTag); break;
                case "+-": AuxiliaryCalculation(ButtonTag); break;
                case "Sqrt": AuxiliaryCalculation(ButtonTag); break;
                case ".": DecimalPoint(); break;
                
                case "=": Calculation(ButtonTag); isnum = false; break;    //計算結(jié)果
                //C 則是 "Clear" 的簡稱,它用于清除計算器中的所有數(shù)據(jù),包括存儲器和寄存器的內(nèi)容。
                case "C":
                    {
                        Value_1 = "";
                        Value_2 = "";
                        OperatorType = "";
                        textBox1!.Text = "0";
                        break;
                    }
                //CE 是 "Clear Entry" 的縮寫,它的主要作用是清除當(dāng)前正在輸入的數(shù)字或者運算符。
                case "CE": textBox1!.Text = "0"; Value_1 = ""; break;
                default: Backspace(); break;     /*"Back":*/
            }
        }
        
        /// <summary>
        /// X,關(guān)閉窗體
        /// </summary>
        private void PictureBox24_Click(object? sender, EventArgs e)
        {
            Close();
        }
 
        /// <summary>
        /// 記錄當(dāng)前輸入的數(shù)字鍵的值
        /// </summary>
        /// <param name="num">鍵值</param>
        public void ReadNumber(string num)
        {
            if (IsNotDecimalPoint)
            {
                if (textBox1!.Text == "0")
                    textBox1.Text = "0.";
                else
                    textBox1.Text += ".";
                IsNotDecimalPoint = false;
            }
            if (textBox1!.Text == "0")
                textBox1.Text = "";
            if (isnum)                      //如果是計算之前的值
            {
                textBox1.Text += num;       //累加輸入值
                Value_2 = textBox1.Text;    //顯示在文本框中
            }
            else                            //計算之后的值
            {
                textBox1.Text += num;       //累加輸入值
                Value_1 = textBox1.Text;    //等待連續(xù)計算
            }
        }
        /// <summary>
        /// +-*/%計算
        /// </summary>
        /// <param name="n"></param>
        public void Calculation(string n)
        {
            ArgumentNullException.ThrowIfNull(n);
 
            double tem_v = 0;               //記錄計算后的結(jié)果
            if (Value_1.Length <= 0 || Value_2.Length <= 0)//判斷是否有計算的兩個值
                return;
            if (OperatorType.Length > 0)    //如果可以計算
            {
                switch (OperatorType)
                {
                    case "+": tem_v = Account.Addition(Convert.ToDouble(Value_1), Convert.ToDouble(Value_2)); break;
                    case "-": tem_v = Account.Subtration(Convert.ToDouble(Value_1), Convert.ToDouble(Value_2)); break;
                    case "*": tem_v = Account.Multiplication(Convert.ToDouble(Value_1), Convert.ToDouble(Value_2)); break;
                    case "/": tem_v = Account.Division(Convert.ToDouble(Value_1), Convert.ToDouble(Value_2)); break;
                }
            }
            if (tem_v == Math.Ceiling(tem_v))         //如果計算結(jié)果為整數(shù)對結(jié)果進行取整
            {
                textBox1!.Text = Convert.ToInt64(tem_v).ToString();
            }
            else
            {
                textBox1!.Text = tem_v.ToString();    //以雙精度進行顯示
            }
            
            Value_1 = textBox1.Text;                  //等待連續(xù)計算
            Value_2 = "";
 
        }
 
        /// <summary>
        /// 輔助計算
        /// </summary>
        /// <param name="str"></param>
        public void AuxiliaryCalculation(string str)
        {
            double tem_v = 0;                        //記錄計算結(jié)果
            switch (str)
            {
                case "%": tem_v = Account.Percentage(Convert.ToDouble(textBox1!.Text)); break;
                case "1/X": tem_v = Account.Reciprocal(Convert.ToDouble(textBox1!.Text)); break;
                case "+-": tem_v = Account.Opposition(Convert.ToDouble(textBox1!.Text)); break;
                case "Sqrt": tem_v = Account.SquareRoot(Convert.ToDouble(textBox1!.Text)); break;
            }
            if (tem_v == Math.Ceiling(tem_v))       //如果計算結(jié)果為整數(shù)對結(jié)果進行取整
            {
                textBox1!.Text = Convert.ToInt64(tem_v).ToString();
            }
            else
            {
                textBox1!.Text = tem_v.ToString();  //以雙精度進行顯示
            }
            Value_1 = textBox1.Text;                //等待連續(xù)計算
            Value_2 = "";
        }
 
        /// <summary>
        /// 刪除輸入的值
        /// </summary>
        public void Backspace()
        {
            var BackspaceStr = textBox1!.Text;  //記錄當(dāng)前文本框中的值
            if (BackspaceStr != "0")            //如果值不為零
            {
                string ToAbs = Math.Abs(Convert.ToDouble(BackspaceStr)).ToString();//獲取該值的絕對值
                if ((BackspaceStr.Length == 1) || (ToAbs.Length == 1))//如果當(dāng)前文本框中只有一個數(shù)值
                {
                    textBox1.Text = "0";        //將文本框清零
                }
                else { textBox1.Text = BackspaceStr[..^1]; }//刪除指定的值
                Value_1 = textBox1.Text;        //顯示刪除后的結(jié)果
            }
        }
        /// <summary>
        /// 非小數(shù)點則為真
        /// </summary>
        public void DecimalPoint()
        {
            if (!textBox1!.Text.Contains('.'))
                IsNotDecimalPoint = true;
            else
                IsNotDecimalPoint = false;
        }
    }
}

2.生成效果

到此這篇關(guān)于C#使用分部類設(shè)計實現(xiàn)一個計算器的文章就介紹到這了,更多相關(guān)C#分部類實現(xiàn)計算器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C#中字符串與字節(jié)數(shù)組的轉(zhuǎn)換方式

    C#中字符串與字節(jié)數(shù)組的轉(zhuǎn)換方式

    這篇文章介紹了C#中字符串與字節(jié)數(shù)組的轉(zhuǎn)換方式,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • C#單例模式(Singleton Pattern)詳解

    C#單例模式(Singleton Pattern)詳解

    這篇文章主要為大家詳細介紹了C#單例模式Singleton Pattern的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • C#生成帶logo的二維碼

    C#生成帶logo的二維碼

    帶logo的二維碼生成分為兩步驟:首先根據(jù)輸入的內(nèi)容生成二維碼圖片,然后讀取本地的logo圖片,通過圖片處理生成帶logo的二維碼。本文對此進行介紹,具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • C#使用log4net結(jié)合sqlite數(shù)據(jù)庫實現(xiàn)記錄日志

    C#使用log4net結(jié)合sqlite數(shù)據(jù)庫實現(xiàn)記錄日志

    因為結(jié)構(gòu)化的數(shù)據(jù)庫存儲的日志信息,可以寫專門的軟件讀取歷史日志信息,通過各種條件篩選,可操作性極大增強,有這方面需求的開發(fā)人員可以考慮,本文給大家介紹了C#使用log4net結(jié)合sqlite數(shù)據(jù)庫記錄日志,需要的朋友可以參考下
    2024-10-10
  • c#基于opencv,開發(fā)攝像頭播放程序

    c#基于opencv,開發(fā)攝像頭播放程序

    這篇文章主要介紹了c#基于opencv,開發(fā)攝像頭播放程序的示例,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-03-03
  • C#將隱私信息(銀行賬戶,身份證號碼)中間部分特殊字符替換成*

    C#將隱私信息(銀行賬戶,身份證號碼)中間部分特殊字符替換成*

    大家在銀行交易某些業(yè)務(wù)時,都可以看到無論是身份證、銀行賬號中間部分都是用*號替換的,下面這篇文章主要介紹C#將隱私信息(銀行賬戶,身份證號碼)中間部分特殊字符替換成*的相關(guān)資料,需要的朋友可以參考下
    2015-08-08
  • 利用C#與PLC通信實現(xiàn)設(shè)備遠程控制與管理

    利用C#與PLC通信實現(xiàn)設(shè)備遠程控制與管理

    PLC是工業(yè)自動化中用于控制機械設(shè)備、生產(chǎn)線等的核心設(shè)備,通過與PLC的通信,我們可以實現(xiàn)設(shè)備的遠程監(jiān)控、數(shù)據(jù)采集等功能,C#作為一種現(xiàn)代化的編程語言,能夠非常方便地與PLC進行通信,本文將介紹如何利用C#與PLC進行通信,并實現(xiàn)設(shè)備的遠程控制與管理
    2025-02-02
  • C#集合之列表的用法

    C#集合之列表的用法

    這篇文章介紹了C#集合之列表的用法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • DataGridView帶圖標(biāo)的單元格實現(xiàn)代碼

    DataGridView帶圖標(biāo)的單元格實現(xiàn)代碼

    這篇文章主要為大家詳細介紹了DataGridView帶圖標(biāo)的單元格的實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • C#中的TemplateMethod模式問題分析

    C#中的TemplateMethod模式問題分析

    這篇文章主要介紹了C#中的TemplateMethod模式,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06

最新評論