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

没注释的源代码

#include <iostream>
#include<string.h>
using namespace std;
void sort(char *s[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 *s[10])
{
    char *temp;
    for(int i=0;i<9;i++)
    {
        for(int j=0;j<9-i;j++)
        {
            if(strcmp(*(s+j),*(s+j+1))>0)
            {
                temp=*(s+j);
                *(s+j)=*(s+j+1);
                *(s+j+1)=temp;
            }
        }
    }
}
 

猜你喜欢

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