我有3个字符串变量,用户输入三个单词
并且程序必须按字母顺序对它进行排序,我尝试用if语句进行排序,但问题是如果用户插入前5个字符是相同的,我厌倦了每次重复代码。
这是我的代码:
#include <iostream>
using namespace std;
int main ()
{
string w1,w2,w3;
cout<<"please enter three words"<<endl;
cin >> w1 >> w2 >> w3;
if(w1[0] == w2[0])
{
if(w1[1] < w2[1] && w1[0] < w3[0]){
cout << w1;
if(w2[0] < w3[0])
{
cout << "\t" << w2 <<"\t" << w3;
}
else
{
cout << "\t" << w3<<"\t" << w2;
}
}else if(w2[1] < w1[1] && w2[0] < w3[0]){
cout << w2;
if(w1[0] < w3[0])
{
cout << "\t" << w1 <<"\t" << w3;
}
else
{
cout << "\t" << w3<<"\t" << w1;
}
}else if(w3[0] < w1[0] && w3[0] < w2[0]){
cout << w3;
if(w1[1] < w2[1])
{
cout << "\t" << w1 <<"\t" << w2;
}
else
{
cout << "\t" << w2<<"\t" << w1;
}
}
}
else if(w2[0] == w3[0])
{
if(w1[0] < w2[0] && w1[0] < w3[0]){
cout << w1;
if(w2[1] < w3[1])
{
cout << "\t" << w2 <<"\t" << w3;
}
else
{
cout << "\t" << w3<<"\t" << w2;
}
}else if(w2[0] < w1[0] && w2[1] < w3[1]){
cout << w2;
if(w1[0] < w3[0])
{
cout << "\t" << w1 <<"\t" << w3;
}
else
{
cout << "\t" << w3<<"\t" << w1;
}
}else if(w3[0] < w1[0] && w3[1] < w2[1]){
cout << w3;
if(w1[0] < w2[0])
{
cout << "\t" << w1 <<"\t" << w2;
}
else
{
cout << "\t" << w2<<"\t" << w1;
}
}
}
else if(w3[0] == w1[0])
{
if(w1[0] < w2[0] && w1[1] < w3[1]){
cout << w1;
if(w2[0] < w3[0])
{
cout << "\t" << w2 <<"\t" << w3;
}
else
{
cout << "\t" << w3<<"\t" << w2;
}
}else if(w2[0] < w1[0] && w2[0] < w3[0]){
cout << w2;
if(w1[1] < w3[1])
{
cout << "\t" << w1 <<"\t" << w3;
}
else
{
cout << "\t" << w3<<"\t" << w1;
}
}else if(w3[1] < w1[1] && w3[0] < w2[0]){
cout << w3;
if(w1[0] < w2[0])
{
cout << "\t" << w1 <<"\t" << w2;
}
else
{
cout << "\t" << w2<<"\t" << w1;
}
}
}
else if(w1[0] == w2[0] && w2[0] == w3[0])
{
if(w1[1] < w2[1] && w1[1] < w3[1]){
cout << w1;
if(w2[1] < w3[1])
{
cout << "\t" << w2 <<"\t" << w3;
}
else
{
cout << "\t" << w3<<"\t" << w2;
}
}else if(w2[1] < w1[1] && w2[1] < w3[1]){
cout << w2;
if(w1[1] < w3[1])
{
cout << "\t" << w1 <<"\t" << w3;
}
else
{
cout << "\t" << w3<<"\t" << w1;
}
}else if(w3[1] < w1[1] && w3[1] < w2[1]){
cout << w3;
if(w1[1] < w2[1])
{
cout << "\t" << w1 <<"\t" << w2;
}
else
{
cout << "\t" << w2<<"\t" << w1;
}
}
}
else if(w1[0] == w2[0] && w2[0] == w3[0] && w1[1] == w2[1] && w2[1] == w3[1])
{
if(w1[2] < w2[2] && w1[2] < w3[2]){
cout << w1;
if(w2[2] < w3[2])
{
cout << "\t" << w2 <<"\t" << w3;
}
else
{
cout << "\t" << w3<<"\t" << w2;
}
}else if(w2[2] < w1[2] && w2[2] < w3[2]){
cout << w2;
if(w1[2] < w3[2])
{
cout << "\t" << w1 <<"\t" << w3;
}
else
{
cout << "\t" << w3<<"\t" << w1;
}
}else if(w3[2] < w1[2] && w3[2] < w2[2]){
cout << w3;
if(w1[2] < w2[2])
{
cout << "\t" << w1 <<"\t" << w2;
}
else
{
cout << "\t" << w2<<"\t" << w1;
}
}
}
else
{
if(w1[0] < w2[0] && w1[0] < w3[0]){
cout << w1;
if(w2[0] < w3[0])
{
cout << "\t" << w2 <<"\t" << w3;
}
else
{
cout << "\t" << w3<<"\t" << w2;
}
}else if(w2[0] < w1[0] && w2[0] < w3[0]){
cout << w2;
if(w1[0] < w3[0])
{
cout << "\t" << w1 <<"\t" << w3;
}
else
{
cout << "\t" << w3<<"\t" << w1;
}
}else if(w3[0] < w1[0] && w3[0] < w2[0]){
cout << w3;
if(w1[0] < w2[0])
{
cout << "\t" << w1 <<"\t" << w2;
}
else
{
cout << "\t" << w2<<"\t" << w1;
}
}
}
//cout << w1[0] << "\t" << "\t" <<w2[0] << "\t" << w3[0];
return 0;
}
如果有办法用for循环但只使用这些库
#include <iostream>
#include <stdio.h>
#include <string>
#include <algorithm>
答案 0 :(得分:2)
我不明白你的程序试图做什么。但考虑到类std::string
具有关系运算符。因此,无需单独比较每个字符。
我会按以下方式编写程序:)您可以更改程序,以便它要求用户输入三个字符串。
#include <iostream>
#include <string>
#include <functional>
#include <utility>
int main()
{
std::string s1 = "tom", s2 = "fox", s3 = "hello";
auto r1 = std::ref( s1 ), r2 = std::ref( s2 ), r3 = std::ref( s3 );
if ( r2.get() < r1.get() ) std::swap( r1, r2 );
if ( r3.get() < r2.get() ) std::swap( r2, r3 );
if ( r2.get() < r1.get() ) std::swap( r1, r2 );
std::cout << s1<< '\t' << s2 << '\t' << s3 << std::endl;
std::cout << r1.get() << '\t' << r2.get() << '\t' << r3.get() << std::endl;
return 0;
}
输出
tom fox hello
fox hello tom
或者你可以使用直截了当的方法
#include <iostream>
#include <string>
int main()
{
std::string s1 = "tom", s2 = "fox", s3 = "hello";
if ( s1 <= s2 && s2 <= s3 )
{
std::cout << s1<< '\t' << s2 << '\t' << s3 << std::endl;
}
else if ( s1 <= s3 && s3 <= s2 )
{
std::cout << s1<< '\t' << s3 << '\t' << s2 << std::endl;
}
else if ( s2 <= s1 && s1 <= s3 )
{
std::cout << s2<< '\t' << s1 << '\t' << s3 << std::endl;
}
else if ( s2 <= s3 && s3 <= s1 )
{
std::cout << s2<< '\t' << s3 << '\t' << s1 << std::endl;
}
else if ( s3 <= s1 && s1 <= s2 )
{
std::cout << s3<< '\t' << s1 << '\t' << s2 << std::endl;
}
if ( s3 <= s2 && s2 <= s1 )
{
std::cout << s3<< '\t' << s2 << '\t' << s1 << std::endl;
}
return 0;
}
答案 1 :(得分:2)
试试这个:
const unsigned int MAX_STRINGS = 3;
string string_container[MAX_STRINGS];
cout<<"please enter three words"<<endl;
cin >> string_container[0]
>> string_container[1]
>> string_container[2];
for (unsigned int i = 1; i < MAX_STRINGS; ++i)
{
if (string_container[i] <= string_container[i - 1])
{
std::swap(string_container[i - 1], string_container[i]);
i = 0;
}
}
它符合for
循环的要求。
编辑1:使用std :: sort
如果您不需要for
循环:
const unsigned int MAX_STRINGS = 3;
string string_container[MAX_STRINGS];
cout<<"please enter three words"<<endl;
cin >> string_container[0]
>> string_container[1]
>> string_container[2];
std::sort(&string_container[0], &string_container[MAX_STRINGS];
答案 2 :(得分:0)
int main () {
string list[3];//Array of strings. Makes things easier.
cout<<"please enter three words"<<endl;
cin >> list[0] >> list[1] >> list[2];
//I might be using swap or compare wrong here
//but i guess it it more important that you know these functions exist.
if (list[0].compare(list[1]) < 0) swap(list[0], list[1]);
if (list[0].compare(list[2]) < 0) swap(list[0], list[2]);
if (list[1].compare(list[2]) < 0) swap(list[1], list[2]);
return 0;
}
你也可以(更好的是)使用嵌套在for循环中的for循环:
int arraysize = 3;
for (int i = 0 ; i < arraysize ; i++) {
for (int j = i+1 ; j < arraysize) ; j++) {
if (list[i].compare(list[j]) < 0) swap(list[i], list[j]);
}
}
答案 3 :(得分:0)
试试这个(这是Thomas Matthews的代码,简单Bubble Sort)
const unsigned int MAX_STRINGS = 3;
string string_container[MAX_STRINGS];
cout<<"please enter three words"<<endl;
cin >> string_container[0]
>> string_container[1]
>> string_container[2];
bool swaped;
do{
swaped = false;
for (unsigned int i = 1; i < MAX_STRINGS; ++i)
{
if (string_container[i] <= string_container[i - 1])
{
std::swap(string_container[i - 1], string_container[i]);
swaped = true;
}
}
}while(swaped);