【问题标题】:double functions, fstreams, and more fun双重功能,fstreams,更有趣
【发布时间】:2013-12-07 16:30:54
【问题描述】:

大图是将信息从一个文件转移到另一个文件。我已经做到了,而且效果很好。我需要做的下一件事是从新文件中找到最高值。基本格式是这样的:我有一群“工人”,我有他们工作的日子,他们工作的小时和分钟。在新文件中,我将其格式化为显示工资率(我将值输入为 cin),然后我得到了他们赚的总金额(每个人赚的钱都是一样的……在 cin 中)。出于某种原因,我无法制作一个(有效的)函数来提取最赚钱的人的名字,以及那是多少钱。我有点沮丧,正在尝试任何我能做的事情,所以新功能是草率和荒谬的,但我希望你能提供帮助。为了保持这是一个一般性问题而不是一个具体问题,我想知道你们是否可以解释如何做这样的事情(找到最高价值并用人名输出它......所以本质上是双倍的钱和一个带有人名的字符串)以我的部分代码为例:

  #include <iostream>

  #include <fstream>

  #include <iomanip>

  #include <string>

  #include <cstdlib>

  using namespace std;

  void getandprintaddress (ifstream&, ofstream&); // this function will be used to get the data from one file and input it into the next with the total amount of money made from each person as well as the next function
  double highestpaid(double& highesttotal, double& totalpay, string highestpaidemp, string name); // this function is what will be used to find the highest amount of money and the person who has it
  void name(); // This is to input my name in the file.

  struct employees // This is the structure with all of the information that will be used to calculate everything. 
  {
     string name; // person's name
     string highestpaidemp; // highest paid employee
     string address; // the address of the employee
     string address2;
     int days; // days worked
     int hours; //hours worked
     double minutes; // minutes worked
      int total_hours; // total hours worked
     double totalpay; // total pay
     double payrate; // pay rate
     double total_minutes; // minutes worked
     double total_time; // total hours and minutes worked
     double highesttotal; // the highest total amount of money made between all employees.

};

int main(){
    ifstream inputFile; 
    ofstream outputFile;
    getandprintaddress(inputFile, outputFile);



return 0;
}

