首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

关于小弟我编写的一个小型计算器

2011-12-19 
关于我编写的一个小型计算器。我编了一个小型计算器,只会加减乘除,但是,我怎么越看越觉的别扭呢?希望大家帮

关于我编写的一个小型计算器。
我编了一个小型计算器,只会加减乘除,但是,我怎么越看越觉的别扭呢?希望大家帮我看看,特别是那些if,是不是用的太多了啊??第一次编写,希望大家别见怪哈。。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
  public partial class Form1 : Form
  {
  double z;
  double a;
  double save;
  public Form1()
  {
  InitializeComponent();
  }
  private void button1_Click(object sender, EventArgs e)
  {
  textBox1.Text += "1";
  }

  private void button2_Click(object sender, EventArgs e)
  {
  textBox1.Text += "2";
  }

  private void button3_Click(object sender, EventArgs e)
  {
  textBox1.Text += "3";
  }

  private void button4_Click(object sender, EventArgs e)
  {
  textBox1.Text += "4";
  }

  private void button5_Click(object sender, EventArgs e)
  {
  textBox1.Text += "5";
  }

  private void button6_Click(object sender, EventArgs e)
  {
  textBox1.Text += "6";
  }
  private void button7_Click(object sender, EventArgs e)
  {
  textBox1.Text += "7";
  }

  private void button8_Click(object sender, EventArgs e)
  {
  textBox1.Text += "8";
  }

  private void button9_Click(object sender, EventArgs e)
  {
  textBox1.Text += "9";
  }

  private void button0_Click(object sender, EventArgs e)
  {
  textBox1.Text += "0";
  }
  private void button_dot_Click(object sender, EventArgs e)
  {
  textBox1.Text += ".";
  }

  private void button_amount_Click(object sender, EventArgs e)
  {
  double equal = Convert.ToDouble(textBox1.Text);
   
  if (save == 1)
  {
  textBox1.Text = Convert.ToString(add(equal, a));
   
  }
  if (save == 2)
  {
  textBox1.Text = Convert.ToString(minus(a, equal));
   
  }
  if (save == 3)
  {
  textBox1.Text = Convert.ToString(multiply(equal, a));
   
  }
  if (save == 4)
  {
  textBox1.Text = Convert.ToString(divide(a, equal));
   
  }
   
   
  }

  private void button_add_Click(object sender, EventArgs e)
  {
  a = Convert.ToDouble(textBox1.Text);
  textBox1.Text = "";  
  save = 1;  
  }

  private void button_minus_Click(object sender, EventArgs e)


  {
  a = Convert.ToDouble(textBox1.Text);
  textBox1.Text = "";  
  save = 2;
  }

  private void button_multiply_Click(object sender, EventArgs e)
  {
  a = Convert.ToDouble(textBox1.Text);
  textBox1.Text = "";
  save = 3;
  }

  private void button_division_Click(object sender, EventArgs e)
  {
  a = Convert.ToDouble(textBox1.Text);
  textBox1.Text = "";
  save = 4;
  }
  private double add(double a,double b)
  {
  z = a + b;
  return z;
  }
  private double minus(double a, double b)
  {
  z = a - b;
  return z;
  }
  private double multiply(double a, double b)
  {
  z = a * b;
  return z;
  }
  private double divide(double a, double b)
  {
  z = a / b;
  return z;
  }

  private void button_Cancel_Click(object sender, EventArgs e)
  {
  textBox1.Text = "0";
  }

   

  }
}

[解决办法]
把这些按钮做成数组,这样管理起来方便点。
Button[] numberButtons;

10个数字键的单击事件全都注册到同一个事件处理程序。如:
在循环中:numberButtons[i].Click += new EventHandler(NumberButton_Click);
4个加减乘除键的单击事件全都注册到同一个事件处理程序。如:
在循环中:functionButtons[i].Click += new EventHandler(FunctionButton_Click);

使用委托。
delegate double NumberOperate(double x, double y);//定义一个委托。

double Add(double x, double y)//符合委托要求的加法算法。
{
return x + y;
}

double Minus(double x, double y)//符合委托要求的减法算法。
{
return x - y;
}
在类中定义一个NumberOperate类型的字段,按了哪个加减乘除键就把该字段设成哪种数字操作功能。
void FunctionButton_Click(object sender, EventArgs e)
{
switch (((Button)sender).Text)
{
case "+":
operate = new NumberOperate(Add); break;
case "-":
operate = new NumberOperate(Minus); break;
//请自己补充完整。

}
}
最后,只要
private void amountButton_Click(object sender, EventArgs e)
{
MessageBox.Show(operate(x, y).ToString());
}

热点排行