对于使用数组的循环c#

时间:2018-05-06 21:16:17

标签: c# arrays

我有一个计算工资单税的程序。我需要一个for循环来写出姓氏,名字,部门,费率,小时,收入,fica,fedtax,statetax和netpay。当我运行程序时,我没有抛出任何错误,似乎无法找到为什么我的输出只显示姓氏。有两个.txt个文件,一个是输出空白,另一个是下面列出的。

这是我的.txt文件的数据内容:

Adams               Paul               3      9.75     40.00
Allen               Cindy              2     11.45     48.00
Allen               Sarah              4     10.30     40.00
Baker               John               1     22.00     43.25
Baker               Toni               1     12.65     40.00
Baldwin             Cindy              2      7.90     25.50
Carson              Robert             1      8.35     52.50
Freeman             Sally              4     15.25     40.00
Garfield            James              3     22.00     40.00
Grimes              Kerri              3     16.50     35.00
Harris              Joan               2     18.65     51.00
Harris              John               2      9.00     47.50
Lawson              LeAnn              4     17.85     40.00
Mason               Debbie             4     22.00     41.50
Masters             Kenneth            3     16.10     40.25
Patterson           Roseanne           2     13.70     38.00
Peterson            Paul               2     22.00     44.00
Randall             Michael            3      8.00     41.00
Rogers              Sherry             1     16.50     30.00
Shepard             Steven             3     10.90     45.50

这是我的代码:

using System;
using System.IO;
using System.Text.RegularExpressions;
using LibUtil;

