teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

25

修正

2017/07/16 18:31

投稿

退会済みユーザー
answer CHANGED
@@ -108,7 +108,6 @@
108
108
 
109
109
  if (valid_nest)
110
110
  {
111
- bool valid_number = true;
112
111
  int nums = 0;
113
112
  int symbol = 0;
114
113
 
@@ -122,10 +121,6 @@
122
121
  {
123
122
  nums++;
124
123
  }
125
- else
126
- {
127
- valid_number = false;
128
- }
129
124
  }
130
125
  else if (("+-*/%^".Contains(e)))
131
126
  {
@@ -135,20 +130,13 @@
135
130
 
136
131
  if (nums <= symbol) return "Invalid expression";
137
132
 
138
- if (valid_number)
133
+ for (int i = 0; i < list.Count; i++)
139
134
  {
140
- for (int i = 0; i < list.Count; i++)
141
- {
142
- if ("[{".Contains(list[i])) list[i] = "(";
135
+ if ("[{".Contains(list[i])) list[i] = "(";
143
- if ("]}".Contains(list[i])) list[i] = ")";
136
+ if ("]}".Contains(list[i])) list[i] = ")";
144
- }
137
+ }
145
138
 
146
- return list;
139
+ return list;
147
- }
148
- else
149
- {
150
- return "Invalid number";
151
- }
152
140
  }
153
141
 
154
142
  return "Missing end bracket";

24

修正

2017/07/16 18:31

投稿

