日期:2014-05-18  浏览次数:20881 次

求教大侠如何封装
这是SL下的关于解二元一次方程的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace 一元二次方程
{
  public partial class MainPage : UserControl
  {
  public MainPage()
  {
  InitializeComponent();
  }

  String a;
  String b;
  String c;
  double x1;
  double x2;

  private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
  {

  a=this.textBox1.Text;
  }


  private void textBox2_TextChanged(object sender, TextChangedEventArgs e)
  {
  b = this.textBox2.Text;
  }

  private void textBox3_TextChanged(object sender, TextChangedEventArgs e)
  {
  c = this.textBox3.Text;
  }

  private void button1_Click(object sender, RoutedEventArgs e)
  {
  double a1 = double.Parse(a);
  double b1 = double.Parse(b);
  double c1 = double.Parse(c);

  if (b1 * b1 - 4 * a1 * c1 > 0)
  {
  x1 = (-b1 - Math.Sqrt(b1 * b1 - 4 * a1 * c1)) / (2 * a1);
  x2 = (-b1 + Math.Sqrt(b1 * b1 - 4 * a1 * c1)) / (2 * a1);
  this.textBox4.Text ="x1与X2分别等于:" + x1.ToString() +";"+ x2.ToString();
  }


  else if (b1 * b1 - 4 * a1 * c1 < 0)
  {
  this.textBox4.Text = "错误,无解";
  }
  else
  {
  x1 = x2 = -b1 / (2 * a1);
  this.textBox4.Text="两根相等,x1=x2=" + x1.ToString();

  }
  }
  }
}

现在老师要求把这段代码进行封装,将double A, B, C封装成一个属性,将X1 X2封装成一个属性。

最后要实现,输入ABC相应的数字要直接能够跳出结果,我不是很懂,这个要怎么弄啊。

我这个代码按F5之后,在页面上可以输入数字然后跳出结果啊,求大侠指点!!!

------解决方案--------------------
把private void button1_Click(object sender, RoutedEventArgs e)写成另外一个方法,

在textBox1_TextChanged、textBox2_TextChanged、textBox3_TextChanged事件都进行调用上面的方法

另外double a1 = double.Parse(a);
double b1 = double.Parse(b);
double c1 = double.Parse(c);
最好写在try catch 里面
------解决方案--------------------
都写成这样了,怎么粘贴就是你的事了。