C++ 使用冒泡排序c+对结构数组的成员进行优先级排序+;

C++ 使用冒泡排序c+对结构数组的成员进行优先级排序+;,c++,arrays,sorting,C++,Arrays,Sorting,我正在尝试编写一个函数,对一组结构顺序进行优先级排序。订单必须按最早日期排定优先级,但如果订单的日期相同,则必须按项目的最大成本排定优先级 我已经使用三个函数按日期对数组进行了排序,这三个函数分别对年、月和日进行气泡排序。我想我可以在订单日期和之前订单日期相等的条件下,对订单项目的成本进行冒泡排序。该函数没有对列表进行完全排序,我似乎无法理解为什么。。。如果你能帮我一点忙,我将不胜感激 这是我的密码: #include <iostream> #include <string&g

我正在尝试编写一个函数,对一组结构顺序进行优先级排序。订单必须按最早日期排定优先级,但如果订单的日期相同,则必须按项目的最大成本排定优先级

我已经使用三个函数按日期对数组进行了排序,这三个函数分别对年、月和日进行气泡排序。我想我可以在订单日期和之前订单日期相等的条件下,对订单项目的成本进行冒泡排序。该函数没有对列表进行完全排序,我似乎无法理解为什么。。。如果你能帮我一点忙,我将不胜感激

这是我的密码:

#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;
  }
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
结构日期{
国际日;
整月;
国际年;
};
结构SizeOfBox{
整数长度;
整数宽度;
内部高度;
};
结构价格项目{
整数美元;
整数美分;
};
结构装运{
字符串食物名;
日期expdate;
SizeOfBox SizeOfBox;
箱子的浮动重量;
炭存储法;
收到装船日期;
价格项目价格项目;
};
结构客户{
字符串客户名称;
字符串城市;
整数距离;
};
结构命令{
字符串客户名称;
字符串itemName;
整数框;
价格项目成本项目;
订单日期;
};
const int NO_OF_装运=100;
装运装运[装运数量];
作废getDataOrders(ifstream和infle);
无效显示订单(订单[],整数总订单);
无效订单(订单[],国际总订单);
无效订单月(订单[],整数总订单);
作废订单日期(订单[],整数总订单);
无效优先级(订单[],整数总订单);
void createoutfile(char inName[],string&outName);
int main()
{
ifstreamindataorders;
流出的液体;
inDataOrders.open(“Orders.txt”);
如果(!inDataOrders)
{
订单[i].客户名称;
填充>>订单[i]。项目名称;
填充>>订单[i]。数量框;
填充>>订单[i].costofitem.dollars>>十进制>>订单[i].costofitem.cents;
填充>>订单[i].orderDate.month>>十进制>>订单[i].orderDate.day>>十进制>>订单[i].orderDate.year;
}
infle.close();
sortYear(订单、totalOrderItems);
sortMonth(订单、totalOrderItems);
sortDay(订单、totalOrderItems);
优先级(订单、totalOrderItems);
显示订单(订单、totalOrderItems);
}
无效显示订单(订单[],整数总订单)
{

您可以尝试对列表进行多次排序,每个元素单独排序。对于您的问题,更常见的解决方案是按所需的特定顺序成对比较元素。当STL可以更有效地为您进行排序时,编写自己的排序函数是不必要的。您可以使用std::sort和lambda对结构进行排序函数(如果编译器支持C++11)

std::排序(装运,装运+无装运,[](常量装运和s1,常量装运和s2)
{
返回s1.expdate.year!=s2.expdate.year?s1.expdate.year

如果您的编译器不支持C++11,您可以以类似的方式重写Shipping的运算符。

问题在于优先级函数中costofItem之间的比较。我修复了这个问题,编写了一个单独的函数来比较货币,并使用该布尔值作为条件。我现在可以理解您编写代码的意图-所以一年一次rt,然后一年一个月等等。虽然它可以工作,但它为可以用几行写的东西编写了大量代码。这很容易出错,因为代码越长,出现错误的机会就越大。此外,它效率不高。而且其他人更难阅读。我建议你学习如何使用STL让你的编码更容易、更快乐、更好。
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);

            }

    }

  }
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;
 });