用数字读取文件的程序

时间:2015-03-07 12:22:40

标签: c#

我正在研究一个用汽车模型和油耗来读取文件的程序。

在文件中我有像A | 12.45这样的车,其中A代表型号和数字代表每100Km的油耗。每辆车都在自己的行中。

文件看起来像这样:

A | 8.11

乙| 14.38

C ^ | 13.05

在这里我们可以看到B的消耗量最高,但是当我在控制台中运行该程序时,它显示为1438 L,中间没有点。我该如何解决这个问题?

我的代码如下:

    static void Main(string[] args)
    {

        StreamReader readF = File.OpenText(@"D:\CarList.txt");
        string line = readF.ReadLine();

        double highest = 0;
        double lowest = 300;

        string nameHighest= "";
        string nameLowest= "";

        while (line != null)
        {

            string[] temp = line.Split('|');


            if (Convert.ToDouble(temp[1]) > highest)
            {
                highest= Convert.ToDouble(temp[1]);
                nameHighest = temp[0];
            }


            if (Convert.ToDouble(temp[1]) < lowest)
            {

                lowest = Convert.ToDouble(temp[1]);
                nameLowest = temp[0];
            }

            line = readF.ReadLine();
        }
        readF.Close();


        Console.WriteLine("Highest consumption: " + nameHighest + ". It consumes " + highest + " L per 100Km.");
        Console.WriteLine("Lowest consumption " + nameLowest + ". It consumes " + lowest + " L per 100Km");

        Console.ReadKey();
    }

谢谢!

1 个答案:

答案 0 :(得分:2)

我强烈怀疑这是因为您的计算机上的默认文化是使用,而不是.作为小数分隔符的文化。您可以使用double.Parse并指定CultureInfo.InvariantCulture使用 使用.作为分隔符的不变文化来解析它。

我实际上会在几个步骤中重写您的代码。

第1步:使用double.Parse,每行只执行一次 - 同样每行提取一次temp[0]

第2步:使用double.NegativeInfinity作为初始&#34;最高&#34;并且double.PositiveInfinity作为初始&#34;最低&#34;,以便任何有限条目将注册。

步骤3:在结尾处使用格式字符串。使用C#6,我们可以使用字符串插值,但至少我们可以使它比以前更简单。

第4步:使用File.ReadLines代替阅读&#34;手动&#34;

此时我们有:

using System;
using System.Globalization;
using System.IO;

class Test
{
    static void Main(string[] args)
    {
        double highest = double.NegativeInfinity;
        double lowest = double.PositiveInfinity;

        string nameHighest= "";
        string nameLowest= "";

        foreach (var line in File.ReadLines("test.txt"))
        {
            string[] temp = line.Split('|');
            string name = temp[0];
            double consumption = double.Parse(
                temp[1],
                CultureInfo.InvariantCulture);

            if (consumption > highest)
            {
                highest = consumption;
                nameHighest = name;
            }

            if (consumption < lowest)
            {
                lowest = consumption;
                nameLowest = name;
            }
        }

        Console.WriteLine(
            "Highest consumption: {0}. It conumes {1}L per 100km",
            nameHighest, highest);
        Console.WriteLine(
            "Lowest consumption: {0}. It conumes {1}L per 100km",
            nameLowest, lowest);
    }
}

步骤5:使用LINQ将数据的读取和解析与使用它分开 - 尽管我将所有内容存储在列表中,以便我们可以在一分钟内完成两次...

using System;
using System.Globalization;
using System.IO;
using System.Linq;

class Test
{
    static void Main(string[] args)
    {
        var cars = File.ReadLines("test.txt")
            .Select(line => line.Split('|'))
            .Select(bits => new { Name = bits[0], Consumption = double.Parse(bits[1]) })
            .ToList();

        if (cars.Count == 0)
        {
            Console.WriteLine("No cars!");
            return;
        }

        var highest = cars[0];
        var lowest = cars[0];

        foreach (var car in cars.Skip(1))
        {
            if (car.Consumption > highest.Consumption)
            {
                highest = car;
            }

            if (car.Consumption < lowest.Consumption)
            {
                lowest = car;
            }
        }

        Console.WriteLine(
            "Highest consumption: {0}. It consumes {1}L per 100km",
            highest.Name, highest.Consumption);
        Console.WriteLine(
            "Lowest consumption: {0}. It consumes {1}L per 100km",
            lowest.Name, lowest.Consumption);
    }
}

第6步:使用MoreLINQ&#39; MinByMaxBy使其变得非常简单:

using MoreLinq;
using System;
using System.Globalization;
using System.IO;
using System.Linq;

class Test
{
    static void Main(string[] args)
    {
        var cars = File.ReadLines("test.txt")
            .Select(line => line.Split('|'))
            .Select(bits => new { Name = bits[0], Consumption = double.Parse(bits[1]) })
            .ToList();

        if (cars.Count == 0)
        {
            Console.WriteLine("No cars!");
            return;
        }

        var highest = cars.MaxBy(c => c.Consumption);
        var lowest = cars.MinBy(c => c.Consumption);

        Console.WriteLine(
            "Highest consumption: {0}. It consumes {1}L per 100km",
            highest.Name, highest.Consumption);
        Console.WriteLine(
            "Lowest consumption: {0}. It consumes {1}L per 100km",
            lowest.Name, lowest.Consumption);
    }
}