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 ...
随机推荐
- qrcode 生成二维码
qrcode 生成二维码 Demo: https://www.hgnulb.cn/freedom/qrcode/qrcode.html qrcodeGithub 地址: https://github. ...
- 修改input中的placeholder属性的颜色
input::-webkit-input-placeholder{ color:#e8e8e8; } input::-moz-placeholder{ /* Mozilla Firefox 19+ * ...
- enumerate()和map()函数用法
一.python enumerate用法 先出一个题目: 1.有一 list= [1, 2, 3, 4, 5, 6] 请打印输出: 0, 1 1, 2 2, 3 3, 4 4, 5 5, 6 打印输出 ...
- Linux_x86_Pwn溢出漏洞
基础栈溢出:未开启任何保护的程序 漏洞程序源码 #include <stdio.h>#include <stdlib.h>#include <unistd.h>v ...
- hdu 1372Knight Moves
E - Knight Moves Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...
- 【UOJ #105】【APIO2014】Beads and wires
http://uoj.ac/problem/105 好神的dp啊. 确定一个点为根之后,蓝线只能是竖着的,不能横跨兄弟. 枚举每个点为根进行树形dp是\(O(n^2)\)的,\(f(x,0/1)\)表 ...
- [Codeforces #172] Tutorial
Link: Codeforces #172 传送门 A: 一眼看上去分两类就可以了 1.每个矩形只有两条边相交,重合的形状为菱形 2.每个矩形四条边都有相交 对于情况1答案为$h*h/sin(a)$ ...
- 【UOJ244】[UER7]短路
[题目大意] (2n+1)*(2n+1)的矩形,由里到外每一层都有一个相同的值.问从左上走到右小经过的点累和的最小值. [思路] 一眼就是贪心.首先能够想到的是,权值最小的那些边要尽可能夺走,所以必定 ...
- bzoj 3595
Splay 每个节点维护一个区间. /************************************************************** Problem: 3595 User ...
- bzoj 1415 无环期望
#include <cstdio> #include <vector> #include <queue> #include <algorithm> #d ...