classPayRoll
{

    const double FICA_RATE = 0.07;
    const double FED_TAX_RATE = 0.22;
    const double STATE_TAX_RATE = 0.05;
    const string INPUT_FILE_NAME = "PayrollDat.Txt";
    const string OUTPUT_FILE_NAME = "PayrollReport.Txt";

    static uint numOfEmployees;
    static string[] lastNameArray = new string[51], firstNameArray = new string[51];
    static uint[] deptArray = new uint[51];
    static double[] rateArray = new double[51], hoursArray = new double[51];
    static double[] earningsArray = new double[51], ficaArray = new double[51];
    static double[] fedTaxArray = new double[51], stateTaxArray = new double[51];
    static double[] netPayArray = new double[51];
    static StreamReader fileIn;
    static StreamWriter fileOut;

    static void Main()
    {
        OpenFiles();
        InputData();
        CalcDetailPayroll();
        PrintReport();
        CloseFiles();
    }

    static void OpenFiles()
    {
        if (File.Exists(INPUT_FILE_NAME))
        {
            fileIn = File.OpenText(INPUT_FILE_NAME);
            Console.WriteLine("{0} was opened", INPUT_FILE_NAME);
        }
        else
        {
            Console.WriteLine("Error: {0} does not exist\n", INPUT_FILE_NAME);
            ConsoleApp.Exit();
        }
        fileOut = File.CreateText(OUTPUT_FILE_NAME);
        if (File.Exists(OUTPUT_FILE_NAME))
            Console.WriteLine("{0} was created\n", OUTPUT_FILE_NAME);
        else
        {
            Console.WriteLine("Error: {0} could not be created\n", OUTPUT_FILE_NAME);
            ConsoleApp.Exit();
        }
    }

    static void ParseLineIn(string lineIn, uint i)
    {
        string[] words = new string[5];

        lineIn = lineIn.Trim();
        while (Regex.IsMatch(lineIn, "[ ]{2}"))
            lineIn = lineIn.Replace("  ", " ");
        words = lineIn.Split(' ');
        lastNameArray[i] = words[0];

        i = 0;
        while (i <= numOfEmployees)
        {
            lastNameArray[i] = words[0];
            firstNameArray[i] = words[1];
            deptArray[i] = uint.Parse(words[2]);
            rateArray[i] = double.Parse(words[3]);
            hoursArray[i] = double.Parse(words[4]);
            i++;
        }
        //Add code to read in data into remaining arrays
    }

    static void InputData()
    {
        uint i;
        string lineIn;

        i = 0;
        while ((lineIn = fileIn.ReadLine()) != null)
        {
            i++;
            ParseLineIn(lineIn, i);
        }
        numOfEmployees = i;
    }

    static void CalcDetailPayroll()
    {
        uint i;
        double basePay, ovtPay;

        for (i = 1; i <= numOfEmployees; i++)
        {
            if (hoursArray[i] <= 40.0)
            {
                basePay = Math.Round(hoursArray[i] * rateArray[i]); //Calculate base pay 
                ovtPay = 0.00;
            }
            else
            {
                basePay = Math.Round(40 * rateArray[i]); //Calculate base pay
                ovtPay = Math.Round(rateArray[i] * (hoursArray[i] - 40.0) * 1.5); //Calculate overtime pay
            }
            //Calculate earnings, fica, fedTax, stateTax, and netPay 

            earningsArray[i] = basePay + ovtPay;
            ficaArray[i] = earningsArray[i] * FICA_RATE;
            fedTaxArray[i] = earningsArray[i] * fedTaxArray[i];
            stateTaxArray[i] = earningsArray[i] * STATE_TAX_RATE;
            netPayArray[i] = earningsArray[i] - (ficaArray[i] + fedTaxArray[i] + stateTaxArray[i]);
        }
    }

    static double Total(double[] doubleArray)
    {
        uint i;
        double total = 0.0;

        for (i = 1; i <= numOfEmployees; i++) ;
        total += earningsArray[i];
        return total;
    }

    static double Mean(double[] doubleArray)
    {
        uint i;
        double sum = 0.0;

        for (i = 1; i <= numOfEmployees; i++)
            sum += doubleArray[i];
        return sum / numOfEmployees;
    }

    static double Max(double[] doubleArray)
    {
        uint i;
        double max;

        max = doubleArray[1];
        for (i = 2; i <= numOfEmployees; i++)
            if (doubleArray[i] > max)
                max = doubleArray[i];
        return max;
    }

    static double Min(double[] doubleArray)
    {
        uint i;
        double min;
        double max;

        min = doubleArray[1];
        for (i = 2; i <= numOfEmployees; i++)
            if (doubleArray[i] < min)
                max = doubleArray[i];
        return min;
    }

    static void PrintReport()
    {
        uint i;

        fileOut.WriteLine("                                         Payroll Report                                                  ");
        fileOut.WriteLine();
        fileOut.WriteLine("   Last Name      First Name    Dept  Rate Hours  Earnings    FICA    Fed Tax  State Tax  Net Pay ");
        fileOut.WriteLine("--------------- --------------- ---- ----- ----- --------- --------- --------- --------- ---------");
        for (i = 1; i <= numOfEmployees; i++)
        {
            fileOut.WriteLine("{0,-9} {1,9} {2,9} {3,9} {4,9} {5,9} {6,9} {7,9} {8,9} {9,9}",
               lastNameArray[i], firstNameArray[i], deptArray[i], rateArray[i],hoursArray[i],earningsArray[i],
               ficaArray[i],fedTaxArray[i],stateTaxArray[i],netPayArray[i]);
        }
        //Create for loop to display last name, firstname, dept, rate, hours, earnings, fica, fedTax, stateTax, and netPay

        fileOut.WriteLine("                                                 --------- --------- --------- --------- ---------");

            fileOut.WriteLine("{0,-49}{1,9:n} {2,9:n} {3,9:n} {4,9:n} {5,9:n}",
                          "Total", Total(earningsArray), Total(ficaArray),
                          Total(fedTaxArray), Total(stateTaxArray), Total(netPayArray));
            fileOut.WriteLine("{0,-49}{1,9:n} {2,9:n} {3,9:n} {4,9:n} {5,9:n}",
                              "Mean", Mean(earningsArray), Mean(ficaArray),
                              Mean(fedTaxArray), Mean(stateTaxArray), Mean(netPayArray));
            fileOut.WriteLine("{0,-49}{1,9:n} {2,9:n} {3,9:n} {4,9:n} {5,9:n}",
                              "Maximum", Max(earningsArray), Max(ficaArray),
                              Max(fedTaxArray), Max(stateTaxArray), Max(netPayArray));
            fileOut.WriteLine("{0,-49}{1,9:n} {2,9:n} {3,9:n} {4,9:n} {5,9:n}",
                              "Minimum", Min(earningsArray), Min(ficaArray),
                              Min(fedTaxArray), Min(stateTaxArray), Min(netPayArray));

    }

    static void CloseFiles()
    {
        fileIn.Close(); fileOut.Close();
    }

}

1 个答案:

答案 0 :(得分:0)

您应该更改ParseLineIn方法以使用从调用方传递的索引。实际上,当您将i设置为零时,您始终设置相同的索引

static void ParseLineIn(string lineIn, uint i)
{
    string[] words = new string[5];

    lineIn = lineIn.Trim();
    while (Regex.IsMatch(lineIn, "[ ]{2}"))
        lineIn = lineIn.Replace("  ", " ");
    words = lineIn.Split(' ');
    lastNameArray[i] = words[0];

    // i = 0;
    // No loop needed, you are reading one line of data and setting 
    // the appropriate index in the arrays
    // while (i <= numOfEmployees)
    //{
        lastNameArray[i] = words[0];
        firstNameArray[i] = words[1];
        deptArray[i] = uint.Parse(words[2]);
        rateArray[i] = double.Parse(words[3]);
        hoursArray[i] = double.Parse(words[4]);
    //    i++;
    //}
}

另请注意,您的Total方法在此行中有错误

for (i = 1; i <= numOfEmployees; i++) ;

删除for循环末尾的分号,以允许执行该行的总和。

说这一切,我真的建议你放弃这种数组方法并构建一个代表你的数据的正确类,并在这些数据的List中收集信息。实际上你的代码只允许50行的文件,请记住NET中的数组从索引零开始。