我怎样才能找到数组总和的每个元素的百分比? C#

How can i find the Percentage of each element for the sum of the array? c#

我需要在数组总和中找到数组中每个值的百分比。

我让用户将数据输入数组 24 次,其中包含看到的汽车数量,一旦输入,然后列出每个数组元素并尝试找出其占数组总和的百分比。

    static void Main(string[] args)
    {
        int[] trafficCount;
        const int hours = 24;
        Traffic traff = new Traffic();
        traff.Report();
    }
    public Traffic()
    {
         trafficCount = new int[hours + 1];
    }
    public void showData()
    {
        Console.Clear();
        int maxValue = trafficCount.Max();
        int maxIndex = trafficCount.ToList().IndexOf(maxValue);
        int sum = trafficCount.Sum();
        Console.WriteLine("Traffic Report");
        Console.WriteLine("-----------------------");
        int percent = maxValue/sum*100 ;


        Console.WriteLine("{0}{1,24}", "Hour", "\tNumber of vehicles  Percent");
        for (int hour = 0; hour < trafficCount.Length; hour++)

            Console.WriteLine("{0,5}{1,24}", hour, trafficCount[hour]);              Console.Write("\t"+percent);
    }

每天给定小时内#cars 百分比的计算由表达式显示:(double)trafficCount[hour]/vehicleTotal 下面的循环。为了让它工作,您需要在 运行 输出之前获得您已经在整个 trafficCount[] 数组中计算的 vehicleTotal

public class Traffic
{
    private int[] trafficCount;

    public static void Main(string[] args)
    {
        Traffic traff = new Traffic(24);
        traff.showData();
    }
    public Traffic(int hours)
    {
        trafficCount = new int[hours];
        // populate with dummy data
        for (int i = 0; i < hours; i++)
        {
            trafficCount[i] = i*2+1; // replace this with real data
        }
    }

    public void showData()
    {
        Console.Clear();
        Console.WriteLine("Traffic Report");
        Console.WriteLine("-----------------------");

        long vehicleTotal = trafficCount.Sum();

        Console.WriteLine("{0}{1,24}", "Hour", "\tNumber of vehicles  Percent");
        for (int hour = 0; hour < trafficCount.Length; hour++)
        {
            var percentageInHour = (double)trafficCount[hour]/ vehicleTotal;
            Console.WriteLine("{0,5}{1,12}{2,17:P}", hour, trafficCount[hour], percentageInHour);
        }
    }
}

基于虚拟数据的输出是:

这是另一个解决方案:

节目

class Program
{
    static void Main(string[] args)
    {
        TrafficScanner scanner = new TrafficScanner(5);
        scanner.Read();

        TrafficReport trafficReport = new TrafficReport(scanner.Vehicles);
        trafficReport.Print();
    }
}

TrafficScanner 跟踪每小时行驶的所有车辆。它持续一定数量的 hours.

public class TrafficScanner
{
    private int[] vehiclesPerHour;

    public TrafficScanner(int hours)
    {
        vehiclesPerHour = new int[hours];
    }

    public void Read()
    {
        int hour = 0;
        while (true)
        {
            string input = Console.ReadLine();
            if (input == "") break;

            int carsThisHours = 0;

            bool isParsedSuccessfully = Int32.TryParse(input, out carsThisHours);
            if (isParsedSuccessfully)
            {
                vehiclesPerHour[hour++] += carsThisHours;
                hour = hour % vehiclesPerHour.Length;
            }
        }
    }

    public int[] Vehicles
    {
        get
        {
            return vehiclesPerHour;
        }
    }
}

TrafficReport打印交通车辆的统计数据。它准备报告中使用的数据。

public class TrafficReport
{
    private int[] vehiclesPerHour;
    private double[] trafficPercentage;

    public TrafficReport(int[] vehicles)
    {
        vehiclesPerHour = vehicles;
    }

    public void Print()
    {
        PrepareData();
        ShowData();
    }

    private void PrepareData()
    {
        int numberOfVehicles = vehiclesPerHour.Sum();

        trafficPercentage = new double[vehiclesPerHour.Length];
        for (int i = 0; i < vehiclesPerHour.Length; ++i)
        {
            trafficPercentage[i] = ((double) vehiclesPerHour[i]) / numberOfVehicles;
        }
    } 

    private void ShowData()
    {
        Console.Clear();
        Console.WriteLine("Traffic Report");
        Console.WriteLine("-----------------------");


        Console.WriteLine("{0}{1,24}", "Hour", "\tNumber of vehicles  Percent");
        for (int hour = 0; hour < trafficPercentage.Length; ++hour)
        {
            Console.WriteLine("{0,3}{1,14}{2,18:P}", hour, vehiclesPerHour[hour], trafficPercentage[hour]);
        }
    }
}

输出:

改进版

主程序

static void Main(string[] args)
{
    TrafficScanner scanner = new TrafficScanner(5);
    scanner.Read();

    VehiclePercentages percentages = new VehiclePercentages(scanner.Vehicles); 

    TrafficReport trafficReport = new TrafficReport(scanner.Vehicles, percentages.Values);
    trafficReport.Print();
}

TrafficScanner(与之前相同)

public class TrafficScanner
{
    private int[] vehiclesPerHour;

    public TrafficScanner(int hours)
    {
        vehiclesPerHour = new int[hours];
    }

    public void Read()
    {
        int hour = 0;
        while (true)
        {
            string input = Console.ReadLine();
            if (input == "") break;

            int carsThisHours = 0;

            bool isParsedSuccessfully = Int32.TryParse(input, out carsThisHours);
            if (isParsedSuccessfully)
            {
                vehiclesPerHour[hour++] += carsThisHours;
                hour = hour % vehiclesPerHour.Length;
            }
        }
    }

    public int[] Vehicles
    {
        get
        {
            return vehiclesPerHour;
        }
    }
}

VehiclePercentages 是从 TrafficReport 中提取的。这个想法是拥有独立的代码,只专注于一件事。 VehiclePercentages的目的是计算每小时的汽车百分比。

public class VehiclePercentages
{
    private double[] trafficPercentages;

    public VehiclePercentages(int[] vehiclesPerHour)
    {
        int numberOfVehicles = vehiclesPerHour.Sum();

        trafficPercentages = new double[vehiclesPerHour.Length];
        for (int i = 0; i < vehiclesPerHour.Length; ++i)
        {
            trafficPercentages[i] = ((double)vehiclesPerHour[i]) / numberOfVehicles;
        }
    }

    public double[] Values
    {
        get
        {
            return trafficPercentages;
        }
    }
}

现在,TrafficReport只做一件事。它以所需的格式打印交通数据。再也不用为获取所需数据而苦恼了。

public class TrafficReport
{
    private int[] vehicles;
    private double[] percentages;

    public TrafficReport(int[] vehiclesPerHour, double[] trafficPercentages)
    {
        vehicles = vehiclesPerHour;
        percentages = trafficPercentages;
    }

    public void Print()
    {
        Console.Clear();
        Console.WriteLine("Traffic Report");
        Console.WriteLine("-----------------------");


        Console.WriteLine("{0}{1,24}", "Hour", "\tNumber of vehicles  Percent");
        for (int hour = 0; hour < percentages.Length; ++hour)
        {
            Console.WriteLine("{0,3}{1,14}{2,18:P}", hour, vehicles[hour], percentages[hour]);
        }
    }
}