C++ primer第五版 第二章

2.4

#include <iostream>

int main()
{
    unsigned int u = 10, u2 = 42;
    std::cout << u - u2 << std::endl;
    std::cout << u2 - u << std::endl;

    int i = 10, i2 = 42;
    std::cout << i2 - i << std::endl;
    std::cout << i - i2 << std::endl;
    std::cout << i - u << std::endl;
    std::cout << u - i << std::endl;

    return 0;
}

2.8

#include <iostream>

int main()
{
    std::cout << "\062\115\012";
    std::cout << "\062\t\115\012";
}

2.34

#include <iostream>

int main()
{
    int i = 0, &r = i;
    auto a = r; // a is an int (r is an alias for i, which has type int)

    const int ci = i, &cr = ci;
    auto b = ci; // b is an int (top-level const in ci is dropped)
    auto c = cr; // c is an int (cr is an alias for ci whose const is top-level)
    auto d = &i; // d is an int* (& ofan int objectis int*)
    auto e = &ci; // e is const int*(& of a const object is low-level const)

    const auto f = ci; // deduced type of ci is int; f has type const int
    auto& g = ci;      // g is a const int& that is bound to ci

    a = 42;
    b = 42;
    c = 42;
    *d = 42;
    e = &c;

    return 0;
}

2.35

#include<iostream>
#include<typeinfo>

int main()
{
    const int i = 42;
    auto j = i;const auto &k = i;auto *p = &i;
    const auto j2 = i,&k2 = i;

    std::cout << "i is " << typeid(i).name() << "\n";
    std::cout << "j  is " << typeid(j).name() << "\n";
    std::cout << "k  is " << typeid(k).name() << "\n";
    std::cout << "p  is " << typeid(p).name() << "\n";
    std::cout << "j2 is " << typeid(j2).name() << "\n";
    std::cout << "k2 is " << typeid(k2).name() << "\n";

    std::cout << std::endl;
    std::cout << std::boolalpha;

    std::cout << "i and j have same type?"
              << std::is_same<decltype(i),decltype(j)>::value << "\n";
    std::cout << "i and k have same type?"
              << std::is_same<decltype(i),decltype(k)>::value << "\n";
    std::cout << "i and j2 have same type?"
              << std::is_same<decltype(i),decltype(j2)>::value << "\n";
    std::cout << "j and j2 have same type?"
              << std::is_same<decltype(j),decltype(j2)>::value << "\n";
    std::cout << "k and k2 have same type?"
              << std::is_same<decltype(k),decltype(k2)>::value << "\n";

    return 0;
}

2.41

#include<iostream>
#include<string>

struct Sale_data{
    std::string bookNo; //ISBN
    unsigned units_sold;//销售数量
    double revenue;//销售收入
};

int main()
{
    Sale_data book;
    double price = 0;
    std::cin >> book.bookNo >> book.units_sold >> price;
    book.revenue = price * book.units_sold;
    std::cout << book.bookNo << " " << book.units_sold << " " << book.revenue << " " << price;

    return 0;
}
#include<iostream>
#include<string>

struct Sale_data{
    std::string bookNo; //ISBN
    unsigned units_sold;//销售数量
    double revenue;//销售收入
};

int main()
{
    double price = 0;
    Sale_data book1,book2;

    std::cin >> book1.bookNo >> book1.units_sold >> price;
    book1.revenue = book1.units_sold * price;

    std::cin >> book2.bookNo >> book2.units_sold >> price;
    book2.revenue = book2.units_sold * price;

    if(book1.bookNo == book2.bookNo)
    {
        unsigned totalcnt = book1.units_sold + book2.units_sold;
        double totalRevenue = book1.revenue + book2.revenue;
        std::cout << book1.bookNo << " " << totalcnt
                  << " " << totalRevenue << " ";
        if(totalcnt != 0)
            std::cout << totalRevenue/totalcnt << std::endl;
        else
            std::cout << "No sale" << std::endl;
        return 0;
    }
    else
    {
        std::cerr << "Data must refer to the same ISBN"
                  << std::endl;
        return -1;
    }
}
 
发布了80 篇原创文章 · 获赞 101 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/BigDream123/article/details/102883113
今日推荐