使用结构成员和指针的 C++ 冒泡排序

标签 c++ sorting structure bubble-sort

我必须使用冒泡排序来组织我的数组,以显示谁工作时间最长,谁工作时间最少。之后,我需要显示公司中工资最高的员工。但是,我不确定如何使用带有指针变量和结构成员的冒泡排序。目前我在 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 << "\n\n\tError openning file: " << "\n\n\t";
        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 语句需要更改,以便它使用数组指针指向的结构中的值进行比较。

关于使用结构成员和指针的 C++ 冒泡排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23121845/

相关文章:

c# - OrderBy异常 "At least one object must implement IComparable"

ruby-on-rails - 你怎么知道一个数组是否有连续的相同元素?

android - 在不丢失登录用户的情况下加密用户数据

c++ - 通过 char* 缓冲区读取 int 的行为是不同的,无论它是正数还是负数

c++ - 在派生类的成员 union 中包含继承的基类成员

sorting - 有没有一种有效的方法来对 RethinkDB 中的联接结果进行排序?

ios - 按数组之一的顺序对两个数组进行排序 [Swift 3.0 - Xcode 8]

c - 将结构(指向结构的指针?)传递给函数

c++ - 使用单个命令进行结构初始化(零值)

c++ - 使用模板类定义声明变量内联