C++ Primer 第三章 标准库类型习题解答

来源:互联网 发布:淘宝网上买东西脏不脏 编辑:程序博客网 时间:2024/06/02 01:35

3.1用适当的Using声明,而不是std::前缀,访问标准库中名字的方法,重新编写2.3节的程序,计算一给定数的给定次幂的结果。

 

Can not findor open the PDB file.

http://www.1000seo.com/soft/1756编译出现问题  

#include"stdafx.h"

#include<iostream>

#include<string>

usingstd::string;

usingstd::cin;

usingstd::cout;

usingstd::endl;

 

int_tmain(int argc, _TCHAR* argv[])

{

      int base, exponent;

      long result = 1;

      cout << "Enter base andexponent : ";

      cin >> base >> exponent;

      if (exponent<0){

             cout << "exponent can’tsmall than 0" << endl;

             return -1;

      }

      if(exponent>0)

      {

             for (int cnt = 1; cnt <=exponent; cnt++)

             

             {

                    result *= base;

                    

             }

             

           cout << base << " raised to the power of" << exponent << ":" << result << endl;

             system("pause");

             return 0;

      }

}

3.2什么是默认构造函数?

答:每个类都定义了该类型对象如何进行初始化。类通过定义一个或多个构造函数来控制类对象的初始化。如果定义某个类的变量时没有进行提供初始化式,它会默认运行一个“默认构造函数”,如果没有提供初始化式,那么它就会使用默认构造函数。不管变量在哪里定义,默认构造函数都会被使用。

3.3列举出三种初始化string对象的方法。

答:string s1;默认构造函数,s1为空串

strings2(s1);s2初始化为s1的一个副本

strings3(“value”);s3初始化为一个字符串字面值副本

3.4ss2的值分别是什么?

strings;

intmain(){

  string s2;

}

s “ ”

s2为 “

3.5编写程序实现从标准输入每次读入一行文本。然后改写程序,每次读入一个单词。

答:

#include"stdafx.h"

 

#include<iostream>

#include<string>

 

usingstd::cin;

usingstd::cout;

usingstd::endl;

usingstd::string;

 

int_tmain(int argc, _TCHAR* argv[])

{

  

  string line;

  while (getline(cin, line))

     cout << line << endl;

     return 0;

 

}

//第二问

#include"stdafx.h"

 

#include<iostream>

#include<string>

 

usingstd::cin;

usingstd::cout;

usingstd::endl;

usingstd::string;

 

int_tmain(int argc, _TCHAR* argv[])

{

  string word;

  //read until end-of-file ,writing each wordto a new line

  while (cin >> word)

     cout << word << endl;

  return 0;

}

3.6解释string类型的输入操作符和getline函数分别如何处理空白字符

string类型的输入操作符:

读取并忽略开头所有的空白字符(如空格,换行符和制表符)

读取字符直至再次遇到空白字符,读取终止。

3.7编写程序读入两个string对象,测试它们是否相等。若不相等,则指出两个较大,接着,改写程序测试它们的长度是否相等,若不相等,则指出哪个较长。

//problem2.cpp : Defines the entry point for the console application.

//

 

#include"stdafx.h"

 

#include<iostream>

#include<string>

 

usingstd::cin;

usingstd::cout;

usingstd::endl;

usingstd::string;

 

int_tmain(int argc, _TCHAR* argv[])

{

  

  string s1, s2;

  cout << "Enter two strings"<< endl;

  cin >> s1 >> s2;

  string::size_type len1, len2;

  len1 = s1.size();

  len2 = s2.size();

  if (len1 == len2)

     cout << "They have samelentgh!" << endl;

  else if (len1 > len2)

     cout << "The length of "<< "\"" << s1

     << "\"is longger than\"" << s1 << "\"" << endl;

  else

     cout << "The length of "<< "\"" << s2

     << "\"is longer than\"" << s1 << "\"" << endl;

  

  system("pause");

  return 0;

}

\“转义字符输出”http://baike.baidu.com/link?url=pDm_FsrMsWcxZ_9VyrjzRU0rYFWBdt0wrNA7hWT1cMBn6vT_LYv3ur5winT2YDfdMYhTs_yxPEDwNyFhj4-ebK

3.8编写一个程序,从标准输入读取多个string对象,把它们连接起来存放到一个更大的string对象中。并输出连接后的string对象。

#include<iostream>

#include<string>

usingnamespace std;

intmain()

{

  string result_str, str;

  cout << "Enter string(ctrl + z toend)" << endl;

  cin >> result_str;

  while (cin >> str)

  {

     result_str = result_str + str;

     cout << "连接后的string对象" << endl << result_str << endl;

  }

  system("pause");

  return 0;

}

3.9下列程序实现什么功能?实现合法吗?如果不合法,说明理由。

string s

cout<<s[0]<<endl;

答:不合法。s是一个空字符串,其长度为0,因此s[0]是无效的。

