《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
像这样