⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainform.cs

📁 设计模式——访问者模式 Visitor模式允许我们在不改动原有结构的基础之上不断增加新的功能。 ICumulation接口定义了方法Cumulate()
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Visitor
{
	public enum Arithmetic
	{
       General,GaoSi
	}
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox txtUpperLimit;
		private System.Windows.Forms.Label labGeneral;
		private System.Windows.Forms.Label labGaoSi;
		private System.Windows.Forms.Label labGeneralTime;
		private System.Windows.Forms.Label labGaoSiTime;
		private System.Windows.Forms.Button btnCalculate;
		private System.Windows.Forms.Label labGernealAverage;
		private System.Windows.Forms.Label labGeneralPower;
		private System.Windows.Forms.Label labGaoSiAverage;
		private System.Windows.Forms.Label labGaoSiPower;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public MainForm()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.txtUpperLimit = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.labGeneral = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.labGaoSi = new System.Windows.Forms.Label();
			this.labGeneralTime = new System.Windows.Forms.Label();
			this.labGaoSiTime = new System.Windows.Forms.Label();
			this.btnCalculate = new System.Windows.Forms.Button();
			this.labGernealAverage = new System.Windows.Forms.Label();
			this.labGeneralPower = new System.Windows.Forms.Label();
			this.labGaoSiAverage = new System.Windows.Forms.Label();
			this.labGaoSiPower = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 64);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(136, 23);
			this.label1.TabIndex = 0;
			this.label1.Text = "General arithmetic";
			// 
			// txtUpperLimit
			// 
			this.txtUpperLimit.Location = new System.Drawing.Point(112, 16);
			this.txtUpperLimit.Name = "txtUpperLimit";
			this.txtUpperLimit.Size = new System.Drawing.Size(216, 21);
			this.txtUpperLimit.TabIndex = 1;
			this.txtUpperLimit.Text = "";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 160);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(136, 23);
			this.label2.TabIndex = 2;
			this.label2.Text = "Gao Si arithmetic";
			// 
			// labGeneral
			// 
			this.labGeneral.Location = new System.Drawing.Point(160, 64);
			this.labGeneral.Name = "labGeneral";
			this.labGeneral.Size = new System.Drawing.Size(256, 16);
			this.labGeneral.TabIndex = 3;
			this.labGeneral.Text = "0";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(16, 16);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(72, 16);
			this.label4.TabIndex = 4;
			this.label4.Text = "Upper Limit";
			// 
			// labGaoSi
			// 
			this.labGaoSi.Location = new System.Drawing.Point(160, 160);
			this.labGaoSi.Name = "labGaoSi";
			this.labGaoSi.Size = new System.Drawing.Size(256, 16);
			this.labGaoSi.TabIndex = 5;
			this.labGaoSi.Text = "0";
			// 
			// labGeneralTime
			// 
			this.labGeneralTime.Location = new System.Drawing.Point(160, 136);
			this.labGeneralTime.Name = "labGeneralTime";
			this.labGeneralTime.Size = new System.Drawing.Size(256, 16);
			this.labGeneralTime.TabIndex = 6;
			this.labGeneralTime.Text = "0";
			// 
			// labGaoSiTime
			// 
			this.labGaoSiTime.Location = new System.Drawing.Point(160, 232);
			this.labGaoSiTime.Name = "labGaoSiTime";
			this.labGaoSiTime.Size = new System.Drawing.Size(256, 16);
			this.labGaoSiTime.TabIndex = 7;
			this.labGaoSiTime.Text = "0";
			// 
			// btnCalculate
			// 
			this.btnCalculate.Location = new System.Drawing.Point(336, 16);
			this.btnCalculate.Name = "btnCalculate";
			this.btnCalculate.TabIndex = 8;
			this.btnCalculate.Text = "calculate";
			this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click);
			// 
			// labGernealAverage
			// 
			this.labGernealAverage.Location = new System.Drawing.Point(160, 88);
			this.labGernealAverage.Name = "labGernealAverage";
			this.labGernealAverage.Size = new System.Drawing.Size(256, 16);
			this.labGernealAverage.TabIndex = 9;
			this.labGernealAverage.Text = "0";
			// 
			// labGeneralPower
			// 
			this.labGeneralPower.Location = new System.Drawing.Point(160, 112);
			this.labGeneralPower.Name = "labGeneralPower";
			this.labGeneralPower.Size = new System.Drawing.Size(256, 16);
			this.labGeneralPower.TabIndex = 10;
			this.labGeneralPower.Text = "0";
			// 
			// labGaoSiAverage
			// 
			this.labGaoSiAverage.Location = new System.Drawing.Point(160, 184);
			this.labGaoSiAverage.Name = "labGaoSiAverage";
			this.labGaoSiAverage.Size = new System.Drawing.Size(256, 16);
			this.labGaoSiAverage.TabIndex = 11;
			this.labGaoSiAverage.Text = "0";
			// 
			// labGaoSiPower
			// 
			this.labGaoSiPower.Location = new System.Drawing.Point(160, 208);
			this.labGaoSiPower.Name = "labGaoSiPower";
			this.labGaoSiPower.Size = new System.Drawing.Size(256, 16);
			this.labGaoSiPower.TabIndex = 12;
			this.labGaoSiPower.Text = "0";
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(440, 269);
			this.Controls.Add(this.labGaoSiPower);
			this.Controls.Add(this.labGaoSiAverage);
			this.Controls.Add(this.labGeneralPower);
			this.Controls.Add(this.labGernealAverage);
			this.Controls.Add(this.btnCalculate);
			this.Controls.Add(this.labGaoSiTime);
			this.Controls.Add(this.labGeneralTime);
			this.Controls.Add(this.labGaoSi);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.labGeneral);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.txtUpperLimit);
			this.Controls.Add(this.label1);
			this.Name = "MainForm";
			this.Text = "AddUp";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new MainForm());
		}