有改进空间—— 3.10 编一个程序,从string对象中去掉标点符号。要求输入到程序的字符串必须含有标点符号,输出结果则是去掉标点符号的string对象。

//problem2.cpp : Defines the entry point for the console application.

//

 

#include"stdafx.h"

 

#include<iostream>

#include<string>

 

usingstd::cin;

usingstd::cout;

usingstd::endl;

usingstd::string;

 

int_tmain(int argc, _TCHAR* argv[])

{

  

  string s, result_str;

  bool has_punct = false;

  char ch;

  cout << "Enter a string : "<< endl;

  getline(cin, s);

  for (string::size_type index = 0; index !=s.size(); ++index)

  {

     ch = s[index];

     if (ispunct(ch))

       has_punct = true;

     else result_str += ch;

  }

  if (has_punct)

     cout << "Reslut:" <<endl << result_str << endl;

  else

  {

     cout << "No punction characterin the string ? !" << endl;

     return -1;

  }

  system("pause");

  return 0;

 

}

习题3.13 读一组整数到vector对象,计算并输出每对相邻元素的和如果读入元素个数为奇数,则提示用户最后一个元素没有求和,并输出其值。然后修改程序:头尾元素两两配对,计算每对元素的和,并输出。

 

code:

// problem2.cpp : Defines theentry point for the console application.

//

 

#include "stdafx.h"

 

#include<iostream>

#include<vector>

 

using std::cin;

using std::cout;

using std::endl;

using std::vector;

using std::string;

 

int _tmain(int argc, _TCHAR*argv[])

{

 

 vector<int>ivec;

 int ival;

 //读数据到vector对象

 cout << "输入数据(按ctrl + z结束):" << endl;

 while (cin >> ival)

 {

    ivec.push_back(ival);

    if (ival == 0) break;

 }

 //计算相邻元素并输出

 if (ivec.size() == 0)

 {

    cout << "没有数据!" <<endl;

    return -1;

 }

 cout << "vector对象中相邻的每对元素之和:"<< endl;

 for (vector<int>::size_type index = 0; index < ivec.size()- 2; index = index + 2)

 {

    cout << ivec[index] + ivec[index + 1] <<"\t";

    if ((index + 1) % 6 == 0)//每行输出6个和

      cout << endl;

 }

 if ((ivec.size()-1) % 2 != 0)//提示输出最后1个元素没有和

    cout << endl;

 cout << "它的值是:" <<ivec[ivec.size() - 2];

    system("pause");

 return 0;

}

//第二问

//problem2.cpp : Defines the entry point for the console application.

//

 

#include"stdafx.h"

 

#include<iostream>

#include<vector>

 

usingstd::cin;

usingstd::cout;

usingstd::endl;

usingstd::vector;

usingstd::string;

 

int_tmain(int argc, _TCHAR* argv[])

{

  

  vector<int>ivec;

  int ival;

  //读数据到vector对象

  cout << "输入数据(按ctrl + z结束):"<< endl;

  

  while (cin >> ival )

  {

     ivec.push_back(ival);

     if (ival == 0) break;

  }

  if (ivec.size() == 0)

  {

     cout << "没有数据!" << endl;

     return -1;

  }

 

  cout << "计算vector对象中首尾元素之和" << endl;

  

  vector<int>::size_type cnt = 0;

 

  for (vector<int>::size_type first = 0,last = ivec.size() - 2; first < last; ++first, --last)

  {

     cout << ivec[first] + ivec[last] <<"\t";

     ++cnt;

     if (cnt % 6 == 0)

       cout << endl;

  }

  

  system("pause")  ;

  return 0;

  

}

 

 

3.14

读入一段文本到vector对象,每个单词储存为vector中的一个元素。把vector对象中每个单词转化为大写字母。输出vector对象中转化后的元素,每八个单词为一行输出。

 #include "stdafx.h"

 

#include<iostream>

#include<vector>

#include<string>

#include<cctype>

 

usingstd::cin;

usingstd::cout;

usingstd::endl;

usingstd::vector;

usingstd::string;

 

int_tmain(int argc, _TCHAR* argv[])

{

 

  vector<string> svec;

  string str;

  //读取文本到vector对象中

  cout << "Enter text(Enter ctrl + zto end) :" << endl;

  while (cin >> str)

  {

     svec.push_back(str);

     if (str == "o")

       break;

  }

  //vector对象中小写字母化为大写字母,并输出

  if(svec.size() == 0)

  {

     cout << "No string!"<< endl;

     return -1;

  }

  for (vector<string>::size_type ix = 0;ix != svec.size()-1; ix++)

  {

     for (string::size_type index = 0; index !=svec[ix].size(); index++)

     if (islower(svec[ix][index]))

     {

       svec[ix][index] =toupper(svec[ix][index]);

       

     }

     cout << svec[ix] << "";

     if ((ix+1) % 8 == 0)

       cout << endl;

  }

  system("pause");

  return 0; 

}


0 0
原创粉丝点击