【发布时间】:2016-05-29 00:56:35
【问题描述】:
我正在尝试查找薪资计划的平均净工资。我想知道是否可以从输出的表格数据创建一个数组并使用该数组来计算平均值?还是我必须以不同的方式做到这一点?到目前为止我的代码:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
class employee {
ifstream fin;
char employeeid[12];
char employeename[20];
char martialstatus;
int hoursworked, overtime;
double hourlyrate, overtimepay, regularpay, grosspay, taxrate, taxamount, netpay, average;
void calculateGrosspay();
void calculateTax();
void calculateNetpay();
void printHeadings();
void printData();
double findAverage();
void printAverage();
public: employee();
~employee();
void printReport(); };
employee::employee(){
fin.open(payrollData.txt);
}; //Constructor
employee::~employee() {
fin.close(); } //Destructor
void employee::calculateGrosspay() {
if (hoursworked > 40) {
overtime = hoursworked - 40;
regularpay = hoursworked * hourlyrate;
overtimepay = overtime * (hourlyrate * 1.5);
grosspay = regularpay + overtimepay;
}
else grosspay = hoursworked * hourlyrate;
} //Calculate gross pay function
void employee::calculateTax() {
taxrate = .30;
taxamount = grosspay*taxrate;
} // calculate tax function
void employee::calculateNetpay() {
netpay = grosspay - taxamount;
} //Calculate net pay
void employee::printHeadings() {
cout << setw(45) << "-Employee Payroll Report-" << endl;
cout << "____________________________________________________________" << endl;
cout << "NAME ID HW OT RT-PAY OT-PAY Gross Tax NetPay" << endl;
cout << "____________________________________________________________" << endl;
} // print table headings
void employee::printData() {
cout << setprecision(2) << setiosflags(ios::fixed | ios::showpoint);
cout << setw(6) << employeename << setw(12) << employeeid << setw(4) << hoursworked << setw(3) << overtime << setw(8) << regularpay << setw(8)
<< overtimepay << setw(8) << grosspay << setw(8) << taxamount << setw(8) << netpay << endl;
} // print data
void employee::printReport() {
int i = 0;
printHeadings();
while (fin >> employeename >> employeeid >> hoursworked >> hourlyrate) {
calculateGrosspay();
calculateTax();
calculateNetpay();
printData();
i++;
}
}
double employee::findAverage() {
return average;
}
void printAverage() {
cout << endl;
cout << "The average netpay is" << average << endl;
}
void main() {
employee.printReport();
employee.findAverage();
employee.printAverage();
}
因此,在程序将数据打印到控制台后,我想找到网络支付的平均值并将其打印到控制台。最好的方法?
【问题讨论】:
-
您可能应该将读取的数据存储在数组结构(如向量)中,然后对向量中的数据运行报告——或查询数据来源的数据库
-
对愚蠢的标题投反对票。
-
感谢 Soren 的帮助,感谢 EJP 成为巨魔。
-
@swindleNswoon:EJP 的意思是你的标题应该传达一些关于你的问题的性质。当前的标题完全没用。
-
Kundor,我对建设性的批评反应非常好。我基本上是从 Reddit 复制并粘贴了这篇文章,但我将来会尽量减少我的标题。你的 cmets 给了我一些我可以工作和改进的东西。这不是 EJP 的情况。对于一个声称自己拥有 CSV 的人来说,他似乎相当不专业。