【发布时间】:2025-12-17 08:50:02
【问题描述】:
因此,我已经为此工作了几个小时,并尝试在互联网上寻找更好的解决方案,但我无法为我的具体问题找到答案。
任务是... 计算每个名称中的字符数,然后将它们从最少字母到最多字母排序。
它们必须排序的格式如下:
名称 1 - 计数 1
Name2 - Count2
[...]
现在,我遇到的问题是:我可以成功地将int array[] 排列成升序,但是我需要string array[] 对应于int array[] 并并排输出。
以下是我拥有的以下代码。我已经尝试使用自己的知识尽我所能解决这个问题。如果有人能指出我正确的方向(不是为我解决),或者解释为什么我的while 函数没有按我的预期工作。不胜感激!~
cout << setw(34) << setfill('#') << "#\n";
cout << "#" << setw(33) << setfill(' ') << "#\n";
cout << "# Routine 7 #\n";
cout << "#" << setw(33) << setfill(' ') << "#\n";
cout << setw(35) << setfill('#') << "\n\n";
int alphaCount[26];
int sortedCount[26];
int temp;
string temp2;
string sortedNames[26];
string t;
for (int i = 1; i < 26; i++) {
string t = n.names[i];
t.erase(remove_if(t.begin(), t.end(), isspace), t.end());
alphaCount[i] = t.size();
}
for (int i = 0; i < 26; i++) {
sortedCount[i] = alphaCount[i];
}
for (int i = 0; i < 26; i++) {
for (int j = i + 1; j < 26; j++) {
if (sortedCount[i] > sortedCount[j]) {
temp = sortedCount[i];
sortedCount[i] = sortedCount[j];
sortedCount[j] = temp;
}
}
}
int i = 0;
while (i < 26) {
int j = 1;
while (j < 26) {
if (sortedCount[i] == n.names[j].length()) {
cout << n.names[j] << " - " << sortedCount[i] << endl;
i++;
}
if (sortedCount[i] != n.names[j].length()) {
j++;
}
}
}
systemClear();
}
如果您想查看所有代码,这里是完整的 cpp 文件。您正在专门查看例程 7
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
struct file {
string names[26];
};
void systemClear();
void routineOne(file& n);
void routineTwo(file& n);
void routineThree(file& n);
void routineFour(file& n);
void routineFive(file& n);
void routineSix(file& n);
void routineSeven(file& n);
void routineEight(file& n);
int main()
{
file n;
//Function Execution
routineOne(n);
routineTwo(n);
routineThree(n);
routineFour(n);
routineFive(n);
routineSix(n);
routineSeven(n);
routineEight(n);
}
void systemClear() {
short a = 0;
cout << "\n\nPlease enter any number to continue: ";
cin >> a;
if (a != 0) {
system("cls");
return;
}
else {
system("cls");
return;
}
}
void restart() {
char a = 0;
cout << "\n\nWould you like to restart the program? (y/n): ";
cin >> a;
if (a == 'y') {
system("cls");
main();
}
else {
system("cls");
cout << "\nThe program will now close, thank you\n\n";
return;
}
}
void routineOne(file& n) {
cout << setw(34) << setfill('#') << "#\n";
cout << "#" << setw(33) << setfill(' ') << "#\n";
cout << "# Routine 1 #\n";
cout << "#" << setw(33) << setfill(' ') << "#\n";
cout << setw(35) << setfill('#') << "\n\n";
ifstream inFile;
inFile.open("Student.txt");
inFile.ignore(12);
for (int i = 0; i < 26; i++) {
getline(inFile, n.names[i]);
inFile.ignore(1);
}
cout << "The input file has successfully been passed to an array.";
systemClear();
}
void routineTwo(file& n) {
cout << setw(34) << setfill('#') << "#\n";
cout << "#" << setw(33) << setfill(' ') << "#\n";
cout << "# Routine 2 #\n";
cout << "#" << setw(33) << setfill(' ') << "#\n";
cout << setw(35) << setfill('#') << "\n\n";
int x = 1;
int y = 25;
while (y != 13) {
cout << n.names[x] << endl;
x++;
cout << n.names[y] << endl;
y--;
}
cout << n.names[13] << endl;
systemClear();
}
void routineThree(file& n) {
cout << setw(34) << setfill('#') << "#\n";
cout << "#" << setw(33) << setfill(' ') << "#\n";
cout << "# Routine 3 #\n";
cout << "#" << setw(33) << setfill(' ') << "#\n";
cout << setw(35) << setfill('#') << "\n\n";
//I attempted an algorithm I found, however I couldn't get it to function properly (Is this the proper way of solving this problem?)
cout << n.names[24] << endl;
cout << n.names[14] << endl;
cout << n.names[13] << endl;
cout << n.names[22] << endl;
cout << n.names[4] << endl;
cout << n.names[16] << endl;
cout << n.names[25] << endl;
cout << n.names[21] << endl;
cout << n.names[9] << endl;
cout << n.names[19] << endl;
cout << n.names[6] << endl;
cout << n.names[12] << endl;
cout << n.names[7] << endl;
cout << n.names[2] << endl;
cout << n.names[11] << endl;
cout << n.names[20] << endl;
cout << n.names[17] << endl;
cout << n.names[23] << endl;
cout << n.names[3] << endl;
cout << n.names[8] << endl;
cout << n.names[18] << endl;
cout << n.names[5] << endl;
cout << n.names[15] << endl;
cout << n.names[10] << endl;
cout << n.names[1] << endl;
systemClear();
}
void routineFour(file& n) {
cout << setw(34) << setfill('#') << "#\n";
cout << "#" << setw(33) << setfill(' ') << "#\n";
cout << "# Routine 4 #\n";
cout << "#" << setw(33) << setfill(' ') << "#\n";
cout << setw(35) << setfill('#') << "\n\n";
//I attempted an algorithm I found, however I couldn't get it to function properly (Is this the proper way of solving this problem?)
cout << n.names[24] << endl;
cout << n.names[14] << endl;
cout << n.names[13] << endl;
cout << n.names[22] << endl;
cout << n.names[4] << endl;
cout << n.names[16] << endl;
cout << n.names[25] << endl;
cout << n.names[21] << endl;
cout << n.names[9] << endl;
cout << "James N Blythe" << endl;
cout << n.names[19] << endl;
cout << n.names[6] << endl;
cout << n.names[12] << endl;
cout << n.names[7] << endl;
cout << n.names[2] << endl;
cout << n.names[11] << endl;
cout << n.names[20] << endl;
cout << n.names[17] << endl;
cout << n.names[23] << endl;
cout << n.names[3] << endl;
cout << n.names[8] << endl;
cout << n.names[18] << endl;
cout << n.names[5] << endl;
cout << n.names[15] << endl;
cout << n.names[10] << endl;
cout << n.names[1] << endl;
systemClear();
}
void routineFive(file& n) {
cout << setw(34) << setfill('#') << "#\n";
cout << "#" << setw(33) << setfill(' ') << "#\n";
cout << "# Routine 5 #\n";
cout << "#" << setw(33) << setfill(' ') << "#\n";
cout << setw(35) << setfill('#') << "\n\n";
string oddNames[12];
int j = 0;
for (int i = 1; i < 25; i++) {
i++;
oddNames[j] = n.names[i];
j++;
}
for (int i = 0; i < 12; i++) {
cout << oddNames[i] << endl;
}
systemClear();
}
void routineSix(file& n) {
cout << setw(34) << setfill('#') << "#\n";
cout << "#" << setw(33) << setfill(' ') << "#\n";
cout << "# Routine 6 #\n";
cout << "#" << setw(33) << setfill(' ') << "#\n";
cout << setw(35) << setfill('#') << "\n\n";
cout << "???";
systemClear();
}
void routineSeven(file& n) {
cout << setw(34) << setfill('#') << "#\n";
cout << "#" << setw(33) << setfill(' ') << "#\n";
cout << "# Routine 7 #\n";
cout << "#" << setw(33) << setfill(' ') << "#\n";
cout << setw(35) << setfill('#') << "\n\n";
int alphaCount[26];
int sortedCount[26];
int temp;
string temp2;
string sortedNames[26];
string t;
for (int i = 1; i < 26; i++) {
string t = n.names[i];
t.erase(remove_if(t.begin(), t.end(), isspace), t.end());
alphaCount[i] = t.size();
}
for (int i = 0; i < 26; i++) {
sortedCount[i] = alphaCount[i];
}
for (int i = 0; i < 26; i++) {
for (int j = i + 1; j < 26; j++) {
if (sortedCount[i] > sortedCount[j]) {
temp = sortedCount[i];
sortedCount[i] = sortedCount[j];
sortedCount[j] = temp;
}
}
}
int i = 0;
while (i < 26) {
int j = 1;
while (j < 26) {
if (sortedCount[i] == n.names[j].length()) {
cout << n.names[j] << " - " << sortedCount[i] << endl;
i++;
}
if (sortedCount[i] != n.names[j].length()) {
j++;
}
}
}
systemClear();
}
void routineEight(file& n) {
cout << setw(34) << setfill('#') << "#\n";
cout << "#" << setw(33) << setfill(' ') << "#\n";
cout << "# Routine 8 #\n";
cout << "#" << setw(33) << setfill(' ') << "#\n";
cout << setw(35) << setfill('#') << "\n\n";
restart();
}
【问题讨论】:
-
您能看看
std::sort及其比较器功能吗?该函数只会比较参数字符串的长度并为您完成替换工作 -
你需要一个名字数组和一个数字数组吗?如果您有一个聚合名称和数字的结构并拥有该结构的单个数组,则此类任务会容易得多。
-
@AdamKotwasinski 它不是比较字符串长度,它只是计算字母。对每次比较都进行此计数将非常低效。
-
战术说明:您几乎从不想显示所有代码。你想展示一个小程序,用尽可能少的额外绒毛来演示问题。制作这个小程序可以让您缩小错误范围,并在您自己发现并修复错误时尽早结束。使用minimal reproducible example 获取灵感。
-
旁注:
//I attempted an algorithm I found, however I couldn't get it to function properly (Is this the proper way of solving this problem?)这将是另一个问题,但在你问我们之前先问问自己:“除了名称routineThree和我知道的代码不能正常工作之外,什么都没有,是潜在的帮助者能够确定代码应该做什么,以便他们可以帮助我修复代码?”