带有结构成员和指针的C++气泡排序
C++ Bubble Sorting with structure members and pointers
我必须用气泡排序来组织我的数组,以显示谁工作时间最多,谁工作时间最少。之后,我需要展示在公司中收入最高的员工。然而,我不确定如何使用带有指针变量和结构成员的冒泡排序。目前,我在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]);
}
相关文章:
- 在气泡排序程序中未声明错误功能
- 气泡排序未正确循环
- 带有枚举方向/类型的气泡排序结构数组
- 在气泡排序中使用递归
- 如何在C++中实现气泡排序?
- 比较迭代器会使程序崩溃,而不会在自定义气泡排序实现中出现错误
- C++气泡排序的问题
- 气泡排序C++ OOP
- OOP 气泡排序C++程序
- 按升序对列表进行排序,C++使用气泡排序
- C++气泡排序
- 此气泡排序代码中的错误是什么?
- 气泡排序 2D 数组
- 如何使用气泡排序从最小到最大对 4 个数组进行排序? C++
- 添加气泡排序代码以排列列表
- 使用向量和气泡排序的动态内存分配
- 带修改(偏移)的气泡排序
- 使用C++进行气泡排序
- 操作计数插入气泡选择
- 气泡阵列在C 实现中排序