用指针数组处理第11题,字符串不等长。 C++

2016-06-23 18:53:39  分类: c++程序设计第三版谭浩强课后答案  参与:

C++编程,用指针或引用方法处理:用指针数组处理第11题,字符串不等长。
第11题内容:在主函数输入10个等长的字符串。用另一函数对它们排序。然后在主函数输出这10个已经排好序的字符串。

以下是此题的【c++源代码】
#include <iostream>
using namespace std;
int main()
{void sort(char *[]);
 int i;
 char *p[10],str[10][20];
 for (i=0;i<10;i++)
   p[i]=str[i];        //将第i个字符串的首地址赋予指针数组p的第i个元素
 cout<<"input 10 strings:"<<endl;
 for (i=0;i<10;i++)
   cin>>p[i];
 sort(p);
 cout<<"Now,the sequence is:"<<endl;
 for (i=0;i<10;i++)
   cout<<p[i]<<endl;
 return 0;
}

void sort(char *s[])
{int i,j;
 char *temp;
 for (i=0;i<9;i++)
   for (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;
       }
}

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

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


本站文章搜索:

<< 上一篇下一篇 >>

搜索

Tags列表

赞助商链接