回答編集履歴

2

ソース追記

2020/09/22 19:33

投稿

cateye
cateye

スコア6851

test CHANGED
@@ -87,3 +87,65 @@
87
87
  }
88
88
 
89
89
  ```
90
+
91
+ 別解
92
+
93
+ ```cpp
94
+
95
+ #include <string>
96
+
97
+ #include <iostream>
98
+
99
+
100
+
101
+ using std::string;
102
+
103
+
104
+
105
+ class Trump
106
+
107
+ {
108
+
109
+ private:
110
+
111
+ static const string number_to_name[];
112
+
113
+ public:
114
+
115
+ const string & operator[](int i)const
116
+
117
+ {
118
+
119
+ return number_to_name[i];
120
+
121
+ }
122
+
123
+ };
124
+
125
+
126
+
127
+ const string Trump::number_to_name[] = {"Joker", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
128
+
129
+
130
+
131
+ int main( )
132
+
133
+ {
134
+
135
+ int pos = 0;
136
+
137
+ Trump tp;
138
+
139
+ //
140
+
141
+ std::cin >> pos;
142
+
143
+ std::cout << tp[pos] << std::endl;
144
+
145
+ //
146
+
147
+ return 0;
148
+
149
+ }
150
+
151
+ ```

1

ソース追記

2020/09/22 19:33

投稿

cateye
cateye

スコア6851

test CHANGED
@@ -1,3 +1,89 @@
1
1
  こちら↓を参考に
2
2
 
3
3
  [static メンバ](https://rinatz.github.io/cpp-book/ch07-10-static-members/)
4
+
5
+ [追記] ※入力の範囲チェックはしていません。
6
+
7
+ ```text
8
+
9
+ usr ~/Project/test % ./a.out
10
+
11
+ 0
12
+
13
+ Joker
14
+
15
+ usr ~/Project/test % ./a.out
16
+
17
+ 2
18
+
19
+ 2
20
+
21
+ usr ~/Project/test % ./a.out
22
+
23
+ 12
24
+
25
+ Q
26
+
27
+ ```
28
+
29
+ ```cpp
30
+
31
+ usr ~/Project/test % cat t3.cpp
32
+
33
+ #include <string>
34
+
35
+ #include <iostream>
36
+
37
+
38
+
39
+ using std::string;
40
+
41
+
42
+
43
+ class Trump
44
+
45
+ {
46
+
47
+ private:
48
+
49
+ static const string number_to_name[]; //C2864エラー
50
+
51
+ public:
52
+
53
+ string Translate(int i) const
54
+
55
+ {
56
+
57
+ return number_to_name[i];
58
+
59
+ }
60
+
61
+ };
62
+
63
+
64
+
65
+ const string Trump::number_to_name[] = {"Joker", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
66
+
67
+
68
+
69
+ int main( )
70
+
71
+ {
72
+
73
+ int pos = 0;
74
+
75
+ Trump tp;
76
+
77
+ //
78
+
79
+ std::cin >> pos;
80
+
81
+ std::cout << tp.Translate(pos) << std::endl;
82
+
83
+ //
84
+
85
+ return 0;
86
+
87
+ }
88
+
89
+ ```