如何将此代码分为.h、.cpp和主驱动程序.cpp?(c++)[已关闭]

y4ekin9u  于 2022-12-15  发布在  其他
关注(0)|答案(1)|浏览(114)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题?**添加详细信息并通过editing this post阐明问题。

昨天关门了。
Improve this question

#include <iostream>

using namespace std;

int main()
{
    
    struct employee E;
    cout << "Each employee details\n";
    
    int i, emp;
    cout << " How many Employees? : ";
    cin >> emp;
    
    for (int i = 0; i < emp; i++)
    {
        cout << "Enter employee first name : ";
        cin >> E.f_name[i];
        cout << "Enter employee last name: ";
        cin >> E.l_name[i];
        cout << "Enter employee gender M/m or F/f : ";
        cin >> E.gender;
        
        switch (E.gender)
        {
            
            case 'M':

            case 'm':

            case 'F':

            case 'f':

                break;
                
            default:
                cout << "Typed wrong. try again. ";
                
                return 0;
        }
        cout << "Enter employee Id number : ";
        cin >> E.id_num[i];
        cout << "Enter employee pay rate : ";
        cin >> E.pay_rate[i];
        cout << "Enter employee worked hours : ";
        cin >> E.hrs_worked[i];
    }
    
    for (int i = 0; i < emp; i++)
    {
        
        if ((E.gender == 'F' || E.gender == 'f'))
        {
            cout << "\nEmployee Name: Ms. " << E.f_name[i] << " " << E.l_name[i]
                    << endl;
        }
        
        else if ((E.gender == 'M' || E.gender == 'm'))
        {
            cout << "\nEmployee Name: Mr. " << E.f_name[i] << " " << E.l_name[i]
                    << endl;
        }
        
        else
        {
            
            return 0;
        }
        cout << "\nEmployee Weekly Gross pay : "
                << E.pay_rate[i] * E.hrs_worked[i] << endl;
    }
    cout << "\n\n Thank you for using " << endl;
    
    return 0;
}

这是我的代码,我试图将其分离到.cpp,.h和主driver.cpp,但不知道我该怎么做..需要添加新的函数集,这是关于员工数据,并必须使用类.h需要一些帮助,请。
我试图分开,但不工作...我希望h,。cpp和主驱动程序的工作一样,我的第一个代码...

yduiuuwa

yduiuuwa1#

你要做的第一件事是定义结构是什么。这里我把它定义为一个人,所有的数据都在结构中:

struct employee
{
    string first_Name;
    string last_Name;
    char gender;
    int id_Number;
    double pay_Rate;
    float hours_Worked;
};

接下来是您编写的代码的实现:

int main()
{
    cout << "Each employee details" << endl;

    int emp;
    cout << "How many Employees? : ";
    cin >> emp;
    employee employees[emp];
    for (int i = 0; i < emp; i++)
    {
        cout << endl;
        cout << "Enter employee first name : ";
        cin >> employees[i].first_Name;
        cout << "Enter employee last name: ";
        cin >> employees[i].last_Name;
        do{
            cout << "Enter employee gender M/m or F/f : ";
            cin >> employees[i].gender;
            if(employees[i].gender != 'm' && employees[i].gender != 'M' && employees[i].gender != 'f' && employees[i].gender != 'F')
                cout << "Typed wrong. try again. ";
        }while(employees[i].gender != 'm' && employees[i].gender != 'M' && employees[i].gender != 'f' && employees[i].gender != 'F');

        cout << "Enter employee Id number : ";
        cin >> employees[i].id_Number;
        cout << "Enter employee pay rate : ";
        cin >> employees[i].pay_Rate;
        cout << "Enter employee worked hours : ";
        cin >> employees[i].hours_Worked;
    }

    for(int i = 0; i < emp; i++)
    {
        cout << "Employee Name: Ms. " << employees[i].first_Name << " " << employees[i].last_Name << endl;
        cout << "Employee Weekly Gross pay : " << employees[i].pay_Rate * employees[i].hours_Worked << endl << endl;
    }

    cout << "Thank you for using " << endl;

    return 0;
}
}

接下来你要从main函数中去掉尽可能多的元素,最好的选择是实现struct方法'read'和'write':

void read(employee employees[], int emp)
{
    for (int i = 0; i < emp; i++)
    {
        cout << endl;
        cout << "Enter employee first name : ";
        cin >> employees[i].first_Name;
        cout << "Enter employee last name: ";
        cin >> employees[i].last_Name;
        do{
            cout << "Enter employee gender M/m or F/f : ";
            cin >> employees[i].gender;
            if(employees[i].gender != 'm' && employees[i].gender != 'M' && employees[i].gender != 'f' && employees[i].gender != 'F')
                cout << "Typed wrong. try again. ";
        }while(employees[i].gender != 'm' && employees[i].gender != 'M' && employees[i].gender != 'f' && employees[i].gender != 'F');

        cout << "Enter employee Id number : ";
        cin >> employees[i].id_Number;
        cout << "Enter employee pay rate : ";
        cin >> employees[i].pay_Rate;
        cout << "Enter employee worked hours : ";
        cin >> employees[i].hours_Worked;
    }
}

void print(employee employees[], int emp)
{
    for(int i = 0; i < emp; i++)
    {
        cout << "Employee Name: Ms. " << employees[i].first_Name << " " << employees[i].last_Name << endl;
        cout << "Employee Weekly Gross pay : " << employees[i].pay_Rate * employees[i].hours_Worked << endl << endl;
    }
}

int main()
{
    cout << "Each employee details" << endl;
    int emp;
    cout << "How many Employees? : ";
    cin >> emp;
    employee employees[emp];
    read(employees, emp);
    print(employees, emp);
    cout << "Thank you for using " << endl;
    return 0;
}

现在我们要把. Struct分成文件,所有方法和函数的头文件都放到头文件'.h'或'.hpp'中,而剩下的代码都放到'. cpp'中。
一个一个三个一个一个一个一个一个四个一个一个一个一个一个五个一个

相关问题