本文地址:http://www.cnblogs.com/archimedes/p/cpp-primer-chapter3-ans.html,转载请注明源地址。

【习题 2.11】
编写程序,要求用户输入两个数——底数( base)和指数( exponent),输出底数的指数次方的结果。

  1. #include <iostream>
  2. #include <math.h>
  3. #include <string>
  4. using namespace std;
  5. int main( )
  6. {
  7. int base, exp;
  8. long result=;
  9. cout<<"请输入底数和指数:"<<endl;
  10. cin>>base>>exp;
  11. if(exp<) {
  12. cout<<"指数不能为负数!"<<endl;
  13. return -;
  14. }
  15. for(int i=; i <= exp; i++)
  16. result *= base;
  17. cout<<base<<"的"<<exp<<"次方为"<<result<<endl;
  18. system("PAUSE");
  19. return ;
  20. }

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

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main( )
  5. {
  6. string str1, str2;
  7. cin>>str1>>str2;
  8. if(str1 == str2)
  9. cout<<"str1与str2相等"<<endl;
  10. else
  11. cout<<"str1与str2不相等"<<endl;
  12. system("PAUSE");
  13. return ;
  14. }

【习题 3.8】

编一个程序,从标准输入读取多个 string 对象,把它们连接起来存放到一个更大的 string 对象中。并输出连接后的 string 对象。接着,改写程序,将连接后相邻 string 对象以空格隔开。

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main( )
  5. {
  6. string str, ss;
  7. cout<<"请输入字符串:\n";
  8. while(cin>>str)
  9. ss = ss + str;
  10. cout<<"连接后的字符串为:"<<ss<<endl;
  11. system("PAUSE");
  12. return ;
  13. }

改写后的程序:

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main( )
  5. {
  6. string str, ss;
  7. cout<<"请输入字符串:\n";
  8. while(cin>>str)
  9. ss= ss + ' ' + str;
  10. cout<<"连接后的字符串为:"<<ss<<endl;
  11. system("PAUSE");
  12. return ;
  13. }

【习题 3.10】

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

  1. #include <iostream>
  2. #include <string>
  3. #include <cctype>
  4. using namespace std;
  5. int main( )
  6. {
  7. string str, ss;
  8. cout<<"请输入字符串:\n";
  9. getline(cin, str);
  10. for(string::size_type i=; i!=str.size(); ++i) {
  11. if(!ispunct(str[i]))
  12. ss+=str[i];
  13. }
  14. cout<<"连接后的字符串为:"<<ss<<endl;
  15. system("PAUSE");
  16. return ;
  17. }

【习题 3.13】

读一组整数到 vector 对象,计算并输出每对相邻元素的和。如果读入元素个数为奇数,则提示用户最后一个元素没有求和,并输出其值。

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. int main( )
  7. {
  8. vector<int> vec;
  9. int n;
  10. while(cin>>n)
  11. vec.push_back(n);
  12. if(!vec.size()) {
  13. cout<<"没有数字!"<<endl;
  14. return -;
  15. }
  16. for(vector<int>::size_type i=; i<vec.size()-; i+=) {
  17. cout<<vec[i]+vec[i+]<<"\t";
  18. if((i+)%==) cout<<endl;
  19. }
  20. if(vec.size()%!=)
  21. cout<<endl<<"最后一个数是:"<<vec[vec.size()-]<<endl;
  22. system("PAUSE");
  23. return ;
  24. }

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

  1. #include <iostream>
  2. #include <cctype>
  3. #include <string>
  4. #include <vector>
  5. using namespace std;
  6. void replace(string &s) //将字符串中的所有的小写字符全部转化为大写
  7. {
  8. for(int i=; i<s.length(); ++i) {
  9. if(islower(s[i]))
  10. s[i]=toupper(s[i]);
  11. }
  12. }
  13. int main( )
  14. {
  15. int n;
  16. string str;
  17. vector<string> vec;
  18. n=;
  19. cout<<"请输入一段文本:\n";
  20. while(cin>>str)
  21. vec.push_back(str);
  22. for(vector<string>::iterator i=vec.begin(); i!=vec.end(); ++i) {
  23. replace(*i);
  24. cout<<(*i);
  25. if(n%==)
  26. cout<<endl;
  27. else
  28. cout<<" ";
  29. n++;
  30. }
  31. system("PAUSE");
  32. return ;
  33. }

【习题 3.18】

