C++编程题:输入十个国家名称用指针数组实现排序输出
原来的程序是这样的,但不满足条件
#include<iostream.h>
#include<string.h>
void ccmp(char *a[]);
void main()
{
char *cname[10]={"China","USA","Australia","Austria","Brazil","Japan","England","Canada","Italy","France"};
ccmp(cname);
for(int i=0;i<10;i++)
cout<<cname[i]<<endl;
}
void ccmp(char *a[10])
{
char *p;int i,j;
for(i=0;i<9;i++)
for(j=0;j<10;j++)
{
if(strcmp(a[i],a[j])>0)
{
p=a[i];
a[i]=a[j];
a[j]=p;
}
}
}
#include <iostream.h>
#include <灶轮string.h>
void ccmp(char * a[]);
void main()
{
char * cname[10]={"China","毁辩圆USA","Australia","Austria","Brazil","Japan","England","Canada","Italy"纤塌,"France"};
ccmp(cname);
for(int i=0;i<10;i++)
cout<<cname[i]<<endl;
}
void ccmp(char * a[10])
{
char * p;int i,j;
for(i=0;i<9;i++)
{
for(j=i;j<10;j++)
{
if(strcmp(a[i],a[j])>0)
{
p=a[i];
a[i]=a[j];
a[j]=p;
}
}
}
}
#include<iostream.h>
#include<string.h>
void ccmp(char *a[]);
int main()
{
char *cname[10]={"China","USA","Australia","Austria","Brazil","Japan","England","Canada","Italy","France"};
ccmp(cname);
for(int i=0;i<10;i++)
cout<<cname[i]<<endl;
return 0;
}
void ccmp(char *a[ ]) //10不用局拦进来。
{
char *p;int i,j;
for(i=0;i<9;i++)
for(j=i;j<10;j++) { //j得从i开始。
if(strcmp(a[i],a[j])>0){
p=a[i];
a[i]=a[j];
a[j]=p;
}
}
}
只是用你的做缺巧以桐扮胡修改而已,修改之处见注释。