简体   繁体   English

为什么我的排序方法不起作用?

[英]Why won't my sorting method work?

I have an issue with my C++ code for college. 我的大学C ++代码有问题。 I can't seem to understand why my sRecSort() method isn't working. 我似乎无法理解为什么我的sRecSort()方法无法正常工作。

Any help? 有什么帮助吗? This is really confusing me! 这真让我感到困惑!

#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;
}

Your algorithm is busted, Pavel pointed you in the right direction, and Sam gave you a good alternative. 您的算法失败了,Pavel为您指明了正确的方向,而Sam为您提供了一个不错的选择。

Your real problem, however, is that you didn't approach the problem in a systematic manner, solving the smaller, simpler problems before moving on to the larger ones. 但是,您真正的问题是,您没有系统地解决问题,而是在解决较大的问题之前先解决了较小,较简单的问题。 You should have first written a simple sort algorithm to sort a single array, along with a unit test program them exercised the code. 您应该首先编写一个简单的排序算法来对单个数组进行排序,再加上一个单元测试程序来执行代码。 Next you should have moved to multiple arrays as you have (or, more correctly, structures) and a unit test that proved the thing still worked. 接下来,您应该已经拥有了多个数组(或者更正确地说是结构),并且已经通过一个单元测试证明了它仍然可以工作。 Continue in this manner until you've built a system that works and does all you want. 以这种方式继续操作,直到您构建了一个可以正常运行并满足您所有需求的系统。

Welcome to the Software Development Process. 欢迎来到软件开发过程。

Okay, the problem is in the inner loop condition. 好的,问题出在内循环条件下。 Can't tell you where exactly -- that's a homework. 无法告诉您确切的位置-这是一项作业。

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

The first element of your "sorted" array will correctly be the lowest. 您的“已排序”数组的第一个元素将正确地是最低的。 But the others... 但是其他...

PS Irrelevant to the problem, but, please, read a chapter about structures in your C++ book. PS与该问题无关,但请阅读C ++书籍中有关结构的章节。

PPS You have chosen the worst sorting algorithm I could possibly imagine. PPS您选择了我可能想象的最差的排序算法。 Try "Bubble sort" , at least. 至少尝试“冒泡排序”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM