使用冒泡排序c ++优先化结构数组的成员

时间:2015-02-22 07:00:58

标签: c++ arrays sorting

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

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

这是我的代码:

#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:

之前和之后的示例

之前:

客户名称|商品名称| #Boxes |成本|订单日期

Sajan |橘子| 20 | 87.00 | 10:12:2014

杰伊|胡萝卜| 08 | 90.09 | 10:12:2014

Pepp |香蕉| 05 | 10.10 | 10:12:2014

史密斯|苹果| 02 | 80.00 | 10:12:2014

史密斯| PineApple | 07 | 50.20 | 10:12:2014

芬恩| PineApple | 08 | 55.55 | 10:12:2014

查理|苹果| 07 | 20.85 | 10:12:2014

Rishi |马铃薯| 02 | 29.99 | 10:12:2014

Gracy |葡萄| 09 | 47.74 | 10:12:2014

布鲁斯|香蕉| 05 | 32.20 | 10:12:2014

Rishi |番茄| 03 | 34.43 | 12:13:2014

Rishi |猕猴桃| 10 | 88.99 | 12:13:2014

Pepp |鸡肉| 18 | 14.30 | 12:13:2014

Sajan |马铃薯| 15 | 99.90 | 12:13:2014

约翰|葡萄| 09 | 78.78 | 12:13:2014

Sajan | PineApple | 07 | 25.45 | 12:13:2014

维杰|猕猴桃| 10 | 88.98 | 12:25:2014

Pepp |鸡肉| 13 | 94.92 | 12:25:2014


后:

客户名称|商品名称| #Boxes |成本|订单日期

杰伊|胡萝卜| 08 | 90.09 | 10:12:2014

Sajan |橘子| 20 | 87.00 | 10:12:2014

Pepp |香蕉| 05 | 10.10 | 10:12:2014

史密斯|苹果| 02 | 80.00 | 10:12:2014

芬恩| PineApple | 08 | 55.55 | 10:12:2014

史密斯| PineApple | 07 | 50.20 | 10:12:2014

Rishi |马铃薯| 02 | 29.99 | 10:12:2014

查理|苹果| 07 | 20.85 | 10:12:2014

Gracy |葡萄| 09 | 47.74 | 10:12:2014

布鲁斯|香蕉| 05 | 32.20 | 10:12:2014

Rishi |猕猴桃| 10 | 88.99 | 12:13:2014

Sajan |马铃薯| 15 | 99.90 | 12:13:2014

约翰|葡萄| 09 | 78.78 | 12:13:2014


修改

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

            }

    }

  }

1 个答案:

答案 0 :(得分:0)

您尝试分别为每个元素对列表进行多次排序。对您来说,更常见的解决方案是按要求的特定顺序成对地比较元素。当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&#39运算符。以类似的方式。