2016-09-16 8 views
-6

私は割り当てが終わったばかりですが、この最後のセクションは私を殺しています。私は研究を行いました。そのソリューションは、クラスで教えられていない概念であり、私の頭の中に完全にあります。私は構造体のソートと配列をbubbleする必要があります。しかし、私は完全に失われています。どんな助けでも大歓迎です。バブル並べ替え構造体の配列

#include "stdafx.h" 
#include <iostream> 
#include <fstream> 
#include <string> 
#include <algorithm> 

struct billingInfo; 
void bubbleSort(billingInfo list[], int length); 
using namespace std; 

int main() 
{ 
    // create struct with a field for zip codes and name of recepients that are being imported from text document 
     struct billingInfo { 
     int zipCode; 
     string name; 
    }; 

    // create array of billingInfo struct 
    billingInfo recipients[20]; 

    // open text file and test if file opened 
    ifstream dataFile("NameZip.txt"); 

    if (dataFile.fail()) 
     cout << "Can't open file." << endl; 
    else cout << "File opened." << endl; 

    int numElements = 0; 
    while (dataFile.peek() != EOF) { 
     dataFile >> recipients[numElements].zipCode; 
     getline(dataFile, recipients[numElements].name); 

     cout << recipients[numElements].zipCode << endl; 
     cout << recipients[numElements].name << endl; 
     numElements++; 
    } 

    system("pause"); 

    return 0; 
} 

void bubbleSort(billingInfo list[], int length) { 
    billingInfo temp; 
    int itterator; 
    int index; 
    for (itterator = 1; itterator < length - itterator - length; itterator++) { 
     for (index = 0; index < length - itterator; index++) { 
      temp = list[index]; 
      list[index] = list[index + 1]; 
      list[index + 1] = temp; 
     } 
    } 
    enter code here 

} 
+0

正確にあなたが経験している問題は何ですか?また、それはイテレータの綴り方ではありません – xaxxon

+0

"_Bubbleは構造体の配列をソートする" - 'struct'は参照データ型であるため、この型の変数(オブジェクト)はパラメータとして使用できるプリミティブな比較可能な値を持ちませんソートのために。構造体のメンバ変数の値に基づいて 'struct'オブジェクトを評価する必要があります。あなたのコードでは、唯一の数値は 'int zipCode'によって保持されます。どのように正確に並べ替えを求められましたか?郵便番号の値の昇順/降順であるはずですか? – progyammer

+0

あまりにも漠然として申し訳ありません。私はフィールド名をアルファベット順にソートしています。バブルソート –

答えて

0
struct billingInfo 
{ 
    int key; // Can be any key 

}; 
typedef struct billingInfo billingInfo; 

// Overload the > operator 
bool operator> (const billingInfo& first,const billingInfo& second) 
{ 
    return first.key > second.key; 
} 

// Make a custom Swap function for your struct 
void swap (billingInfo& first,billingInfo& second) 
{ 
    int temp; 
    temp = first.key; 
    first.key = second.key; 
    second.key = temp; 
} 

// Not a single line of change in Bubble Sort Code. 
// I hope the below code is bubble sort :p 
void bubble_sort (billingInfo *list,int length) 
{ 
    for (int i=0;i<length-1;i++) 
     for (int j=0;j<length-i-1;j++) 
      if (list[j] > list[j+1]) // will work since we overloaded 
       swap (list[j],list[j+1]); // Our custom function 
} 
+0

Plsは正解であればそれを受け入れ、upvoteします。 – PRP

関連する問題