【发布时间】:2013-12-24 18:57:38
【问题描述】:
问题我从用户那里得到了 5 个名字并按字母顺序显示它们。这是我的代码。调试时,问题似乎出在 sorted 函数中。它应该告诉我数组何时完全排序。有人可以告诉我我的问题在哪里。谢谢
#include<iostream>
#include<string>
using namespace std;
void swap(string,string);
bool sorted (string [3]);
void main ()
{
string firstname[3];
string sortfirstname[3];
int orginialindex[3];
string lastname[3];
float gpa[3];
int id[3];
for (int i=0;i<3;i++)
{
cout<<"Enter firstname"<<endl;
cin>>firstname[i];
}
for (int i=0;i<3;i++)
{
sortfirstname[i]=firstname[i];
}
while (!(sorted(sortfirstname)))
{
for (int i=0;i<3;i++) //3-2-1
{
if (sortfirstname[i]>sortfirstname[i+1])
{
swap(sortfirstname[i],sortfirstname[i+1]);
}
}
}
cout<<sortfirstname[0]<<sortfirstname[1]<<sortfirstname[2];
}
void swap (string a, string b)
{
string temp = b;
b = a;
a = temp;
}
bool sorted (string sortfirstname[3])
{
bool sort;
for (int i=0;i<3;i++)
{
if (sortfirstname[i]<sortfirstname[i+1])
sort = true;
else
sort = false;
}
return sort;
}
【问题讨论】:
-
bool sorted (string sortfirstname[3]) 应该是 bool sorted (string sortfirstname),还是打错了?