您的位置:首页 > 其它

用.net做一个简单计算器

2005-06-24 16:06 211 查看
这是开始学习.net时候用C#做的一个简单计算器,功能比较简单,主要是当时为了学习.net这个软件环境而编写的,化了一个晚上的时间,希望对初学者有所参考。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Calc
{
/// <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.TextBox textBox1;
/// <summary>
/// 必需的设计器变量。
double num1=0,num2=0,result=0;
int type=0;
int dot_num=1;
bool is_num1=false;
bool is_dot=false;
bool is_fushu=false;
bool doted=false;
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;
private System.Windows.Forms.Button button18;
private System.Windows.Forms.Button button19;
private System.Windows.Forms.Button button20;
/// </summary>
private System.ComponentModel.Container components = null;

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

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

/// <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.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.textBox1 = new System.Windows.Forms.TextBox();
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.button18 = new System.Windows.Forms.Button();
this.button19 = new System.Windows.Forms.Button();
this.button20 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// 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.ActiveCaption;
this.button1.Location = new System.Drawing.Point(48, 64);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(40, 23);
this.button1.TabIndex = 0;
this.button1.Text = "1";
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.ActiveCaption;
this.button2.Location = new System.Drawing.Point(104, 64);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(40, 23);
this.button2.TabIndex = 1;
this.button2.Text = "2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button3.ForeColor = System.Drawing.SystemColors.ActiveCaption;
this.button3.Location = new System.Drawing.Point(152, 64);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(40, 23);
this.button3.TabIndex = 2;
this.button3.Text = "3";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button4.ForeColor = System.Drawing.SystemColors.ActiveCaption;
this.button4.Location = new System.Drawing.Point(48, 112);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(40, 23);
this.button4.TabIndex = 3;
this.button4.Text = "4";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button5.ForeColor = System.Drawing.SystemColors.ActiveCaption;
this.button5.Location = new System.Drawing.Point(104, 112);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(40, 23);
this.button5.TabIndex = 4;
this.button5.Text = "5";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button6
//
this.button6.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button6.ForeColor = System.Drawing.SystemColors.ActiveCaption;
this.button6.Location = new System.Drawing.Point(152, 112);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(40, 23);
this.button6.TabIndex = 5;
this.button6.Text = "6";
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// button7
//
this.button7.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button7.ForeColor = System.Drawing.SystemColors.ActiveCaption;
this.button7.Location = new System.Drawing.Point(48, 160);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(40, 23);
this.button7.TabIndex = 6;
this.button7.Text = "7";
this.button7.Click += new System.EventHandler(this.button7_Click);
//
// button8
//
this.button8.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button8.ForeColor = System.Drawing.SystemColors.ActiveCaption;
this.button8.Location = new System.Drawing.Point(104, 160);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(40, 23);
this.button8.TabIndex = 7;
this.button8.Text = "8";
this.button8.Click += new System.EventHandler(this.button8_Click);
//
// button9
//
this.button9.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button9.ForeColor = System.Drawing.SystemColors.ActiveCaption;
this.button9.Location = new System.Drawing.Point(152, 160);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(40, 23);
this.button9.TabIndex = 8;
this.button9.Text = "9";
this.button9.Click += new System.EventHandler(this.button9_Click);
//
// button10
//
this.button10.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button10.ForeColor = System.Drawing.SystemColors.ActiveCaption;
this.button10.Location = new System.Drawing.Point(48, 200);
this.button10.Name = "button10";
this.button10.Size = new System.Drawing.Size(40, 23);
this.button10.TabIndex = 9;
this.button10.Text = "0";
this.button10.Click += new System.EventHandler(this.button10_Click);
//
// button11
//
this.button11.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button11.ForeColor = System.Drawing.SystemColors.ActiveCaption;
this.button11.Location = new System.Drawing.Point(104, 200);
this.button11.Name = "button11";
this.button11.Size = new System.Drawing.Size(40, 23);
this.button11.TabIndex = 10;
this.button11.Text = "+/-";
this.button11.Click += new System.EventHandler(this.button11_Click);
//
// button12
//
this.button12.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button12.ForeColor = System.Drawing.SystemColors.ActiveCaption;
this.button12.Location = new System.Drawing.Point(152, 200);
this.button12.Name = "button12";
this.button12.Size = new System.Drawing.Size(40, 23);
this.button12.TabIndex = 11;
this.button12.Text = ".";
this.button12.Click += new System.EventHandler(this.button12_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(48, 32);
this.textBox1.Name = "textBox1";
this.textBox1.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.textBox1.Size = new System.Drawing.Size(248, 21);
this.textBox1.TabIndex = 12;
this.textBox1.Text = "";
this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// button13
//
this.button13.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button13.ForeColor = System.Drawing.Color.Red;
this.button13.Location = new System.Drawing.Point(208, 64);
this.button13.Name = "button13";
this.button13.Size = new System.Drawing.Size(40, 23);
this.button13.TabIndex = 13;
this.button13.Text = "+";
this.button13.Click += new System.EventHandler(this.button13_Click);
//
// button14
//
this.button14.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button14.ForeColor = System.Drawing.Color.Red;
this.button14.Location = new System.Drawing.Point(208, 112);
this.button14.Name = "button14";
this.button14.Size = new System.Drawing.Size(40, 23);
this.button14.TabIndex = 14;
this.button14.Text = "-";
this.button14.Click += new System.EventHandler(this.button14_Click);
//
// button15
//
this.button15.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button15.ForeColor = System.Drawing.Color.Red;
this.button15.Location = new System.Drawing.Point(208, 160);
this.button15.Name = "button15";
this.button15.Size = new System.Drawing.Size(40, 23);
this.button15.TabIndex = 15;
this.button15.Text = "*";
this.button15.Click += new System.EventHandler(this.button15_Click);
//
// button16
//
this.button16.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button16.ForeColor = System.Drawing.Color.Red;
this.button16.Location = new System.Drawing.Point(208, 200);
this.button16.Name = "button16";
this.button16.Size = new System.Drawing.Size(40, 23);
this.button16.TabIndex = 16;
this.button16.Text = "/";
this.button16.Click += new System.EventHandler(this.button16_Click);
//
// button17
//
this.button17.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button17.ForeColor = System.Drawing.Color.Red;
this.button17.Location = new System.Drawing.Point(256, 200);
this.button17.Name = "button17";
this.button17.Size = new System.Drawing.Size(40, 23);
this.button17.TabIndex = 17;
this.button17.Text = "=";
this.button17.Click += new System.EventHandler(this.button17_Click);
//
// button18
//
this.button18.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button18.ForeColor = System.Drawing.SystemColors.ActiveCaption;
this.button18.Location = new System.Drawing.Point(256, 160);
this.button18.Name = "button18";
this.button18.Size = new System.Drawing.Size(40, 23);
this.button18.TabIndex = 18;
this.button18.Text = "1/x";
this.button18.Click += new System.EventHandler(this.button18_Click);
//
// button19
//
this.button19.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button19.ForeColor = System.Drawing.Color.Red;
this.button19.Location = new System.Drawing.Point(256, 64);
this.button19.Name = "button19";
this.button19.Size = new System.Drawing.Size(40, 23);
this.button19.TabIndex = 19;
this.button19.Text = "CE";
this.button19.Click += new System.EventHandler(this.button19_Click);
//
// button20
//
this.button20.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button20.ForeColor = System.Drawing.SystemColors.ActiveCaption;
this.button20.Location = new System.Drawing.Point(256, 112);
this.button20.Name = "button20";
this.button20.Size = new System.Drawing.Size(40, 23);
this.button20.TabIndex = 20;
this.button20.Text = "%";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.SystemColors.ControlLight;
this.ClientSize = new System.Drawing.Size(320, 246);
this.Controls.Add(this.button20);
this.Controls.Add(this.button19);
this.Controls.Add(this.button18);
this.Controls.Add(this.button17);
this.Controls.Add(this.button16);
this.Controls.Add(this.button15);
this.Controls.Add(this.button14);
this.Controls.Add(this.button13);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button12);
this.Controls.Add(this.button11);
this.Controls.Add(this.button10);
this.Controls.Add(this.button9);
this.Controls.Add(this.button8);
this.Controls.Add(this.button7);
this.Controls.Add(this.button6);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "我的计算器";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
void func(int numb)
{
if(is_num1==false) //the num1
{
if(is_dot==true)
{
double tempNum=1;
for(int i=0;i<dot_num;i++)
{
tempNum=10*tempNum;
}
if(!is_fushu)
num1=num1+(double)numb/(double)(tempNum);
else
num1=num1-(double)numb/(double)(tempNum);
dot_num++;
}
else
{
if(!is_fushu)
num1=num1*10+numb;
else
num1=num1*10-numb;
}
textBox1.Text=num1.ToString();
}
else //the num2
{
if(is_dot==true)
{
double tempNum=1;
for(int i=0;i<dot_num;i++)
{
tempNum=10*tempNum;
}
if(!is_fushu)
num2=num2+(double)numb/(double)(tempNum);
else
num2=num2-(double)numb/(double)(tempNum);
dot_num++;
}
else
{
if(!is_fushu)
num2=num2*10+numb;
else
num2=num2*10-numb;
}
textBox1.Text=num2.ToString();
}
}
void calType()
{
is_num1=true;
dot_num=1;
is_dot=false;
textBox1.Text=".0";
is_fushu=false;
doted=false;
}
[STAThread]
static void Main()
{
Application.Run(new Form1());

}

private void button1_Click(object sender, System.EventArgs e)
{
func(1);
}

private void button2_Click(object sender, System.EventArgs e)
{
func(2);
}

private void button12_Click(object sender, System.EventArgs e) //dot
{
is_dot=true;
if(doted==false)
{
doted=true;
dot_num=1;
}
// else
// dot_num++;
/*if(is_num1)
{
textBox1.Text=Convert.ToString(num2)+".";
}
else
{
textBox1.Text=Convert.ToString(num1)+".";
}*/
}

private void button13_Click(object sender, System.EventArgs e) //"+"
{
type=1;
calType();
}

private void button14_Click(object sender, System.EventArgs e) //"-"
{
type=2;
calType();
}

private void button15_Click(object sender, System.EventArgs e) //"*
{
type=3;
calType();
}

private void button16_Click(object sender, System.EventArgs e) //"/"
{
type=4;
calType();
}

private void button17_Click(object sender, System.EventArgs e) //the "="
{
switch(type)
{
case 1:result=num1+num2;
break;
case 2:result=num1-num2;
break;
case 3:result=num1*num2;
break;
case 4:result=num1/num2;
break;
}
textBox1.Text=result.ToString();
num1=0;
num2=0;
is_num1=false;
dot_num=1;
is_dot=false;
is_fushu=false;
doted=false;
}

private void button3_Click(object sender, System.EventArgs e)
{
func(3);
}

private void button4_Click(object sender, System.EventArgs e)
{
func(4);
}

private void button5_Click(object sender, System.EventArgs e)
{
func(5);
}

private void button6_Click(object sender, System.EventArgs e)
{
func(6);
}

private void button7_Click(object sender, System.EventArgs e)
{
func(7);
}

private void button8_Click(object sender, System.EventArgs e)
{
func(8);
}

private void button9_Click(object sender, System.EventArgs e)
{
func(9);
}

private void button10_Click(object sender, System.EventArgs e)
{
func(0);
}

private void button19_Click(object sender, System.EventArgs e) //the "CE"
{
if(is_num1)
num2=0;
else
{
num1=0;
num2=0;
}
textBox1.Text="0.";
is_dot=false;
dot_num=1;
is_fushu=false;
doted=false;
}

private void textBox1_TextChanged(object sender, System.EventArgs e)
{

}

private void button11_Click(object sender, System.EventArgs e)
{
if(is_num1)
{
num2=-num2;
textBox1.Text=Convert.ToString(num2);
}
else
{
num1=-num1;
textBox1.Text=Convert.ToString(num1);
}
is_fushu=!is_fushu;
}

private void button18_Click(object sender, System.EventArgs e)
{
if(is_num1)
{
num2=1/num2;
textBox1.Text=Convert.ToString(num2);
}
else
{
num1=1/num1;
textBox1.Text=Convert.ToString(num1);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: