18

私はこの問題に苦労しており、ある種の顧客名、顧客ID、そして最終的には未払い額が必要になります。プログラム全体を把握しましたが、並べ替えに必要な最後のプロトタイプを把握できません。私はCustomersという構造体を持っており、int main()部分も提供します。プロトタイプSortData()でgtを開始するための助けが必要です。

struct Customers {
    string Name;
    string Id;
    float OrderAmount;
    float Tax;
    float AmountDue;
};

const int MAX_CUSTOMERS = 1000;
bool MoreCustomers(int);
Customers GetCustomerData();
void OutputResults(Customers [], int);
void SortData(const int, const int, Customers []);

int main() {
    Customers c[MAX_CUSTOMERS]; 
    int Count = 0;      
    do {
      c[Count++] = GetCustomerData();   
    } while (MoreCustomers(Count));     


    for (int i = 0; i < Count; i++) {
        c[i].Tax = 0.05f * c[i].OrderAmount;        
        c[i].AmountDue = c[i].OrderAmount + c[i].Tax;   
    }

    SortData(0, Count, c);     //0:Sorts by customer name       
    OutputResults(c, Count);            
    GeneralSort(1, Count, c);   //1:Sorts by ID     
    OutputResults(c, Count);        
    GeneralSort(2, Count, c);   //2: Sorts by amount due        
    OutputResults(c, Count);        

    return 0;                       
}


void SortData(const int SortItem, const int count, CustomerProfile c[]) {
     //0: Sort by name
    //1: Sort by ID
    //3: Sort by amount due
}
4

5 に答える 5

49

ヘッダーstd::sortで宣言されているC++の標準ソート関数を使用する必要があります。<algorithm>

カスタムソート関数を使用してソートする場合、左側の値が右側の値よりも小さいかどうかを示す述語関数を提供する必要があります。したがって、最初に名前で、次にIDで、次に未払い額で、すべて昇順で並べ替える場合は、次のように実行できます。

bool customer_sorter(Customer const& lhs, Customer const& rhs) {
    if (lhs.Name != rhs.Name)
        return lhs.Name < rhs.Name;
    if (lhs.Id != rhs.Id)
        return lhs.Id < rhs.Id;
    return lhs.AmountDue < rhs.AmountDue;
}

sort次に、その関数を呼び出しに渡します。

std::sort(customers.begin(), customers.end(), &customer_sorter);

これは、顧客を含むと呼ばれるSTLコンテナー(サンプルコードのような配列ではない)があることを前提としていcustomersます。

于 2009-05-17T01:48:55.447 に答える
14

あなたの例のように、実際にCベースの配列でSTL範囲関数を使用できることは見過ごされがちです。そのため、実際に STL ベースのコンテナーを使用する必要はありません (ここでは、それを行うメリットについては議論しません :-))。

したがって、Chris からの回答に基づいて、次のように並べ替えを呼び出すことができます。

std::sort( customers, customers+Count, &customer_sorter);
于 2009-05-17T10:47:45.373 に答える
2

2 つの CustomerProfile タイプを比較する比較関数を記述するだけで済みます。この機能があれば、STL ソート ( http://www.sgi.com/tech/stl/sort.htmlまたはhttp://msdn.microsoft.com/en-us/library/ecdecxh1を参照) を使用できます。 (VS.80).aspx ) または古い C qsort: http://en.wikipedia.org/wiki/Qsort_(C_Standard_Library)。これが宿題でない限り、独自のソートアルゴリズムを作成しないことをお勧めします。比較は、使用したいテクノロジーによって異なります。次のようになります。

int CompareCustomerProfile(
   const CustomerProfile* pC1,
   const CustomerProfile* pC2)
{
 int result = strcmp(pC1->name, pC2->name);
 if (0 != result) return result; 

  result = strcmp(pC1->ID, pC2->ID);
  if (0 != result) return result;

  if (pC1->amountDue < pC2->amountDue) return -1;
 if (pC1->amountDue > pC2->amountDue) return 1;

  return 0
}

これは、例の「文字列」タイプが char* であることを前提としています。Unicode またはマルチバイト型を使用する場合は、明らかに、適切な Unicode またはマルチバイト比較を使用する必要があります。次に、比較関数を使用してアルゴリズムを呼び出すだけです。例えば。qsortを使用:

qsort(c, Count, sizeof(CustomerProfile), CompareCustomerProfiler).

これ宿題なら、ここでやり方を聞くべきではありません...

于 2009-05-17T02:05:02.990 に答える
1

C ++には、クリエイティブなグーグルを使用した多くの並べ替えの実装があります。唯一の違いは、数値を並べ替える代わりに、構造体を並べ替えていることです。

したがってif(a[i]<a[j])、使用するアルゴリズムに次のようなものがある場合は、 `if(isFirstCustomerLowerThanOther(a [i]

次に、次の構造を持つ関数を作成します。

bool isFirstCustuomerLowerThanOther(const Customer& firstCustomer, const Customer& secondCustomer)
{
 // Implement based on your key preferences
}

さらに良いことに、C ++を使用する場合は、STLのソートアルゴリズムを使用できます(ここでも、情報と注文を渡す方法についてはgoogleを参照してください。

于 2009-05-17T01:47:15.043 に答える
0

プログラミングや C++ の初心者を想定しているため、おそらく探しているものは次のとおりです。

#include <search.h> // for the qsort()

int
CompareByName( const void *elem1, const void *elem2 )
{
  return ((Customers*)elem1)->Name > ((Customers*)elem2)->Name? 1 : -1;
}

int
CompareByOrderAmount( const void *elem1, const void *elem2 )
{
  return ((Customers*)elem1)->OrderAmount > ((Customers*)elem2)->OrderAmount? 1 : -1;
}

void SortData( int SortItem, int count, Customers customers[] )
{
  switch (SortItem) {
  case 0:
    qsort(customers, count, sizeof(Customers), CompareByName);
    break;
  case 1:
    qsort(customers, count, sizeof(Customers), CompareByOrderAmount);
    break;
  // ...
  }
}

void test()
{
  Customers cust[10];

  cust[0].Name = "ten";
  cust[1].Name = "six";
  cust[2].Name = "five";
  SortData( 0, 3, cust );
  cout << cust[0].Name << endl;
  cout << cust[1].Name << endl;
  cout << cust[2].Name << endl;
}
于 2009-05-17T11:52:03.067 に答える