c++第十天

《c++ primer, 5E》

第64页到第68页,笔记:

1、利用花括号初始化data member。

2、预处理器是在编译之前执行的一段程序

3、类似SALES_DATA_H通常称为【预处理变量】,有两种状态:已经定义 和 未定义。

防止重复包含的实例代码

#ifndef SALES_DATA_H        //检查结果为真,就执行下面的代码,直到#endif
#define SALES_DATA_H        //预处理变量变为【已定义】,再次包含不再拷贝#ifndef到#endif之间的代码
#include<string>
struct Sales_data{
    std::string bookNo;
    unsigned units_sold = 0;
    double price = 0.0;
    double revenue = 0.0;
};
#endif

 

 

遇到的问题:

课后练习:

练习2.39

prog1.cpp:1:12: error: expected ';' after struct definition struct Foo{}

 

练习2.40

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

 

练习2.41

1

#include<iostream>
#include<string>
struct Sales_data{
    std::string bookNo;
    unsigned units_sold = 0;
    double price = 0.0;
    double revenue = 0.0;
};
int main()
{
    Sales_data data;
    // std::cin >> data;
    std::cin >> data.bookNo >> data.units_sold >> data.price;
    // std::cout << data;
    std::cout << data.bookNo << " " << data.units_sold << " " << data.price << std::endl;
    return 0;
}

 

2

#include<iostream>
#include<string>
struct Sales_data{
    std::string bookNo;
    unsigned units_sold = 0;
    double price = 0.0;
    double revenue = 0.0;
};
int main()
{
    Sales_data data1, data2;
    // std::cin >> data1 >> data2;
    std::cin >> data1.bookNo >> data1.units_sold >> data1.price;
    std::cin >> data2.bookNo >> data2.units_sold >> data2.price;
    // std::cout << data1 + data2;
    std::cout << data1.bookNo << " " << data1.units_sold + data2.units_sold << " " << data1.price << std::endl;
    return 0;
}

 

3

#include<iostream>
#include<string>
struct Sales_data{
    std::string bookNo;
    unsigned units_sold = 0;
    double price = 0.0;
    double revenue = 0.0;
};
int main()
{
    Sales_data data, sum_data;
    while(std::cin >> data.bookNo >> data.units_sold >> data.price)
        sum_data.units_sold += data.units_sold;
    std::cout << data.bookNo << " " << sum_data.units_sold << " " << data.price << std::endl;
    return 0;
}

 

 4写这种代码简直就是。。。

#include<iostream>
#include<string>
struct Sales_data{
    std::string bookNo;
    unsigned units_sold = 0;
    double price = 0.0;
    double revenue = 0.0;
};
int main()
{
    int num = 0;
    std::string last_bookNo;
    Sales_data temp_data;
    // 读取第一个
    // std::cin >> temp_data;
    std::cin >> temp_data.bookNo >> temp_data.units_sold >> temp_data.price;
    last_bookNo = temp_data.bookNo;
    num = 1;
    // while(std::cin >> temp_data){
    while(std::cin >> temp_data.bookNo >> temp_data.units_sold >> temp_data.price){
        if(temp_data.bookNo == last_bookNo){
            ++num;
        }
        else{
            std::cout << last_bookNo << " " << num;
            last_bookNo = temp_data.bookNo;
            num = 1;
        }
    }
    std::cout << last_bookNo << " " << num;
    return 0;
}

 

5

#include<iostream>
#include<string>
struct Sales_data{
    std::string bookNo;
    unsigned units_sold = 0;
    double price = 0.0;
    double revenue = 0.0;
};
int main()
{
    Sales_data total;
    if(std::cin >> total.bookNo >> total.units_sold >> total.price){
        Sales_data trans;
        // while(std::cin >> total)
        while(std::cin >> trans.bookNo >> trans.units_sold >> trans.price){
            if(total.bookNo == trans.bookNo){
                total.units_sold += trans.units_sold;
            }else{
                // std::cout << total << std::endl;
                std::cout << total.bookNo << " " << total.units_sold << " " << total.price << std::endl;
                // total = trans;
                total = trans;
            }
        }
        std::cout << total.bookNo << " " << total.units_sold << " " << total.price << std::endl;
    }else{
        std::cerr << "No data?" << std::endl;
        return -1;
    }
    return 0;
}

 

练习2.42

像这样

 

转载于:https://www.cnblogs.com/xkxf/p/6369312.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值