为什么赢了';我的分类方法不行吗? 我对我的C++代码有一个问题。我似乎不明白为什么我的sRecSort()方法不起作用
有什么帮助吗?这真让我困惑为什么赢了';我的分类方法不行吗? 我对我的C++代码有一个问题。我似乎不明白为什么我的sRecSort()方法不起作用,c++,sorting,input,C++,Sorting,Input,有什么帮助吗?这真让我困惑 #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;
#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;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
void sRecSort(字符串n[],整数s[],字符串e[],整数len){
对于(int i=0;i cout好的,问题出在内环条件下。不能告诉你确切的位置——这是一个家庭作业
for (int i = 0; i < len; i++){
for (int j = 1; j < len; j++){ // <--- this line is wrong
for(int i=0;i 对于(intj=1;j
for (int i = 0; i < len; i++){
for (int j = 1; j < len; j++){ // <--- this line is wrong
for(int i=0;i 对于(intj=1;j
然而,你真正的问题是,你没有系统地解决这个问题,先解决更小、更简单的问题,然后再去解决更大的问题。你应该先编写一个简单的排序算法来对单个数组进行排序,然后编写一个单元测试程序,让他们执行代码。接下来,你应该转到多个数组按照你已经做过的(或者更准确地说,结构)和一个证明它仍然有效的单元测试。以这种方式继续,直到你构建了一个可以正常工作并满足你所有需求的系统
欢迎来到软件开发过程。您的算法失败了,Pavel为您指出了正确的方向,Sam为您提供了一个很好的选择
然而,你真正的问题是,你没有系统地解决这个问题,先解决更小、更简单的问题,然后再去解决更大的问题。你应该先编写一个简单的排序算法来对单个数组进行排序,然后编写一个单元测试程序,让他们执行代码。接下来,你应该转到多个数组按照你已经做过的(或者更准确地说,结构)和一个证明它仍然有效的单元测试。以这种方式继续,直到你构建了一个可以正常工作并满足你所有需求的系统
欢迎来到软件开发过程。明确说明方法不起作用的原因可能会很有用。仅仅说它不起作用并没有多大帮助。它实际上不会改变我传递给它的数组。或者更确切地说,它是如何起作用的。如果您告诉我们如何起作用,这有助于确定原因。:)抱歉,我是批评它的人没有回答问题,但如果您解释了如何对其进行排序,则更容易回答。此外,我是否建议不要使用单字母参数名称。我打算通过选择排序对其进行排序。明确说明方法不起作用的原因可能很有用。仅说它不起作用不是很有帮助。它不起作用ally更改我传递给它的数组。或者更确切地说,它是如何工作的。如果您告诉我们如何工作,这有助于确定原因。:)很抱歉,如果您不回答问题而提出批评,但如果您解释了如何对其进行排序,则回答会更容易。此外,我是否可以建议不要使用单字母参数名称。I实际上,OP在评论中提到他应该使用选择排序(现在很容易去维基百科,复制现成的源代码,或者实现伪代码——尽管出于教育目的,我建议尝试理解算法并自己实现,而不看任何代码)。谢谢,我不知道“选择排序”的名称,所以我建议改为气泡排序:)但我看到的错误背后的算法实际上比选择排序更糟糕。实际上,OP在评论中提到他应该使用选择排序(现在很容易去维基百科,复制现成的源代码,或者实现伪代码——尽管出于教育目的,我建议尝试理解算法并自己实现,而不看任何代码)。谢谢,我不知道“选择排序”的名称,所以我建议改为气泡排序:).但我看到的错误背后的算法实际上比选择排序更糟糕。