0

我在编写这个程序时遇到了一些问题。我相信我需要重载 > 运算符,但我不知道该怎么做。有人可以为我解释一下吗?

到目前为止我的代码(不完整):

#include <iostream>
#include <fstream> //Includes the file stream for inputting and outputting files.
#include <string> //Fixes the error i get with some operands.
#include <iomanip> //Helps with report formatting.

using namespace std;

void sort(record A[], int size, int sortField);

struct record
{
string CustId, SPID, CustLN, CustFN;
int Q1;
double P1;
int Q2;
double P2;
int Q3;
double P3;
string LOD;
bool ShipRec;
string NCD, PreMethod;
double TotalSales;
};

int main()
{
ifstream InFile("master.txt");
ofstream OutFile1("report1.txt");
ofstream OutFile2("report2.txt");
ofstream OutFile3("report3.txt");
ofstream OutFile11("report11.txt");
ofstream OutFile13("report13.txt");
ofstream OutFile15("report15.txt");

record Customer[100];
while (!InFile.eof())
{
    int i = 0;

    InFile >> Customer[i].CustId >> Customer[i].SPID >> Customer[i].CustLN >>     Customer[i].CustFN >> Customer[i].Q1 >> Customer[i].P1 >> Customer[i].Q2 >> Customer[i].P2 >> Customer[i].Q3 >> Customer[i].P3 >> Customer[i].LOD >> Customer[i].ShipRec >> Customer[i].NCD >> Customer[i].PreMethod;

    Customer[i].TotalSales = (Customer[i].Q1 * Customer[i].P1) + (Customer[i].Q2 * Customer[i].P2) + (Customer[i].Q3 * Customer[i].P3);


}


}

void sort(record A[], int size, int sortField)
{
if (sortField = 1)
{
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
            if (A[j] > A[j + 1])
            {
                record temp = A[j];
                A[j] = A[j + 1];
                A[j + 1] = temp;
            }
        }
    }
}

else if (sortField = 2)
{
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
            if (A[j] > A[j + 1])
            {
                record temp = A[j];
                A[j] = A[j + 1];
                A[j + 1] = temp;
            }
        }
    }
}

else if (sortField = 3)
{
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
            if (A[j] > A[j + 1])
            {
                record temp = A[j];
                A[j] = A[j + 1];
                A[j + 1] = temp;
            }
        }
    }
}

else if (sortField = 11)
{
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
            if (A[j] > A[j + 1])
            {
                record temp = A[j];
                A[j] = A[j + 1];
                A[j + 1] = temp;
            }
        }
    }
}

else if (sortField = 13)
{
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
            if (A[j] > A[j + 1])
            {
                record temp = A[j];
                A[j] = A[j + 1];
                A[j + 1] = temp;
            }
        }
    }
}

else if (sortField = 15)
{
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
            if (A[j] > A[j + 1])
            {
                record temp = A[j];
                A[j] = A[j + 1];
                A[j + 1] = temp;
            }
        }
    }
}

}
4

2 回答 2

1

sort 字段是你的函数的一个参数,所以你不能写一个常规的operator<,因为它不能看到那个参数的值。(除非你让它全球化。不要那样做)。

假设没有特殊原因您必须编写自己的O(n 2 )算法,最简单的解决方案是:

class SortByField {
  int sortField_;
public:
  explicit SortByField(int f) : sortField_(f) {}
  // default copy ctor is fine
  bool operator() (record const &a, record const &b) const;
};

void sort(record A[], int size, int sortField) {
  std::sort(A, A+size, SortByField(sortField));
}

现在,剩下的就是编写函数调用运算符。我猜它应该看起来像这样:

bool SortByField::operator() (record const &a, record const &b) const {
  switch (sortField_) {
  case 1: return a.CustID < b.CustID;
  case 2: return a.SPID < b.SPID;
  // ...
  default: return false; // maybe we should check sortField is valid before this?
  }
}
于 2013-11-13T00:26:30.710 回答
0

我假设sortField选择要比较的字段。如果是这种情况,您应该将代码更改为:

if (sortField == 1) // <-- use '==' for comparisson
{
  for (int i = 0; i < size; i++)
  {
    for (int j = 0; j < size-1; j++) // <-- should probably be size-1
    {
      if (A[j].CustId > A[j + 1].CustId) // <-- test the sortField
      {
        record temp = A[j];
        A[j] = A[j + 1];
        A[j + 1] = temp;
      }
    }
  }
}

同样对于其他sortField

于 2013-11-13T00:09:26.980 回答