回答編集履歴

7

またコメント修正忘れ

2020/03/16 14:06

投稿

jimbe
jimbe

スコア13209

test CHANGED
@@ -60,7 +60,7 @@
60
60
 
61
61
  * chars の各文字を循環出力する.
62
62
 
63
- * next() で次の文字に移動し, 最後の文字の後は先頭の文字に戻る.
63
+ * countUp() で次の文字に移動し, 最後の文字の後は先頭の文字に戻る.
64
64
 
65
65
  * この時, 登録されていれば CarryOverListener#carryOver() を呼び出す.
66
66
 

6

出力方法を変更

2020/03/16 14:06

投稿

jimbe
jimbe

スコア13209

test CHANGED
@@ -28,9 +28,17 @@
28
28
 
29
29
  StringCounter stringCounter = new StringCounter(moto, charsRow);
30
30
 
31
- stringCounter.outputAllPatternTo(System.out); //表示
32
-
33
- stringCounter.outputAllPatternTo("ShinIrekae.txt"); //ファイル
31
+ try (PrintStream out = new PrintStream("ShinIrekae.txt");) {
32
+
33
+ do {
34
+
35
+ System.out.println(stringCounter);
36
+
37
+ out.println(stringCounter);
38
+
39
+ } while(stringCounter.countUp());
40
+
41
+ }
34
42
 
35
43
  }
36
44
 
@@ -38,12 +46,16 @@
38
46
 
39
47
  }
40
48
 
49
+
50
+
41
51
  interface CarryOverListener {
42
52
 
43
53
  void carryOver();
44
54
 
45
55
  }
46
56
 
