2014-12-13 2 views
0

데이터 형식 Student 배열 student_database을 버블 정렬을 사용하여 멤버 ID 배열이 최소에서 최대까지 정렬하려고합니다. 나는 C++ 교과서에서 예제를 복사했다. 프로그램이 컴파일되지만 배열에는 아무런 영향을주지 않습니다. 포인터를 사용해야합니까? 혼란스러워, 고마워.C++에서 버블 정렬을 사용하여 구조 배열의 멤버 정렬

using namespace std; 
#include <iostream> 
#include <string> 
#include <fstream> 

struct Student 
{ 
    int ID; 
    int examscore[3]; 
    int examtotal; 
    string lettergrade; 
}; 

void inputinfo(Student[], int&); 
void lettergrade(Student[], int); 
void countgrades(Student[], int); 
void IDsort(Student[], int); 
void sorthi(Student[], int); 
void sortlow(); 
void maxexam(Student[], int, int); 
void outputall(Student[], int); 
void outputstudent(Student[]); 
ifstream infile; 
ofstream outfile; 

int main() 
{ 
    string filename; 
    int numofstudents = 0; 
    int operation; 
    int user; 
    cout << "Enter the filename: "; 
    cin >> filename; 
    infile.open(filename); 
    if (infile.fail()) // checks to see if file is opened correctly 
    { 
     cout << "FILE_OPEN_FAILURE > CHECK_TO_SEE_IF_THE_FILE_IS_IN_THE_SAME_FOLDER \n_AS_YOUR_PROGRAM_FILE \nCHECK_YOUR_SPELLING_AS_WELL" << endl; 
    } 

    Student student_database[300]; 
    inputinfo(student_database, numofstudents); 
    outputall(student_database, numofstudents); 
    cout << endl << "Class size of: " << numofstudents << endl; 

    do { 
     cout << "Welcome. Enter a number from the menu to display the requested information." << endl << "---------------------------------------------------------" << endl; 
     cout << "1. Student with the highest score on exam 1" << endl; 
     cout << "2. Student with the highest score on exam 2" << endl; 
     cout << "3. Student with the highest score on exam 3" << endl; 
     cout << "4. Students ID in ascending numerical order" << endl; 
     cout << "5. Sort total exam scores from least to greatest" << endl; 
     cout << "6. Sort total exam scores from greatest to least" << endl; 
     cout << "7. Total number grade results for the entire class" << endl; 
     cin >> operation; 
     cout << endl; 

     switch (operation) 
     { 
     case 1: 
      maxexam(student_database, numofstudents, operation); 
      break; 
     case 2: 
      maxexam(student_database, numofstudents, operation); 
      break; 
     case 3: 
      maxexam(student_database, numofstudents, operation); 
      break; 
     case 4: 
      IDsort(student_database, numofstudents); 
      break; 
     case 5: 
      sorthi(student_database, numofstudents); 
      break; 
     case 6: 
      //sortlow(); 
      break; 
     case 7: 
      countgrades(student_database, numofstudents); 
      break; 
     default: 
      break; 
     } 
     cout << "Would you like to request more information?"; 
     cout << endl << "1. Yes? 0. No?" << endl; 
     cout << "Answer: "; 
     cin >> user; 
     cout << endl << endl; 
    } while (user == 1); 
} 

void IDsort(Student student_database[], int numofstudents) 
{ 
    bool swap = false; 
    Student temp; 
    while (!swap) 
    { 
     swap = true; 
     for (int i = 0; i < (numofstudents - 1); i++) 
     { 
      if (student_database[i].ID > student_database[i + 1].ID) 
      { 
       temp = student_database[i]; 
       student_database[i] = student_database[i + 1]; 
       student_database[i] = temp; 
       swap = false; 
      } 
     } 
    } 
} 
+1

당신이 디버거에서 코드를 밟은 적이 있습니까? – OldProgrammer

답변

1

이 스왑은 잘못된 것입니다 :

temp = student_database[i]; 
student_database[i] = student_database[i + 1]; 
student_database[i] = temp; 

당신은 student_database[i]을 변경하지만 이전 값으로 재 할당. student_database[i + 1]을 업데이트해야합니다. 더 나은

student_database[i + 1] = temp; 

어쨌든 std::swap() 사용 :

std::swap(student_database[i], student_database[i + 1]);