[英]Why won't my sorting method work?
我的大學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為您提供了一個不錯的選擇。
但是,您真正的問題是,您沒有系統地解決問題,而是在解決較大的問題之前先解決了較小,較簡單的問題。 您應該首先編寫一個簡單的排序算法來對單個數組進行排序,再加上一個單元測試程序來執行代碼。 接下來,您應該已經擁有了多個數組(或者更正確地說是結構),並且已經通過一個單元測試證明了它仍然可以工作。 以這種方式繼續操作,直到您構建了一個可以正常運行並滿足您所有需求的系統。
歡迎來到軟件開發過程。
好的,問題出在內循環條件下。 無法告訴您確切的位置-這是一項作業。
for (int i = 0; i < len; i++){
for (int j = 1; j < len; j++){ // <--- this line is wrong
您的“已排序”數組的第一個元素將正確地是最低的。 但是其他...
PS與該問題無關,但請閱讀C ++書籍中有關結構的章節。
PPS您選擇了我可能想象的最差的排序算法。 至少嘗試“冒泡排序” 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.