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

标签 c++ sorting input

我的大学 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;
}

最佳答案

您的算法失败了,Pavel 为您指明了正确的方向,而 Sam 为您提供了一个不错的选择。

然而,您真正的问题是您没有以系统的方式处理问题,没有在解决更大、更简单的问题之前解决更小、更简单的问题。您应该首先编写了一个简单的排序算法来对单个数组进行排序,并编写了一个单元测试程序来执行代码。接下来,您应该已经转移到您拥有的多个数组(或者更准确地说,结构)和一个证明事情仍然有效的单元测试。以这种方式继续,直到您构建了一个可以正常工作并且可以满足您所有需求的系统。

欢迎来到软件开发流程。

关于c++ - 为什么我的排序方法不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1583509/

相关文章:

java - 通过 jni 将相同的 Android surfaceview 传递给 C++ 代码会导致不同的值

c++ - 带括号的成员函数的地址错误

Javascript:将包含日期的数组按降序排序

mysql - 从表中检索离某个日期最近的前 10 个结果并保持升序排序

javascript - 为什么我无法在 javascript 代码中获取输入值

javascript - AngularJS 双向绑定(bind)。返回用户插入的输入

c++ - c++类成员函数的前向定义

c++ - 为什么我的有线球体在平移和改变摄像机角度时会变成椭球体?

javascript - 如何使用 Angular2 管道按离今天最近的日期排序

php - 过滤输入和事件