C++编程:用指向指针的指针的方法对5个字符串排序并输出

2016-06-24 10:05:16  分类: c++程序设计第三版谭浩强课后答案  参与:

C++编程:用指向指针的指针的方法对5个字符串排序并输出

以下是此题的【c++源代码】,需要【c源代码】请点击进入
#include <iostream>
using namespace std;
           
int main()
{void sort(char **p);
 const int m=20;        //定义字符串的最大长度
 int i;
 char **p,*pstr[5],str[5][m];
 for (i=0;i<5;i++)
   pstr[i]=str[i];   /*将第i个字符串的首地址赋予指针数组 pstr 的第i个元素*/
 cout<<"input 5 strings:"<<endl;
 for (i=0;i<5;i++)
    cin>>pstr[i];
 p=pstr;
 sort(p);
 cout<<"strings sorted:"<<endl;
 for (i=0;i<5;i++)
    cout<<pstr[i]<<endl;
 return 0;
}

void sort(char **p)            //冒泡法对5个字符串排序函数
{int i,j;
 char *temp;
 for (i=0;i<5;i++)
  {for (j=i+1;j<5;j++)
    {if (strcmp(*(p+i),*(p+j))>0)      //比较后交换字符串地址
      {temp=*(p+i);
       *(p+i)=*(p+j);
       *(p+j)=temp;
      }
     }
  }
}

来源:c++程序设计第三版谭浩强课后答案

本文链接:http://www.wb98.com/cjia/post/cjia_6.19.html


本站文章搜索:

<< 上一篇下一篇 >>

搜索

Tags列表

赞助商链接