2016-09-26 1 views
1

그래서 사용자가 5 가지 유형의 정보가있는 특정 양의 구조체를 만든 다음 해당 유형 중 하나를 기준으로 정렬 할 수 있도록해야합니다. 예를 들어, 모든 데이터를 입력 한 후 등급 또는 이름순으로 정렬합니다. 다른 구조체에 걸쳐 다른 이름이나 등급의 배열을 만드는 방법은 어떻게됩니까?여러 구조체를 만든 다음 요소별로 정렬

#include <iostream> 
#include <string> 

struct student 
{ 
    std::string studentName; 
    std::string studentIDNumber; 
    int currentExamGrade; 
    int priorExamGrade; 
    double GPA; 
}; 

void createStudent() 
{ 
    int numStudents; 
    std::cout << "Enter number of students\n"; 
    std::cin >> numStudents; 

    while (numStudents > 0) 
    { 
     student name; 

     std::cout << "Enter the student's name\n"; 
     std::cin >> name.studentName; 

     std::cout << "Enter the student's ID number\n"; 
     std::cin >> name.studentIDNumber; 

     std::cout << "Enter the student's current exam grade\n"; 
     std::cin >> name.currentExamGrade; 

     std::cout << "Enter the student's prior exam grade\n"; 
     std::cin >> name.priorExamGrade; 

     std::cout << "Enter the student's GPA\n"; 
     std::cin >> name.GPA; 

     numStudents -= 1; 
    } 
} 

int main() 
{ 
    createStudent(); 

    int choice; 
    std::cout << "How do you want to sort the list?\n (Enter 1 for name, 2  for ID number, 3 for current exam grade, 4 for prior exam grade, 5 for GPA\n"; 
    std::cin >> choice; 

    return 0; 

}  
+0

그래서, Captn, 배열을 정렬하는 것 :

토크 싼 쇼 나 코드입니다? 학생은 괜찮 았지만 읽은 후에는 데이터를 버리고 다른 데이터를 읽습니다. –

+0

그건 내 질문의 일부입니다. – CptJohnMiller74

+1

좋습니다. [std :: vector] (http://www.cplusplus.com/reference/vector/vector/)에 대해 읽을 시간. 예제를 살펴보십시오. –

답변

0

확인. 내가 C에서 내장 된 정렬 기능을 모르겠다 + +. std::sort()

테이크 입력를 살펴 보자은 :

How do you make an array of structs in C?

Creating an array of structs in C++

,691,363 :

이제 input values into struct array + printing out

구조체의 배열을 만들려면 (210)

지금 이러한 배열을 정렬하려면이 문제에 접근하는 방법 Sorting a vector of custom objects

:

std::sort() 당신이하는 비 tivial 개체를 기반으로 사용자 정의 기능을 제공 할 수는 비교할 수 있습니다. 하나 개의 기능의 예는 다음과 같습니다

bool gpa_comparision_function (student s1,student s2) 
{ 
    return s1.GPA < s2.GPA; 
} 

이 정렬 기능에 의해 비교 될 필요가 2 명 학생으로 인자를 가지고 S1과 GPA에 따라 S2 학생들의 순서를 반환합니다.

이유문은 s1.GPA < s2.GPA이 중요한 이유입니다.

그래서 학생 (S1)는 GPA = 5.0S2가있는 경우 GPA는 = 4.0 우리가 순서 S2, S1 (오름차순) 그래서이 기능은, 그 S2를 주장하는 것은 S1 전에 거짓 와야한다 반환되고 싶어요있다.

그래서 정렬 함수의 호출 :

sort (arr_of_students,arr_of_students+N,gpa_comparision_function); 

통지 제 3 인수, gpa_comparision_function, 이것이 우리의 비교 함수를 사용하여 내장 기능을 말하고있다.

#include <iostream> 
#include <string> 
#include <algorithm> 
using namespace std; 
struct student 
{ 
    std::string studentName; 
    std::string studentIDNumber; 
    int currentExamGrade; 
    int priorExamGrade; 
    double GPA; 
}; 
bool gpa_comparision_function (student s1,student s2) 
{ 
    return s1.GPA < s2.GPA; 
} 
bool currentExamGrade_comparision_function (student s1,student s2) 
{ 
    return s1.currentExamGrade < s2.currentExamGrade; 
} 
int main() 
{ 
    // Create an array of structs 
    student arr_of_students[100]; // A better way will be vector<student> 

    // Take input 
    // ... 

    // Now lets sort, assuming number of students is N 
    // Based on GPA 
    sort (arr_of_students,arr_of_students+N,gpa_comparision_function); 

    // Based on currentExamGrade 
    sort (arr_of_students,arr_of_students+N,currentExamGrade_comparision_function); 

    return 0; 
} 
+0

코드 경로 제공 단계를 밟은 경우 적어도 모든 단계를 거쳐야합니다. 이러한 구조를 읽는 방법을 보여주고 ** 일어나는 일을 설명 할 수 있습니다 **. 그렇지 않으면 이것은 최악의 경우 허영심이나 명성을 얻는 것입니다. 배우는 것을 돕는 것과 아무런 관련이 없습니다. –

+0

@AdrianColomitchi 사용자 지정 비교 함수 사용에 대한 설명을 추가했습니다. 나는 입력이 어떻게 취해질지를 설명하지 않을 것이다. 나는 대답을 제공하는 링크를 발견 할 것이다. – PRP

관련 문제