2009-10-17 67 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

明确指出_why_该方法不起作用可能有用。只是说它不起作用不是很有帮助。 – mgbowen 2009-10-17 23:37:48

+0

它实际上并没有改变我传递给它的数组。 – 2009-10-17 23:39:05

+0

或者说,*如何*它不工作。如果你告诉我们如何,这有助于确定原因。 :) – chaos 2009-10-17 23:39:15

回答

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在评论中提到他应该使用选择排序(现在很简单,去维基百科,要么复制现成的源代码,要么实现伪代码 - 尽管出于教育目的,我会建议试着理解算法并自己实现它,而不需要查看任何代码)。 – UncleBens 2009-10-18 11:26:48

+0

谢谢,我不知道“选择排序”的名称,所以我建议泡泡排序:)。但是我在错误后面看到的算法实际上是比选择排序更糟的**。 – 2009-10-18 11:50:16

4

你的算法被破坏,帕维尔指出你在正确的方向,山姆给你一个很好的选择。

然而,你真正的问题在于,你没有系统地解决问题,解决更小,更简单的问题,然后再转向更大的问题。您应该先编写一个简单的排序算法来对单个数组进行排序,并使用单元测试程序对代码进行排序。接下来,你应该已经移动到了多个数组(因为你有(或者更准确的说,结构)和一个单元测试,证明它仍然有效。以这种方式继续下去,直到你建立了一个可以工作的系统,并且做到了你想要的。

欢迎来到软件开发流程。

相关问题