57
+
58
+
47
59
  /**
48
60
 
49
61
  * chars の各文字を循環出力する.
@@ -70,7 +82,7 @@
70
82
 
71
83
  }
72
84
 
73
- void next() {
85
+ void countUp() {
74
86
 
75
87
  if(++index >= chars.length) {
76
88
 
@@ -86,10 +98,12 @@
86
98
 
87
99
  @Override
88
100
 
89
- public void carryOver() { next(); }
101
+ public void carryOver() { countUp(); }
90
102
 
91
103
  }
92
104
 
105
+
106
+
93
107
  class StringCounter implements CarryOverListener {
94
108
 
95
109
  private List<CharCounter> ccList;
@@ -114,104 +128,112 @@
114
128
 
115
129
  }
116
130
 
131
+ /**
132
+
133
+ * 最下位桁をカウントアップ
134
+
135
+ * @return 処理後も有効値なら true (最上位桁でキャリーオーバーが発生したら false)
136
+
137
+ */
138
+
139
+ boolean countUp() {
140
+
117
- void outputAllPatternTo(String filename) throws IOException {
141
+ if(!carry) ccList.get(ccList.size()-1).countUp();
142
+
118
-
143
+ return !carry;
144
+
145
+ }
146
+
147
+ boolean isCarryOver() { return carry; }
148
+
149
+ @Override
150
+
151
+ public void carryOver() { carry = true; }
152
+
153
+ @Override
154
+
155
+ public String toString() {
156
+
119
- try (PrintStream out = new PrintStream(filename);) {
157
+ StringBuilder sb = new StringBuilder();
158
+
120
-
159
+ for(CharCounter cc : ccList) sb.append(cc.get());
160
+
121
- outputAllPatternTo(out);
161
+ return sb.toString();
162
+
163
+ }
164
+
165
+ }
166
+
167
+
168
+
169
+ class CharsRow {
170
+
171
+ private static String[] ROWS = {
172
+
173
+ "あぁかがさざただなはばぱまやゃらわ",
174
+
175
+ "いぃきぎしじちぢにひびぴみりゐ",
176
+
177
+ "うぅくぐすずつづぬふぶぷむゆゅる",
178
+
179
+ "えぇけげせぜてでねへべぺめれゑ",
180
+
181
+ "おぉこごそぞとどのほぼぽもよょろ",
182
+
183
+ };
184
+
185
+ private Map<Character,char[]> cache = new HashMap<>();
186
+
187
+ void check(String moto) {
188
+
189
+ for(char c : moto.toCharArray()){
190
+
191
+ char[] r = get(c);
192
+
193
+ System.out.println(c + "は、" + r[0] + (r.length>=2?"段にあります":"です"));
122
194
 
123
195
  }
124
196
 
125
197
  }
126
198
 
199
+ char[] get(char c) {
200
+
201
+ for(String r : ROWS) {
202
+
203
+ if(r.indexOf(c) >= 0) {
204
+
127
- void outputAllPatternTo(PrintStream out) {
205
+ char[] a = cache.get(r.charAt(0));
128
-
206
+
129
- carry = false;
207
+ if(a == null) {
130
-
208
+
131
- for (CharCounter last=ccList.get(ccList.size()-1); !carry; last.next()) {
209
+ a = r.toCharArray();
132
-
210
+
133
- for(CharCounter cc : ccList) out.print(cc.get());
211
+ cache.put(r.charAt(0), a);
212
+
134
-
213
+ }
214
+
135
- out.println();
215
+ return a;
216
+
217
+ }
136
218
 
137
219
  }
138
220
 
221
+ char[] a = cache.get(c);
222
+
223
+ if(a == null) {
224
+
225
+ a = new char[]{c};
226
+
227
+ cache.put(c, a);
228
+
229
+ }
230
+
231
+ return a;
232
+
139
233
  }
140
234
 
141
- @Override
142
-
143
- public void carryOver() { carry = true; }
144
-
145
235
  }
146
236
 
147
- class CharsRow {
148
-
149
- private static String[] ROWS = {
150
-
151
- "あぁかがさざただなはばぱまやゃらわ",
152
-
153
- "いぃきぎしじちぢにひびぴみりゐ",
154
-
155
- "うぅくぐすずつづぬふぶぷむゆゅる",
156
-
157
- "えぇけげせぜてでねへべぺめれゑ",
158
-
159
- "おぉこごそぞとどのほぼぽもよょろ",
160
-
161
- };
162
-
163
- private Map<Character,char[]> cache = new HashMap<>();
164
-
165
- void check(String moto) {
166
-
167
- for(char c : moto.toCharArray()){
168
-
169
- char[] r = get(c);
170
-
171
- System.out.println(c + "は、" + r[0] + (r.length>=2?"段にあります":"です"));
172
-
173
- }
174
-
175
- }
176
-
177
- char[] get(char c) {
178
-
179
- for(String r : ROWS) {
180
-
181
- if(r.indexOf(c) >= 0) {
182
-
183
- char[] a = cache.get(r.charAt(0));
184
-
185
- if(a == null) {
186
-
187
- a = r.toCharArray();
188
-
189
- cache.put(r.charAt(0), a);
190
-
191
- }
192
-
193
- return a;
194
-
195
- }
196
-
197
- }
198
-
199
- char[] a = cache.get(c);
200
-
201
- if(a == null) {
202
-
203
- a = new char[]{c};
204
-
205
- cache.put(c, a);
206
-
207
- }
208
-
209
- return a;
210
-
211
- }
212
-
213
- }
214
-
215
237
  ```
216
238
 
217
239
  ```plain text

5

バグ修正

2020/03/16 13:56

投稿

jimbe
jimbe

スコア13209

test CHANGED
@@ -126,6 +126,8 @@
126
126
 
127
127
  void outputAllPatternTo(PrintStream out) {
128
128
 
129
+ carry = false;
130
+
129
131
  for (CharCounter last=ccList.get(ccList.size()-1); !carry; last.next()) {
130
132
 
131
133
  for(CharCounter cc : ccList) out.print(cc.get());

4

コード内コメント直し忘れ

2020/03/16 13:37

投稿

jimbe
jimbe

スコア13209

test CHANGED
@@ -46,7 +46,7 @@
46
46
 
47
47
  /**
48
48
 
49
- * source の各文字を循環出力する.
49
+ * chars の各文字を循環出力する.
50
50
 
51
51
  * next() で次の文字に移動し, 最後の文字の後は先頭の文字に戻る.
52
52
 

3

コード改良

2020/03/16 13:33

投稿

jimbe
jimbe

スコア13209

test CHANGED
@@ -26,7 +26,7 @@
26
26
 
27
27
 
28
28
 
29
- StringCounter stringCounter = new StringCounter(moto);
29
+ StringCounter stringCounter = new StringCounter(moto, charsRow);
30
30
 
31
31
  stringCounter.outputAllPatternTo(System.out); //表示
32
32
 
@@ -96,12 +96,10 @@
96
96
 
97
97
  private boolean carry;
98
98
 
99
- StringCounter(String moto) {
99
+ StringCounter(String moto, CharsRow charsRow) {
100
100
 
101
101
  ccList = new ArrayList<>();
102
102
 
103
- CharsRow charsRow = new CharsRow();
104
-
105
103
  CarryOverListener prev = this;
106
104
 
107
105
  for(char c : moto.toCharArray()){

2

クラス分け

2020/03/16 13:06

投稿

jimbe
jimbe

スコア13209

test CHANGED
@@ -20,180 +20,198 @@
20
20
 
21
21
 
22
22
 
23
+ CharsRow charsRow = new CharsRow();
24
+
23
- hikaku(moto);
25
+ charsRow.check(moto);
24
-
26
+
27
+
28
+
25
- mk(moto);
29
+ StringCounter stringCounter = new StringCounter(moto);
30
+
31
+ stringCounter.outputAllPatternTo(System.out); //表示
32
+
33
+ stringCounter.outputAllPatternTo("ShinIrekae.txt"); //ファイル
26
34
 
27
35
  }
28
36
 
29
37
  }
30
38
 
31
-
39
+ }
40
+
32
-
41
+ interface CarryOverListener {
42
+
43
+ void carryOver();
44
+
45
+ }
46
+
47
+ /**
48
+
49
+ * source の各文字を循環出力する.
50
+
51
+ * next() で次の文字に移動し, 最後の文字の後は先頭の文字に戻る.
52
+
53
+ * この時, 登録されていれば CarryOverListener#carryOver() を呼び出す.
54
+
55
+ */
56
+
57
+ class CharCounter implements CarryOverListener {
58
+
59
+ private CarryOverListener listener;
60
+
61
+ private char[] chars;
62
+
63
+ private int index;
64
+
65
+ CharCounter(char[] chars, CarryOverListener listener) {
66
+
67
+ this.chars = chars;
68
+
69
+ this.listener = listener;
70
+
71
+ }
72
+
73
+ void next() {
74
+
75
+ if(++index >= chars.length) {
76
+
77
+ index = 0;
78
+
79
+ if(listener != null) listener.carryOver();
80
+
81
+ }
82
+
83
+ }
84
+
85
+ char get() { return chars[index]; }
86
+
87
+ @Override
88
+
89
+ public void carryOver() { next(); }
90
+
91
+ }
92
+
93
+ class StringCounter implements CarryOverListener {
94
+
95
+ private List<CharCounter> ccList;
96
+
97
+ private boolean carry;
98
+
33
- private static void hikaku(String moto){
99
+ StringCounter(String moto) {
100
+
101
+ ccList = new ArrayList<>();
102
+
103
+ CharsRow charsRow = new CharsRow();
104
+
105
+ CarryOverListener prev = this;
34
106
 
35
107
  for(char c : moto.toCharArray()){
36
108
 
109
+ CharCounter cc = new CharCounter(charsRow.get(c), prev);
110
+
111
+ ccList.add(cc);
112
+
113
+ prev = cc;
114
+
115
+ }
116
+
117
+ }
118
+
119
+ void outputAllPatternTo(String filename) throws IOException {
120
+
121
+ try (PrintStream out = new PrintStream(filename);) {
122
+
123
+ outputAllPatternTo(out);
124
+
125
+ }
126
+
127
+ }
128
+
129
+ void outputAllPatternTo(PrintStream out) {
130
+
131
+ for (CharCounter last=ccList.get(ccList.size()-1); !carry; last.next()) {
132
+
133
+ for(CharCounter cc : ccList) out.print(cc.get());
134
+
135
+ out.println();
136
+
137
+ }
138
+
139
+ }
140
+
141
+ @Override
142
+
143
+ public void carryOver() { carry = true; }
144
+
145
+ }
146
+
147
+ class CharsRow {
148
+
149
+ private static String[] ROWS = {
150
+
151
+ "あぁかがさざただなはばぱまやゃらわ",
152
+
153
+ "いぃきぎしじちぢにひびぴみりゐ",
154
+
155
+ "うぅくぐすずつづぬふぶぷむゆゅる",
156
+
157
+ "えぇけげせぜてでねへべぺめれゑ",
158
+
159
+ "おぉこごそぞとどのほぼぽもよょろ",
160
+
161
+ };
162
+
163
+ private Map<Character,char[]> cache = new HashMap<>();
164
+
165
+ void check(String moto) {
166
+
167
+ for(char c : moto.toCharArray()){
168
+
37
- String r = getCharsRow(c);
169
+ char[] r = get(c);
38
-
39
- if(r != null) {
170
+
40
-
41
- System.out.println(c + "は、" + r.charAt(0) + (r.length()>=2?"段にあります":"です"));
171
+ System.out.println(c + "は、" + r[0] + (r.length>=2?"段にあります":"です"));
172
+
173
+ }
174
+
175
+ }
176
+
177
+ char[] get(char c) {
178
+
179
+ for(String r : ROWS) {
180
+
181
+ if(r.indexOf(c) >= 0) {
182
+
183
+ char[] a = cache.get(r.charAt(0));
184
+
185
+ if(a == null) {
186
+
187
+ a = r.toCharArray();
188
+
189
+ cache.put(r.charAt(0), a);
190
+
191
+ }
192
+
193
+ return a;
42
194
 
43
195
  }
44
196
 
45
197
  }
46
198
 
199
+ char[] a = cache.get(c);
200
+
201
+ if(a == null) {
202
+
203
+ a = new char[]{c};
204
+
205
+ cache.put(c, a);
206
+
207
+ }
208
+
209
+ return a;
210
+
47
211
  }
48
212
 
49
-
50
-
51
- private static void mk(String moto) throws IOException {
52
-
53
- List<CharCounter> ccList = new ArrayList<>();
54
-
55
- CarryFlag carryFlag = new CarryFlag();
56
-
57
- CarryOverListener prev = carryFlag;
58
-
59
- for(char c : moto.toCharArray()){
60
-
61
- String r = getCharsRow(c);
62
-
63
- CharCounter cc = new CharCounter(r!=null?r:""+c, prev);
64
-
65
- ccList.add(cc);
66
-
67
- prev = cc;
68
-
69
- }
70
-
71
-
72
-
73
- try (BufferedWriter bw = new BufferedWriter(new FileWriter("ShinIrekae.txt"));) {
74
-
75
- StringBuilder sb = new StringBuilder();
76
-
77
- for (CharCounter last = ccList.get(ccList.size()-1); !carryFlag.isOn(); last.next()) {
78
-
79
- sb.delete(0, sb.length());
80
-
81
- for(CharCounter cc : ccList) sb.append(cc.get());
82
-
83
- System.out.println(sb.toString());
84
-
85
- bw.write(sb.toString());
86
-
87
- bw.newLine();
88
-
89
- }
90
-
91
- }
92
-
93
- }
94
-
95
-
96
-
97
- private static String getCharsRow(char c) {
98
-
99
- for(String r : ROWS) {
100
-
101
- if(r.indexOf(c) >= 0) return r;
102
-
103
- }
104
-
105
- return null;
106
-
107
- }
108
-
109
-
110
-
111
- private static String A = "あぁかがさざただなはばぱまやゃらわ";
112
-
113
- private static String I = "いぃきぎしじちぢにひびぴみりゐ";
114
-
115
- private static String U = "うぅくぐすずつづぬふぶぷむゆゅる";
116
-
117
- private static String E = "えぇけげせぜてでねへべぺめれゑ";
118
-
119
- private static String O = "おぉこごそぞとどのほぼぽもよょろ";
120
-
121
- private static String N = "ん";
122
-
123
- private static String[] ROWS = {A,I,U,E,O,N};
124
-
125
213
  }
126
214
 
127
- interface CarryOverListener {
128
-
129
- void carryOver();
130
-
131
- }
132
-
133
- class CarryFlag implements CarryOverListener {
134
-
135
- private boolean flag = false;
136
-
137
- @Override
138
-
139
- public void carryOver() {
140
-
141
- flag = true;
142
-
143
- }
144
-
145
- boolean isOn() { return flag; }
146
-
147
- }
148
-
149
- /**
150
-
151
- * source の各文字を循環出力する.
152
-
153
- * next() で次の文字に移動し, 最後の文字の後は先頭の文字に戻る.
154
-
155
- * この時, 登録されていれば CarryOverListener#carryOver() を呼び出す.
156
-
157
- */
158
-
159
- class CharCounter implements CarryOverListener {
160
-
161
- private CarryOverListener listener;
162
-
163
- private char[] chars;
164
-
165
- private int index;
166
-
167
- CharCounter(String source, CarryOverListener l) {
168
-
169
- this.chars = source.toCharArray();
170
-
171
- this.listener = l;
172
-
173
- index = 0;
174
-
175
- }
176
-
177
- void next() {
178
-
179
- if(++index >= chars.length) {
180
-
181
- index = 0;
182
-
183
- if(listener != null) listener.carryOver();
184
-
185
- }
186
-
187
- }
188
-
189
- char get() { return chars[index]; }
190
-
191
- @Override
192
-
193
- public void carryOver() { next(); }
194
-
195
- }
196
-
197
215
  ```
198
216
 
199
217
  ```plain text

1

getCharsRow 追加

2020/03/16 12:40

投稿

jimbe
jimbe

スコア13209

test CHANGED
@@ -32,19 +32,13 @@
32
32
 
33
33
  private static void hikaku(String moto){
34
34
 
35
- final String[] rows = {A,I,U,E,O,N};
36
-
37
35
  for(char c : moto.toCharArray()){
38
36
 
39
- for(String r : rows) {
37
+ String r = getCharsRow(c);
40
-
38
+
41
- if(r.indexOf(c) >= 0) {
39
+ if(r != null) {
42
-
40
+
43
- System.out.println(c + "は、" + r.charAt(0) + (r.length()>=2?"段にあります":"です"));
41
+ System.out.println(c + "は、" + r.charAt(0) + (r.length()>=2?"段にあります":"です"));
44
-
45
- continue;
46
-
47
- }
48
42
 
49
43
  }
50
44
 
@@ -62,54 +56,54 @@
62
56
 
63
57
  CarryOverListener prev = carryFlag;
64
58
 
65
- String[] rows = {A,I,U,E,O};
66
-
67
59
  for(char c : moto.toCharArray()){
68
60
 
69
- CharCounter cc = null;
70
-
71
- for(String r : rows) {
61
+ String r = getCharsRow(c);
72
-
73
- if(r.indexOf(c) >= 0) {
62
+
74
-
75
- cc = new CharCounter(r, prev);
63
+ CharCounter cc = new CharCounter(r!=null?r:""+c, prev);
64
+
76
-
65
+ ccList.add(cc);
66
+
77
- break;
67
+ prev = cc;
78
-
68
+
79
- }
69
+ }
70
+
71
+
72
+
73
+ try (BufferedWriter bw = new BufferedWriter(new FileWriter("ShinIrekae.txt"));) {
74
+
75
+ StringBuilder sb = new StringBuilder();
76
+
77
+ for (CharCounter last = ccList.get(ccList.size()-1); !carryFlag.isOn(); last.next()) {
78
+
79
+ sb.delete(0, sb.length());
80
+
81
+ for(CharCounter cc : ccList) sb.append(cc.get());
82
+
83
+ System.out.println(sb.toString());
84
+
85
+ bw.write(sb.toString());
86
+
87
+ bw.newLine();
80
88
 
81
89
  }
82
90
 
83
- if(cc == null) cc = new CharCounter(""+c, prev);
84
-
85
- ccList.add(cc);
86
-
87
- prev = cc;
88
-
89
91
  }
90
92
 
91
-
92
-
93
- try (BufferedWriter bw = new BufferedWriter(new FileWriter("ShinIrekae.txt"));) {
94
-
95
- StringBuilder sb = new StringBuilder();
96
-
97
- for (CharCounter last = ccList.get(ccList.size()-1); !carryFlag.isOn(); last.next()) {
98
-
99
- sb.delete(0, sb.length());
100
-
101
- for(CharCounter cc : ccList) sb.append(cc.get());
102
-
103
- System.out.println(sb.toString());
104
-
105
- bw.write(sb.toString());
106
-
107
- bw.newLine();
108
-
109
- }
93
+ }
94
+
95
+
96
+
97
+ private static String getCharsRow(char c) {
98
+
99
+ for(String r : ROWS) {
100
+
101
+ if(r.indexOf(c) >= 0) return r;
110
102
 
111
103
  }
112
104
 
105
+ return null;
106
+
113
107
  }
114
108
 
115
109
 
@@ -126,6 +120,8 @@
126
120
 
127
121
  private static String N = "ん";
128
122
 
123
+ private static String[] ROWS = {A,I,U,E,O,N};
124
+
129
125
  }
130
126
 
131
127
  interface CarryOverListener {