C#分部类、分割类的用法,及用分割类设计一个计算器

2024-02-20 05:04

本文主要是介绍C#分部类、分割类的用法,及用分割类设计一个计算器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

一、涉及到的知识点

1.分部类

2.分部类主要应用在以下两个方面

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

4.事件处理程序

5.Math.Ceiling方法

6.Text.Contains()     

7.pictureBox.Tag属性

二、实例 

1.源码

2.生成效果


        在开发一些大型项目或者特殊部署时,可能需要把一个类、结构或接口放在几个文件中来分别进行处理。等到编译时,再自动把它们整合起来,这时就用到了分部类。 

        分部类相当于将一个会计部门(类、结构或接口)分成两个部门,这两个部门可以单独对公司各部门的账目进行审核,而在繁忙时期,两个部门也可以相互调动人员(这里的人员相当于类中的方法、属性、变量等),或是合成一个整体进行工作 

一、涉及到的知识点

1.分部类

        分部类是C#4.5中的一个新特性,它的出现使得程序的结构更加合理,代码组织更加紧密。开发人员可以将类、结构或接口的定义拆分到两个或多个源文件中,每个源文件包含类定义的一部分,编译应用程序时,VS会把所有部分组合起来,这样的类被称为分部类。

        定义分部类时需要使用partial关键字,分部类的每个部分都必须包含一个partial关键字,并且其声明必须与其他部分位于同一命名空间。使用分部类时,要成为同一类型的各个部分的所有分部类型定义都必须在同一程序集和同一模块(.exe或.dll文件)中进行定义,分部类定义不能跨越多个模块。使用分部类时,各个部分必须具有相同的可访问性,如public、private等。

2.分部类主要应用在以下两个方面

  • 当项目比较庞大时,使用分部类可以拆分一个类至几个文件中,这样可以使不同的开发人员同时进行工作,提高了工作效率。
  • 使用自动生成的文件源时,无须重新创建源文件即可将代码添加到类中。VS在创建Windows窗体和Web服务包装代码等时都使用此方法。

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

        C#编码规范中规定,一个类中的代码最好不要超过500行,但是,如果实际开发中,确实需要在一个类中包含超过500行以上的代码时,该怎么办呢?这时可以使用分部类将该类分成几部分,其中每个部分包含一部分实现代码。

        例如,本例中,把一个public class Account类,用partial关键字,分割成两个public partial class Account类。

//分部类
//分割类的用法
namespace _124
{public partial class Account{#region 执行两个数的加法运算/// <summary>/// 执行两个数的加法运算/// </summary>/// <param name="Former">加数</param>/// <param name="After">被加数</param>/// <returns>返回相加后的结果</returns>public static double Addition(double Former, double After){return Former + After;}#endregion#region 执行两个数的减法运算/// <summary>/// 执行两个数的减法运算/// </summary>/// <param name="Former">减数</param>/// <param name="After">被减数</param>/// <returns>返回相减后的结果</returns>public static double Subtration(double Former, double After){return Former - After;}#endregion#region 执行两个数的乘法运算/// <summary>/// 执行两个数的乘法运算/// </summary>/// <param name="Former">乘数</param>/// <param name="After">被乘数</param>/// <returns>返回相乘后的结果</returns>public static double Multiplication(double Former, double After){return Former * After;}#endregion#region 执行两个数的除法运算/// <summary>/// 执行两个数的除法运算/// </summary>/// <param name="Former">除数</param>/// <param name="After">被除数</param>/// <returns>返回相除后的结果</returns>public static double Division(double Former, double After){if (After == 0){MessageBox.Show("被除数不能为0。");return 0;}return Former / After;}#endregion}public partial class Account{/// <summary>/// 计算一个数的倒数/// </summary>/// <param name="num">数据</param>/// <returns>返回倒数值</returns>public static double Reciprocal(double num){if (num == 0){MessageBox.Show("分母不能为0。");return 0;}return 1 / num;}/// <summary>/// 计算一个数的开方/// </summary>/// <param name="num">数据</param>/// <returns>返回开方后的值</returns>public static double SquareRoot(double num){if (num <= 0){MessageBox.Show("负数不能开方。");return 0;}return Math.Sqrt(num);}/// <summary>/// 求一个数的相反数/// </summary>/// <param name="num">数据</param>/// <returns>相反数</returns>public static double Opposition(double num){return -num;}/// <summary>/// 一个数的百分比/// </summary>/// <param name="num">数据</param>/// <returns>返回百分比</returns>public static double Percentage(double num){return num / 100;}}
}

4.事件处理程序

        在C#编程中,事件处理程序是一个方法,它用于响应特定事件的发生。例如,当一个按钮被单击时,可以使用事件处理程序来响应这个事件并执行相应的代码。例如:

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

    在这个例子中,pictureBox22控件的Click事件被添加了一个事件处理程序,即pictureBox21_Click方法。当pictureBox22控件被单击时,pictureBox21_Click方法将被执行。这样就可以在pictureBox22控件被单击时,响应这个事件并执行相应的代码。

        这样的设计,可以集中处理功能类似的事件,极大地减少代码量,并且代码更易读、更易维护。

5.Math.Ceiling方法

