C++primer习题--第3章
本文地址:http://www.cnblogs.com/archimedes/p/cpp-primer-chapter3-ans.html,转载请注明源地址。
【习题 2.11】
编写程序,要求用户输入两个数——底数( base)和指数( exponent),输出底数的指数次方的结果。
- #include <iostream>
- #include <math.h>
- #include <string>
- using namespace std;
- int main( )
- {
- int base, exp;
- long result=;
- cout<<"请输入底数和指数:"<<endl;
- cin>>base>>exp;
- if(exp<) {
- cout<<"指数不能为负数!"<<endl;
- return -;
- }
- for(int i=; i <= exp; i++)
- result *= base;
- cout<<base<<"的"<<exp<<"次方为"<<result<<endl;
- system("PAUSE");
- return ;
- }
【习题 3.7】
编一个程序读入两个 string 对象,测试它们是否相等。若不相等,则指出两个中哪个较大。接着,改写程序测试它们的长度是否相等,若不相等,则指出两个中哪个较长。
- #include <iostream>
- #include <string>
- using namespace std;
- int main( )
- {
- string str1, str2;
- cin>>str1>>str2;
- if(str1 == str2)
- cout<<"str1与str2相等"<<endl;
- else
- cout<<"str1与str2不相等"<<endl;
- system("PAUSE");
- return ;
- }
【习题 3.8】
编一个程序,从标准输入读取多个 string 对象,把它们连接起来存放到一个更大的 string 对象中。并输出连接后的 string 对象。接着,改写程序,将连接后相邻 string 对象以空格隔开。
- #include <iostream>
- #include <string>
- using namespace std;
- int main( )
- {
- string str, ss;
- cout<<"请输入字符串:\n";
- while(cin>>str)
- ss = ss + str;
- cout<<"连接后的字符串为:"<<ss<<endl;
- system("PAUSE");
- return ;
- }
改写后的程序:
- #include <iostream>
- #include <string>
- using namespace std;
- int main( )
- {
- string str, ss;
- cout<<"请输入字符串:\n";
- while(cin>>str)
- ss= ss + ' ' + str;
- cout<<"连接后的字符串为:"<<ss<<endl;
- system("PAUSE");
- return ;
- }
【习题 3.10】
编一个程序,从 string 对象中去掉标点符号。要求输入到程序的字符串必须含 有标点符号,输出结果则是去掉标点符号后的 string 对象。
- #include <iostream>
- #include <string>
- #include <cctype>
- using namespace std;
- int main( )
- {
- string str, ss;
- cout<<"请输入字符串:\n";
- getline(cin, str);
- for(string::size_type i=; i!=str.size(); ++i) {
- if(!ispunct(str[i]))
- ss+=str[i];
- }
- cout<<"连接后的字符串为:"<<ss<<endl;
- system("PAUSE");
- return ;
- }
【习题 3.13】
读一组整数到 vector 对象,计算并输出每对相邻元素的和。如果读入元素个数为奇数,则提示用户最后一个元素没有求和,并输出其值。
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- int main( )
- {
- vector<int> vec;
- int n;
- while(cin>>n)
- vec.push_back(n);
- if(!vec.size()) {
- cout<<"没有数字!"<<endl;
- return -;
- }
- for(vector<int>::size_type i=; i<vec.size()-; i+=) {
- cout<<vec[i]+vec[i+]<<"\t";
- if((i+)%==) cout<<endl;
- }
- if(vec.size()%!=)
- cout<<endl<<"最后一个数是:"<<vec[vec.size()-]<<endl;
- system("PAUSE");
- return ;
- }
【习题 3.14】
读入一段文本到 vector 对象,每个单词存储为 vector 中的一个元素。把 vector 对象中每个单词转化为大写字母。输出 vector 对象中转化后的元素, 每八个单词为一行输出。
- #include <iostream>
- #include <cctype>
- #include <string>
- #include <vector>
- using namespace std;
- void replace(string &s) //将字符串中的所有的小写字符全部转化为大写
- {
- for(int i=; i<s.length(); ++i) {
- if(islower(s[i]))
- s[i]=toupper(s[i]);
- }
- }
- int main( )
- {
- int n;
- string str;
- vector<string> vec;
- n=;
- cout<<"请输入一段文本:\n";
- while(cin>>str)
- vec.push_back(str);
- for(vector<string>::iterator i=vec.begin(); i!=vec.end(); ++i) {
- replace(*i);
- cout<<(*i);
- if(n%==)
- cout<<endl;
- else
- cout<<" ";
- n++;
- }
- system("PAUSE");
- return ;
- }
【习题 3.18】
编写程序来创建有 10 个元素的 vector 对象。用迭代器把每个元素值改为当前 值的 2 倍,输出 vector 的所有元素。
- #include <iostream>
- #include <vector>
- using namespace std;
- int main( )
- {
- vector<int> vec(,);
- for(vector<int>::iterator it=vec.begin(); it!=vec.end(); it++) {
- *it=(*it)*;
- cout<<(*it)<<" ";
- }
- cout<<endl;
- system("PAUSE");
- return ;
- }
C++primer习题--第3章的更多相关文章
- C++primer习题--第1章
本文地址:http://www.cnblogs.com/archimedes/p/cpp-primer-chapter1-ans.html,转载请注明源地址. [习题 1.3] 编一个程序,在标准输出 ...
- C++primer习题--第4章
本文地址:http://www.cnblogs.com/archimedes/p/cpp-primer-chapter4-ans.html,转载请注明源地址. [习题 4.7] 编写必要的代码将一个数 ...
- 《C++Primer》第五版习题答案--第一章【学习笔记】
C++Primer第五版习题解答---第一章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2022/1/7 第一章:开始 练习1.3 #includ ...
- 《C++Primer》第五版习题答案--第二章【学习笔记】
C++Primer第五版习题解答---第二章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/9 第二章:变量和基本类型 练习2.1: 类 ...
- 《python核心编》程课后习题——第三章
核心编程课后习题——第三章 3-1 由于Python是动态的,解释性的语言,对象的类型和内存都是运行时确定的,所以无需再使用之前对变量名和变量类型进行申明 3-2原因同上,Python的类型检查是在运 ...
- 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 ...
- C Primer Plus_第5章_运算符、表达式和语句_编程练习
Practice 1. 输入分钟输出对应的小时和分钟. #include #define MIN_PER_H 60 int main(void) { int mins, hours, minutes; ...
- C Primer Plus_第四章_字符串和格式化输入输出_编程练习
Practice 1.输入名字和姓氏,以"名字,姓氏"的格式输出打印. #include int main(void) { char name[20]; char family[2 ...
- [C++ Primer Plus] 第10章、对象和类(二)课后习题
1. bank.h #include <string> using namespace std; class BankAccount { private: std::string m_na ...
随机推荐
- 了解PHP中Stream(流)的概念与用法
Stream是PHP开发里最容易被忽视的函数系列(SPL系列,Stream系列,pack函数,封装协议)之一,但其是个很有用也很重要的函数.Stream可以翻译为“流”,在Java里,流是一个很重要的 ...
- 宝塔面板php扩展安装
yum install libmcrypt libmcrypt-devel mcrypt mhash wget http://pecl.php.net/get/mcrypt-1.0.1.tgz tar ...
- CodeForces 805C Find Amir
直觉. 先走$1$走到$n$,然后从$n$走到$2$,然后从$2$走到$n-1$,然后从$n-1$走到$3$.一次花费为$0$,一次花费为$1$. #include <cstdio> #i ...
- git clone https
git clone 不需要输入密码步骤 1, vim ~/.git-credentials 2, git config --global credential.helper store 3, vim ...
- Latex 学习之旅
学习资料 A simple guide to LaTeX - Step by Step LaTeX WikiBook LaTeX 科技排版 TeXdoc Online (TeX and LaTeX d ...
- BASE64Decoder BASE64Encoder jar包问题
操作 对项目右击--->build path--->configure build path---> 选中默认jre OK,操作完毕, import sun.misc.BASE64D ...
- java 读入文件 BufferedReader
package com.mkyong; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOExcep ...
- 【洛谷】2990:[USACO10OPEN]牛跳房子Cow Hopscotch【单调队列优化DP】
P2990 [USACO10OPEN]牛跳房子Cow Hopscotch 题目描述 The cows have reverted to their childhood and are playing ...
- 10.十进制转m进制
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题解 查看运行结果 题目描述 Description 将十进制数n转换成m进制数 m<=16 n<=1 ...
- 01-项目简介Springboot简介入门配置项目准备
总体课程主要分为4个阶段课程: ------------------------课程介绍------------------------ 01-项目简介Springboot简介入门配置项目准备02-M ...