回答編集履歴

1

2020/03/19 15:25

投稿

papinianus
papinianus

スコア12705

test CHANGED
@@ -1 +1,85 @@
1
- インターフェイスをジェネリクスにして IQuestion<T> にする
1
+ ~~インターフェイスをジェネリクスにして IQuestion<T> にする~~
2
+
3
+ 回答が全てintではなかったですね。すみません。
4
+
5
+
6
+
7
+ ```csharp
8
+
9
+ public class CalcQuestion : IQuestion<int, int>
10
+
11
+ {
12
+
13
+ private readonly int First;
14
+
15
+ private readonly int Second;
16
+
17
+ public CalcQuestion(int x, int y)
18
+
19
+ {
20
+
21
+ First = x;
22
+
23
+ Second = y;
24
+
25
+ }
26
+
27
+
28
+
29
+ public string Inquire() => $"{First} x {Second} = ?";
30
+
31
+
32
+
33
+ public bool IsCorrect(int answer) => answer == First * Second;
34
+
35
+
36
+
37
+ public int GetCorrectAnswer() => First * Second;
38
+
39
+ }
40
+
41
+ public class CombinationQuestion : IQuestion<(int, int), IEnumerable<(int, int)>>
42
+
43
+ {
44
+
45
+ private readonly int Prod;
46
+
47
+ private readonly IEnumerable<(int, int)> Pairs;
48
+
49
+ public CombinationQuestion(int x)
50
+
51
+ {
52
+
53
+ Prod = x;
54
+
55
+ Pairs = Enumerable.Range(1, x).Select(i => x % i == 0 ? (i, x / i) : (0, 0)).Where(t => t.Item1 != 0);
56
+
57
+ }
58
+
59
+
60
+
61
+ public string Inquire() => $"? x ? = {Prod}";
62
+
63
+ public bool IsCorrect((int, int) answer) => Pairs.Contains(answer);
64
+
65
+
66
+
67
+ public IEnumerable<(int, int)> GetCorrectAnswer() => Pairs;
68
+
69
+ }
70
+
71
+
72
+
73
+ public interface IQuestion<in T, out U>
74
+
75
+ {
76
+
77
+ string Inquire();
78
+
79
+ bool IsCorrect(T answer);
80
+
81
+ U GetCorrectAnswer();
82
+
83
+ }
84
+
85
+ ```