随笔 - 4, 文章 - 0, 评论 - 0, 引用 - 0
数据加载中……

C++ Primer 4th 习题答案 第三章

返回索引

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

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

#include 
"stdafx.h"
#include 
<iostream>
#include 
<string>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    
// 每次读入一行
    /*string line;
    while(getline(cin, line)){
        cout << line << endl;
    }
    getchar();
*/

    
// 每次读入一个单词
    /*string word;
    while(cin >> word){
        cout << word << endl;
    }
    getchar();
*/
}

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


#include 
"stdafx.h"
#include 
<string>
#include 
<iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    
// 判断那个字符串较大
    /*string word1;
    string word2;
    cin >> word1;
    cin >> word2;

    if(word1 == word2)
    {
        cout << "两个字符串相等" << endl;
    }else if (word1 > word2)
    {
        cout << "word1大于word2" << endl;
     }
    else
    {
        cout << "word2大于word1" << endl;
    }
    getchar();
*/

    
// 判断那个字符串长度较大
    string word1, word2;
    cin 
>> word1 >> word2;
    
if(word1.size() == word2.size()){
        cout 
<< "word1与word2相等" << endl;
    }
else if (word1.size() > word2.size()){
        cout 
<< "word1长度大于word2" << endl;
    }
else{
        cout 
<< "word2长度大于word1" << endl;
    }

    getchar();
}


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

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

#include 
"stdafx.h"
#include 
<string>
#include 
<iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    
string s1, s2, s3;
    cin 
>> s1 >> s2 >> s3;
    
string s4;
    s4 
= s1 + "," + s2 + "," + s3;
    cout 
<< s4 << endl;

    
for(string::size_type ix = 0; ix != s4.size(); ++ix)
    {
        
if(ispunct(s4[ix]))
        {
            s4[ix] 
= ' ';
        }
    }
    cout 
<< s4;

    getchar();
}


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

#include 
"stdafx.h"
#include 
<vector>
#include 
<iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    vector
<int> vec;
    vec.push_back(
1);
    vec.push_back(
2);
    vec.push_back(
3);
    vec.push_back(
4);
    vec.push_back(
5);

    
if(vec.size() % 2 != 0){
        cout 
<< "最后一个元素没有求和" << vec[vec.size()-1<< endl;
    }

    
for(size_t ix = 0; ix != vec.size()-1; ix += 2){    
        cout 
<< (vec[ix] + vec[ix+1]) << endl;
    }

    
for(size_t ix = 0, iy = vec.size()-1; ix != iy; ++ix, --iy){
        cout 
<< (vec[ix] + vec[iy]) << endl;
    }
    getchar();
}

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

#include 
"stdafx.h"
#include 
<vector>
#include 
<iostream>
#include 
<string>
#include 
<cctype>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    
string line;
    vector
<string> vec;
    
while(cin >> line){
        
for(string::size_type ix = 0; ix != line.size(); ++ix){
            line[ix] 
= toupper(line[ix]);
        }
        vec.push_back(line);
    }

    
for(size_t ix = 0; ix != vec.size(); ++ix){
        
if(ix % 8 == 0) cout << "\n";
        cout 
<< vec[ix] << ' ';
    }
    getchar();
}

// 习题3.18 编写程序来创建有10个元素的VECTOR对象,用迭代器把每个元素值改为当前值2倍
//

#include 
"stdafx.h"
#include 
<iostream>
#include 
<vector>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    vector
<int> vec;
    
for(int i = 1; i <= 10++i){
        vec.push_back(i);
    }

    vector
<int>::iterator it = vec.begin();
    
for(; it != vec.end(); ++it){
        cout 
<< (*it) * 2 << endl;
    }
    getchar();
}

// 习题3.24考虑这样的序列1,2,3,5,8,13,21并初始化一个将该序列数字所对应
// 的位置置为1的bitset<32>对象.然后换个方法,给定一个空的bitset对象,编写
// 一小段程序把相应的数位设置为1
//

#include 
"stdafx.h"
#include 
<bitset>
#include 
<iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    
string str("100000001000010010111");
    bitset
<32> bitstr(str);
    cout 
<< bitstr << endl;
    bitset
<32> bit;
    bit[
0= 1;
    bit[
1= 1;
    bit[
2= 1;
    bit[
4= 1;
    bit[
7= 1;
    bit[
12= 1;
    bit[
20= 1;
    cout 
<< bit << endl;
    getchar();
}

返回索引

posted on 2008-07-26 11:31 bendise 阅读(541) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理