        Math.Ceiling是 C# 中的一个方法,用于将一个浮点数向上取整到最接近的整数。如果浮点数已经是整数,则该方法不会对其做任何修改。
        例如,Math.Ceiling(3.14)的结果是4,因为 3.14 最接近的整数是4。Math.Ceiling(5.0)的结果是5,因为 5.0 已经是一个整数。
        需要注意的是,如果浮点数是负数,则Math.Ceiling方法会将其向上取整到最接近的负整数。例如,Math.Ceiling(-3.14)的结果是-3,因为-3.14最接近的负整数是-3。   

6.Text.Contains()     

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

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

        Text.Contains()方法接受一个字符串参数,用于指定要查找的子字符串。它还接受一个可选的StringComparison枚举值参数,用于指定字符串比较的类型。如果不指定这个参数,则默认使用StringComparison.CurrentCulture,它使用当前文化来比较字符串。

if (!textBox1!.Text.Contains('.', StringComparison.CurrentCulture))
//等价语句
if (!textBox1!.Text.Contains('.'))

7.pictureBox.Tag属性

        设置pictureBox的Tag属性的字符串内容=该按键的功能,比如,设置按键CE的Tag属性为"CE"。其它按键属性的设置以此类推。

// 
// 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();

         设置pictureBox的Tag属性的用途是当触发按钮事件时,把该按钮的Tag属性作为Switch表达式的参数列表,从而响应按钮事件。例如:

private void PictureBox21_Click(object? sender, EventArgs e)
{ButtonTag = (sender as PictureBox)!.Tag!.ToString()!;//获取当前按钮的标识switch (ButtonTag){//点击到数字键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;    //计算结果//C 则是 "Clear" 的简称,它用于清除计算器中的所有数据,包括存储器和寄存器的内容。case "C":{Value_1 = "";Value_2 = "";OperatorType = "";textBox1!.Text = "0";break;}//CE 是 "Clear Entry" 的缩写,它的主要作用是清除当前正在输入的数字或者运算符。case "CE": textBox1!.Text = "0"; Value_1 = ""; break;default: Backspace(); break;     /*"Back":*/}
}

二、实例 

        本实例将使用分部类制作一个计算器,其中主要是用分部类来分别记录计算器的计算方法,如将实现加、减、乘和除的方法放在一个分部类中,而将实现正负、开方、百分比和倒数的方法放在另一个分部类中。

1.源码

// 使用分部类实现多种计算方法
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 = "";       //操作数1public string Value_2 = "";       //操作数2public string OperatorType = "";  //运算符种类string ButtonTag = "";            //记录当前输入的键值bool isnum = false;               //判断输入的是计算的那个值,他就是一个开关,按下运算符号时翻转,按下等号后,要复原bool IsNotDecimalPoint = false;   //是否包含小数点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/// 根据键值触发相应的功能/// </summary>private void PictureBox21_Click(object? sender, EventArgs e){ButtonTag = (sender as PictureBox)!.Tag!.ToString()!;//获取当前按钮的标识switch (ButtonTag){//点击到数字键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;    //计算结果//C 则是 "Clear" 的简称,它用于清除计算器中的所有数据,包括存储器和寄存器的内容。case "C":{Value_1 = "";Value_2 = "";OperatorType = "";textBox1!.Text = "0";break;}//CE 是 "Clear Entry" 的缩写,它的主要作用是清除当前正在输入的数字或者运算符。case "CE": textBox1!.Text = "0"; Value_1 = ""; break;default: Backspace(); break;     /*"Back":*/}}/// <summary>/// X,关闭窗体/// </summary>private void PictureBox24_Click(object? sender, EventArgs e){Close();}/// <summary>/// 记录当前输入的数字键的值/// </summary>/// <param name="num">键值</param>public void ReadNumber(string num){if (IsNotDecimalPoint){if (textBox1!.Text == "0")textBox1.Text = "0.";elsetextBox1.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;    //等待连续计算}}/// <summary>/// +-*/%计算/// </summary>/// <param name="n"></param>public void Calculation(string n){ArgumentNullException.ThrowIfNull(n);double tem_v = 0;               //记录计算后的结果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))         //如果计算结果为整数对结果进行取整{textBox1!.Text = Convert.ToInt64(tem_v).ToString();}else{textBox1!.Text = tem_v.ToString();    //以双精度进行显示}Value_1 = textBox1.Text;                  //等待连续计算Value_2 = "";}/// <summary>/// 辅助计算/// </summary>/// <param name="str"></param>public void AuxiliaryCalculation(string str){double tem_v = 0;                        //记录计算结果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))       //如果计算结果为整数对结果进行取整{textBox1!.Text = Convert.ToInt64(tem_v).ToString();}else{textBox1!.Text = tem_v.ToString();  //以双精度进行显示}Value_1 = textBox1.Text;                //等待连续计算Value_2 = "";}/// <summary>/// 删除输入的值/// </summary>public void Backspace(){var BackspaceStr = textBox1!.Text;  //记录当前文本框中的值if (BackspaceStr != "0")            //如果值不为零{string ToAbs = Math.Abs(Convert.ToDouble(BackspaceStr)).ToString();//获取该值的绝对值if ((BackspaceStr.Length == 1) || (ToAbs.Length == 1))//如果当前文本框中只有一个数值{textBox1.Text = "0";        //将文本框清零}else { textBox1.Text = BackspaceStr[..^1]; }//删除指定的值Value_1 = textBox1.Text;        //显示删除后的结果}}/// <summary>/// 非小数点则为真/// </summary>public void DecimalPoint(){if (!textBox1!.Text.Contains('.'))IsNotDecimalPoint = true;elseIsNotDecimalPoint = false;}}
}