void getandprintaddress(ifstream& inputFile, ofstream& outputFile) // the main function that will be used to get and output all the information.
    {
        struct employees();
        ifstream getdata;
        ofstream outdata;
        int employees_t;

        employees employeeinfo;

        string inputfile;
        string outputfile;

cout << "What is the name of your input file? "<<endl; // this will be the file you open
            cin>>inputfile; 


    getdata.open(inputfile.c_str());


    if(getdata.fail()){ // this is meant to be used if someone enters a file that isn't there
            cout << "The input file has failed to open. \n";
            exit(1);
    }


    cout << "What is the name of your output file? \n"; // what you want the title of the new file to be.
                cin>>outputfile;
                outdata.open(outputfile.c_str());


if(outdata.fail())
{
   //This is if the new file is invalid

    cout << "The outputfile failed to open. \n";
    exit(1);


}


    cout << "How many employees are there? \n" ;
                    cin >> employees_t; // Enter how many employees there are

    cout << "What is the employees hourly payrate? \n";
                    cin >> employeeinfo.payrate; // how much each employee makes.

    for ( int info = 0; info < employees_t; info++)
         {
            employeeinfo.highesttotal = 0.0; // this will be needed for calculating the highest paid employee

            employeeinfo.total_minutes = 0.0; // This is needed to calculate total minutes

            employeeinfo.total_hours = 0; // Same as the total_minutes, but for hours instead.

            employeeinfo.total_time = 0.0; // Needed to calculate total time 

            string line1;

            getline(getdata, employeeinfo.name);

            outdata << "Name: " << employeeinfo.name <<endl; // employees name

            getline(getdata, employeeinfo.address);

            outdata << "Address: \n"; // Employees address

            outdata<< employeeinfo.address <<endl;

            getline(getdata, employeeinfo.address2);

            outdata <<employeeinfo.address2 <<endl;

            getdata >> employeeinfo.days;

            outdata << "Days worked: " <<employeeinfo.days << endl; // Days worked

            for (int totalworked=0; totalworked<employeeinfo.days; totalworked++)
                { 
                    // Because the employees work different amount of days, this loop is needed to post the individual information from each employee

                    getdata >> employeeinfo.hours >> employeeinfo.minutes;

                    employeeinfo.minutes = employeeinfo.minutes / 60;

                    employeeinfo.total_hours = employeeinfo.total_hours + employeeinfo.hours;

                    employeeinfo.total_minutes = employeeinfo.total_minutes + employeeinfo.minutes;

                    employeeinfo.total_time = employeeinfo.total_minutes + employeeinfo.total_hours;

                    employeeinfo.totalpay = employeeinfo.total_time * employeeinfo.payrate;

                    outdata << employeeinfo.hours <<" hours "<< employeeinfo.minutes*60 << " minutes"<< endl;

                }
            outdata << fixed << showpoint << setprecision(1); // Setting the total hours worked to 1 decimal so that to include the minutes

            outdata << "Total hours worked: "<<employeeinfo.total_time<<endl; // Total hours worked

            outdata << fixed << showpoint << setprecision(2); // Setting the decimal to two places

            outdata << "Hourly pay: $"<<employeeinfo.payrate << endl; //Hourly pay

            outdata << "Gross pay: $" <<employeeinfo.totalpay <<endl; // Gross pay

            getline(getdata,line1);

            getline(getdata,line1);

            outdata << "\n";

            double highestpaid(employeeinfo.highesttotal, employeeinfo.totalpay);
        }

    };
double highestpaid(double& highesttotal, double&totalpay, string highestpaidemp, string name)
    {

        if (highesttotal < totalpay)
        {
            highesttotal = totalpay;
            highestpaidemp = name;


        }

return highestpaid(double& highesttotal, double&totalpay, string highestpaidemp, string name);
    };

【问题讨论】:

  • 文件格式如何?你能缩进你的代码并添加cmets吗?很难读。
  • 是的,很抱歉!无论如何我都需要这样做。

标签: c++


【解决方案1】:

好的,我将回答您的一般性问题,而不是花太长时间搜索您的代码,但希望两者都适用。

你有你的结构员工

将它们全部粘贴在员工向量中(我将其称为工人)

vector<struct employee> workers

使用向量意味着您可以继续在末尾添加更多内容,但如果您有固定数量的工人,数组也可以工作

现在每个工人都可以通过一个整数索引来标识,从 0 到 n-1,其中 n 是工人的数量。现在可以迭代(如果工人数量非常多,则可以使用更快的方法,但在这里可能没问题)以找到最高薪水

double highestPay = 0; 
int highestPaidIndex = 0;
for(int i = 0; i++; i < n)
{
    if(workers[i].totalpay > highestPay)
    {
        highestPay = workers[i].totalpay;
        highestPaidIndex = i;
    }
}

现在highestPaidIndex 是最高薪员工的索引,其名称为workers[highestPaidIndex].name 和总收入workers[highestPaidIndex].totalpay

希望能解决您的一般问题和具体问题

【讨论】:

  • 我以前没有使用过向量,但这似乎是一个很棒的主意。当我制作矢量时,我需要做什么才能获得其中的所有名称和信息?
  • 所以我建议的向量是由结构组成的。您只需定义向量中结构的任何元素的值。因此,如果工人 2 工作了 10 小时,只需执行 workers[2].hoursWorked = 10 (或任何你合适的变量名称)。当然,您希望遍历所有工作人员,从文件中为每个工作人员添加信息,初始化一个大向量,包含结构,这些结构本身将包含有关每个特定工作人员的所有信息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多