《C++ Primer》第五版课后习题解答_第三章(1)(01-05)

系统环境: windows 10 1703

编译环境:Visual studio 2017


3.1

//1.9
#include 
using std::cout;
using std::endl;

int main()
{
    int sum = 0, i = 50;
    while (i <= 100)
    {
        sum += i;
        ++i;
    }
    cout << "The sum is: " << sum << endl;
    return 0;
}
//1.10
#include 
using std::cout;
using std::endl;

int main()
{
    int i = 10;
    while (i >= 0)
    {
        cout << i << endl;
        --i;
    }
    return 0;
}
//1.11
#include 
using std::cout;
using std::endl;
using std::cin;

int main()
{
    cout << "Enter two integers:" << "\n" << endl;
    int i1 = 0, i2 = 0;
    cin >> i1 >> i2;
    if (i1 >= i2)
    {
        i1 = i1 + i2;
        i2 = i1 - i2;
        i1 = i1 - i2;
    }
    cout << "\n" << endl;
    for (int i = i1; i <= i2; ++i)
    {
        cout << i << endl;
    }
    return 0;
}
//综合1.5.1节,1.5.2节,1.6节程序如下
#include 
#include 
using std::cout;
using std::endl;
using std::cin;
using std::string;

//创建 Sales_data 类
struct Sales_data {
    string bookID;
    unsigned sum_sold = 0;
    float price = 0.0;
    float revenue = 0.0;
};

int main()
{
    cout << "Enter sales record:" << "\n" << endl;
    Sales_data trans, book;
    int cnt = 1;
    if (cin >> book.bookID >> book.sum_sold >> book.price) //读取第一个数据
    {
        book.revenue = book.sum_sold * book.price;
        while (cin >> trans.bookID >> trans.sum_sold >> trans.price) //读取剩余的数据
        {
            trans.revenue = trans.sum_sold * trans.price;
            if (trans.bookID == book.bookID) //判断读入的 bookID 是否和第一个数据的 bookID 相同
            {
                book.revenue += trans.revenue;
                book.sum_sold += trans.sum_sold;
                ++cnt;
            }
            else
            {
                cout << cnt << " " << book.bookID << " " << book.sum_sold << " " << book.revenue << endl;
                book.bookID = trans.bookID;
                book.sum_sold = trans.sum_sold;
                book.revenue = trans.revenue;
                cnt = 1;
            }
        }
        cout << cnt << " " << book.bookID << " " << book.sum_sold << " " << book.revenue << endl;
    }
    return 0;
}

3.2

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

//读入一整行
int main()
{
    string s;
    while (getline(cin, s))
        cout << s << endl;
    return 0;
}
#include 
#include 
using std::cout;
using std::endl;
using std::cin;
using std::string;

//一次读入一个词
int main()
{
    string s;
    while (cin >> s)
        cout << s << endl;
    return 0;
}

3.3

string 类的输入运算符,自动忽略开头的空白,并从第一个真正的字符还是读取,直到遇见下一处空白开始;

getline 会读取一行中的所有内容,直到遇到一个换行符为止(换行符也被读入了)。


3.4

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

//比大小
int main()
{
    string s1, s2;
    cout << "Enter two strings:" << endl;
    cin >> s1 >> s2;
    if (s1 == s2)
    {
        cout << "Two strings are equal" << endl;
    }
    else
    {
        if (s1 > s2)
        {
            cout << s1 << endl;
        }
        else
        {
            cout << s2 << endl;
        }
    }
    return 0;
}
#include 
#include 
using std::cout;
using std::endl;
using std::cin;
using std::string;

//比长度
int main()
{
    string s1, s2;
    cout << "Enter two strings:" << endl;
    cin >> s1 >> s2;
    if (s1.size() == s2.size())
    {
        cout << "Two strings are isometric" << endl;
    }
    else
    {
        if (s1.size() > s2.size())
        {
            cout << s1 << endl;
        }
        else
        {
            cout << s2 << endl;
        }
    }
    return 0;
}

3.5

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

//直接连接
int main()
{
    string sum, s;
    while (cin >> s)
    {
        sum += s;
    }
    cout << sum << endl;
    return 0;
}
#include 
#include 
using std::cout;
using std::endl;
using std::cin;
using std::string;

//用空格连接
int main()
{
    string sum, s;
    while (cin >> s)
    {
        sum += s;
        sum += " ";
    }
    cout << sum << endl;
    return 0;
}

你可能感兴趣的:(C++,Primer,习题)