C++Primer Chapter3课后习题
2016-06-08 00:00
309 查看
练习3-2 编写程序实现从标准输入每次读入一行文本。然后改写程序,每次读入一个单词。
#include <iostream>
#include <string>
using namespace std;
void test3_2_1(){
string line;
//一次读入一行 直到文件结束
while(getline(cin,line)){
cout<<line<<endl;//输出相应行以验证
}
}
void test3_2_2(){
string word;
//一次读入一个单词 直到文件结束
while(cin>>word)
cout<<word<<endl;
}
int main(){
test3_2_1();
//test3_2_2();
return 0;
}
练习3-3 解释string 类型的输入操作符和getline 函数分别如何处理空白字符。
string类型的输入操作符对空白字符的处理:读取并忽略有效字符(非空白字符)之前所有的空白字符,然后读取字符直至再次遇到空白字符,读取终止(该空白字符仍留在输入流中)。getline函数对空白字符的处理:不忽略行开头的空白字符,读取字符直至遇到换行符,读取终止并丢弃换行符(换行符从输入流中去掉但并不存储在string对象中)。
练习3-4 编一个程序读入两个string 对象,测试它们是否相等。若不相等,则指出两个中哪个较大。接着,改写程序测试它们的长度是否相等,若不相等,则指出两个中哪个较长。
void test3_4(){
string one;
string two;
// input one
//input two
getline(cin,one);
getline(cin,two);
if(one == two){
cout<<"they are same"<<endl;
}else{
cout<<"they are not same"<<endl;
}
if(one.size()==two.size()){
cout<<"they have same size"<<endl;
}else{
cout<<"they dont have same size"<<endl;
}
}
练习3-5
编一个程序,从标准输入读取多个string 对象,把它们连接起来存放到一个更大的string 对象中,并输出连接后的string 对象。接着,改写程序,将连接后相邻string 对象以空格隔开。 void test3_5(){
string line;
string result;
//一次读入一行 直到文件结束
while(getline(cin,line)){
cout<<line<<endl;//输出相应行以验证
result +=line;
result +=" ";//test3_5
}
cout<< result<<endl;
}
int main(){
//test3_2_1();
//test3_2_2();
//test3_4();
test3_5();
return 0;
}
练习3-6编写一段程序,使用范围for循环将字符串内的所有字符用X代替。
练习3-7若将上一题的循环控制变量替换为char,又有什么效果,编程验证。
void test3_6(){
string str = "123456";
for (auto &c : str) c = 'X';
cout << str << " ";
}
void test3_7(){
string str = "123456";
for (char &c : str) c = 'X';
cout << str << " ";
}
练习3-8分别用while和for重写上一题,你感觉哪个更好
显然,类型推断节省代码
void test3_7(){
string str = "123456";
for (char &c : str) c = 'X';
cout << str << " ";
}
void test3_8(){
string str = "123456";
for (size_t i = 0; i < str.size(); i++)
{
str[i] = 'X';
}
cout << str << endl;
str = "123456";
int i = 0;
while (i<str.size()){
str[i] = 'X';
i++;
}
cout << str << endl;
}
练习3-10编写一段程序,读入一个包含标点符号的字符串,然后将标点符号去除后输出字符串剩余部分。
void test3_10(){
string word;
string temp;
//一次读入一个单词 直到文件结束
while (cin >> word){
for (auto &c : word)
{
if (!ispunct(c)){
temp += c;
}
}
cout <<"word "<< word << endl;
cout <<"temp "<< temp << endl;
temp = "";
}
}
练习3-20 读入一组整数并把它们存入一个vector对象,将其相邻结果求和并输出。改写你的程序,每次把第一个和最后一个相加,第二个和倒数第二个相加,以此类推。
void test3_20(){
vector<int> len;
for (size_t i = 0; i < 10; i++)
{
len.push_back(i);
}
cout <<"i + i + 1 "<< endl;
for (size_t i = 0; i < len.size()-1; i++)
{
cout << len[i] + len[i + 1] << endl;
}
cout << "0 len" << endl;
for (size_t i = 0; i < len.size(); i++)
{
cout << len[i] + len[len.size() - 1 - i] <<endl;
}
}
练习3-21利用迭代器重做3.3.3节第一个练习
void test3_21(){
vector<int> len;
for (size_t i = 0; i < 10; i++)
{
len.push_back(i);
}
for (auto it = len.begin(); it != len.cend(); ++it){
cout << *it << endl;
}
}
练习3-22修改输出text程序,把第一段改成大写形式,再输出它
void test3_22(){
string word;
vector<string> text;
while (cin >> word){// the end is ctrl + z
text.push_back(word);
}
for (auto &j : text) {
for (auto it = j.begin(); it != j.end(); ++it){
*it = toupper(*it);
}
};
for (auto i : text) {
cout << i << endl;
};
}
练习3-23编写一段程序,创建一个包含10个整数的vector对象,使用迭代器使其为原来的两倍,输出内容,检查输出是否正确。
void test3_23(){
vector<int> len;
for (size_t i = 0; i < 10; i++)
{
len.push_back(i);
}
for (auto it = len.begin(); it != len.cend(); ++it){
*it *= 2;
}
for (auto it = len.begin(); it != len.cend(); ++it){
cout << *it << endl;
}
}
#include <iostream>
#include <string>
using namespace std;
void test3_2_1(){
string line;
//一次读入一行 直到文件结束
while(getline(cin,line)){
cout<<line<<endl;//输出相应行以验证
}
}
void test3_2_2(){
string word;
//一次读入一个单词 直到文件结束
while(cin>>word)
cout<<word<<endl;
}
int main(){
test3_2_1();
//test3_2_2();
return 0;
}
练习3-3 解释string 类型的输入操作符和getline 函数分别如何处理空白字符。
string类型的输入操作符对空白字符的处理:读取并忽略有效字符(非空白字符)之前所有的空白字符,然后读取字符直至再次遇到空白字符,读取终止(该空白字符仍留在输入流中)。getline函数对空白字符的处理:不忽略行开头的空白字符,读取字符直至遇到换行符,读取终止并丢弃换行符(换行符从输入流中去掉但并不存储在string对象中)。
练习3-4 编一个程序读入两个string 对象,测试它们是否相等。若不相等,则指出两个中哪个较大。接着,改写程序测试它们的长度是否相等,若不相等,则指出两个中哪个较长。
void test3_4(){
string one;
string two;
// input one
//input two
getline(cin,one);
getline(cin,two);
if(one == two){
cout<<"they are same"<<endl;
}else{
cout<<"they are not same"<<endl;
}
if(one.size()==two.size()){
cout<<"they have same size"<<endl;
}else{
cout<<"they dont have same size"<<endl;
}
}
练习3-5
编一个程序,从标准输入读取多个string 对象,把它们连接起来存放到一个更大的string 对象中,并输出连接后的string 对象。接着,改写程序,将连接后相邻string 对象以空格隔开。 void test3_5(){
string line;
string result;
//一次读入一行 直到文件结束
while(getline(cin,line)){
cout<<line<<endl;//输出相应行以验证
result +=line;
result +=" ";//test3_5
}
cout<< result<<endl;
}
int main(){
//test3_2_1();
//test3_2_2();
//test3_4();
test3_5();
return 0;
}
练习3-6编写一段程序,使用范围for循环将字符串内的所有字符用X代替。
练习3-7若将上一题的循环控制变量替换为char,又有什么效果,编程验证。
void test3_6(){
string str = "123456";
for (auto &c : str) c = 'X';
cout << str << " ";
}
void test3_7(){
string str = "123456";
for (char &c : str) c = 'X';
cout << str << " ";
}
练习3-8分别用while和for重写上一题,你感觉哪个更好
显然,类型推断节省代码
void test3_7(){
string str = "123456";
for (char &c : str) c = 'X';
cout << str << " ";
}
void test3_8(){
string str = "123456";
for (size_t i = 0; i < str.size(); i++)
{
str[i] = 'X';
}
cout << str << endl;
str = "123456";
int i = 0;
while (i<str.size()){
str[i] = 'X';
i++;
}
cout << str << endl;
}
练习3-10编写一段程序,读入一个包含标点符号的字符串,然后将标点符号去除后输出字符串剩余部分。
void test3_10(){
string word;
string temp;
//一次读入一个单词 直到文件结束
while (cin >> word){
for (auto &c : word)
{
if (!ispunct(c)){
temp += c;
}
}
cout <<"word "<< word << endl;
cout <<"temp "<< temp << endl;
temp = "";
}
}
练习3-20 读入一组整数并把它们存入一个vector对象,将其相邻结果求和并输出。改写你的程序,每次把第一个和最后一个相加,第二个和倒数第二个相加,以此类推。
void test3_20(){
vector<int> len;
for (size_t i = 0; i < 10; i++)
{
len.push_back(i);
}
cout <<"i + i + 1 "<< endl;
for (size_t i = 0; i < len.size()-1; i++)
{
cout << len[i] + len[i + 1] << endl;
}
cout << "0 len" << endl;
for (size_t i = 0; i < len.size(); i++)
{
cout << len[i] + len[len.size() - 1 - i] <<endl;
}
}
练习3-21利用迭代器重做3.3.3节第一个练习
void test3_21(){
vector<int> len;
for (size_t i = 0; i < 10; i++)
{
len.push_back(i);
}
for (auto it = len.begin(); it != len.cend(); ++it){
cout << *it << endl;
}
}
练习3-22修改输出text程序,把第一段改成大写形式,再输出它
void test3_22(){
string word;
vector<string> text;
while (cin >> word){// the end is ctrl + z
text.push_back(word);
}
for (auto &j : text) {
for (auto it = j.begin(); it != j.end(); ++it){
*it = toupper(*it);
}
};
for (auto i : text) {
cout << i << endl;
};
}
练习3-23编写一段程序,创建一个包含10个整数的vector对象,使用迭代器使其为原来的两倍,输出内容,检查输出是否正确。
void test3_23(){
vector<int> len;
for (size_t i = 0; i < 10; i++)
{
len.push_back(i);
}
for (auto it = len.begin(); it != len.cend(); ++it){
*it *= 2;
}
for (auto it = len.begin(); it != len.cend(); ++it){
cout << *it << endl;
}
}
相关文章推荐
- 学长的训诫C++
- #1094 : Lost in the City 找到几个当年c语言留下的坑
- 函数指针的正确理解 from C++ Primer Plus P242
- Michael &Scott 无锁队列 C++ 实现
- C++中的虚函数详解
- NDK工具使用,JNI开发
- C++实现多线程全局内存池
- Leetcode_c++:Construct Binary Tree from Preorder and Inorder Traversal(105)
- c++实验7--最大公约数和最小公倍数
- Leetcode_c++: Subsets II (090)
- c++/c 产生随机数
- Leetcode_c++: Merge Sorted Array (088)
- c语言实例之函数求参顺序
- 差分约束系统详解 BZOJ 2330 糖果
- C++ error:LNK2005 已经在*.obj中定义
- Leetcode_c++: Maximal Rectangle (085)
- c++:工资类
- Rectangle Area
- 那些年C语言老师没有教给你的东西(一)
- 第15周阅读程序(2)