//      private IAddUp arithmetic=null;
//		private IAddUp GetArithmetic(Arithmetic ari)//Sington pattern
//		{
//			if(arithmetic==null)
//			{
//				if(ari==Arithmetic.General)
//				{
//					arithmetic=new Gereral();
//				}
//				else
//				{
//					arithmetic=new GaoSi();
//				}
//			}
//			return arithmetic;
//		}
		private ICumulation _gereralArithmetic=new GereralArithmetic();
		private ICumulation _gaoSiArithmetic=new GaoSiArithmetic();

		//visitor
		private ICumulationVisitor _average=new AverageVisitor();
		private ICumulationVisitor _power=new PowerVisitor();

		private void btnCalculate_Click(object sender, System.EventArgs e)
		{
			long UpperLimit=0;
			if(this.txtUpperLimit.Text.Length>9)
			{
                MessageBox.Show("please input number that smaller than 1000000000");
				return;
			}
			try
			{
                UpperLimit=Convert.ToInt64(this.txtUpperLimit.Text);
			}
			catch(System.FormatException ex)
			{
                MessageBox.Show("please input number");
				return;
			}
			if(UpperLimit<=0)
			{
                MessageBox.Show("please input number that bigger than zero");
				return;
			}
			DateTime dt=DateTime.Now;
			this.labGeneral.Text=_gereralArithmetic.Cumulate(UpperLimit).ToString();
			//visitor
			this.labGernealAverage.Text=_gereralArithmetic.Accept(_average,UpperLimit).ToString();
			this.labGeneralPower.Text=_gereralArithmetic.Accept(_power,UpperLimit).ToString();
			//end visitor
			this.labGeneralTime.Text = Convert.ToString(System.DateTime.Now - dt);
			dt=DateTime.Now;
			this.labGaoSi.Text=_gaoSiArithmetic.Cumulate(UpperLimit).ToString();
			//visitor
			this.labGaoSiAverage.Text=_gaoSiArithmetic.Accept(_average,UpperLimit).ToString();
			this.labGaoSiPower.Text=_gaoSiArithmetic.Accept(_power,UpperLimit).ToString();
			//end visitor
			this.labGaoSiTime.Text = Convert.ToString(System.DateTime.Now - dt);

		}



	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -