2009-10-17 2 views
1

대학용 C++ 코드에 문제가 있습니다. 내 sRecSort() 메서드가 작동하지 않는 이유를 이해할 수 없습니다.정렬 방법이 작동하지 않는 이유는 무엇입니까?

어떤 도움이 필요합니까? 이것은 나에게 정말로 혼란 스럽다!

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

void sRecSort(string n[], int s[], string e[], int len){ 
for (int i = 0; i < len; i++){ 
    for (int j = 1; j < len; j++){ 
    if (s[j] < s[i]){ 
    string tempName = " "; 
    string tempName2 = " "; 
    int tempGrade,tempGrade2; 
    string tempEmail = " "; 
    string tempEmail2 = " "; 
    tempName = n[i]; 
    tempName2 = n[j]; 
    tempGrade = s[i]; 
    tempGrade2 = s[j]; 
    tempEmail = e[i]; 
    tempEmail2 = e[j]; 

    s[i] = tempGrade2; 
    s[j] = tempGrade; 
    n[i] = tempName2; 
    n[j] = tempName; 
    e[i] = tempEmail2; 
    e[j] = tempEmail; 
    } 
    } 
} 
} 
void printLowestRecord(char inFileName[]){ 
string tempSubString = " "; 
string names[12] = {" "}; 
int grades[12] = {0}; 
string emails[12] = {""}; 
int firstSpace = -1; 
int secondSpace = -1; 
ifstream inputMe(inFileName); 
while (!inputMe.eof()){ 
    for (int i = 0; i < 12; i++){ 
    getline(inputMe, tempSubString); 
    for (int w = 0; w < strlen(tempSubString.c_str()); w++){ 
    if (tempSubString[w] != ' '){ 
    continue; 
    } 
    else{ 
    if (firstSpace == -1){ 
     firstSpace = w; 
    } 
    else if (firstSpace != -1 && secondSpace == -1){ 
     secondSpace = w; 
     names[i] = tempSubString.substr(0, firstSpace); 
     grades[i] = atoi((tempSubString.substr(firstSpace + 1, secondSpace - (firstSpace + 1))).c_str()); 
     emails[i] = tempSubString.substr(secondSpace + 1, tempSubString.length() - (secondSpace + 1)); 
     break; 

    } 
    } 
    } 
    firstSpace = -1; 
    secondSpace = -1; 
    } 
} 

sRecSort(names,grades,emails,12); 
    cout << names[0] << " " << grades[0] << " " << emails[0] << endl; 
inputMe.close(); 
} 

void sortFileRecords(char inFileName[], char outFileName[]){ 
ifstream inputFile(inFileName); 
ofstream outputFile(outFileName); 
string tempSubString = " "; 
string names[12] = {" "}; 
int grades[12] = {0}; 
string emails[12] = {" "}; 
int firstSpace = -1; 
int secondSpace = -1; 
while (!inputFile.eof()){ 
    for (int i = 0; i < 12; i++){ 
    getline(inputFile, tempSubString); 
    for (int w = 0; w < strlen(tempSubString.c_str()); w++){ 
    if (tempSubString[w] != ' '){ 
    continue; 
    } 
    else{ 
    if (firstSpace == -1){ 
     firstSpace = w; 
    } 
    else if (firstSpace != -1 && secondSpace == -1){ 
     secondSpace = w; 
     names[i] = tempSubString.substr(0, firstSpace); 
     grades[i] = atoi((tempSubString.substr(firstSpace + 1, secondSpace - (firstSpace + 1))).c_str()); 
     emails[i] = tempSubString.substr(secondSpace + 1, tempSubString.length() - (secondSpace + 1)); 
     break; 
    } 
    } 
    } 
    firstSpace = -1; 
    secondSpace = -1; 
    } 
} 
int tempSmallest = grades[0]; 
int idxCatch = 0; 
for (int x = 1; x < 12; x++){ 
    if (grades[x] < tempSmallest){ 
    tempSmallest = grades[x]; 
    idxCatch = x; 
    } 
} 

for (int e = 0; e < 12; e++){ 
    cout << names[e] << " " << grades[e] << " " << emails[e] << endl; 
} 
//string tmpStringForInt = " "; 
//stringstream tmpSS; 
/*for (int q = 0; q < 12; q++){ 
    tmpSS << grades[q]; 
    tmpStringForInt = tmpSS.str(); 
    outputFile << names[q] << " " << tmpStringForInt << " " << emails[q] << endl; 
}*/ 
inputFile.close(); 
outputFile.close(); 
} 

int main (int argc, char * const argv[]) { 
printLowestRecord("gradebook.txt"); 
sortFileRecords("gradebook.txt", "sortedGradebook.txt"); 
    return 0; 
} 
+3

메소드가 작동하지 않는다고 명시 적으로 말하면 유용 할 수 있습니다. 그냥 작동하지 않는다고 말하는 것은별로 도움이되지 않습니다. – mgbowen

+0

실제로 전달하는 배열은 변경되지 않습니다. –

+0

또는 오히려, * 어떻게 * 작동하지 않습니다. 방법을 말해 주면 그 이유를 결정하는 데 도움이됩니다. :) – chaos

답변

2

좋아, 문제는 내부 루프 상태입니다. 정확히 어디에서 숙제인지는 말할 수 없습니다.

for (int i = 0; i < len; i++){ 
    for (int j = 1; j < len; j++){ // <--- this line is wrong 

"정렬 된"배열의 첫 번째 요소가 올바르게 가장 낮습니다. 그러나 나머지는 ...

P.S. 이 문제와 관련이 없지만 C++ 책의 구조에 대한 장을 읽으십시오.

P.P.S. 내가 상상할 수있는 최악의 정렬 알고리즘을 선택하셨습니다. 적어도 "Bubble sort"을 시도하십시오.

+0

사실, OP는 그가 Selection Sort를 사용하기로되어 있다는 의견에 언급했습니다. (현재 위키 백과로 가서 준비된 소스를 복사하거나 의사 코드를 구현하기가 쉽지 않습니다.) 교육적인 목적으로는 코드를 보지 않고 알고리즘을 이해하고 구현하는 것이 좋습니다. – UncleBens

+0

감사합니다. "선택 정렬"의 이름을 알지 못했기 때문에 풍선 정렬을 대신 제안했습니다. :) 그러나 내가 오류 뒤에있는 알고리즘은 실제로 선택 정렬보다 ** ** 나쁘다. –

4

알고리즘이 파열되어 파벨이 올바른 방향으로 지적했는데 샘이 좋은 대안을 제시했습니다.

그러나 실제 문제는 체계적인 방식으로 문제에 접근하지 않아 더 큰 문제로 이동하기 전에 더 작고 단순한 문제를 해결한다는 것입니다. 먼저 단일 정렬 알고리즘을 작성하여 단일 배열을 정렬하고 단위 테스트 프로그램과 함께 코드를 수행해야합니다. 다음으로는 여러 개의 배열로 이동 했어야합니다 (또는 구조가 더 정확함). 그리고 단위 테스트는 여전히 작동 함을 증명합니다. 작동하는 시스템을 구축하고 원하는 모든 작업을 수행 할 때까지이 방식으로 계속하십시오.

소프트웨어 개발 프로세스에 오신 것을 환영합니다.

관련 문제