退会済みユーザー
answer CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  class Program
8
8
  {
9
- static Object Split_Nums(string str)
9
+ static List<string> Split_Nums(string str)
10
10
  {
11
11
  str = str.Replace(" ", "");
12
12
  str = str.Replace("**", "^");
@@ -279,10 +279,8 @@
279
279
  {
280
280
  var nums = Split_Nums(str);
281
281
 
282
- if (nums is String) return (string)nums;
282
+ var result = Check(nums);
283
283
 
284
- var result = Check((List<string>)nums);
285
-
286
284
  if (result is String) return (string)result;
287
285
 
288
286
  double ans = Calc_Porland(Translate((List<string>)result));

23

修正

2017/07/16 18:06

投稿

退会済みユーザー
answer CHANGED
@@ -1,277 +1,301 @@
1
1
  ```C#
2
2
  using System;
3
3
  using System.Collections.Generic;
4
- using System.Text.RegularExpressions;
5
4
  using System.Linq;
6
- namespace calc_porland
5
+ using System.Text.RegularExpressions;
6
+
7
+ class Program
7
8
  {
8
- class Program
9
- {
10
- //式分解
11
- static List<string> split_nums(string str)
12
- {
13
- str = str.Replace(" ", "");
14
- str = str.Replace("**", "^");
15
- str = str.Replace("f", "");
16
-
17
- Regex re = new Regex(@"\((?<nega>-\d+?(|.\d+?))\)");
18
-
19
- List<string> negative = new List<string>();
20
-
21
- MatchCollection mc = re.Matches(str);
22
-
23
- foreach(Match m in mc)
24
- {
25
- negative.Add(m.Groups["nega"].Value);
26
- }
27
-
28
- str = re.Replace(str, "f");
29
- List<string> for_translate = new List<string>();
30
- List<string> tmp = new List<string>();
31
- for (int i = 0; i < str.Length; i++)
32
- {
33
- if ("+-*/%^()[]{}".Contains(str[i]))
34
- {
35
- if (tmp.Count > 0)
36
- {
37
- for_translate.Add(string.Join("", tmp));
38
- tmp.Clear();
39
- }
40
- for_translate.Add(str[i].ToString());
41
- }
42
- else
43
- {
44
- tmp.Add(str[i].ToString());
45
- }
46
- if(i == str.Length - 1)
47
- {
48
- if(tmp.Count > 0)
49
- {
50
- for_translate.Add(string.Join("", tmp));
51
- }
52
- }
53
- }
54
- for(int i =0;i < for_translate.Count; i++)
55
- {
56
- if (for_translate[i] == "f")
57
- {
58
- for_translate[i] = negative[0];
59
- negative.RemoveAt(0);
60
- }
61
- }
62
-
63
- return for_translate;
64
- }
65
- //かっこが正しいか数値が正しいかを判定
66
- static Object check(List<string> list)
67
- {
68
- bool valid_nest = true;
69
- List<string> nest = new List<string>();
70
- Dictionary<string, string> p_dic = new Dictionary<string, string>()
71
- {
72
- { ")", "(" },
73
- { "]", "[" },
74
- { "}", "{" }
75
- };
76
- foreach(string e in list)
77
- {
78
- if ("([{".Contains(e))
79
- {
80
- nest.Add(e);
81
- }
82
- else if (")]}".Contains(e))
83
- {
84
- if(nest.Count < 1)
85
- {
86
- valid_nest = false;
87
- break;
88
- }
89
- if(nest.Last() != p_dic[e])
90
- {
91
- valid_nest = false;
92
- break;
93
- }
94
- nest.RemoveAt(nest.Count - 1);
95
- }
96
- }
97
- if (nest.Any()) valid_nest = false;
98
- if(valid_nest)
99
- {
100
- bool valid_number = true;
101
- int nums = 0;
102
- int symbol = 0;
103
- foreach(string e in list)
104
- {
105
- if ("+-*/%^()[]{}".Contains(e) == false)
106
- {
107
- double d = 0;
108
- if (double.TryParse(e, out d))
109
- {
110
- nums++;
111
- }
112
- else
113
- {
114
- valid_number = false;
115
- }
116
- }
117
- else if(("+-*/%^".Contains(e)))
118
- {
119
- symbol++;
120
- }
121
- }
122
- if (nums <= symbol) return "式が不正です";
123
- if(valid_number)
124
- {
125
- for (int i = 0; i < list.Count; i++)
126
- {
127
- if ("[{".Contains(list[i])) list[i] = "(";
128
- if ("]}".Contains(list[i])) list[i] = ")";
129
- }
130
- return list;
131
- }
132
- else
133
- {
134
- return "数値が不正です";
135
- }
136
- }
137
- return "かっこが不正です";
138
- }
139
- //逆ポーランド記法に変換
140
- static List<string> translate(List<string> list)
141
- {
142
- List<string> stack = new List<string>();
143
- List<string> for_calc = new List<string>();
144
- for(int i = 0; i < list.Count; i++)
145
- {
146
- if(list[i] == "(")
147
- {
148
- stack.Add(list[i]);
149
- }
150
- else if(list[i] == ")")
151
- {
152
- while (stack.Last() != "(")
153
- {
154
- for_calc.Add(stack.Last());
155
- stack.RemoveAt(stack.Count - 1);
156
- }
157
- stack.RemoveAt(stack.Count - 1);
158
- }
159
- else if ("+-*/%^".Contains(list[i]))
160
- {
161
- if(stack.Count > 0)
162
- {
163
- if ("+-".Contains(list[i]))
164
- {
165
- while ("*/%^".Contains(stack.Last()))
166
- {
167
- for_calc.Add(stack.Last());
168
- stack.RemoveAt(stack.Count - 1);
169
- if (stack.Count < 1) break;
170
- }
171
- }
172
- else if (list[i] == "*")
173
- {
174
- while ("/%^".Contains(stack.Last()))
175
- {
176
- for_calc.Add(stack.Last());
177
- stack.RemoveAt(stack.Count - 1);
178
- if (stack.Count < 1) break;
179
- }
180
- }
181
- else if ("/%".Contains(list[i]))
182
- {
183
- while (stack.Last() == "^")
184
- {
185
- for_calc.Add(stack.Last());
186
- stack.RemoveAt(stack.Count - 1);
187
- if (stack.Count < 1) break;
188
- }
189
- }
190
- }
191
- stack.Add(list[i]);
192
- }
193
- else
194
- {
195
- for_calc.Add(list[i]);
196
- }
197
- if(i == list.Count - 1)
198
- {
199
- if (stack.Count > 0)
200
- {
201
- stack.Reverse();
202
- foreach(string e in stack)
203
- {
204
- for_calc.Add(e);
205
- }
206
- }
207
- }
208
- }
209
- return for_calc;
210
- }
211
- //逆ポーランド記法を解く
212
- static double calc_porland(List<string> list)
213
- {
214
- List<double> stack = new List<double>();
215
- foreach(string e in list)
216
- {
217
- double d = 0;
218
- if(double.TryParse(e, out d))
219
- {
220
- stack.Add(d);
221
- }
222
- else
223
- {
224
- double a = stack.Last(); stack.RemoveAt(stack.Count - 1);
225
- double b = stack.Last(); stack.RemoveAt(stack.Count - 1);
226
- double acue = 0;
227
- switch (e)
228
- {
229
- case "+":
230
- acue = b + a;
231
- break;
232
- case "-":
233
- acue = b - a;
234
- break;
235
- case "*":
236
- acue = b * a;
237
- break;
238
- case "/":
239
- acue = b / a;
240
- break;
241
- case "%":
242
- acue = b % a;
243
- break;
244
- case "^":
245
- acue = Math.Pow(b, a);
246
- break;
247
- }
248
- stack.Add(acue);
249
- }
250
- }
251
- return stack[0];
252
- }
253
- //上のメソッドの結果を合体
254
- static string porland(string str)
255
- {
256
- List<string> for_check = split_nums(str);
257
- var result = check(for_check);
258
- if (result is String)
259
- {
260
- return (string)result;
261
- }
262
- else
263
- {
264
- return calc_porland(translate((List<string>)result)).ToString();
265
- }
266
- }
267
- static void Main(string[] args)
268
- {
269
- //string input = "[{(1+2)*(3+4)}+{(5+6)*(7+8)}]*[{(9+10)*(11+12)}+{(13+14)*(15+16)}]";
270
- string input = "[{(1+2)*(3+4)}+{(5+6)*(7+8)}]*[{(9+10)*(11+12)}+{(13+14)*(15+16)}]*(-1)+0.1";
271
- Console.WriteLine(porland(input));
272
- Console.ReadKey();
273
- }
274
- }
9
+ static Object Split_Nums(string str)
10
+ {
11
+ str = str.Replace(" ", "");
12
+ str = str.Replace("**", "^");
13
+ str = str.Replace(",", "");
14
+
15
+ Regex re = new Regex(@"\((?<nega>-\d+?(|.\d+?))\)");
16
+
17
+ List<string> negative = new List<string>();
18
+
19
+ MatchCollection mc = re.Matches(str);
20
+
21
+ foreach (Match m in mc)
22
+ {
23
+ negative.Add(m.Groups["nega"].Value);
24
+ }
25
+
26
+ str = re.Replace(str, "f");
27
+
28
+ List<string> for_translate = new List<string>();
29
+ List<string> tmp = new List<string>();
30
+
31
+ for (int i = 0; i < str.Length; i++)
32
+ {
33
+ if ("+-*/%^()[]{}".Contains(str[i]))
34
+ {
35
+ if (tmp.Count > 0)
36
+ {
37
+ for_translate.Add(string.Join("", tmp));
38
+ tmp.Clear();
39
+ }
40
+
41
+ for_translate.Add(str[i].ToString());
42
+ }
43
+ else
44
+ {
45
+ tmp.Add(str[i].ToString());
46
+ }
47
+
48
+ if (i == str.Length - 1)
49
+ {
50
+ if (tmp.Count > 0)
51
+ {
52
+ for_translate.Add(string.Join("", tmp));
53
+ }
54
+ }
55
+ }
56
+
57
+ for (int i = 0; i < for_translate.Count; i++)
58
+ {
59
+ if (for_translate[i] == "f")
60
+ {
61
+ for_translate[i] = negative[0];
62
+ negative.RemoveAt(0);
63
+ }
64
+ }
65
+
66
+ return for_translate;
67
+ }
68
+
69
+ //かっこが正しいか数値が正しいかを判定
70
+ static Object Check(List<string> list)
71
+ {
72
+ bool valid_nest = true;
73
+
74
+ List<string> nest = new List<string>();
75
+
76
+ Dictionary<string, string> p_dic = new Dictionary<string, string>()
77
+ {
78
+ { ")", "(" },
79
+ { "]", "[" },
80
+ { "}", "{" }
81
+ };
82
+
83
+ foreach (string e in list)
84
+ {
85
+ if ("([{".Contains(e))
86
+ {
87
+ nest.Add(e);
88
+ }
89
+ else if (")]}".Contains(e))
90
+ {
91
+ if (nest.Count < 1)
92
+ {
93
+ valid_nest = false;
94
+ break;
95
+ }
96
+
97
+ if (nest.Last() != p_dic[e])
98
+ {
99
+ valid_nest = false;
100
+ break;
101
+ }
102
+
103
+ nest.RemoveAt(nest.Count - 1);
104
+ }
105
+ }
106
+
107
+ if (nest.Any()) valid_nest = false;
108
+
109
+ if (valid_nest)
110
+ {
111
+ bool valid_number = true;
112
+ int nums = 0;
113
+ int symbol = 0;
114
+
115
+ foreach (string e in list)
116
+ {
117
+ if ("+-*/%^()[]{}".Contains(e) == false)
118
+ {
119
+ double d = 0;
120
+
121
+ if (double.TryParse(e, out d))
122
+ {
123
+ nums++;
124
+ }
125
+ else
126
+ {
127
+ valid_number = false;
128
+ }
129
+ }
130
+ else if (("+-*/%^".Contains(e)))
131
+ {
132
+ symbol++;
133
+ }
134
+ }
135
+
136
+ if (nums <= symbol) return "Invalid expression";
137
+
138
+ if (valid_number)
139
+ {
140
+ for (int i = 0; i < list.Count; i++)
141
+ {
142
+ if ("[{".Contains(list[i])) list[i] = "(";
143
+ if ("]}".Contains(list[i])) list[i] = ")";
144
+ }
145
+
146
+ return list;
147
+ }
148
+ else
149
+ {
150
+ return "Invalid number";
151
+ }
152
+ }
153
+
154
+ return "Missing end bracket";
155
+ }
156
+
157
+ //逆ポーランド記法に変換
158
+ static List<string> Translate(List<string> list)
159
+ {
160
+ List<string> stack = new List<string>();
161
+ List<string> for_calc = new List<string>();
162
+
163
+ for (int i = 0; i < list.Count; i++)
164
+ {
165
+ if (list[i] == "(")
166
+ {
167
+ stack.Add(list[i]);
168
+ }
169
+ else if (list[i] == ")")
170
+ {
171
+ while (stack.Last() != "(")
172
+ {
173
+ for_calc.Add(stack.Last());
174
+ stack.RemoveAt(stack.Count - 1);
175
+ }
176
+
177
+ stack.RemoveAt(stack.Count - 1);
178
+ }
179
+ else if ("+-*/%^".Contains(list[i]))
180
+ {
181
+ if (stack.Count > 0)
182
+ {
183
+ if ("+-".Contains(list[i]))
184
+ {
185
+ while ("+-*/%^".Contains(stack.Last()))
186
+ {
187
+ for_calc.Add(stack.Last());
188
+ stack.RemoveAt(stack.Count - 1);
189
+
190
+ if (stack.Count < 1) break;
191
+ }
192
+ }
193
+ else if ("*/%".Contains(list[i]))
194
+ {
195
+ while ("*/%^".Contains(stack.Last()))
196
+ {
197
+ for_calc.Add(stack.Last());
198
+ stack.RemoveAt(stack.Count - 1);
199
+
200
+ if (stack.Count < 1) break;
201
+ }
202
+ }
203
+ }
204
+
205
+ stack.Add(list[i]);
206
+ }
207
+ else
208
+ {
209
+ for_calc.Add(list[i]);
210
+ }
211
+
212
+ if (i == list.Count - 1)
213
+ {
214
+ if (stack.Count > 0)
215
+ {
216
+ stack.Reverse();
217
+
218
+ foreach (string e in stack)
219
+ {
220
+ for_calc.Add(e);
221
+ }
222
+ }
223
+ }
224
+ }
225
+
226
+ return for_calc;
227
+ }
228
+
229
+ //逆ポーランド記法を解く
230
+ static double Calc_Porland(List<string> list)
231
+ {
232
+ List<string> stack = new List<string>();
233
+
234
+ while (list.Any())
235
+ {
236
+ stack.Add(list[0]); list.RemoveAt(0);
237
+
238
+ if ("+-*/%^".Contains(stack.Last()))
239
+ {
240
+ List<string> tmp = stack.GetRange(stack.Count() - 3, 3);
241
+
242
+ stack.RemoveRange(stack.Count() - 3, 3);
243
+
244
+ double a = double.Parse(tmp[0]);
245
+ double b = double.Parse(tmp[1]);
246
+
247
+ double d = 0.0D;
248
+
249
+ switch (tmp[2])
250
+ {
251
+ case "+":
252
+ d = a + b;
253
+ break;
254
+ case "-":
255
+ d = a - b;
256
+ break;
257
+ case "*":
258
+ d = a * b;
259
+ break;
260
+ case "/":
261
+ d = a / b;
262
+ break;
263
+ case "%":
264
+ d = a % b;
265
+ break;
266
+ case "^":
267
+ d = Math.Pow(a, b);
268
+ break;
269
+ }
270
+
271
+ stack.Add(d.ToString());
272
+ }
273
+ }
274
+
275
+ return double.Parse(stack[0]);
276
+ }
277
+
278
+ static string Calculate(string str)
279
+ {
280
+ var nums = Split_Nums(str);
281
+
282
+ if (nums is String) return (string)nums;
283
+
284
+ var result = Check((List<string>)nums);
285
+
286
+ if (result is String) return (string)result;
287
+
288
+ double ans = Calc_Porland(Translate((List<string>)result));
289
+
290
+ return ans.ToString();
291
+ }
292
+
293
+ static void Main(string[] args)
294
+ {
295
+ string input = "[{(1+2)*(3+4)}+{(5+6)*(7+8)}]*[{(9+10)*(11+12)}+{(13+14)*(15+16)}]*(-1)+0.1";
296
+ Console.WriteLine(Calculate(input));
297
+ Console.ReadKey();
298
+ }
275
299
  }
276
300
  ```
277
301
  参考にしたサイト

22

テスト3

2017/07/16 17:56

投稿

退会済みユーザー
answer CHANGED
File without changes

21

テスト2

2017/07/11 12:48

投稿

退会済みユーザー
answer CHANGED
File without changes

20

テスト

2017/07/11 12:47

投稿

退会済みユーザー
answer CHANGED
File without changes

19

修正

2017/07/11 12:45

投稿

退会済みユーザー
answer CHANGED
File without changes

18

修正

2017/07/07 12:14

投稿

退会済みユーザー
answer CHANGED
File without changes

17

修正

2017/07/06 00:18

投稿

退会済みユーザー
answer CHANGED
File without changes

16

修正

2017/07/06 00:15

投稿

退会済みユーザー
answer CHANGED
File without changes

15

修正

2017/06/29 03:07

投稿

退会済みユーザー
answer CHANGED
File without changes

14

変更が反映されない?

2017/06/29 03:05

投稿

退会済みユーザー
answer CHANGED
@@ -1,297 +1,277 @@
1
1
  ```C#
2
2
  using System;
3
3
  using System.Collections.Generic;
4
+ using System.Text.RegularExpressions;
4
5
  using System.Linq;
5
-
6
6
  namespace calc_porland
7
7
  {
8
- class Program
9
- {
10
- //式分解
11
- static List<string> split_nums(string str)
12
- {
13
- str = str.Replace(" ", "");
14
- str = str.Replace("**", "^");
15
-
16
- List<string> for_translate = new List<string>();
17
- List<string> tmp = new List<string>();
18
-
19
- for (int i = 0; i < str.Length; i++)
20
- {
21
- if ("+-*/%^()[]{}".Contains(str[i]))
22
- {
23
- if (tmp.Count > 0)
24
- {
25
- for_translate.Add(string.Join("", tmp));
26
- tmp.Clear();
27
- }
28
-
29
- for_translate.Add(str[i].ToString());
30
- }
31
- else
32
- {
33
- tmp.Add(str[i].ToString());
34
- }
35
-
36
- if(i == str.Length - 1)
37
- {
38
- if(tmp.Count > 0)
39
- {
40
- for_translate.Add(string.Join("", tmp));
41
- }
42
- }
43
- }
44
-
45
- return for_translate;
46
- }
47
-
48
- //かっこが正しいか数値が正しいかを判定
49
- static Object check(List<string> list)
50
- {
51
- bool valid_nest = true;
52
-
53
- List<string> nest = new List<string>();
54
-
55
- Dictionary<string, string> p_dic = new Dictionary<string, string>()
56
- {
57
- { ")", "(" },
58
- { "]", "[" },
59
- { "}", "{" }
60
- };
61
-
62
- foreach(string e in list)
63
- {
64
- if ("([{".Contains(e))
65
- {
66
- nest.Add(e);
67
- }
68
- else if (")]}".Contains(e))
69
- {
70
- if(nest.Count < 1)
71
- {
72
- valid_nest = false;
73
- break;
74
- }
75
-
76
- if(nest.Last() != p_dic[e])
77
- {
78
- valid_nest = false;
79
- break;
80
- }
81
-
82
- nest.RemoveAt(nest.Count - 1);
83
- }
84
- }
85
-
86
- if (nest.Any()) valid_nest = false;
87
-
88
- if(valid_nest)
89
- {
90
- bool valid_number = true;
91
- int nums = 0;
92
- int symbol = 0;
93
-
94
- foreach(string e in list)
95
- {
96
- if ("+-*/%^()[]{}".Contains(e) == false)
97
- {
98
- double d = 0;
99
-
100
- if (double.TryParse(e, out d))
101
- {
102
- nums++;
103
- }
104
- else
105
- {
106
- valid_number = false;
107
- }
108
- }
109
- else if(("+-*/%^".Contains(e)))
110
- {
111
- symbol++;
112
- }
113
- }
114
-
115
- if (nums <= symbol) return "式が不正です";
116
-
117
- if(valid_number)
118
- {
119
- for (int i = 0; i < list.Count; i++)
120
- {
121
- if ("[{".Contains(list[i])) list[i] = "(";
122
- if ("]}".Contains(list[i])) list[i] = ")";
123
- }
124
-
125
- return list;
126
- }
127
- else
128
- {
129
- return "数値が不正です";
130
- }
131
- }
132
-
133
- return "かっこが不正です";
134
- }
135
-
136
- //逆ポーランド記法に変換
137
- static List<string> translate(List<string> list)
138
- {
139
- List<string> stack = new List<string>();
140
- List<string> for_calc = new List<string>();
141
-
142
- for(int i = 0; i < list.Count; i++)
143
- {
144
- if(list[i] == "(")
145
- {
146
- stack.Add(list[i]);
147
- }
148
- else if(list[i] == ")")
149
- {
150
- while (stack.Last() != "(")
151
- {
152
- for_calc.Add(stack.Last());
153
- stack.RemoveAt(stack.Count - 1);
154
- }
155
-
156
- stack.RemoveAt(stack.Count - 1);
157
- }
158
- else if ("+-*/%^".Contains(list[i]))
159
- {
160
- if(stack.Count > 0)
161
- {
162
- if ("+-".Contains(list[i]))
163
- {
164
- while ("*/%^".Contains(stack.Last()))
165
- {
166
- for_calc.Add(stack.Last());
167
- stack.RemoveAt(stack.Count - 1);
168
-
169
- if (stack.Count < 1) break;
170
- }
171
- }
172
- else if (list[i] == "*")
173
- {
174
- while ("/%^".Contains(stack.Last()))
175
- {
176
- for_calc.Add(stack.Last());
177
- stack.RemoveAt(stack.Count - 1);
178
-
179
- if (stack.Count < 1) break;
180
- }
181
- }
182
- else if ("/%".Contains(list[i]))
183
- {
184
- while (stack.Last() == "^")
185
- {
186
- for_calc.Add(stack.Last());
187
- stack.RemoveAt(stack.Count - 1);
188
-
189
- if (stack.Count < 1) break;
190
- }
191
- }
192
- }
193
-
194
- stack.Add(list[i]);
195
- }
196
- else
197
- {
198
- for_calc.Add(list[i]);
199
- }
200
-
201
- if(i == list.Count - 1)
202
- {
203
- if (stack.Count > 0)
204
- {
205
- stack.Reverse();
206
-
207
- foreach(string e in stack)
208
- {
209
- for_calc.Add(e);
210
- }
211
- }
212
- }
213
- }
214
-
215
- return for_calc;
216
- }
217
-
218
- //逆ポーランド記法を解く
219
- static double calc_porland(List<string> list)
220
- {
221
- List<double> stack = new List<double>();
222
-
223
- foreach(string e in list)
224
- {
225
- double d = 0;
226
-
227
- if(double.TryParse(e, out d))
228
- {
229
- stack.Add(d);
230
- }
231
- else
232
- {
233
- double a = stack.Last(); stack.RemoveAt(stack.Count - 1);
234
- double b = stack.Last(); stack.RemoveAt(stack.Count - 1);
235
-
236
- double acue = 0;
237
-
238
- switch (e)
239
- {
240
- case "+":
241
- acue = b + a;
242
- break;
243
- case "-":
244
- acue = b - a;
245
- break;
246
- case "*":
247
- acue = b * a;
248
- break;
249
- case "/":
250
- acue = b / a;
251
- break;
252
- case "%":
253
- acue = b % a;
254
- break;
255
- case "^":
256
- acue = Math.Pow(b, a);
257
- break;
258
- }
259
-
260
- stack.Add(acue);
261
- }
262
- }
263
-
264
- return stack[0];
265
- }
266
-
267
- //上のメソッドの結果を合体
268
- static string porland(string str)
269
- {
270
- List<string> for_check = split_nums(str);
271
-
272
- var result = check(for_check);
273
-
274
- if (result is String)
275
- {
276
- return (string)result;
277
- }
278
- else
279
- {
280
- return calc_porland(translate((List<string>)result)).ToString();
281
- }
282
- }
283
-
284
- static void Main(string[] args)
285
- {
286
-
287
- //string input = "[{(1+2)*(3+4)}+{(5+6)*(7+8)}]*[{(9+10)*(11+12)}+{(13+14)*(15+16)}]";
288
-
289
- string input = Console.ReadLine();
290
-
291
- Console.WriteLine(porland(input));
292
- Console.ReadKey();
293
- }
294
- }
8
+ class Program
9
+ {
10
+ //式分解
11
+ static List<string> split_nums(string str)
12
+ {
13
+ str = str.Replace(" ", "");
14
+ str = str.Replace("**", "^");
15
+ str = str.Replace("f", "");
16
+
17
+ Regex re = new Regex(@"\((?<nega>-\d+?(|.\d+?))\)");
18
+
19
+ List<string> negative = new List<string>();
20
+
21
+ MatchCollection mc = re.Matches(str);
22
+
23
+ foreach(Match m in mc)
24
+ {
25
+ negative.Add(m.Groups["nega"].Value);
26
+ }
27
+
28
+ str = re.Replace(str, "f");
29
+ List<string> for_translate = new List<string>();
30
+ List<string> tmp = new List<string>();
31
+ for (int i = 0; i < str.Length; i++)
32
+ {
33
+ if ("+-*/%^()[]{}".Contains(str[i]))
34
+ {
35
+ if (tmp.Count > 0)
36
+ {
37
+ for_translate.Add(string.Join("", tmp));
38
+ tmp.Clear();
39
+ }
40
+ for_translate.Add(str[i].ToString());
41
+ }
42
+ else
43
+ {
44
+ tmp.Add(str[i].ToString());
45
+ }
46
+ if(i == str.Length - 1)
47
+ {
48
+ if(tmp.Count > 0)
49
+ {
50
+ for_translate.Add(string.Join("", tmp));
51
+ }
52
+ }
53
+ }
54
+ for(int i =0;i < for_translate.Count; i++)
55
+ {
56
+ if (for_translate[i] == "f")
57
+ {
58
+ for_translate[i] = negative[0];
59
+ negative.RemoveAt(0);
60
+ }
61
+ }
62
+
63
+ return for_translate;
64
+ }
65
+ //かっこが正しいか数値が正しいかを判定
66
+ static Object check(List<string> list)
67
+ {
68
+ bool valid_nest = true;
69
+ List<string> nest = new List<string>();
70
+ Dictionary<string, string> p_dic = new Dictionary<string, string>()
71
+ {
72
+ { ")", "(" },
73
+ { "]", "[" },
74
+ { "}", "{" }
75
+ };
76
+ foreach(string e in list)
77
+ {
78
+ if ("([{".Contains(e))
79
+ {
80
+ nest.Add(e);
81
+ }
82
+ else if (")]}".Contains(e))
83
+ {
84
+ if(nest.Count < 1)
85
+ {
86
+ valid_nest = false;
87
+ break;
88
+ }
89
+ if(nest.Last() != p_dic[e])
90
+ {
91
+ valid_nest = false;
92
+ break;
93
+ }
94
+ nest.RemoveAt(nest.Count - 1);
95
+ }
96
+ }
97
+ if (nest.Any()) valid_nest = false;
98
+ if(valid_nest)
99
+ {
100
+ bool valid_number = true;
101
+ int nums = 0;
102
+ int symbol = 0;
103
+ foreach(string e in list)
104
+ {
105
+ if ("+-*/%^()[]{}".Contains(e) == false)
106
+ {
107
+ double d = 0;
108
+ if (double.TryParse(e, out d))
109
+ {
110
+ nums++;
111
+ }
112
+ else
113
+ {
114
+ valid_number = false;
115
+ }
116
+ }
117
+ else if(("+-*/%^".Contains(e)))
118
+ {
119
+ symbol++;
120
+ }
121
+ }
122
+ if (nums <= symbol) return "式が不正です";
123
+ if(valid_number)
124
+ {
125
+ for (int i = 0; i < list.Count; i++)
126
+ {
127
+ if ("[{".Contains(list[i])) list[i] = "(";
128
+ if ("]}".Contains(list[i])) list[i] = ")";
129
+ }
130
+ return list;
131
+ }
132
+ else
133
+ {
134
+ return "数値が不正です";
135
+ }
136
+ }
137
+ return "かっこが不正です";
138
+ }
139
+ //逆ポーランド記法に変換
140
+ static List<string> translate(List<string> list)
141
+ {
142
+ List<string> stack = new List<string>();
143
+ List<string> for_calc = new List<string>();
144
+ for(int i = 0; i < list.Count; i++)
145
+ {
146
+ if(list[i] == "(")
147
+ {
148
+ stack.Add(list[i]);
149
+ }
150
+ else if(list[i] == ")")
151
+ {
152
+ while (stack.Last() != "(")
153
+ {
154
+ for_calc.Add(stack.Last());
155
+ stack.RemoveAt(stack.Count - 1);
156
+ }
157
+ stack.RemoveAt(stack.Count - 1);
158
+ }
159
+ else if ("+-*/%^".Contains(list[i]))
160
+ {
161
+ if(stack.Count > 0)
162
+ {
163
+ if ("+-".Contains(list[i]))
164
+ {
165
+ while ("*/%^".Contains(stack.Last()))
166
+ {
167
+ for_calc.Add(stack.Last());
168
+ stack.RemoveAt(stack.Count - 1);
169
+ if (stack.Count < 1) break;
170
+ }
171
+ }
172
+ else if (list[i] == "*")
173
+ {
174
+ while ("/%^".Contains(stack.Last()))
175
+ {
176
+ for_calc.Add(stack.Last());
177
+ stack.RemoveAt(stack.Count - 1);
178
+ if (stack.Count < 1) break;
179
+ }
180
+ }
181
+ else if ("/%".Contains(list[i]))
182
+ {
183
+ while (stack.Last() == "^")
184
+ {
185
+ for_calc.Add(stack.Last());
186
+ stack.RemoveAt(stack.Count - 1);
187
+ if (stack.Count < 1) break;
188
+ }
189
+ }
190
+ }
191
+ stack.Add(list[i]);
192
+ }
193
+ else
194
+ {
195
+ for_calc.Add(list[i]);
196
+ }
197
+ if(i == list.Count - 1)
198
+ {
199
+ if (stack.Count > 0)
200
+ {
201
+ stack.Reverse();
202
+ foreach(string e in stack)
203
+ {
204
+ for_calc.Add(e);
205
+ }
206
+ }
207
+ }
208
+ }
209
+ return for_calc;
210
+ }
211
+ //逆ポーランド記法を解く
212
+ static double calc_porland(List<string> list)
213
+ {
214
+ List<double> stack = new List<double>();
215
+ foreach(string e in list)
216
+ {
217
+ double d = 0;
218
+ if(double.TryParse(e, out d))
219
+ {
220
+ stack.Add(d);
221
+ }
222
+ else
223
+ {
224
+ double a = stack.Last(); stack.RemoveAt(stack.Count - 1);
225
+ double b = stack.Last(); stack.RemoveAt(stack.Count - 1);
226
+ double acue = 0;
227
+ switch (e)
228
+ {
229
+ case "+":
230
+ acue = b + a;
231
+ break;
232
+ case "-":
233
+ acue = b - a;
234
+ break;
235
+ case "*":
236
+ acue = b * a;
237
+ break;
238
+ case "/":
239
+ acue = b / a;
240
+ break;
241
+ case "%":
242
+ acue = b % a;
243
+ break;
244
+ case "^":
245
+ acue = Math.Pow(b, a);
246
+ break;
247
+ }
248
+ stack.Add(acue);
249
+ }
250
+ }
251
+ return stack[0];
252
+ }
253
+ //上のメソッドの結果を合体
254
+ static string porland(string str)
255
+ {
256
+ List<string> for_check = split_nums(str);
257
+ var result = check(for_check);
258
+ if (result is String)
259
+ {
260
+ return (string)result;
261
+ }
262
+ else
263
+ {
264
+ return calc_porland(translate((List<string>)result)).ToString();
265
+ }
266
+ }
267
+ static void Main(string[] args)
268
+ {
269
+ //string input = "[{(1+2)*(3+4)}+{(5+6)*(7+8)}]*[{(9+10)*(11+12)}+{(13+14)*(15+16)}]";
270
+ string input = "[{(1+2)*(3+4)}+{(5+6)*(7+8)}]*[{(9+10)*(11+12)}+{(13+14)*(15+16)}]*(-1)+0.1";
271
+ Console.WriteLine(porland(input));
272
+ Console.ReadKey();
273
+ }
274
+ }
295
275
  }
296
276
  ```
297
277
  参考にしたサイト
@@ -300,8 +280,6 @@
300
280
  [逆ポーランド記法変換ツール](http://tamanegi-core.net/memo/article/polish_notation_converter)
301
281
  [rubyについて質問です。](https://teratail.com/questions/80654)のkatoy様の回答
302
282
  [逆ポーランド記法への変換2](http://www.gg.e-mansion.com/~kkatoh/program/novel2/novel208.html)
303
-
304
-
305
283
  素人ですが考えてみました。
306
284
  このプログラムが正確に動くかどうかは分かりません。
307
- B型単純式についてはよくわからなかったので何もしていません。
285
+ B型単純式についてはよくわからなかったので何もしていません。

13

修正

2017/06/28 21:32

投稿

退会済みユーザー
answer CHANGED
File without changes

12

修正

2017/06/28 21:28

投稿

退会済みユーザー
answer CHANGED
File without changes

11

修正

2017/06/28 21:26

投稿

退会済みユーザー
answer CHANGED
@@ -195,8 +195,7 @@
195
195
  }
196
196
  else
197
197
  {
198
- double d = 0;
199
- if(double.TryParse(list[i], out d)) for_calc.Add(list[i]);
198
+ for_calc.Add(list[i]);
200
199
  }
201
200
 
202
201
  if(i == list.Count - 1)
@@ -212,6 +211,7 @@
212
211
  }
213
212
  }
214
213
  }
214
+
215
215
  return for_calc;
216
216
  }
217
217
 
@@ -283,8 +283,11 @@
283
283
 
284
284
  static void Main(string[] args)
285
285
  {
286
- string input = "[{(1+2)*(3+4)}+{(5+6)*(7+8)}]*[{(9+10)*(11+12)}+{(13+14)*(15+16)}]";
287
286
 
287
+ //string input = "[{(1+2)*(3+4)}+{(5+6)*(7+8)}]*[{(9+10)*(11+12)}+{(13+14)*(15+16)}]";
288
+
289
+ string input = Console.ReadLine();
290
+
288
291
  Console.WriteLine(porland(input));
289
292
  Console.ReadKey();
290
293
  }

10

修正

2017/06/28 16:57

投稿

退会済みユーザー
answer CHANGED
@@ -112,10 +112,7 @@
112
112
  }
