回答編集履歴

1

書式修正

2017/02/01 08:02

投稿

hikochang
hikochang

スコア648

test CHANGED
@@ -8,197 +8,199 @@
8
8
 
9
9
 
10
10
 
11
- > using System;
12
-
13
- > using System.Linq;
14
-
15
- > using System.Text.RegularExpressions;
16
-
17
- >
18
-
19
- > namespace HexCalc
20
-
21
- > {
22
-
23
- > class Program
24
-
25
- > {
26
-
27
- > static void Main(string[] args)
28
-
29
- > {
30
-
31
- > while (true)
32
-
33
- > {
34
-
35
- > string input = Console.ReadLine();
36
-
37
- > // エラーチェック
38
-
39
- > Match match = Regex.Match(input,
40
-
41
- > @"((?<=[0-9a-zA-Z][ \t]*)\()|" + // "3(":数字直後のカッコ開始
42
-
43
- > @"((?<=[\+\-\*\/][ \t]*)\))|" + // "+)":演算子直後のカッコ終了
44
-
45
- > @"((?<=[\+\-\*\/][ \t]*)[\*\/])|" + // "+/":加減直後の乗除算
46
-
47
- > @"([\+\-\*\/][ \t]*[\+\-\*\/][ \t]*[\+\-\*\/])|" + // "*-+":3連続
48
-
49
- > @"((?<=\/[ \t]*)0(?![0-9a-zA-Z]))|" + // "/0 ":ゼロ割
50
-
51
- > @"((?<=^[ \t]*)[\*\/])|" + // "^*":先頭の乗除算
52
-
53
- > @"([\+\-\*\/](?=[ \t]*$))|" + // "+$":末尾
54
-
55
- > @"(^[ \t]*$)"); // "":
56
-
57
- > if (match.Success)
58
-
59
- > {
60
-
61
- > Console.WriteLine(input);
62
-
63
- > Console.WriteLine(string.Concat(Enumerable.Repeat(" ", match.Index)) + "^");
64
-
65
- > continue;
66
-
67
- > }
68
-
69
- >
70
-
71
- > // カッコの対応チェック
72
-
73
- > int level = 0;
74
-
75
- > int i = 0;
76
-
77
- > for( i=0; i< input.Length; i++ )
78
-
79
- > {
80
-
81
- > level += (input[i] == '(') ? +1 : 0;
82
-
83
- > level += (input[i] == ')') ? -1 : 0;
84
-
85
- > if (level < 0)
86
-
87
- > {
88
-
89
- > break;
90
-
91
- > }
92
-
93
- > }
94
-
95
- > if (level != 0)
96
-
97
- > {
98
-
99
- > Console.WriteLine(input);
100
-
101
- > Console.WriteLine(string.Concat(Enumerable.Repeat(" ", i)) + "^");
102
-
103
- > continue;
104
-
105
- > }
106
-
107
- > match = Regex.Match(input, @"(?<![0-9a-zA-Z])[0-9a-zA-Z]+(?![0-9a-zA-Z])");
108
-
109
- > while (match.Success)
110
-
111
- > {
112
-
113
- > input = Regex.Replace(input, match.Value, Int64.Parse(match.Value, System.Globalization.NumberStyles.AllowHexSpecifier).ToString());
114
-
115
- > match = match.NextMatch();
116
-
117
- > }
118
-
119
- >
120
-
121
- > input = '(' + input + ')';
122
-
123
- > while ((match = Regex.Match(input, @"\([ \t\+\-\*\/\d\.]*\)")).Success)
124
-
125
- > {
126
-
127
- > string result = match.Value.Trim(new char[] { '(', ')' });
128
-
129
- > Match m;
130
-
131
- > double r;
132
-
133
- > while ((m = Regex.Match(result, @"(?<=(^|[\+\-\*\/])[ \t]*)[\+\-]?\d+(\.\d+)?[ \t]*[\*\/][ \t]*[\+\-]?\d+(\.\d+)?(?!\d)")).Success)
134
-
135
- > {
136
-
137
- > if (0 <= m.Value.IndexOf("*"))
138
-
139
- > {
140
-
141
- > string[] op = m.Value.Split('*');
142
-
143
- > r = double.Parse(op[0]) * double.Parse(op[1]);
144
-
145
- > }
146
-
147
- > else
148
-
149
- > {
150
-
151
- > string[] op = m.Value.Split('/');
152
-
153
- > r = double.Parse(op[0]) / double.Parse(op[1]);
154
-
155
- > }
156
-
157
- > result = Regex.Replace(result, Regex.Escape(m.Value), r.ToString());
158
-
159
- > }
160
-
161
- > while ((m = Regex.Match(result, @"(?<=(^|[\+\-\*\/])[ \t]*)[\+\-]?\d+(\.\d+)?[ \t]*[\+\-][ \t]*[\+\-]?\d+(\.\d+)?(?!\d)")).Success)
162
-
163
- > {
164
-
165
- > MatchCollection op = Regex.Matches(result, @"(?<=(^|[\+\-\*\/])[ \t]*)[\+\-]?\d+(\.\d+)?[ \t]*(?!\d)");
166
-
167
- > if (0 <= m.Value.IndexOf("+"))
168
-
169
- > {
170
-
171
- > r = double.Parse(op[0].Value) + double.Parse(op[1].Value);
172
-
173
- > }
174
-
175
- > else
176
-
177
- > {
178
-
179
- > r = double.Parse(op[0].Value) - double.Parse(op[1].Value);
180
-
181
- > }
182
-
183
- > result = Regex.Replace(result, Regex.Escape(m.Value), r.ToString());
184
-
185
- > }
186
-
187
- > input = Regex.Replace(input, Regex.Escape(match.Value), result);
188
-
189
- > }
190
-
191
- > Console.WriteLine("10進数:"+input);
192
-
193
- > }
194
-
195
- > }
196
-
197
- > }
198
-
199
- > }
200
-
201
-
11
+ ```C#
12
+
13
+ using System;
14
+
15
+ using System.Linq;
16
+
17
+ using System.Text.RegularExpressions;
18
+
19
+
20
+
21
+ namespace HexCalc
22
+
23
+ {
24
+
25
+ class Program
26
+
27
+ {
28
+
29
+ static void Main(string[] args)
30
+
31
+ {
32
+
33
+ while (true)
34
+
35
+ {
36
+
37
+ string input = Console.ReadLine();
38
+
39
+ // エラーチェック
40
+
41
+ Match match = Regex.Match(input,
42
+
43
+ @"((?<=[0-9a-zA-Z][ \t]*)\()|" + // "3(":数字直後のカッコ開始
44
+
45
+ @"((?<=[\+\-\*\/][ \t]*)\))|" + // "+)":直後のカッコ終了
46
+
47
+ @"((?<=[\+\-\*\/][ \t]*)[\*\/])|" + // "+/":加減算の直後の乗除算
48
+
49
+ @"([\+\-\*\/][ \t]*[\+\-\*\/][ \t]*[\+\-\*\/])|" + // "*-+":演算子の3連続
50
+
51
+ @"((?<=\/[ \t]*)0(?![0-9a-zA-Z]))|" + // "/0 ":ゼロ割
52
+
53
+ @"((?<=^[ \t]*)[\*\/])|" + // "^*":先頭乗除
54
+
55
+ @"([\+\-\*\/](?=[ \t]*$))|" + // "+$":末尾の演算子
56
+
57
+ @"(^[ \t]*$)"); // "":空
58
+
59
+ if (match.Success)
60
+
61
+ {
62
+
63
+ Console.WriteLine(input);
64
+
65
+ Console.WriteLine(string.Concat(Enumerable.Repeat(" ", match.Index)) + "^");
66
+
67
+ continue;
68
+
69
+ }
70
+
71
+
72
+
73
+ // カッコの対応チェック
74
+
75
+ int level = 0;
76
+
77
+ int i = 0;
78
+
79
+ for( i=0; i< input.Length; i++ )
80
+
81
+ {
82
+
83
+ level += (input[i] == '(') ? +1 : 0;
84
+
85
+ level += (input[i] == ')') ? -1 : 0;
86
+
87
+ if (level < 0)
88
+
89
+ {
90
+
91
+ break;
92
+
93
+ }
94
+
95
+ }
96
+
97
+ if (level != 0)
98
+
99
+ {
100
+
101
+ Console.WriteLine(input);
102
+
103
+ Console.WriteLine(string.Concat(Enumerable.Repeat(" ", i)) + "^");
104
+
105
+ continue;
106
+
107
+ }
108
+
109
+ match = Regex.Match(input, @"(?<![0-9a-zA-Z])[0-9a-zA-Z]+(?![0-9a-zA-Z])");
110
+
111
+ while (match.Success)
112
+
113
+ {
114
+
115
+ input = Regex.Replace(input, match.Value, Int64.Parse(match.Value, System.Globalization.NumberStyles.AllowHexSpecifier).ToString());
116
+
117
+ match = match.NextMatch();
118
+
119
+ }
120
+
121
+
122
+
123
+ input = '(' + input + ')';
124
+
125
+ while ((match = Regex.Match(input, @"\([ \t\+\-\*\/\d\.]*\)")).Success)
126
+
127
+ {
128
+
129
+ string result = match.Value.Trim(new char[] { '(', ')' });
130
+
131
+ Match m;
132
+
133
+ double r;
134
+
135
+ while ((m = Regex.Match(result, @"(?<=(^|[\+\-\*\/])[ \t]*)[\+\-]?\d+(\.\d+)?[ \t]*[\*\/][ \t]*[\+\-]?\d+(\.\d+)?(?!\d)")).Success)
136
+
137
+ {
138
+
139
+ if (0 <= m.Value.IndexOf("*"))
140
+
141
+ {
142
+
143
+ string[] op = m.Value.Split('*');
144
+
145
+ r = double.Parse(op[0]) * double.Parse(op[1]);
146
+
147
+ }
148
+
149
+ else
150
+
151
+ {
152
+
153
+ string[] op = m.Value.Split('/');
154
+
155
+ r = double.Parse(op[0]) / double.Parse(op[1]);
156
+
157
+ }
158
+
159
+ result = Regex.Replace(result, Regex.Escape(m.Value), r.ToString());
160
+
161
+ }
162
+
163
+ while ((m = Regex.Match(result, @"(?<=(^|[\+\-\*\/])[ \t]*)[\+\-]?\d+(\.\d+)?[ \t]*[\+\-][ \t]*[\+\-]?\d+(\.\d+)?(?!\d)")).Success)
164
+
165
+ {
166
+
167
+ MatchCollection op = Regex.Matches(result, @"(?<=(^|[\+\-\*\/])[ \t]*)[\+\-]?\d+(\.\d+)?[ \t]*(?!\d)");
168
+
169
+ if (0 <= m.Value.IndexOf("+"))
170
+
171
+ {
172
+
173
+ r = double.Parse(op[0].Value) + double.Parse(op[1].Value);
174
+
175
+ }
176
+
177
+ else
178
+
179
+ {
180
+
181
+ r = double.Parse(op[0].Value) - double.Parse(op[1].Value);
182
+
183
+ }
184
+
185
+ result = Regex.Replace(result, Regex.Escape(m.Value), r.ToString());
186
+
187
+ }
188
+
189
+ input = Regex.Replace(input, Regex.Escape(match.Value), result);
190
+
191
+ }
192
+
193
+ Console.WriteLine("10進数:"+input);
194
+
195
+ }
196
+
197
+ }
198
+
199
+ }
200
+
201
+ }
202
+
203
+ ```
202
204
 
203
205
 
204
206