回答編集履歴

2

コードを追加しました。

2020/02/21 07:51

投稿

ameagari_hare
ameagari_hare

スコア39

test CHANGED
@@ -58,4 +58,6 @@
58
58
 
59
59
  }
60
60
 
61
+ }
62
+
61
- }```
63
+ ```

1

コードを追加しました。

2020/02/21 07:51

投稿

ameagari_hare
ameagari_hare

スコア39

test CHANGED
@@ -6,4 +6,56 @@
6
6
 
7
7
  https://wandbox.org/permlink/ARajJldEjsVlk1kA
8
8
 
9
+ ```C++
10
+
11
+ class Fraction;
12
+
13
+ Fraction operator+( Fraction, Fraction );
14
+
15
+ Fraction operator-( Fraction, Fraction );
16
+
17
+ Fraction operator*( Fraction, Fraction );
18
+
19
+ Fraction operator/( Fraction, Fraction );
20
+
21
+
22
+
23
+ class Fraction {
24
+
25
+ ...
26
+
27
+ };
28
+
29
+
30
+
31
+ int main() {
32
+
33
+ Fraction half(5, 10);
34
+
9
- デバッグ目的のようなので、十分かと思います。
35
+ Fraction sixth(6, 36);
36
+
37
+
38
+
39
+ std::cout << "operator +: " << half << " + " << sixth << " = " << half + sixth << "\n";
40
+
41
+ std::cout << "operator -: " << half << " - " << sixth << " = " << half - sixth << "\n";
42
+
43
+ std::cout << "operator *: " << half << " * " << sixth << " = " << half * sixth << "\n";
44
+
45
+ std::cout << "operator /: " << half << " / " << sixth << " = " << half / sixth << "\n";
46
+
47
+
48
+
49
+ // 以下の正しいやり方が知りたい
50
+
51
+ std::vector<std::string> ops_str{"+", "-", "*", "/"};
52
+
53
+ std::vector ops{&operator+, &operator-, &operator*, &operator/};
54
+
55
+ for (int i = 0; i < 4; ++i) {
56
+
57
+ std::cout << "operator " << ops_str[i] << ": " << half << " " << ops_str[i] << " " << sixth << " = " << ops[i]( half, sixth ) << "\n";
58
+
59
+ }
60
+
61
+ }```