質問

私は、顧客名、顧客ID、そして最後に伴う金額のソートを必要とし、この問題に苦労しています。私は、プログラム全体を考え出していますが、ソートを行うために必要な最後のプロトタイプを把握することはできません。私は、構造体は、顧客と呼ばれており、私はまた、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
}
役に立ちましたか?

解決

あなたは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と呼ばれます。

他のヒント

ITSは、多くの場合、あなたが実際にあなたの例のように、CベースのアレイでSTLの範囲の機能を使用できることを見落とし。だから、実際には(私がやってのメリットを議論しませんここでその: - )STLベースのコンテナを使用して上に移動する必要はありません。)

次のように

だから、クリスからの回答を踏まえ、あなたはソートを呼び出すことができます:

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

あなたは2つのだけCustomerProfileタイプを比較する比較関数を記述する必要があります。あなたがこの機能を持っていたら、(STLのソートのいずれかを使用のhttpを見ることができます:// WWWを.sgi.com /ハイテク/ STL / sort.htmlするまたは<のhref = "http://msdn.microsoft.com/en-us/library/ecdecxh1(VS.80).aspx" のrel =」 noreferrer nofollowを "> http://msdn.microsoft.com/en-us/library/ecdecxh1(VS.80).aspxのの)または古いCのqsort:<のhref =" のhttp:// EN。 wikipedia.org/wiki/Qsort_(C_Standard_Library)」のrel = "nofollowをnoreferrer"> 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).
これはの宿題である場合は、

さて、あなたは

...それを行う方法をここで尋ねるべきではありません

あなたは創造のグーグルとC ++でのソートの実装の多くを見つけることができます。.. 唯一の違いは、代わりに数字を並べ替えの、あなたは構造体のソートされていることである。

あなたが使用するアルゴリズムでif(a[i]<a[j])のようなものがある場所に

だから、 `のような呼び出しを行う場合(isFirstCustomerLowerThanOther([I]

この今、次の構造と機能を作成します:

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

さらに良いことに、あなたがCを使用する場合は、STLのソートalgortihmを使用することができ++(再び、情報のため、それに順序を渡す方法については、グーグルます。

私はあなたがプログラミングや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;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top