C++——在主函数中输入10个等长的字符串,用另一函数对它们排序 ,然后在主丽数输出这10个已排好序的字符串。用指针数组的方法处理

没注释的源代码

#include <iostream>
#include<string.h>
using namespace std;
void sort(char *str[10]);
int main()
{
    char *p[10],str[10][20];
    for(int i=0;i<10;i++)
    {
        p[i]=str[i];
    }
    cout<<"please input 10 strings:"<<endl;
    for(int i=0;i<10;i++)
    {
        cin>>p[i];
    }
    sort(p);
    cout<<"now the sequence is:"<<endl;
    for(int i=0;i<10;i++)
    {
        cout<<p[i]<<endl;
    }
    return 0;
}
void sort(char *str[10])
{
    for(int i=0;i<9;i++)
    {
        int k=i;
        for (int j=i+1;j<10;j++)
        {
            if (strcmp(str[k],str[j])>0)
            {
                k=j;
            }
        }
        if (k!=i)
        {
            char *p=str[k];
            str[k]=str[i];
            str[i]=p;
        }
    }
}
 

猜你喜欢

转载自blog.csdn.net/2303_80770781/article/details/143437589