113
113
  }
114
114
 
115
- if (nums <= symbol)
116
- {
117
- return "式が不正です";
115
+ if (nums <= symbol) return "式が不正です";
118
- }
119
116
 
120
117
  if(valid_number)
121
118
  {
@@ -193,6 +190,7 @@
193
190
  }
194
191
  }
195
192
  }
193
+
196
194
  stack.Add(list[i]);
197
195
  }
198
196
  else
@@ -214,7 +212,6 @@
214
212
  }
215
213
  }
216
214
  }
217
-
218
215
  return for_calc;
219
216
  }
220
217
 

9

修正

2017/06/28 16:46

投稿

退会済みユーザー
answer CHANGED
@@ -295,10 +295,11 @@
295
295
  }
296
296
  ```
297
297
  参考にしたサイト
298
- [逆ポーランド記法への変換2](http://www.gg.e-mansion.com/~kkatoh/program/novel2/novel208.html)
298
+ [【Ruby】【アルゴリズム】逆ポーランド記法](http://sekai.hateblo.jp/entry/2013/09/15/185314)
299
+ [逆ポーランド記法による計算式を計算する電卓](http://calc.exinfo.biz/)
299
300
  [逆ポーランド記法変換ツール](http://tamanegi-core.net/memo/article/polish_notation_converter)
300
- [【Ruby】【アルゴリズム】逆ポーランド記法](http://sekai.hateblo.jp/entry/2013/09/15/185314)
301
301
  [rubyについて質問です。](https://teratail.com/questions/80654)のkatoy様の回答
302
+ [逆ポーランド記法への変換2](http://www.gg.e-mansion.com/~kkatoh/program/novel2/novel208.html)
302
303
 
303
304
 
304
305
  素人ですが考えてみました。

8

修正

2017/06/28 16:44

投稿

退会済みユーザー
answer CHANGED
@@ -76,6 +76,7 @@
76
76
  if(nest.Last() != p_dic[e])
77
77
  {
78
78
  valid_nest = false;
79
+ break;
79
80
  }
80
81
 
81
82
  nest.RemoveAt(nest.Count - 1);

7

修正

2017/06/28 16:38

投稿

退会済みユーザー
answer CHANGED
@@ -35,7 +35,10 @@
35
35
 
36
36
  if(i == str.Length - 1)
37
37
  {
38
+ if(tmp.Count > 0)
39
+ {
38
- for_translate.Add(string.Join("", tmp));
40
+ for_translate.Add(string.Join("", tmp));
41
+ }
39
42
  }
40
43
  }
41
44
 
@@ -79,9 +82,13 @@
79
82
  }
80
83
  }
81
84
 
85
+ if (nest.Any()) valid_nest = false;
86
+
82
87
  if(valid_nest)
83
88
  {
84
89
  bool valid_number = true;
90
+ int nums = 0;
91
+ int symbol = 0;
85
92
 
86
93
  foreach(string e in list)
87
94
  {
@@ -91,18 +98,27 @@
91
98
 
92
99
  if (double.TryParse(e, out d))
93
100
  {
94
- //Do Nothing!
101
+ nums++;
95
102
  }
96
103
  else
97
104
  {
98
105
  valid_number = false;
99
106
  }
100
107
  }
108
+ else if(("+-*/%^".Contains(e)))
109
+ {
110
+ symbol++;
111
+ }
101
112
  }
102
113
 
114
+ if (nums <= symbol)
115
+ {
116
+ return "式が不正です";
117
+ }
118
+
103
119
  if(valid_number)
104
120
  {
105
- for (int i = 0; i < list.Count - 1; i++)
121
+ for (int i = 0; i < list.Count; i++)
106
122
  {
107
123
  if ("[{".Contains(list[i])) list[i] = "(";
108
124
  if ("]}".Contains(list[i])) list[i] = ")";
@@ -188,12 +204,11 @@
188
204
  {
189
205
  if (stack.Count > 0)
190
206
  {
207
+ stack.Reverse();
208
+
191
- while (stack[0] != "(")
209
+ foreach(string e in stack)
192
210
  {
193
- if(stack.Last() != "") for_calc.Add(stack.Last());
194
- stack.RemoveAt(stack.Count - 1);
195
-
196
- if (stack.Count < 1) break;
211
+ for_calc.Add(e);
197
212
  }
198
213
  }
199
214
  }

6

修正

2017/06/28 16:35

投稿

退会済みユーザー
answer CHANGED
@@ -279,10 +279,12 @@
279
279
  }
280
280
  ```
