c ++与结构sorting

这个问题需要一些客户姓名,客户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 } 

你应该使用C ++的标准sorting函数std::sort ,在<algorithm>头文件中声明。

当使用自定义sortingfunction进行sorting时,必须提供谓词函数 ,该函数说明左侧值是否小于右侧值。 所以,如果你想按姓名sorting,然后按ID,然后按数量sorting,所有按升序排列,你可以这样做:

 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容器(而不是一个数组,就像你在示例代码中所说的那样)包含客户的客户。

它经常被忽略,你可以实际使用基于C的数组的STL范围函数,就像你的例子。 所以你实际上不必转向使用基于STL的容器(我不会在这里讨论这样做的优点:-))。

所以,基于Chris的回答,你可以按如下方式调用sorting:

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

您只需编写一个比较两个CustomerProfiletypes的比较函数。 一旦你有这个function,你可以使用STLsorting(请参阅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 ) 。 我会build议不要编写自己的sortingalgorithm,除非这是一项家庭作业。 你的比较取决于你喜欢使用的技术,它可能看起来像这样:

 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 } 

这假定在你的例子中'string'types是一个char *。 如果使用Unicode或多字节types,那么必须使用适当的Unicode或多字节比较。 然后你可以用你的比较函数调用algorithm。 例如。 使用qsort:

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

现在,如果这一个家庭作业,你不应该在这里问如何做…

你可以在C ++中find很多类似的实现与创造性的谷歌search。唯一的区别是,而不是sorting数字,你正在sorting结构。

因此,只要在algorithm中有if(a[i]<a[j]) ,就可以调用if(isFirstCustomerLowerThanOther(a [i]

现在,创build一个具有以下结构的函数:

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

更好的是,如果你使用C ++,你可以使用STL的sortingalgortihm(同样,谷歌的信息和如何传递给它的顺序。

我假设你是编程或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; }