2016-09-16 3 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

정확하게 당신이 겪고있는 문제가 무엇입니까? 또한 iterator를 철자하는 방법이 아닙니다 – 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