如何找到数组总和的每个元素的百分比? C#

时间:2017-02-17 23:56:50

标签: c# arrays percentage

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

我将用户输入的数据输入到一个数组中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);
    }

3 个答案:

答案 0 :(得分:0)

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

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);
        }
    }
}

基于虚拟数据的输出是:

enter image description here

答案 1 :(得分:0)

这是另一种解决方案:

该计划

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]);
        }
    }
}

输出:

enter image description here

答案 2 :(得分:0)

改进版

主程序

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]);
        }
    }
}