【发布时间】:2016-09-09 02:22:44
【问题描述】:
我有以下文本文件:
1 Feb Jane Auckland LF1 190.21
2 Feb Jane Auckland BDHIWAY 390.62
7 Feb Adeeva Sharif LSZ 2000.00
8 Feb Adeeva Sharif LF2 52.00
4 Feb Jane Auckland ILZERO 101.03
9 Feb Jerome Velence ILFIVE 4110.00
如您所见,文本文件中有三个人(简·奥克兰、阿德瓦·谢里夫和杰罗姆·韦伦斯),但每个人都有附加值,并在文本文件中散布,被其他人隔开。
我想要做的是一次只读取一个人的文本文件。但是,我希望将它们的重合值(最后一个值)相加,以便我可以将它们通过我的税务功能并打印工资单并检查每个人。它看起来类似于:
Pay slip for Jane Auckland from file examplep1.txt
+-------------------------+
| Commission : $ 681.86 |
| Tax : $ 0.00 |
| ---------- |
| Net Pay : $ 681.86 |
+-------------------------+
OFFICIAL CHEQUE FOR BANK 'BBPL'
Please pay Jane Auckland an amount of $681.86
[ ] WIC - 420 1337 911
Pay slip for Adeeva Sharif from file examplep1.txt
+-------------------------+
| Commission : $ 2052.00 |
| Tax : $ 101.65 |
| ---------- |
| Net Pay : $ 1950.35 |
+-------------------------+
OFFICIAL CHEQUE FOR BANK 'BBPL'
Please pay Adeeva Sharif an amount of $1950.35
[ ] WIC - 420 1337 911
Pay slip for Jerome Velence from file examplep1.txt
+-------------------------+
| Commission : $ 4110.00 |
| Tax : $ 631.78 |
| ---------- |
| Net Pay : $ 3478.23 |
+-------------------------+
OFFICIAL CHEQUE FOR BANK 'BBPL'
Please pay Jane Auckland an amount of $3478.23
[ ] WIC - 420 1337 911
这是我的税务职能:
double calculateTax(double income)
{
double centsPerDollar;
double initialTax;
double minimumTax;
income = round(income); //Rounds income to the nearest interger.
if (income >= 0 && income <= 1517) //First tax bracket.
{
centsPerDollar = 0.00;
initialTax = 0.00;
minimumTax = 0.00;
tax = 0.00;
taxedIncome = income;
}
else if (income >= 1518 && income <= 3083) //Second tax bracket.
{
centsPerDollar = 0.19;
initialTax = 0.00;
minimumTax = 1517.00;
tax = (income - minimumTax) * centsPerDollar + initialTax;
taxedIncome = income - ((income - minimumTax) * centsPerDollar + initialTax);
}
else if (income >= 3084 && income <= 6667) //Third tax bracket.
{
centsPerDollar = 0.325;
initialTax = 298.00;
minimumTax = 3083.00;
tax = (income - minimumTax) * centsPerDollar + initialTax;
taxedIncome = income - ((income - minimumTax) * centsPerDollar + initialTax);
}
else if (income >= 6668 && income <= 15000) //Fourth tax bracket.
{
centsPerDollar = 0.37;
initialTax = 1462.00;
minimumTax = 6667.00;
tax = (income - minimumTax) * centsPerDollar + initialTax;
taxedIncome = income - ((income - minimumTax) * centsPerDollar + initialTax);
}
else if (income >= 15001) //Fifth tax bracket.
{
centsPerDollar = 0.45;
initialTax = 4546.00;
minimumTax = 15000.00;
tax = (income - minimumTax) * centsPerDollar + initialTax;
taxedIncome = income - ((income - minimumTax) * centsPerDollar + initialTax);
}
else { fprintf(stderr, "Must be a positive number."); } //Error check for negative numbers.
}
总而言之,我想要做的是用他们自己的价值观来个性化每个人,并将所述价值观通过我的税务功能并打印出每个人的工资单和支票,如示例所示。
提前致谢。
【问题讨论】:
-
文本文件的第一个问题,名称组件由分隔字段的相同分隔符分隔。使用不同的分隔符,例如制表符
'\t'或分号';',以免产生歧义。 -
没有标准的函数可以直接跳到你的最终目标。最好将过程分解为文件解析,然后是最终结果整理。即读取每一行,解析,存储解析的信息,然后在文件完全解析后整理最终结果。
-
您的方法可能使问题变得困难。为什么不将文件中的所有值读取到 struct 数组 中,然后按名称对数组进行排序,然后简单地将每个名称的值相加?您甚至可以创建一个单独的结构来保存名称,并为从文件读取的详细列表中填充的每个人求和。
-
注意:不需要
income >= 1518 &&,income >= 3084 &&... -
简·奥克兰怎么会从给定的样本数据中得到两张薪水?哦,我明白了:这只是一个不完整的 search-n-replace 操作。没关系!