web-dev-qa-db-ja.com

構造体でC ++ソート

私は、ある種の顧客名、顧客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
}
16
abin mavumkal

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

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

39

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

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

std::sort( customers, customers+Count, &customer_sorter);
13
CodeBuddy

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

これがis宿題である場合、ここでそれを行う方法を尋ねる必要はありません...

2
Remus Rusanu

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を使用します)。

1
Uri

私はあなたがプログラミングまたは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;
}
0