编写程序来创建有 10 个元素的 vector 对象。用迭代器把每个元素值改为当前 值的 2 倍,输出 vector 的所有元素。

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. int main( )
  5. {
  6. vector<int> vec(,);
  7. for(vector<int>::iterator it=vec.begin(); it!=vec.end(); it++) {
  8. *it=(*it)*;
  9. cout<<(*it)<<" ";
  10. }
  11. cout<<endl;
  12. system("PAUSE");
  13. return ;
  14. }

C++primer习题--第3章的更多相关文章

  1. C++primer习题--第1章

    本文地址:http://www.cnblogs.com/archimedes/p/cpp-primer-chapter1-ans.html,转载请注明源地址. [习题 1.3] 编一个程序,在标准输出 ...

  2. C++primer习题--第4章

    本文地址:http://www.cnblogs.com/archimedes/p/cpp-primer-chapter4-ans.html,转载请注明源地址. [习题 4.7] 编写必要的代码将一个数 ...

  3. 《C++Primer》第五版习题答案--第一章【学习笔记】

    C++Primer第五版习题解答---第一章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2022/1/7 第一章:开始 练习1.3 #includ ...

  4. 《C++Primer》第五版习题答案--第二章【学习笔记】

    C++Primer第五版习题解答---第二章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/9 第二章:变量和基本类型 练习2.1: 类 ...

  5. 《python核心编》程课后习题——第三章

    核心编程课后习题——第三章 3-1 由于Python是动态的,解释性的语言,对象的类型和内存都是运行时确定的,所以无需再使用之前对变量名和变量类型进行申明 3-2原因同上,Python的类型检查是在运 ...

  6. C Primer Plus_第6章_循环_编程练习

    1.题略 #include int main(void) { int i; char ch[26]; for (i = 97; i <= (97+25); i++) { ch[i-97] = i ...

  7. C Primer Plus_第5章_运算符、表达式和语句_编程练习

    Practice 1. 输入分钟输出对应的小时和分钟. #include #define MIN_PER_H 60 int main(void) { int mins, hours, minutes; ...

  8. C Primer Plus_第四章_字符串和格式化输入输出_编程练习

    Practice 1.输入名字和姓氏,以"名字,姓氏"的格式输出打印. #include int main(void) { char name[20]; char family[2 ...

  9. [C++ Primer Plus] 第10章、对象和类(二)课后习题

    1. bank.h #include <string> using namespace std; class BankAccount { private: std::string m_na ...

随机推荐

  1. 了解PHP中Stream(流)的概念与用法

    Stream是PHP开发里最容易被忽视的函数系列(SPL系列,Stream系列,pack函数,封装协议)之一,但其是个很有用也很重要的函数.Stream可以翻译为“流”,在Java里,流是一个很重要的 ...

  2. 宝塔面板php扩展安装

    yum install libmcrypt libmcrypt-devel mcrypt mhash wget http://pecl.php.net/get/mcrypt-1.0.1.tgz tar ...

  3. CodeForces 805C Find Amir

    直觉. 先走$1$走到$n$,然后从$n$走到$2$,然后从$2$走到$n-1$,然后从$n-1$走到$3$.一次花费为$0$,一次花费为$1$. #include <cstdio> #i ...

  4. git clone https

    git clone 不需要输入密码步骤 1, vim ~/.git-credentials 2, git config --global credential.helper store 3, vim ...

  5. Latex 学习之旅

    学习资料 A simple guide to LaTeX - Step by Step LaTeX WikiBook LaTeX 科技排版 TeXdoc Online (TeX and LaTeX d ...

  6. BASE64Decoder BASE64Encoder jar包问题

    操作 对项目右击--->build path--->configure build path---> 选中默认jre OK,操作完毕, import sun.misc.BASE64D ...

  7. java 读入文件 BufferedReader

    package com.mkyong; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOExcep ...

  8. 【洛谷】2990:[USACO10OPEN]牛跳房子Cow Hopscotch【单调队列优化DP】

    P2990 [USACO10OPEN]牛跳房子Cow Hopscotch 题目描述 The cows have reverted to their childhood and are playing ...

  9. 10.十进制转m进制

    时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题解 查看运行结果 题目描述 Description 将十进制数n转换成m进制数 m<=16 n<=1 ...

  10. 01-项目简介Springboot简介入门配置项目准备

    总体课程主要分为4个阶段课程: ------------------------课程介绍------------------------ 01-项目简介Springboot简介入门配置项目准备02-M ...