C++Primer第五版 习题答案 第三章 字符串、向量和数组(Strings, Vectors, and Arrays)

练习3.1

使用恰当的using 声明重做 1.4.1节和2.6.2节的练习。

3.1.1

#include <iostream>

using std::cout;
using std::endl;

int main()
{
   
	int i = 50 ,sum = 0;

	while(i <= 100)
	{
   
		sum += i;
		++i;
	}
	cout << sum << endl;

	return 0;
}

3.1.2

#include <iostream>

using std::cout;
using std::endl;

int main()
{
   
	int i = 10 ,sum = 0;

	while(i >= 0)
	{
   
		sum += i;
		--i;
	}
	cout << sum << endl;

	return 0;
}

3.1.3

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
   
    int small = 0, big = 0;
    cout << "please input two integers:";
    cin >> small >> big;

    if (small > big) {
   
        int tmp = small;
        small = big;
        big = tmp;
    }

    while (small <= big) {
   
        cout << small << " ";
        ++small;
    }
    cout << endl;

    return 0;
}

3.1.4

#include <iostream>
#include <string>
#include "../ch02/ex2_42.h"

using std::cin;
using std::cout;
using std::endl;
using std::cerr;

int main()
{
   
    Sales_data data1, data2;

    // code to read into data1 and data2
    double price = 0;  // price per book, used to calculate total revenue

    // read the first transactions: ISBN, number of books sold, price per book 
    cin >> data1.bookNo >> data1.units_sold >> price;
    // calculate total revenue from price and units_sold
    data1.revenue = data1.units_sold * price;

    // read the second transaction
    cin >> data2.bookNo >> data2.units_sold >> price;
    data2.revenue = data2.units_sold * price;

    // code to check whether data1 and data2 have the same ISBN
    //        and if so print the sum of data1 and data2
    if (data1.bookNo == data2.bookNo)
    {
   
        unsigned totalCnt = data1.units_sold + data2.units_sold;
        double totalRevenue = data1.revenue + data2.revenue;

        // print: ISBN, total sold, total revenue, average price per book
        cout << data1.bookNo << " " << totalCnt
            << " " << totalRevenue << " ";
        if (totalCnt != 0)
            cout << totalRevenue / totalCnt << endl;
        else
            cout << "(no sales)" << endl;

        return 0;  // indicate success
    }
    else
    {
     // transactions weren't for the same ISBN
        cerr << "Data must refer to the same ISBN" << endl;
        return -1; // indicate failure
    }
}

练习3.2

编写一段程序从标准输入中一次读入一行,然后修改该程序使其一次读入一个词。

3.2.1

#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
int main()
{
   
    std::string str_in;
    while(getline(cin,str_in))
    {
   
        cout << str_in << endl;
    }

    return 0;
}

3.2.2

#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
int main()
{
   
    std::string str_in;

    while(cin>>str_in)
    {
   
        cout << str_in << endl;
    }
    return 0;
}

练习3.3

请说明string类的输入运算符和getline函数分别是如何处理空白字符的。

输入运算符碰到空格、回车和制表符,忽略前面空白;getline碰到回车,不忽略空白。

练习3.4

编写一段程序读取两个字符串,比较其是否相等并输出结果。如果不相等,输出比较大的那个字符串。改写上述程序,比较输入的两个字符串是否等长,如果不等长,输出长度较大的那个字符串。

3.4.1

#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
int main()
{
   
    std::string str_in1,str_in2;

    cin>>str_in1>>str_in2;
    if(str_in1 != str_in2)
    {
   
        if(str_in1>str_in2)
        {
   
            cout << str_in1 << endl;
        }else
        {
   
            cout << str_in2 << endl;
        }
    }

    return 0;
}

3.4.2

#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
int main()
{
   
    std::string str_in1,str_in2;

    cin>>str_in1>>str_in2;
    
    if(str_in1.size() != str_in2.size())
    {
   
        if(str_in1.size()>str_in2.size())
        {
   
            cout << str_in1 << endl;
        }else
        {
   
            cout << str_in2 << endl;
        }
    }

    return 0;
}

练习3.5

编写一段程序从标准输入中读入多个字符串并将他们连接起来,输出连接成的大字符串。然后修改上述程序,用空格把输入的多个字符串分割开来。

3.5.1

#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
int main()
{
   
    std::string str_in,str_sum;

    while(cin>>str_in)
    {
   
        str_sum = str_sum + str_in;
    }
    cout << str_sum << endl;
    return 0;
}

3.5.2

#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
int main()
{
   
    std::string str_in,str_sum;

    while(cin>>str_in)
    {
   
        str_sum = str_sum + str_in + " ";
    }
    cout << str_sum << endl;
    return 0;
}

练习3.6

编写一段程序,使用范围for语句将字符串内所有字符用X代替。

#include <string>
#include <iostream>

using std::cin;
using std::cout;
using std::endl;
using std::string;

int main()
{
   
    string str = "1122aabbcc 33";

    for(auto &c : str)
    {
   
        c = 'X';
    }
    cout<<str<<endl;

    return 0;
}

练习3.7

就上一题完成的程序而言,如果将循环控制变量的类型设置为char将发生什么?先估计一下结果,然后实际编程进行验证。

将变量的类型设置为char,即将变量的基本类型设置为char,与auto自动推断的类型相同,因此,结果与上题结果相同。

#include <string>
#include <iostream>

using std::cin;
using std::cout;
using std::endl;
using std::string;

int main()
{
   
    string str = "1122aabbcc 33";

    for(char c : str)
    {
   
        c = 'X';
    }
    cout<<str<<endl;

    return 0;
}

练习3.8

分别用while循环和传统for循环重写第一题的程序,你觉得哪种形式更好呢?为什么?

个人感觉范围for循环更好,更简洁,且声明的变量作用域在范围for循环内,不会对其他程序产生影响。

3.8.1

#include <string>
#include <iostream>

using std::cin;
using std::cout;
using std::endl;
using std::string;

int main()
{
   
    string str = "1122aabbcc 33";

    for(string::size_type index = 0;index < str.size();++index)
    {
   
        str[index] = 'X';
    }
    cout<<str<<endl;

    return 0
评论 34
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值