提交 37f29be5 编写于 作者: J jzplp

14.42-14.53

上级 a6c66d69
#include<iostream>
#include<vector>
#include<functional>
#include<algorithm>
int main()
{
std::vector<int> v{1,2,334,23,2353,466345,72457,3125,5166};
std::vector<int> v{2,334,23,2353,466345,72457,3125,5166};
int count = 0, g;
std::cin >> g;
for(int i : v)
if(i != (g * std::divides<int>((i,g)))
std::cout << i << " not!" << std::endl;
bool b = std::any_of(v.begin(),v.end(), std::bind(std::modulus<int>(), std::placeholders::_1, g));
if(b)
std::cout << "not!" << std::endl;
else
std::cout << "yes!" << std::endl;
return 0;
}
#include<iostream>
#include<functional>
#include<algorithm>
#include<map>
#include<string>
#include<sstream>
int add(int i, int j)
{
return i + j;
}
struct divide
{
int operator()(int denominator, int divisor)
{
return denominator / divisor;
}
};
int main()
{
auto mod = [](int i, int j){ return i % j; };
std::map<std::string, std::function<int(int, int)>> binops =
{
{"+", add}, {"-", std::minus<int>()}, {"/", divide()}, {"*", [](int i, int j){ return i * j; }}, {"%", mod}
};
std::string line;
while(std::cin >> line)
{
int i,j;
char o;
std::istringstream is(line);
is >> i;
is >> o;
is >> j;
std::string op{o};
std::cout << binops[op](i, j) << std::endl;
}
return 0;
}
#include <iostream>
#include <string>
#include "Sales_data.h"
Sales_data & Sales_data::operator+=(const Sales_data & sa)
{
amount += sa.amount;
totalPrice += sa.totalPrice;
return *this;
}
Sales_data & Sales_data::operator=(const std::string & s)
{
ISBN = s;
totalPrice = 0.0;
amount = 0;
return *this;
}
inline double Sales_data::avg_price() const
{
if(amount)
return totalPrice / amount;
return 0;
}
Sales_data operator+(const Sales_data & sa, const Sales_data & sb)
{
Sales_data sum = sa;
sum += sb;
return sum;
}
std::istream & operator>>(std::istream & is, Sales_data & sa)
{
double price = 0;
is >> sa.ISBN >> sa.amount >> price;
if(is)
sa.totalPrice = price * sa.amount;
else
sa = Sales_data();
return is;
}
std::ostream & operator<<(std::ostream & os, const Sales_data & sa)
{
os << sa.isbn() << " " << sa.amount << " " << sa.totalPrice << " " << sa.avg_price();
return os;
}
bool operator==(const Sales_data &lhs, const Sales_data &rhs)
{
return lhs.isbn() == rhs.isbn() && lhs.totalPrice == rhs.totalPrice && lhs.amount == rhs.amount;
}
bool operator!=(const Sales_data &lhs, const Sales_data &rhs)
{
return !(lhs == rhs);
}
#ifndef SALES_DATA_H
#define SALES_DATA_H
#include <iostream>
#include<string>
struct Sales_data;
Sales_data operator+(const Sales_data & sa, const Sales_data & sb);
std::istream & operator>>(std::istream & is, Sales_data & sa);
std::ostream & operator<<(std::ostream & os, const Sales_data & sa);
bool operator==(const Sales_data &lhs, const Sales_data &rhs);
bool operator!=(const Sales_data &lhs, const Sales_data &rhs);
class Sales_data
{
friend Sales_data operator+(const Sales_data & sa, const Sales_data & sb);
friend std::istream & operator>>(std::istream & is, Sales_data & sa);
friend std::ostream & operator<<(std::ostream & os, const Sales_data & sa);
friend bool operator==(const Sales_data &lhs, const Sales_data &rhs);
public:
Sales_data() = default;
Sales_data(const std::string &s) : ISBN(s) { }
Sales_data(const std::string &s, int a, double t) : ISBN(s), amount(a), totalPrice(t * a) { }
Sales_data(std::istream &is) { is >> *this; }
std::string isbn() const { return ISBN; }
Sales_data & operator+=(const Sales_data & sa);
Sales_data & operator=(const std::string & s);
explicit operator double() const { return totalPrice; }
explicit operator std::string() const { return isbn(); }
private:
std::string ISBN;
double totalPrice = 0.0;
int amount = 0;
inline double avg_price() const;
};
#endif
#include <iostream>
#include <string>
#include "Sales_data.h"
int main()
{
Sales_data total(std::cin);
std::cout << std::string(total) << std::endl;
std::cout << double(total) << std::endl;
return 0;
}
#ifndef TREE_H
#define TREE_H
#include<iostream>
class Tree
{
friend bool operator==(const Tree & t1, const Tree & t2);
friend std::ostream & operator<<(std::ostream & os, const Tree & t);
friend std::istream & operator>>(std::istream & is, Tree & t);
public:
Tree() : Tree(0, nullptr, nullptr) { }
Tree(int d) : Tree(d, nullptr, nullptr) { }
Tree(int d, Tree * l, Tree * r) : data(d), left(l), right(r) { }
explicit operator bool() const;
private:
int data = 0;
Tree * left = nullptr;
Tree * right = nullptr;
};
bool operator==(const Tree & t1, const Tree & t2)
{
return (t1.data == t2.data && t1.left == t2.left && t1.right == t2.right);
}
bool operator!=(const Tree & t1, const Tree & t2)
{
return !(t1 == t2);
}
std::ostream & operator<<(std::ostream & os, const Tree & t)
{
os << t.data;
return os;
}
std::istream & operator>>(std::istream & is, Tree & t)
{
is >> t.data;
if(!is)
t.data = 0;
return is;
}
Tree::operator bool() const
{
if(data == 0 && left == nullptr && right == nullptr)
return false;
return true;
}
#endif
#include <iostream>
#include <string>
#include "Tree.h"
int main()
{
Tree t1(1), t2(2), t3;
std::cout << bool(t2) << " " << bool(t3) << std::endl;
std::cin >> t3;
std::cout << t3 << std::endl;
std::cout << (t1 == t2) << " " << (t1 != t2) << std::endl;
std::cout << t1 << " " << t2 << std::endl;
return 0;
}
......@@ -254,14 +254,85 @@ biggies函数 用函数对象类代替lambda
* **练习14.42**
```
(a)
std::plus<int>()(i, 1024);
std::count_if(v.begin(), v.end(), std::bind(std::greater<int>(), std::placeholders::_1, 1024));
(b)
std::equal_to<std::string>()(s, "pooh");
std::find_not_if(v.begin(), v.end(), std::bind(std::equal_to<std::string>(), std::placeholders::_1, "pooh"));
(c)
std::multiplies<int>()(i, 2);
std::tranform(v.begin(), v.end(), v.begin(), std::bind(std::multiplies<int>(),std::placeholders::_1, 2));
```
* **练习14.43**
[14.43 程序代码](14.43.cpp)
* **练习14.44**
[14.44 简单的二元运算计算器 程序代码](14.44.cpp)
* **练习14.45**
Sales_data类 书上的版本
增加了转换为string和double的类型转换运算符
[14.45 Sales_data.h程序代码](14.45/Sales_data.h)
[14.45 Sales_data.cpp程序代码](14.45/Sales_data.cpp)
[14.45 仅本题测试程序代码](14.45/main.cpp)
* **练习14.46**
应该定义,但是必须声明成explicit的。如果是隐式的,可能会造成意外结果
* **练习14.47**
```
operator const int();
转换为const int类型,并且只能是非常量类对象才能使用
operator int() const;
转换为int类型,常量和非常量类对象都可以使用
```
* **练习14.48**
可以定义,但是必须是explicit的,否则可能造成意外结果
* **练习14.49**
Tree类 自己的简单版本(始于7.40题)
增加了转换为bool的类型转换运算符
[14.49 Tree.h程序代码](14.49/Tree.h)
[14.49 测试程序代码](14.49/main.cpp)
* **练习14.50**
```
ex1: 初始化错误
1. LongDouble转换为double,double转换为int
2. LongDouble转换为float,float转换为int
ex2: 初始化正确
LongDouble到float
```
* **练习14.51**
```
可能的类型转换序列:
1. double转换为LongDouble, 调用void calc(LongDouble)
2. double转换为int,调用void calc(int)
最佳可行函数为2,因为通过算术类型的转化比类类型转换的等级要高
```
* **练习14.52**
```
ld = si + ld;
1. SmallInt转换为int,LongDouble转换为double,double转换为int,调用int的加法
2. SmallInt转换为int,LongDouble转换为float,float转换为int,调用int的加法
3. SmallInt转换为int,int转换为double,LongDouble转换为double,调用double的加法
没有哪种方式明确的优于另外的几种,因此会发生错误
ld = si + ld;
1. SmallInt转换为int,int转换为double, 调用LongDoule operator+(LongDouble&, double)
2. LongDouble转换为double,SmallInt转换为int,int转换为double,调用double的加法
3. LongDouble转换为double,double转换为int,SmallInt转换为int,调用int的加法
4. LongDouble转换为float,float转换为int,SmallInt转换为int,调用int的加法
5. 调用LongDouble LongDouble::operator(const SmallInt&)
第5种不需要转换,优于其他几种
```
* **练习14.53**
```
不合法,会产生二义性
1. s1转换为int,int转换为double,调用double的加法
2. double转换为int,int转换为SmallInt,调用SmallInt SmallInt::operator(const SmallInt&, const SmallInt&)
修改的方式有很多种,其中一种为:
double d = int(s1) + 3.14;
```
......@@ -4,6 +4,7 @@ C++ Primer 第五版中文版 练习题答案代码(个人所做)
注:中文版与英文版不同的,一般按照中文版处理。
* [书上的源代码 zip压缩包](C++Primer-FiveEdition-SourceCode.zip)(来源于书籍相关网站)
## 答案目录
* [第一章 练习题答案](Chapter-1/chapter-1-answer.md)
......@@ -19,4 +20,6 @@ C++ Primer 第五版中文版 练习题答案代码(个人所做)
* [第十一章 练习题答案](Chapter-11/chapter-11-answer.md)
* [第十二章 练习题答案](Chapter-12/chapter-12-answer.md)
* [第十三章 练习题答案](Chapter-13/chapter-13-answer.md)
* [第十四章 练习题答案](Chapter-14/chapter-14-answer.md)
\ No newline at end of file
* [第十四章 练习题答案](Chapter-14/chapter-14-answer.md)
* [第十五章 练习题答案](Chapter-15/chapter-15-answer.md)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册