按字母顺序对给定文本进行排序,无附加库

Sorting given text alphabetically w/o additional library

本文关键字:排序 文本 顺序      更新时间:2023-10-16

我的作业是编写一个应用程序,按字母顺序对给定的文本进行排序。为此,我只被允许使用"vector"、"string"answers"iostream"库。

我成功了,但现在遇到了一个奇怪的问题——当我试图对一个简短的文本进行排序时,一切都很好,但随着输入的增加,程序似乎陷入了无限循环或效率问题。例如文本

"Albert Einstein 14 March 1879 – 18 April 1955 was a German-born theoretical physicist who developed the theory of relativity one of the two pillars of modern physics alongside quantum mechanics His work is also known[...]" 

一切都很好,直到"机械"这个短语。在添加了这个或任何其他词之后,程序将永远运行,就像我之前提到的那样。

在这种情况下,恐怕我不得不粘贴整个代码(请原谅(。

#include <iostream>
#include <string>
#include <vector>
int compare(std::string first, std::string second){
int flag = 1;
int i;
if (first.size() <= second.size()){
for (i = 0; i<first.size(); ++i ){
if (first[i] == second[i]){
continue;}
else if (first[i] > second[i]){
flag = 0;
break;}
else {
break;}}}
else {
for (i = 0; i<second.size(); ++i ){
if (first[i] == second[i]) {
continue;}
else if (first[i] > second[i]){
flag = 0;
break;}
else {
break;}
}
int m = second.size() - 1;
if (first[m] == second[m]){
flag = 0;}}
return flag;}

int main() {
std::vector<std::string> text;
std::string word;
std::string tmp;
while(std::cin >> word){
text.push_back(word);}
int mistakes, m = 1;
while(m) {
mistakes = 0;
for (int index = 1; index < text.size() ; ++index){
if (!(compare(text[index-1], text[index]))){
tmp = text[index];
text[index] = text[index-1];
text[index-1] = tmp;
mistakes += 1;}}
m = mistakes;}
for (auto element: text){
std::cout << element << " ";}}

我很想知道如何解决它,以及为什么会出现这个问题——至少执行时间不会随着输入的长度而增加,但更像是"工作/不工作",这与效率问题不同。

您错过了一些条件,因此while循环无限运行。例如:

  1. 如果第一对单词的顺序错误,则while循环执行的值所在的错误变量永远不会变为0。阴性测试案例是:"球苹果"。在这种情况下,您的代码可以无限运行
  2. 在你的比较方法中,因为下面的代码行测试用例,比如这个"苹果球"给出了错误的答案。这里first[m]=second[m]=l,因此根据您的条件,它将返回false并交换它们。它将用"球"代替"苹果",这是错误的。

    int m = second.size() - 1;
    if (first[m] == second[m]){
    flag = 0;
    }
    
  3. 您还需要处理大小写单词之间进行比较的情况。例如:"Month also"。在这种情况下,答案应该是"也是月份"。所以在比较两个字符串之前,应该先将它们置于相同的大小写中,然后再进行比较。

  4. 1874应该在18之后的数字比较情况。(你可以添加这个(

以下是更正后的代码。


#include <iostream>
#include <string>
#include <vector>
#include <cctype>
int compare(std::string first, std::string second){
// this is to handle the comparison of two words with mixed case(upper/lower) of letters.
// earlier solution failed for comparison between 'Month' and 'a'
for(int i=0;i<first.size();i++){
first[i] = tolower(first[i]);
}
for(int i=0;i<second.size();i++){
second[i] = tolower(second[i]);
}
int flag = 1;
int i;
if (first.size() <= second.size()){
for (i = 0; i<first.size(); ++i ){
if (first[i] == second[i]){
continue;}
else if (first[i] > second[i]){
flag = 0;
break;}
else {
break;}}}
else {
for (i = 0; i<second.size(); ++i ){
if (first[i] == second[i]) {
continue;}
else if (first[i] > second[i]){
flag = 0;
break;}
else {
break;}
}
}
return flag;}

int main() {
std::vector<std::string> text;
std::string word;
std::string tmp;
while(std::cin >> word){
text.push_back(word);}

// bubble short
for(int i=0;i<(text.size()-1);i++){
for(int j=0;j<(text.size()-1-i);j++){
if (!(compare(text[j], text[j+1]))){
tmp = text[j];
text[j] = text[j+1];
text[j+1] = tmp;
}
}
}

for (auto element: text){
std::cout << element << " ";}}