使用冒泡排序c++对结构数组的成员进行优先级排序
prioritizing a member of a structure array using bubble sort c++
我正试图编写一个函数,对结构顺序的数组进行优先级排序。订单必须按最早日期排定优先级,但如果订单的日期相同,则必须按项目的最大成本排定优先级。
我已经使用三个函数按日期对数组进行了排序,这三个函数分别为年、月和日。我想我可以在订单日期和之前的订单日期相等的条件下,对订单项目的成本进行泡沫排序。函数没有对列表进行完全排序,我似乎不明白为什么。。。如果能提供一点帮助,我们将不胜感激。
这是我的代码:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct Date{
int day;
int month;
int year;
};
struct SizeOfBox{
int length;
int width;
int height;
};
struct PriceOfItem{
int dollars;
int cents;
};
struct Shipment {
string foodName;
Date expdate;
SizeOfBox sizeofbox;
float weightOfBox;
char storageMethod;
Date shipmentrecieved;
PriceOfItem priceofitem;
};
struct Customers{
string customerName;
string city;
int distance;
};
struct Orders{
string customerName;
string itemName;
int numberOfBoxes;
PriceOfItem costofitem;
Date orderDate;
};
const int NO_OF_SHIPMENTS = 100;
Shipment shipments[NO_OF_SHIPMENTS];
void getDataOrders(ifstream& inFile);
void displayOrders(Orders orders[],int totalOrders);
void sortYear (Orders orders[],int totalOrders);
void sortMonth (Orders orders[],int totalOrders);
void sortDay (Orders orders[],int totalOrders);
void priority (Orders orders[],int totalOrders);
void creatOutFile(char inName[],string& outName);
int main()
{
ifstream inDataOrders;
ofstream out;
inDataOrders.open("Orders.txt");
if (!inDataOrders)
{
cout << "The Orders input file does not exist. Program terminates!" <<endl;
return 1;
}
out.open("Priority Orders.txt");
getDataOrders(inDataOrders);
return 0;
}
void getDataOrders(ifstream& inFile){
char decimal;
int totalOrderItems;
inFile >> totalOrderItems; /// the length of the list is on the
/// first line in the text file being read
Orders orders[totalOrderItems];
for(int i = 0; i < totalOrderItems; i++){
inFile >> orders[i].customerName;
inFile >> orders[i].itemName;
inFile >> orders[i].numberOfBoxes;
inFile >> orders[i].costofitem.dollars >> decimal >> orders[i].costofitem.cents;
inFile >> orders[i].orderDate.month >> decimal >> orders[i].orderDate.day >> decimal >> orders[i].orderDate.year;
}
inFile.close();
sortYear(orders, totalOrderItems);
sortMonth(orders,totalOrderItems);
sortDay(orders,totalOrderItems);
priority(orders,totalOrderItems);
displayOrders(orders, totalOrderItems);
}
void displayOrders(Orders orders[],int totalOrders)
{
cout << setw(10) << setfill(' ') << "Customer Name" << " | "
<< "Item Name" << " | "
<< "#Boxes" << " | "
<< "Cost" << " | "
<< "Order Date" << "n"<< endl;
for(int i = 0; i < totalOrders; i++){
cout << left << setw(10) << orders[i].customerName << " | "
<< left << setw(10) << orders[i].itemName << " | "
<< right << setw(2) << setfill('0') << orders[i].numberOfBoxes << " | "
<< right << setw(2) << setfill('0') << orders[i].costofitem.dollars <<"." << setw(2) << setfill('0') << orders[i].costofitem.cents << " | "
<< right << setw(2) << setfill('0') << orders[i].orderDate.month <<":" << setw(2) << setfill('0') << orders[i].orderDate.day <<":" << setw(2) << setfill('0') << orders[i].orderDate.year << " "
<< setfill(' ') << endl;
}
}
void sortYear (Orders orders[],int totalOrders)
{
int p, w;
for (p=0; p<totalOrders; p++)
{
for (w=1; w<= totalOrders-1; w++)
if (orders[w-1].orderDate.year > orders[w].orderDate.year)
{
swap(orders[w-1].customerName, orders[w].customerName);
swap(orders[w-1].itemName, orders[w].itemName);
swap(orders[w-1].numberOfBoxes, orders[w].numberOfBoxes);
swap(orders[w-1].costofitem.dollars, orders[w].costofitem.dollars);
swap(orders[w-1].costofitem.cents, orders[w].costofitem.cents);
swap(orders[w-1].orderDate.month, orders[w].orderDate.month);
swap(orders[w-1].orderDate.day, orders[w].orderDate.day);
swap(orders[w-1].orderDate.year, orders[w].orderDate.year);
}
}
}
void sortMonth (Orders orders[],int totalOrders)
{
int p, w;
for (p=0; p<totalOrders; p++)
{
for (w=1; w<= totalOrders-1; w++)
if (orders[w].orderDate.year == orders[w-1].orderDate.year)
if (orders[w-1].orderDate.month > orders[w].orderDate.month)
{
swap(orders[w-1].customerName, orders[w].customerName);
swap(orders[w-1].itemName, orders[w].itemName);
swap(orders[w-1].numberOfBoxes, orders[w].numberOfBoxes);
swap(orders[w-1].costofitem.dollars, orders[w].costofitem.dollars);
swap(orders[w-1].costofitem.cents, orders[w].costofitem.cents);
swap(orders[w-1].orderDate.month, orders[w].orderDate.month);
swap(orders[w-1].orderDate.day, orders[w].orderDate.day);
swap(orders[w-1].orderDate.year, orders[w].orderDate.year);
}
}
}
void sortDay (Orders orders[],int totalOrders)
{
int p, w;
for (p=0; p<totalOrders; p++)
{
for (w=1; w<= totalOrders-1; w++)
if (orders[w].orderDate.month == orders[w-1].orderDate.month)
if (orders[w-1].orderDate.day > orders[w].orderDate.day)
{
swap(orders[w-1].customerName, orders[w].customerName);
swap(orders[w-1].itemName, orders[w].itemName);
swap(orders[w-1].numberOfBoxes, orders[w].numberOfBoxes);
swap(orders[w-1].costofitem.dollars, orders[w].costofitem.dollars);
swap(orders[w-1].costofitem.cents, orders[w].costofitem.cents);
swap(orders[w-1].orderDate.month, orders[w].orderDate.month);
swap(orders[w-1].orderDate.day, orders[w].orderDate.day);
swap(orders[w-1].orderDate.year, orders[w].orderDate.year);
}
}
}
void priority (Orders orders[],int totalOrders)
{
for (int p=0; p<totalOrders; p++)
{
for (int x=0;x<totalOrders-1;x++)
{
if ((orders[x+1].orderDate.year == orders[x].orderDate.year) &&
(orders[x+1].orderDate.month == orders[x].orderDate.month) &&
(orders[x+1].orderDate.day == orders[x].orderDate.day) &&
(orders[x+1].costofitem.dollars >= orders[x].costofitem.dollars) &&
(orders[x+1].costofitem.cents > orders[x].costofitem.cents))
{
swap(orders[x+1].customerName, orders[x].customerName);
swap(orders[x+1].itemName, orders[x].itemName);
swap(orders[x+1].numberOfBoxes, orders[x].numberOfBoxes);
swap(orders[x+1].costofitem.dollars, orders[x].costofitem.dollars);
swap(orders[x+1].costofitem.cents, orders[x].costofitem.cents);
swap(orders[x+1].orderDate.month, orders[x].orderDate.month);
swap(orders[x+1].orderDate.day, orders[x].orderDate.day);
swap(orders[x+1].orderDate.year, orders[x].orderDate.year);
}
cout << x+1 << " is swapped with " <<x<< endl;
}
cout << p << " outside loop " << endl;
}
它似乎正确地交换了一些订单,但没有交换其他订单,换言之,最高成本应该在每个相同的日期达到最高,但事实并非如此。
以下是在使用函数void priority:之前和之后的示例
之前:
客户名称|商品名称|#方框|成本|订单日期
Sajan|Oranges|20|87.00|10:12:2014
周|胡萝卜|08|90.09|10:2:2014
百事可乐|香蕉|05|10.10|10:2:2014
Smith |苹果|02|80.00|10:2:2014
Smith | PineApple | 07 | 50.20 | 10:12:2014
Finn|PineApple|08|55.55|10:2:2014
Charlie |苹果|07|20.85|10:2:2014
里希|土豆|02|29.99|10:2:2014
Gracy |葡萄|09 |47.74 |10:2:2014
Bruce|Bananas|05|32.20|10:12:2014
Rishi | Tomato | 03 | 34.43 | 12:13:2014
Rishi | Kiwi | 10 | 88.99 | 12:13:2014
百事可乐|鸡肉|18|14.30|12:13:2014
Sajan |马铃薯| 15 | 99.90 | 2014年12月13日
John | Grapes | 09 | 78.78 | 2014年12月13日
Sajan|PineApple|07|25.45|12:13:2014
Vijay|Kiwi | 10 | 88.98 | 12:25:2014
百事可乐|鸡肉|13|94.92|12:25:2014
之后:
客户名称|商品名称|#方框|成本|订单日期
周|胡萝卜|08|90.09|10:2:2014
Sajan|Oranges|20|87.00|10:12:2014
百事可乐|香蕉|05|10.10|10:2:2014
Smith |苹果|02|80.00|10:2:2014
Finn|PineApple|08|55.55|10:2:2014
Smith | PineApple | 07 | 50.20 | 10:12:2014
里希|土豆|02|29.99|10:2:2014
Charlie |苹果|07|20.85|10:2:2014
Gracy |葡萄|09 |47.74 |10:2:2014
Bruce|Bananas|05|32.20|10:12:2014
Rishi | Kiwi | 10 | 88.99 | 12:13:2014
Sajan |马铃薯| 15 | 99.90 | 2014年12月13日
John | Grapes | 09 | 78.78 | 2014年12月13日
编辑
bool lessThan ( Orders orders[],int x)
{
if (orders[x-1].costofitem.dollars < orders[x].costofitem.dollars)
return true;
else if (orders[x-1].costofitem.dollars == orders[x].costofitem.dollars)
return orders[x-1].costofitem.cents < orders[x].costofitem.cents;
return false;
}
void priority (Orders orders[],int totalOrders)
{
for (int p=0; p<totalOrders; p++)
{
for (int x=0;x<totalOrders-1;x++)
{
if ((orders[x+1].orderDate.year == orders[x].orderDate.year) &&
(orders[x+1].orderDate.month == orders[x].orderDate.month) &&
(orders[x+1].orderDate.day == orders[x].orderDate.day) &&
(lessThan(orders,x))
{
swap(orders[x+1].customerName, orders[x].customerName);
swap(orders[x+1].itemName, orders[x].itemName);
swap(orders[x+1].numberOfBoxes, orders[x].numberOfBoxes);
swap(orders[x+1].costofitem.dollars, orders[x].costofitem.dollars);
swap(orders[x+1].costofitem.cents, orders[x].costofitem.cents);
swap(orders[x+1].orderDate.month, orders[x].orderDate.month);
swap(orders[x+1].orderDate.day, orders[x].orderDate.day);
swap(orders[x+1].orderDate.year, orders[x].orderDate.year);
}
}
}
您尝试对列表进行多次排序,分别对每个元素进行排序。对于您的问题,一个更常见的解决方案是根据您想要的特定顺序成对比较元素。当STL可以更有效地为您完成任务时,编写自己的排序函数是不必要的。您可以使用std::sort和lambda函数对结构进行排序(如果您的编译器支持C++11)
std::sort(shipments, shipments+NO_OF_SHIPMENTS, [] (const Shipment &s1, const Shipment &s2)
{
return s1.expdate.year != s2.expdate.year ? s1.expdate.year < s2.expdate.year :
s1.expdate.month != s2.expdate.month ? s1.expdate.month < s2.expdate.month :
s1.expdate.day != s2.expdate.day ? s1.expdate.day < s2.expdate.day :
s1.priceOfItem.dollar*100+s1.priceOfItem.cents < s2.priceOfItem.dollar*100+s2.priceOfItem.cents;
});
如果编译器不支持C++11,则可以重写Shipment的运算符<以类似的方式。
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 助记符和指向成员语法的指针
- 用于访问容器<T>数据成员的正确 API
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 好友与成员运算符重载的优先级
- 在优先级队列被推送到队列后,如何编辑对象成员
- C :原始成员与继承成员的优先级
- 标准的哪一部分决定了现场成员与初始化器列表的优先级
- 运算符重载,成员函数和非成员函数,哪一个具有优先级
- 优先级队列成员函数不起作用
- 通过指针递增成员 int 时,++ 与 -> 的运算符优先级如何工作?
- 使用冒泡排序c++对结构数组的成员进行优先级排序
- 访问类的私有成员的自定义优先级队列比较器
- 优先级队列没有名为 top() 的成员
- 以C++和运算符优先级键入指向数组成员的强制转换指针
- const成员函数的优先级高于返回值类型匹配