我有以下的文本文件:如何读取与文本文件中特定字符串一致的值?
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
正如你可以看到有三个人(简奥克兰,Adeeva谢里夫和杰罗姆韦伦采)在文本文件中但每种都连接到他们的价值和周围蔓延文本文件正在被其他人分开..
我想要做的是一次只读取一个人的文本文件。不过,我希望他们的合并值(最后一个值)加在一起,这样我就可以通过我的税务功能并打印工资单并检查每个人。这将类似于此:
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''或分号'''',这样就没有问题了。 –
没有标准功能可以直接跳到最终目标。如果您将流程分解为文件解析和最终结果整理,那么最好。也就是说,读取每一行,解析它,存储解析的信息,然后在文件完全解析后整理最终结果。 – kaylum
您的方法可能会使问题变得困难。为什么不把文件中的所有值读入一个struct *数组,然后按名称对数组进行排序,并简单地将每个名称的值相加?你甚至可以创建一个单独的结构体来存储名称,并且从你从文件中读取的详细列表中填充每个人的总和。 –