《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:" << "
" << endl; int i1 = 0, i2 = 0; cin >> i1 >> i2; if (i1 >= i2) { i1 = i1 + i2; i2 = i1 - i2; i1 = i1 - i2; } cout << "
" << 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:" << "
" << 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は、行のすべての内容を1つの改行に遭遇するまで読み取ります(改行も読み込まれます).
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;
}