2.生成效果

 

这篇关于C#分部类、分割类的用法,及用分割类设计一个计算器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/727122

相关文章

不懂推荐算法也能设计推荐系统

本文以商业化应用推荐为例,告诉我们不懂推荐算法的产品,也能从产品侧出发, 设计出一款不错的推荐系统。 相信很多新手产品,看到算法二字,多是懵圈的。 什么排序算法、最短路径等都是相对传统的算法(注:传统是指科班出身的产品都会接触过)。但对于推荐算法,多数产品对着网上搜到的资源,都会无从下手。特别当某些推荐算法 和 “AI”扯上关系后,更是加大了理解的难度。 但,不了解推荐算法,就无法做推荐系

2. c#从不同cs的文件调用函数

1.文件目录如下: 2. Program.cs文件的主函数如下 using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using System.Windows.Forms;namespace datasAnalysis{internal static

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount

用命令行的方式启动.netcore webapi

用命令行的方式启动.netcore web项目 进入指定的项目文件夹,比如我发布后的代码放在下面文件夹中 在此地址栏中输入“cmd”,打开命令提示符,进入到发布代码目录 命令行启动.netcore项目的命令为:  dotnet 项目启动文件.dll --urls="http://*:对外端口" --ip="本机ip" --port=项目内部端口 例: dotnet Imagine.M

怎么让1台电脑共享给7人同时流畅设计

在当今的创意设计与数字内容生产领域,图形工作站以其强大的计算能力、专业的图形处理能力和稳定的系统性能,成为了众多设计师、动画师、视频编辑师等创意工作者的必备工具。 设计团队面临资源有限,比如只有一台高性能电脑时,如何高效地让七人同时流畅地进行设计工作,便成为了一个亟待解决的问题。 一、硬件升级与配置 1.高性能处理器(CPU):选择多核、高线程的处理器,例如Intel的至强系列或AMD的Ry

基于51单片机的自动转向修复系统的设计与实现

文章目录 前言资料获取设计介绍功能介绍设计清单具体实现截图参考文献设计获取 前言 💗博主介绍:✌全网粉丝10W+,CSDN特邀作者、博客专家、CSDN新星计划导师,一名热衷于单片机技术探索与分享的博主、专注于 精通51/STM32/MSP430/AVR等单片机设计 主要对象是咱们电子相关专业的大学生,希望您们都共创辉煌!✌💗 👇🏻 精彩专栏 推荐订阅👇🏻 单片机

bytes.split的用法和注意事项

当然,我很乐意详细介绍 bytes.Split 的用法和注意事项。这个函数是 Go 标准库中 bytes 包的一个重要组成部分,用于分割字节切片。 基本用法 bytes.Split 的函数签名如下: func Split(s, sep []byte) [][]byte s 是要分割的字节切片sep 是用作分隔符的字节切片返回值是一个二维字节切片,包含分割后的结果 基本使用示例: pa

SprinBoot+Vue网络商城海鲜市场的设计与实现

目录 1 项目介绍2 项目截图3 核心代码3.1 Controller3.2 Service3.3 Dao3.4 application.yml3.5 SpringbootApplication3.5 Vue 4 数据库表设计5 文档参考6 计算机毕设选题推荐7 源码获取 1 项目介绍 博主个人介绍:CSDN认证博客专家,CSDN平台Java领域优质创作者,全网30w+

单片机毕业设计基于单片机的智能门禁系统的设计与实现

文章目录 前言资料获取设计介绍功能介绍程序代码部分参考 设计清单具体实现截图参考文献设计获取 前言 💗博主介绍:✌全网粉丝10W+,CSDN特邀作者、博客专家、CSDN新星计划导师,一名热衷于单片机技术探索与分享的博主、专注于 精通51/STM32/MSP430/AVR等单片机设计 主要对象是咱们电子相关专业的大学生,希望您们都共创辉煌!✌💗 👇🏻 精彩专栏 推荐订

Spring的设计⽬标——《Spring技术内幕》

读《Spring技术内幕》第二版,计文柯著。 如果我们要简要地描述Spring的设计⽬标,可以这么说,Spring为开发者提供的是⼀个⼀站式的轻量级应⽤开发框架(平台)。 作为平台,Spring抽象了我们在 许多应⽤开发中遇到的共性问题;同时,作为⼀个轻量级的应⽤开发框架,Spring和传统的J2EE开发相⽐,有其⾃⾝的特点。 通过这些⾃⾝的特点,Spring充分体现了它的设计理念:在