带有结构成员和指针的C++气泡排序

C++ Bubble Sorting with structure members and pointers

本文关键字:C++ 气泡 排序 指针 结构 成员      更新时间:2023-10-16

我必须用气泡排序来组织我的数组,以显示谁工作时间最多,谁工作时间最少。之后,我需要展示在公司中收入最高的员工。然而,我不确定如何使用带有指针变量和结构成员的冒泡排序。目前,我在arraySort函数中有一个用于冒泡排序的通用代码,但它需要改进。非常感谢。

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std; 
struct incomeInfo {
    string id;
    string name;
    int hours;
    double hRate;
    double regPay = 0;
    double otPay = 0;
};
const int ARRAY_SIZE = 25; 
incomeInfo income[ARRAY_SIZE]; 
void getIncome(incomeInfo[], int&);
void compute(incomeInfo *, int);
void display(incomeInfo[], int);
void summary(incomeInfo[], int);
void sortArray(incomeInfo[], int);
void mostPay(incomeInfo[], int);
int main()
{
    incomeInfo income[ARRAY_SIZE];
    int count = 0;                          
    getIncome(income, count);
    compute(income, count);
    display(income, count);
    summary(income, count);
    sortArray(income, count);
    mostPay(income, count);
    return 0;
}
void getIncome(incomeInfo income[], int &count)
{
    ifstream inputFile;                 
    char line[50];                      

    inputFile.open("Payroll.txt");

    if (inputFile.fail())
    {
        cout << "nntError openning file: " << "nnt";
        system("pause");
        exit(1);
    }
    else
    {
        while (!inputFile.eof())    
        {
            inputFile.getline(line, 50, ',');        
            income[count].id = line;
            inputFile.getline(line, 50, ',');
            income[count].name = line;
            inputFile.getline(line, 50, ',');
            income[count].hours = atoi(line);       
            inputFile.getline(line, 50, ',');
            income[count].hRate = atof(line);           
            count++;
        }
    }
    inputFile.close();
    return;
}

void compute(incomeInfo *ptrI, int count)    
{                                                
    for (int i = 0; i<count; i++)
    if (ptrI->hours <= 40)
    {
        ptrI->regPay = ptrI->hours * ptrI->hRate;
        ptrI++;
    }
    else if (ptrI->hours > 40)
    {
        ptrI->regPay = 40 * ptrI->hRate;
        ptrI->otPay = (ptrI->hours - 40) * (ptrI->hRate + (ptrI->hRate* .5));
        ptrI++;
    }
        return;
}

void display(incomeInfo income[], int count)
{
    cout << fixed << showpoint << setprecision(2);
    cout << setw(15) << left << "ID" << setw(16) << "Name";
    cout << left << setw(8) << "Hours" << setw(14) << "Hourly Rate" << setw(14) << "Regular Pay" << setw(14) << "Overtime Pay" <<endl;

    for (int i = 0; i < count; i++)
    {
        cout << setw(14) << left << income[i].id << setw(15) << income[i].name;
        cout << right << setw(6) << income[i].hours << setw(12) << income[i].hRate;
        cout << setw(14) << income[i].regPay << setw(14) << income[i].otPay << endl;
    }
    return;
}

void sortArray(incomeInfo income[], int count)
{
    {
        bool swap;
        int temp;
        do
        {
            swap = false;
            for (int count = 0; count < (size - 1); count++)
            {
                if (array[count] > array[count + 1])
                {
                    temp = array[count];
                    array[count] = array[count + 1];
                    array[count + 1] = temp;
                    swap = true;
                }
            }
        } while (swap);
    }
}

void summary(incomeInfo income[], int count)
{
        cout << endl << endl << "Total payroll amount for the company = $";
        cout << (income[0].regPay + income[0].otPay) + (income[1].regPay + income[1].otPay) + (income[2].regPay + income[2].otPay) + (income[3].regPay + income[3].otPay) + (income[4].regPay + income[4].otPay) << endl;
}

void mostPay(incomeInfo[], int count)
{
    cout << endl << endl << "Employee who earns most money: ";
}

气泡排序的逻辑将是相同的,只是您将使用指针数组。只有if语句需要更改,以便使用数组指针指向的结构中的值进行比较。

您的代码不会在当前状态下编译,因为您引用了不存在的"size"answers"array"变量,但以下是冒泡排序的样子:

void sortArray(incomeInfo income[], int count) {
    for (int i = 0; i < count; i++)
        for (int j = 1; j < count; j++)
            if (income[j - 1].hours > income[j].hours)
                std::swap(income[j - 1], income[j]);
}