这次作业做得不好T T~

各种事情忙完之后又来不及了。。。

内容写得乱七八糟的,

界面是模拟MIUI自带计算器。

作业要求:

1. 类似WIN7简单计算器;

2. 设计UI;

3. 响应键盘输入。

上图:

代码:

Calculator.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculator
{
	public partial class Form1 : Form
	{
		public static double	Num0, Num1;
		public static bool	Dot, Flag;
		public static int	k;

		public Form1()
		{
			InitializeComponent();
		}


		private void button16_Click( object sender, EventArgs e )
		{
			string Str = Convert.ToString( textBox1.Text );
			if ( Str != "" && Str[0] != '.' && (Str[0] < '0' || Str[0] > '9') )
				button1_Click( sender, e );
			textBox1.Text += '0';
		}


		private void button13_Click( object sender, EventArgs e )
		{
			string Str = Convert.ToString( textBox1.Text ); if ( Str != "" && Str[0] != '.' && (Str[0] < '0' || Str[0] > '9') )
				button1_Click( sender, e );
			textBox1.Text += '1';
		}


		private void button14_Click( object sender, EventArgs e )
		{
			string Str = Convert.ToString( textBox1.Text ); if ( Str != "" && Str[0] != '.' && (Str[0] < '0' || Str[0] > '9') )
				button1_Click( sender, e );
			textBox1.Text += '2';
		}


		private void button15_Click( object sender, EventArgs e )
		{
			string Str = Convert.ToString( textBox1.Text ); if ( Str != "" && Str[0] != '.' && (Str[0] < '0' || Str[0] > '9') )
				button1_Click( sender, e );
			textBox1.Text += '3';
		}


		private void button9_Click( object sender, EventArgs e )
		{
			string Str = Convert.ToString( textBox1.Text ); if ( Str != "" && Str[0] != '.' && (Str[0] < '0' || Str[0] > '9') )
				button1_Click( sender, e );
			textBox1.Text += '4';
		}


		private void button10_Click( object sender, EventArgs e )
		{
			string Str = Convert.ToString( textBox1.Text ); if ( Str != "" && Str[0] != '.' && (Str[0] < '0' || Str[0] > '9') )
				button1_Click( sender, e );
			textBox1.Text += '5';
		}


		private void button11_Click( object sender, EventArgs e )
		{
			string Str = Convert.ToString( textBox1.Text ); if ( Str != "" && Str[0] != '.' && (Str[0] < '0' || Str[0] > '9') )
				button1_Click( sender, e );
			textBox1.Text += '6';
		}


		private void button5_Click( object sender, EventArgs e )
		{
			string Str = Convert.ToString( textBox1.Text ); if ( Str != "" && Str[0] != '.' && (Str[0] < '0' || Str[0] > '9') )
				button1_Click( sender, e );
			textBox1.Text += '7';
		}


		private void button6_Click( object sender, EventArgs e )
		{
			string Str = Convert.ToString( textBox1.Text ); if ( Str != "" && Str[0] != '.' && (Str[0] < '0' || Str[0] > '9') )
				button1_Click( sender, e );
			textBox1.Text += '8';
		}


		private void button7_Click( object sender, EventArgs e )
		{
			string Str = Convert.ToString( textBox1.Text ); if ( Str != "" && Str[0] != '.' && (Str[0] < '0' || Str[0] > '9') )
				button1_Click( sender, e );
			textBox1.Text += '9';
		}


		private void button2_Click( object sender, EventArgs e )
		{
			if ( textBox1.Text == "" )
				return;
			string Str = Convert.ToString( textBox1.Text ); if ( Str[0] != '.' && (Str[0] < '0' || Str[0] > '9') )
				button1_Click( sender, e );
			if ( textBox1.Text == "" )
				return;
			textBox1.Text = textBox1.Text.Remove( textBox1.TextLength - 1 );
		}


		private void button1_Click( object sender, EventArgs e )
		{
			textBox1.Text = ""; Dot = false; Flag = false; k = 0;
		}


		private void Form1_Load( object sender, EventArgs e )
		{
			textBox1.Text = ""; Dot = false; Flag = false; k = 0;
		}


		private void button17_Click( object sender, EventArgs e )
		{
			string Str = Convert.ToString( textBox1.Text ); if ( Str[0] != '.' && (Str[0] < '0' || Str[0] > '9') )
				button1_Click( sender, e );
			if ( Dot == false )
			{
				textBox1.Text += '.'; Dot = true;
			}
		}


		private void button3_Click( object sender, EventArgs e )
		{
			if ( k != 0 )
				button18_Click( sender, e );
			k = 1; if ( textBox1.Text == "" )
				return;
			string Str = Convert.ToString( textBox1.Text ); if ( Str[0] == '.' && (Str[0] < '0' || Str[0] > '9') )
			{
				button1_Click( sender, e ); return;
			}
			Num0 = Convert.ToDouble( textBox1.Text ); textBox1.Text = ""; Dot = false; Flag = true;
		}


		private void button4_Click( object sender, EventArgs e )
		{
			if ( k != 0 )
				button18_Click( sender, e );
			k = 2; if ( textBox1.Text == "" )
				return;
			string Str = Convert.ToString( textBox1.Text ); if ( Str[0] == '.' && (Str[0] < '0' || Str[0] > '9') )
			{
				button1_Click( sender, e ); return;
			}
			Num0 = Convert.ToDouble( textBox1.Text ); textBox1.Text = ""; Flag = true;
		}


		private void button8_Click( object sender, EventArgs e )
		{
			if ( k != 0 )
				button18_Click( sender, e );
			k = 3; if ( textBox1.Text == "" )
				return;
			string Str = Convert.ToString( textBox1.Text ); if ( Str[0] == '.' && (Str[0] < '0' || Str[0] > '9') )
			{
				button1_Click( sender, e ); return;
			}
			Num0 = Convert.ToDouble( textBox1.Text ); textBox1.Text = ""; Flag = true;
		}


		private void button12_Click( object sender, EventArgs e )
		{
			if ( k != 0 )
				button18_Click( sender, e );
			k = 4; if ( textBox1.Text == "" )
				return;
			string Str = Convert.ToString( textBox1.Text ); if ( Str[0] == '.' && (Str[0] < '0' || Str[0] > '9') )
			{
				button1_Click( sender, e ); return;
			}
			Num0 = Convert.ToDouble( textBox1.Text ); textBox1.Text = ""; Flag = true;
		}


		private void button18_Click( object sender, EventArgs e )
		{
			if ( Flag == false )
				return;
			if ( k == 0 )
				return;
			if ( textBox1.Text == "" )
				return;
			string Str = Convert.ToString( textBox1.Text ); if ( Str[0] != '.' && (Str[0] < '0' || Str[0] > '9') )
				return;
			Num1 = Convert.ToDouble( textBox1.Text ); double Ans = 0.0; if ( k == 1 )
				Ans = Num0 / Num1;
			if ( k == 2 )
				Ans = Num0 * Num1;
			if ( k == 3 )
				Ans = Num0 - Num1;
			if ( k == 4 )
				Ans = Num0 + Num1;
			textBox1.Text = Convert.ToString( Ans ); Num0 = Ans;
		}


		private void Form1_KeyPress( object sender, KeyPressEventArgs e )
		{
			if ( e.KeyChar == '0' )
				button16_Click( sender, e );
			if ( e.KeyChar == '1' )
				button13_Click( sender, e );
			if ( e.KeyChar == '2' )
				button14_Click( sender, e );
			if ( e.KeyChar == '3' )
				button15_Click( sender, e );
			if ( e.KeyChar == '4' )
				button9_Click( sender, e );
			if ( e.KeyChar == '5' )
				button10_Click( sender, e );
			if ( e.KeyChar == '6' )
				button11_Click( sender, e );
			if ( e.KeyChar == '7' )
				button5_Click( sender, e );
			if ( e.KeyChar == '8' )
				button6_Click( sender, e );
			if ( e.KeyChar == '9' )
				button7_Click( sender, e );
			if ( e.KeyChar == '.' )
				button17_Click( sender, e );
			if ( e.KeyChar == '+' )
				button12_Click( sender, e );
			if ( e.KeyChar == '-' )
				button8_Click( sender, e );
			if ( e.KeyChar == '*' )
				button4_Click( sender, e );
			if ( e.KeyChar == '/' )
				button3_Click( sender, e );
			if ( e.KeyChar == '\n' )
				button18_Click( sender, e );
			if ( e.KeyChar == '\b' )
				button2_Click( sender, e );
			if ( e.KeyChar == (char) (27) )
				button1_Click( sender, e );
		}


		private void button18_Leave( object sender, EventArgs e )
		{
			button18.Focus();
		}
	}
}

转载保留版权:http://haipz.com/blog/i/701 - 海胖博客