阵列和测量单位转换

时间:2012-03-22 17:01:04

标签: c# arrays file-io text-files

using (read = new StreamReader("C:/Users/Sam Smith/Desktop/convert.txt"))
            {
                while (!read.EndOfStream)
                {
                    lineFromFile = read.ReadLine();
                    units = lineFromFile.Split(',');

                    if (units.Contains(splitEntry[0]) && units.Contains(splitEntry[1]))
                    {
                        firstUnit = units[0];
                        secondUnit = units[1];
                        userConvertValue = Convert.ToDouble(splitEntry[2]);
                        fileConvertValue = Convert.ToDouble(units[2]);
                        result = fileConvertValue * userConvertValue;
                    }
                    if (units.Contains(splitEntry[0]) && units.Contains(splitEntry[1]))
                    {
                        firstUnit = units[1];
                        secondUnit = units[0];
                        userConvertValue = Convert.ToDouble(splitEntry[2]);
                        fileConvertValue = Convert.ToDouble(units[2]);
                        result = userConvertValue / fileConvertValue;
                    }
                    if (!units.Contains(splitEntry[0]) || !units.Contains(splitEntry[1]))
                    {
                        Console.WriteLine("Error, measurement unit not recognised.");
                    }

上面我有一个包含单位测量类型(磅,盎司,英里等)的文本文件,此文件中的文本被拆分为字符串数组。 用户以下列格式输入两个测量单位,以转换为两个单位: 单元,单元,量 在文本文件中,两个单位的转换量是每三个分割字符串,如下所示: 单元,单元,2.80 单位,单位,1.27(等)

有没有办法对每组单位及其转换金额进行分组?例如,如果用户尝试转换两个特定单位,程序将在计算最终结果时知道要使用的转换值。 可能有点模糊,但很难解释。 编辑:用户不与文件交互,程序只是从文件中提取数据,然后将其拆分为字符串(',')并存储在数组中。

1 个答案:

答案 0 :(得分:0)

如果我没有弄错你,以下代码应该满足您的要求(这是非常基本的,没有错误处理等):

public enum Unit
{
  Pound,
  Kilo,
  Kilometer,
  Mile
}

public class UnitMapping
{

  public UnitMapping(Unit source, Unit target, double factor)
  {
    SourceUnit = source;
    TargetUnit = target;
    Factor = factor;
  }

  public Unit SourceUnit { get; private set; }

  public Unit TargetUnit { get; private set; }

  public double Factor { get; private set; }

}

public class UnitCalculator
{
  public const string FILE_INPUT = @"Kilo,Pound,0.45359237
                                     Kilometer,Mile,1.609344";


  private List<UnitMapping> mappings;

  public UnitCalculator()
  {
    this.mappings = new List<UnitMapping>();

    // parse the mappings
    foreach (var line in FILE_INPUT.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
    {
      var fields = line.Split(',');

      var source = (Unit)Enum.Parse(typeof(Unit), fields[0]);
      var target = (Unit)Enum.Parse(typeof(Unit), fields[1]);
      double factor = double.Parse(fields[2], CultureInfo.InvariantCulture);

      this.mappings.Add(new UnitMapping(source, target, factor));
    }
  }

  public double Convert(Unit source, Unit target, double value)
  {
    foreach (var mapping in this.mappings)
    {
      if (mapping.SourceUnit == source && mapping.TargetUnit == target)
      {
        return value * mapping.Factor;
      }
      else if (mapping.SourceUnit == target && mapping.TargetUnit == source)
      {
        return value * (1 / mapping.Factor);
      }
    }

    throw new InvalidOperationException("No mapping could be found for this conversion.");
  }

}

像这样调用它:

static void Main(string[] args)
{
  var calc = new UnitCalculator();
  Console.WriteLine(calc.Convert(Unit.Mile, Unit.Kilometer, 1));
}

如果您不知道单位,也可以使用字符串。