栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++primer(第五版)习题答案

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++primer(第五版)习题答案

前两章习题较简单,这里就不作整理了,直接从第三章开始(持续更新):

Chapter 3. Strings, Vectors and Arrays Exercise 3.1

part 1

#include 

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

int main()
{
    int sum = 0, val = 1;
    while (val <= 10) {
        sum += val;
        ++val;
    }
    cout << "Sum of 1 to 10 inclusive is " << sum << endl;
    return 0;
}

part 2

#include 

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

struct Sales_data   {
    std::string bookNo;
    unsigned unit_sold = 0;
    double revenue = 0.0;
};

int main()
{
    Sales_data data1, data2;
    double price = 0;
    cin >> data1.bookNo >> data1.unit_sold >> price;
    data1.revenue = data1.unit_sold * price;
    cin >> data2.bookNo >> data2.unit_sold >> price;
    data2.revenue = data2.unit_sold * price;
    if (data1.bookNo == data2.bookNo)   {
        unsigned totalCnt = data1.unit_sold + data2.unit_sold;
        double totalRevenue = data1.revenue + data2. revenue;
        cout << data1.bookNo << " " << totalCnt << " " << totalRevenue << " ";
        if (totalCnt != 0)
            cout << totalRevenue/totalCnt << endl;
        else
            cout << "(no sale)" << endl;
        return 0;
    }
    else{
        cerr << "Data must refer to the same ISBN" << endl;
        return -1;
    } 
}
Exercise 3.2

part 1

#include 
#include 

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

int main()
{
    string line;
    while (getline(cin, line))  {
        cout << line << endl;
    }
    return 0;
}

part 2

#include 
#include 

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

int main()
{
    string word;
    while (cin >> word)
        cout << word << endl;
    return 0;
}
Exercise 3.3

string类输入运算符(>>):忽视一切空格,从第一个非空格字符开始,到第一个空格结束;

getline():包含换行符前的所有空格;

Exercise 3.4

part 1

#include 
#include 

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

int main()
{
    string st1, st2;
    cin >> st1 >> st2;
    if (st1 == st2)
        cout << "The two strings are equal" << endl;
    else if (st1 > st2)
        cout << "The maximum string is " << st1 << endl;
    else 
        cout << "The maximum string is " << st2 << endl;   
    return 0;
}

part 2

#include 
#include 

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

int main()
{
    string st1, st2;
    cin >> st1 >> st2;
    if (st1.size() == st2.size())
        cout << "The two strings have the same length " << endl;
    else if (st1.size() > st2.size())
        cout << "The longest string is " << st1 << endl;
    else 
        cout << "The longest string is " << st2 << endl;   
    return 0;
}
Exercise 3.5

part 1

#include 
#include 

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

int main()
{
    string total_str, str;
    while (cin >> str)
        total_str += str;
    cout << "The concatenated string is " << total_str << endl;
    return 0;
}

part 2

#include 
#include 

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

int main()
{
    string total_str, str;
    while (cin >> str)
        if (total_str.empty())
            total_str += str;
        else
            total_str += " " +str;
    cout << "The concatenated string is " << total_str << endl;
    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/767358.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号