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

📄 form1.cs

📁 用C#做的计算器
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace calculator
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button button4;
		private System.Windows.Forms.Button button5;
		private System.Windows.Forms.Button button6;
		private System.Windows.Forms.Button button7;
		private System.Windows.Forms.Button button8;
		private System.Windows.Forms.Button button9;
		private System.Windows.Forms.Button button10;
		private System.Windows.Forms.Button button11;
		private System.Windows.Forms.Button button12;
		private System.Windows.Forms.Button button13;
		private System.Windows.Forms.Button button14;
		private System.Windows.Forms.Button button15;
		private System.Windows.Forms.Button button16;
		private System.Windows.Forms.Button button17;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.TextBox Screen;
		
		public string Number1 = "";
		public string Number2 = "";
		public bool IsReal = false;
		public bool Op_Add = false;
		public bool Op_Minus = false;
		public bool Op_Mul = false;
		public bool Op_Div = false;
		public int SelectWays = 0;				//==========
		public int NextInput = 0;

		public void TypeInNumber( string KeyType )			//0~9键值,小数点
		{
			if( SelectWays == 12 || SelectWays == 20 )
			{
				SelectWays = 0;
				Number1 = "";
				Number2 = "";
				Screen.Text = "";
				NextInput = 0;
			}
			if( SelectWays == 0 && NextInput == 0 )
			{
				Screen.Text = "";
				NextInput = -1;
			}
			else if( SelectWays == 10 && NextInput == 1 )
			{
				Screen.Text = "";
				SelectWays = 100;
				NextInput = 2;
			}
			if( Screen.Text.Length < 15 )				//限制键入数字的长度为15位以内
			{
				Screen.Text += KeyType;
			}
			if( ( SelectWays == 10 || SelectWays == 100 ) && NextInput == 2 )
			{
				Number2 = Screen.Text;
			}
		}

		public void ShowResult()					// “=”显示结果
		{		
			if( SelectWays == 100 || SelectWays == 11 )
			{
				if( Op_Div == true )
				{
					if( Number2 != "0" )
					{
						Screen.Text = Convert.ToString( Convert.ToDouble( Number1 ) / Convert.ToDouble( Number2 ) );
					}
					else
					{
						Clear();
						MessageBox.Show("0 cannot be divided!");
					}
				}
				else if( Op_Mul == true )
				{
					Screen.Text = Convert.ToString( Convert.ToDouble( Number1 ) * Convert.ToDouble( Number2 ) );
				}
				else if( Op_Minus == true )
				{
					Screen.Text = Convert.ToString( Convert.ToDouble( Number1 ) - Convert.ToDouble( Number2 ) );
				}
				else if( Op_Add == true )
				{
					Screen.Text = Convert.ToString( Convert.ToDouble( Number1 ) + Convert.ToDouble( Number2 ) );
				}
				Number1 = Screen.Text;
				if( SelectWays == 100 )
				{
					SelectWays = 12;
				}
				else if( SelectWays == 11 )
				{
					SelectWays = 10;
					NextInput = 1;
				}
			}
			else if( SelectWays == 10 )
			{
				SelectWays = 20;
				Number1 = Screen.Text;
				if( Op_Div == true )
				{
					Screen.Text = "1";
				}
				else if( Op_Mul == true )
				{
					Screen.Text = Convert.ToString( Convert.ToDouble( Screen.Text ) * Convert.ToDouble( Number1 ) );
				}
				else if( Op_Minus == true )
				{
					Screen.Text = Convert.ToString( Convert.ToDouble( Screen.Text ) - Convert.ToDouble( Number1 ) );
				}
				else if( Op_Add == true )
				{
					Screen.Text = Convert.ToString( Convert.ToDouble( Screen.Text ) + Convert.ToDouble( Number1 ) );
				}
			}
			else if( SelectWays == 20 )
			{
				if( Op_Div == true )
				{
					Screen.Text = "1";
				}
				else if( Op_Mul == true )
				{
					Screen.Text = Convert.ToString( Convert.ToDouble( Screen.Text ) * Convert.ToDouble( Number1 ) );
				}
				else if( Op_Minus == true )
				{
					Screen.Text = Convert.ToString( Convert.ToDouble( Screen.Text ) - Convert.ToDouble( Number1 ) );
				}
				else if( Op_Add == true )
				{
					Screen.Text = Convert.ToString( Convert.ToDouble( Screen.Text ) + Convert.ToDouble( Number1 ) );
				}
			}
			Number2 = "";
	//		MessageBox.Show( Convert.ToString(SelectWays) );
		}

		public void Operate( string op )		//四则运算
		{
			if( SelectWays == 0 || SelectWays == 12 || SelectWays == 20 )
			{
				SelectWays = 10;
				Number1 = Screen.Text;
				NextInput = 1;
			}
			else if( SelectWays == 100 )
			{
				SelectWays = 11;
				ShowResult();
			}
			Op_Add =   false;
			Op_Minus = false;
			Op_Mul =   false;
			Op_Div =   false;
			switch( op )
			{
				case "div":
					Op_Div =   true;
					break;
				case "mul":
					Op_Mul =   true;
					break;
				case "minus":
					Op_Minus = true;
					break;
				case "add":
					Op_Add =   true;
					break;
			}
		}
		public void Clear()			//清空
		{
			Screen.Text = "0";
			Number1 = "";
			Number2 = "";
			IsReal = false;
			Op_Add =   false;
			Op_Minus = false;
			Op_Mul =   false;
			Op_Div =   false;
			SelectWays = 0;
			NextInput = 0;
		}
		public Form1()
		{
			//
			// 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.Screen = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.button4 = new System.Windows.Forms.Button();
			this.button5 = new System.Windows.Forms.Button();
			this.button6 = new System.Windows.Forms.Button();
			this.button7 = new System.Windows.Forms.Button();
			this.button8 = new System.Windows.Forms.Button();
			this.button9 = new System.Windows.Forms.Button();
			this.button10 = new System.Windows.Forms.Button();
			this.button11 = new System.Windows.Forms.Button();
			this.button12 = new System.Windows.Forms.Button();
			this.button13 = new System.Windows.Forms.Button();
			this.button14 = new System.Windows.Forms.Button();
			this.button15 = new System.Windows.Forms.Button();
			this.button16 = new System.Windows.Forms.Button();
			this.button17 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// Screen
			// 
			this.Screen.BackColor = System.Drawing.SystemColors.ControlLightLight;
			this.Screen.Enabled = false;
			this.Screen.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.Screen.ForeColor = System.Drawing.SystemColors.WindowFrame;
			this.Screen.Location = new System.Drawing.Point(8, 8);
			this.Screen.MaxLength = 0;
			this.Screen.Name = "Screen";
			this.Screen.Size = new System.Drawing.Size(208, 29);
			this.Screen.TabIndex = 0;
			this.Screen.TabStop = false;
			this.Screen.Text = "0";
			this.Screen.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			this.Screen.WordWrap = false;
			// 
			// button1
			// 
			this.button1.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.button1.ForeColor = System.Drawing.SystemColors.ControlLightLight;
			this.button1.Location = new System.Drawing.Point(8, 48);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(40, 32);
			this.button1.TabIndex = 6;
			this.button1.Text = "7";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.button2.ForeColor = System.Drawing.SystemColors.ControlLightLight;
			this.button2.Location = new System.Drawing.Point(64, 48);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(40, 32);
			this.button2.TabIndex = 7;
			this.button2.Text = "8";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button3
			// 
			this.button3.ForeColor = System.Drawing.SystemColors.HighlightText;
			this.button3.Location = new System.Drawing.Point(120, 48);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(40, 32);
			this.button3.TabIndex = 8;
			this.button3.Text = "9";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// button4

⌨️ 快捷键说明

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