281
281
  参考にしたサイト
282
+ [逆ポーランド記法への変換2](http://www.gg.e-mansion.com/~kkatoh/program/novel2/novel208.html)
283
+ [逆ポーランド記法変換ツール](http://tamanegi-core.net/memo/article/polish_notation_converter)
282
284
  [【Ruby】【アルゴリズム】逆ポーランド記法](http://sekai.hateblo.jp/entry/2013/09/15/185314)
283
285
  [rubyについて質問です。](https://teratail.com/questions/80654)のkatoy様の回答
284
- [逆ポーランド記法への変換2](http://www.gg.e-mansion.com/~kkatoh/program/novel2/novel208.html)
285
286
 
287
+
286
288
  素人ですが考えてみました。
287
289
  このプログラムが正確に動くかどうかは分かりません。
288
290
  B型単純式についてはよくわからなかったので何もしていません。

5

修正

2017/06/28 15:58

投稿

退会済みユーザー
answer CHANGED
@@ -281,6 +281,7 @@
281
281
  参考にしたサイト
282
282
  [【Ruby】【アルゴリズム】逆ポーランド記法](http://sekai.hateblo.jp/entry/2013/09/15/185314)
283
283
  [rubyについて質問です。](https://teratail.com/questions/80654)のkatoy様の回答
284
+ [逆ポーランド記法への変換2](http://www.gg.e-mansion.com/~kkatoh/program/novel2/novel208.html)
284
285
 
285
286
  素人ですが考えてみました。
286
287
  このプログラムが正確に動くかどうかは分かりません。

4

修正

2017/06/28 15:57

投稿

退会済みユーザー
answer CHANGED
@@ -280,6 +280,7 @@
280
280
  ```
281
281
  参考にしたサイト
282
282
  [【Ruby】【アルゴリズム】逆ポーランド記法](http://sekai.hateblo.jp/entry/2013/09/15/185314)
283
+ [rubyについて質問です。](https://teratail.com/questions/80654)のkatoy様の回答
283
284
 
284
285
  素人ですが考えてみました。
285
286
  このプログラムが正確に動くかどうかは分かりません。

3

修正

2017/06/28 15:53

投稿

退会済みユーザー
answer CHANGED
@@ -3,36 +3,225 @@
3
3
  using System.Collections.Generic;
4
4
  using System.Linq;
5
5
 
6
- namespace polandtest1
6
+ namespace calc_porland
7
7
  {
8
8
  class Program
9
9
  {
10
+ //式分解
10
- static void Main(string[] args)
11
+ static List<string> split_nums(string str)
11
12
  {
13
+ str = str.Replace(" ", "");
14
+ str = str.Replace("**", "^");
12
15
 
16
+ List<string> for_translate = new List<string>();
13
- Console.Write("式を入力してください:");
17
+ List<string> tmp = new List<string>();
14
18
 
15
- string input = Console.ReadLine() ;
19
+ for (int i = 0; i < str.Length; i++)
20
+ {
21
+ if ("+-*/%^()[]{}".Contains(str[i]))
22
+ {
23
+ if (tmp.Count > 0)
24
+ {
25
+ for_translate.Add(string.Join("", tmp));
26
+ tmp.Clear();
27
+ }
16
28
 
17
- List<string> strs = input.Split(' ').ToList();
29
+ for_translate.Add(str[i].ToString());
30
+ }
31
+ else
32
+ {
33
+ tmp.Add(str[i].ToString());
34
+ }
18
35
 
36
+ if(i == str.Length - 1)
37
+ {
38
+ for_translate.Add(string.Join("", tmp));
39
+ }
40
+ }
41
+
42
+ return for_translate;
43
+ }
44
+
45
+ //かっこが正しいか数値が正しいかを判定
46
+ static Object check(List<string> list)
47
+ {
48
+ bool valid_nest = true;
49
+
50
+ List<string> nest = new List<string>();
51
+
52
+ Dictionary<string, string> p_dic = new Dictionary<string, string>()
53
+ {
54
+ { ")", "(" },
55
+ { "]", "[" },
56
+ { "}", "{" }
57
+ };
58
+
59
+ foreach(string e in list)
60
+ {
61
+ if ("([{".Contains(e))
62
+ {
63
+ nest.Add(e);
64
+ }
65
+ else if (")]}".Contains(e))
66
+ {
67
+ if(nest.Count < 1)
68
+ {
69
+ valid_nest = false;
70
+ break;
71
+ }
72
+
73
+ if(nest.Last() != p_dic[e])
74
+ {
75
+ valid_nest = false;
76
+ }
77
+
78
+ nest.RemoveAt(nest.Count - 1);
79
+ }
80
+ }
81
+
82
+ if(valid_nest)
83
+ {
84
+ bool valid_number = true;
85
+
86
+ foreach(string e in list)
87
+ {
88
+ if ("+-*/%^()[]{}".Contains(e) == false)
89
+ {
90
+ double d = 0;
91
+
92
+ if (double.TryParse(e, out d))
93
+ {
94
+ //Do Nothing!
95
+ }
96
+ else
97
+ {
98
+ valid_number = false;
99
+ }
100
+ }
101
+ }
102
+
103
+ if(valid_number)
104
+ {
105
+ for (int i = 0; i < list.Count - 1; i++)
106
+ {
107
+ if ("[{".Contains(list[i])) list[i] = "(";
108
+ if ("]}".Contains(list[i])) list[i] = ")";
109
+ }
110
+
111
+ return list;
112
+ }
113
+ else
114
+ {
115
+ return "数値が不正です";
116
+ }
117
+ }
118
+
119
+ return "かっこが不正です";
120
+ }
121
+
122
+ //逆ポーランド記法に変換
123
+ static List<string> translate(List<string> list)
124
+ {
125
+ List<string> stack = new List<string>();
126
+ List<string> for_calc = new List<string>();
127
+
128
+ for(int i = 0; i < list.Count; i++)
129
+ {
130
+ if(list[i] == "(")
131
+ {
132
+ stack.Add(list[i]);
133
+ }
134
+ else if(list[i] == ")")
135
+ {
136
+ while (stack.Last() != "(")
137
+ {
138
+ for_calc.Add(stack.Last());
139
+ stack.RemoveAt(stack.Count - 1);
140
+ }
141
+
142
+ stack.RemoveAt(stack.Count - 1);
143
+ }
144
+ else if ("+-*/%^".Contains(list[i]))
145
+ {
146
+ if(stack.Count > 0)
147
+ {
148
+ if ("+-".Contains(list[i]))
149
+ {
150
+ while ("*/%^".Contains(stack.Last()))
151
+ {
152
+ for_calc.Add(stack.Last());
153
+ stack.RemoveAt(stack.Count - 1);
154
+
155
+ if (stack.Count < 1) break;
156
+ }
157
+ }
158
+ else if (list[i] == "*")
159
+ {
160
+ while ("/%^".Contains(stack.Last()))
161
+ {
162
+ for_calc.Add(stack.Last());
163
+ stack.RemoveAt(stack.Count - 1);
164
+
165
+ if (stack.Count < 1) break;
166
+ }
167
+ }
168
+ else if ("/%".Contains(list[i]))
169
+ {
170
+ while (stack.Last() == "^")
171
+ {
172
+ for_calc.Add(stack.Last());
173
+ stack.RemoveAt(stack.Count - 1);
174
+
175
+ if (stack.Count < 1) break;
176
+ }
177
+ }
178
+ }
179
+ stack.Add(list[i]);
180
+ }
181
+ else
182
+ {
183
+ double d = 0;
184
+ if(double.TryParse(list[i], out d)) for_calc.Add(list[i]);
185
+ }
186
+
187
+ if(i == list.Count - 1)
188
+ {
189
+ if (stack.Count > 0)
190
+ {
191
+ while (stack[0] != "(")
192
+ {
193
+ if(stack.Last() != "") for_calc.Add(stack.Last());
194
+ stack.RemoveAt(stack.Count - 1);
195
+
196
+ if (stack.Count < 1) break;
197
+ }
198
+ }
199
+ }
200
+ }
201
+
202
+ return for_calc;
203
+ }
204
+
205
+ //逆ポーランド記法を解く
206
+ static double calc_porland(List<string> list)
207
+ {
19
208
  List<double> stack = new List<double>();
20
209
 
21
- foreach(string e in strs)
210
+ foreach(string e in list)
22
211
  {
23
- double i;
212
+ double d = 0;
24
213
 
25
- if(double.TryParse(e, out i))
214
+ if(double.TryParse(e, out d))
26
215
  {
27
- stack.Add(i);
216
+ stack.Add(d);
28
217
  }
29
218
  else
30
219
  {
31
- double acue = 0;
32
-
33
220
  double a = stack.Last(); stack.RemoveAt(stack.Count - 1);
34
221
  double b = stack.Last(); stack.RemoveAt(stack.Count - 1);
35
222
 
223
+ double acue = 0;
224
+
36
225
  switch (e)
37
226
  {
38
227
  case "+":
@@ -47,46 +236,51 @@
47
236
  case "/":
48
237
  acue = b / a;
49
238
  break;
239
+ case "%":
240
+ acue = b % a;
241
+ break;
242
+ case "^":
243
+ acue = Math.Pow(b, a);
244
+ break;
50
245
  }
51
246
 
52
247
  stack.Add(acue);
53
248
  }
54
249
  }
55
250
 
251
+ return stack[0];
252
+ }
253
+
254
+ //上のメソッドの結果を合体
255
+ static string porland(string str)
256
+ {
257
+ List<string> for_check = split_nums(str);
258
+
259
+ var result = check(for_check);
260
+
56
- foreach(double s in stack)
261
+ if (result is String)
57
262
  {
58
- Console.WriteLine(s.ToString());
263
+ return (string)result;
59
264
  }
265
+ else
266
+ {
267
+ return calc_porland(translate((List<string>)result)).ToString();
268
+ }
269
+ }
60
270
 
271
+ static void Main(string[] args)
272
+ {
273
+ string input = "[{(1+2)*(3+4)}+{(5+6)*(7+8)}]*[{(9+10)*(11+12)}+{(13+14)*(15+16)}]";
274
+
275
+ Console.WriteLine(porland(input));
61
276
  Console.ReadKey();
62
277
  }
63
278
  }
64
279
  }
65
280
  ```
66
- 入力値
67
- ```
68
- 1 2 + 3 4 + * 5 6 + 7 8 + * + 9 10 + 11 12 + * 13 14 + 15 16 + * + *
69
- ```
70
- 出力
71
- ```
72
- 236964
73
- ```
74
- 回答のチェックに使ったサイト
75
- [逆ポーランド記法変換ツール](http://tamanegi-core.net/memo/article/polish_notation_converter#introduction)
76
- [逆ポーランド記法による計算式を計算する電卓](http://calc.exinfo.biz/)
77
-
78
- 入力値
79
- ```
80
- 1,2+3,4+*5,6+7,8+*+9,10+11,12+*13,14+15,16+*+*
81
- ```
82
- 出力
83
- ```
84
- 236964
85
- ```
86
-
87
281
  参考にしたサイト
88
282
  [【Ruby】【アルゴリズム】逆ポーランド記法](http://sekai.hateblo.jp/entry/2013/09/15/185314)
89
283
 
90
284
  素人ですが考えてみました。
91
285
  このプログラムが正確に動くかどうかは分かりません。
92
- B型単純式についてはよくわからなかったので何もしていません。
286
+ B型単純式についてはよくわからなかったので何もしていません。

2

修正

2017/06/28 15:51

投稿

退会済みユーザー
answer CHANGED
@@ -72,6 +72,7 @@
72
72
  236964
73
73
  ```
74
74
  回答のチェックに使ったサイト
75
+ [逆ポーランド記法変換ツール](http://tamanegi-core.net/memo/article/polish_notation_converter#introduction)
75
76
  [逆ポーランド記法による計算式を計算する電卓](http://calc.exinfo.biz/)
76
77
 
77
78
  入力値

1

修正

2017/05/29 13:33

投稿

退会済みユーザー
answer CHANGED
@@ -26,7 +26,6 @@
26
26
  {
27
27
  stack.Add(i);
28
28
  }
29
-
30
29
  else
31
30
  {
32
31
  double acue = 0;