自考新教材--p60_5_2

源程序:

#include <iostream>

#include <string>

using namespace std;

int main()

{

  int n,i,length=0;

  cout << "输入几个字符串:";

  cin >> n;

  string *p;

  string *A = new string[n];

  p = A;

  cout << endl;

  cout << "请输入:";

  for (i = 0; i < n; i++)

  {

    cin >> A[i];

    length = length + A[i].length();

  }

  p = A;   //将指针重新指向数组的首地址

  cout << endl;

  string max=A[0];

  string min = A[0];

  for (i = 1; i < n; i++)

  {

    if (p[i].length() > max.length())

      max = p[i];

    if (p[i].length() < min.length())

      min = p[i];

  }

  cout << "最长的字符串为:"<<max<<"\n"<<"最短的字符串为:"<<min<<endl;

  cout << "字符串的平均长度为:" << length / n << endl;

  delete []p;

  system("pause");

  return 0;

}

运行结果:

posted @ 2019-12-03 09:05  bobo哥  阅读(171)  评论(0编辑  收藏  举报