单位转换器C#

时间:2012-12-23 23:55:32

标签: c# .net winforms unit-conversion

我今天没有别的事可做,所以我决定尝试创建一个简单的单位转换器。到目前为止,它运作良好,但由于我使用了很多“如果”的陈述,它花了太长时间。我在网上搜索了Visual Studio中制作的单位转换器的例子,但我找到的唯一一个是在VS Basic中使用在线转换器进行计算的货币转换器。那么有更快的方法来创建单位转换器吗?

enter image description here

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (listBox1.SelectedItem == listBox2.SelectedItem) 
        {
            txtAns.Text = txtFirstUnit.Text;
        }

        if (listBox1.SelectedItem == "Feet" && listBox2.SelectedItem == "Inches") 
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) * 12).ToString();
        }

        if (listBox1.SelectedItem == "Inches" && listBox2.SelectedItem == "Feet")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) / 12).ToString();
        }

        if (listBox1.SelectedItem == "Yard" && listBox2.SelectedItem == "Feet")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) * 3).ToString();
        }

        if (listBox1.SelectedItem == "Feet" && listBox2.SelectedItem == "Yard")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) / 3).ToString();
        }

        if (listBox1.SelectedItem == "Yard" && listBox2.SelectedItem == "Inches")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) * 36).ToString();
        }

        if (listBox1.SelectedItem == "Inches" && listBox2.SelectedItem == "Yard")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) / 36).ToString();
        }

        if (listBox1.SelectedItem == "Mile" && listBox2.SelectedItem == "Inches")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) * 63360).ToString();
        }

        if (listBox1.SelectedItem == "Inches" && listBox2.SelectedItem == "Mile")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) / 63360).ToString();
        }

        if (listBox1.SelectedItem == "Mile" && listBox2.SelectedItem == "Feet")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) * 5280).ToString();
        }

        if (listBox1.SelectedItem == "Feet" && listBox2.SelectedItem == "Mile")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) / 5280).ToString();
        }

        if (listBox1.SelectedItem == "Mile" && listBox2.SelectedItem == "Yard")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) * 1760).ToString();
        }

        if (listBox1.SelectedItem == "Yard" && listBox2.SelectedItem == "Mile")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) / 1760).ToString();
        }

        if (listBox1.SelectedItem == "Inches" && listBox2.SelectedItem == "Meters")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) / 39.370).ToString();
        }

        if (listBox1.SelectedItem == "Meters" && listBox2.SelectedItem == "Inches")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) * 39.370).ToString();
        }

        if (listBox1.SelectedItem == "Meters" && listBox2.SelectedItem == "Feet")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) * 3.2808).ToString();
        }

        if (listBox1.SelectedItem == "Feet" && listBox2.SelectedItem == "Meters")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) / 3.2808).ToString();
        }

        if (listBox1.SelectedItem == "Meters" && listBox2.SelectedItem == "Yard")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) * 1.0936).ToString();
        }

        if (listBox1.SelectedItem == "Yard" && listBox2.SelectedItem == "Meters")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) / 1.0936).ToString();
        }

        if (listBox1.SelectedItem == "Meters" && listBox2.SelectedItem == "Miles")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) * 0.00062137).ToString();
        }

        if (listBox1.SelectedItem == "Miles" && listBox2.SelectedItem == "Meters")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) / 0.00062137).ToString();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

面向对象的方法将摆脱if (lst.SelectedItem) ==语句。

考虑这个代码,每个单元都有不同的类。找不到if语句。

  public Form1()
  {
     InitializeComponent();

     fromList.Items.Add(new CentimeterFromMillimeterConverter());
     toList.Items.Add(new CentimeterToMillimeterConverter());
  }

  void Convert(double amount)
  {
     var from = (FromMillimeterConverter) fromList.SelectedItem;
     var to = (FromMillimeterConverter) toList.SelectedItem;
     to.Convert(from.Convert(amount));
  }

public abstract class ToMillimeterConverter
{
  public abstract double Convert(double unit);
  public override string ToString()
  {
     return GetType().Name.Replace("ToMillimeterConverter", "");
  }
}

public class CentimeterToMillimeterConverter : ToMillimeterConverter
{
  public override double Convert(double centimeters)
  {
     return 10 * centimeters;
  }
}

public abstract class FromMillimeterConverter
{
  public abstract double Convert(double unit);
  public override string ToString()
  {
     return GetType().Name.Replace("FromMillimeterConverter", "");
  }
}

public class CentimeterFromMillimeterConverter : FromMillimeterConverter
{
  public override double Convert(double centimeters)
  {
     return centimeters / 10;
  }
}