回答編集履歴

25

修正

2017/07/16 18:31

投稿

退会済みユーザー
test CHANGED
@@ -218,8 +218,6 @@
218
218
 
219
219
  {
220
220
 
221
- bool valid_number = true;
222
-
223
221
  int nums = 0;
224
222
 
225
223
  int symbol = 0;
@@ -246,49 +244,145 @@
246
244
 
247
245
  }
248
246
 
247
+ }
248
+
249
+ else if (("+-*/%^".Contains(e)))
250
+
251
+ {
252
+
253
+ symbol++;
254
+
255
+ }
256
+
257
+ }
258
+
259
+
260
+
261
+ if (nums <= symbol) return "Invalid expression";
262
+
263
+
264
+
265
+ for (int i = 0; i < list.Count; i++)
266
+
267
+ {
268
+
269
+ if ("[{".Contains(list[i])) list[i] = "(";
270
+
271
+ if ("]}".Contains(list[i])) list[i] = ")";
272
+
273
+ }
274
+
275
+
276
+
249
- else
277
+ return list;
278
+
279
+ }
280
+
281
+
282
+
283
+ return "Missing end bracket";
284
+
285
+ }
286
+
287
+
288
+
289
+ //逆ポーランド記法に変換
290
+
291
+ static List<string> Translate(List<string> list)
292
+
293
+ {
294
+
295
+ List<string> stack = new List<string>();
296
+
297
+ List<string> for_calc = new List<string>();
298
+
299
+
300
+
301
+ for (int i = 0; i < list.Count; i++)
302
+
303
+ {
304
+
305
+ if (list[i] == "(")
306
+
307
+ {
308
+
309
+ stack.Add(list[i]);
310
+
311
+ }
312
+
313
+ else if (list[i] == ")")
314
+
315
+ {
316
+
317
+ while (stack.Last() != "(")
318
+
319
+ {
320
+
321
+ for_calc.Add(stack.Last());
322
+
323
+ stack.RemoveAt(stack.Count - 1);
324
+
325
+ }
326
+
327
+
328
+
329
+ stack.RemoveAt(stack.Count - 1);
330
+
331
+ }
332
+
333
+ else if ("+-*/%^".Contains(list[i]))
334
+
335
+ {
336
+
337
+ if (stack.Count > 0)
338
+
339
+ {
340
+
341
+ if ("+-".Contains(list[i]))
250
342
 
251
343
  {
252
344
 
345
+ while ("+-*/%^".Contains(stack.Last()))
346
+
347
+ {
348
+
253
- valid_number = false;
349
+ for_calc.Add(stack.Last());
350
+
351
+ stack.RemoveAt(stack.Count - 1);
352
+
353
+
354
+
355
+ if (stack.Count < 1) break;
356
+
357
+ }
254
358
 
255
359
  }
256
360
 
361
+ else if ("*/%".Contains(list[i]))
362
+
363
+ {
364
+
365
+ while ("*/%^".Contains(stack.Last()))
366
+
367
+ {
368
+
369
+ for_calc.Add(stack.Last());
370
+
371
+ stack.RemoveAt(stack.Count - 1);
372
+
373
+
374
+
375
+ if (stack.Count < 1) break;
376
+
257
- }
377
+ }
258
-
259
- else if (("+-*/%^".Contains(e)))
378
+
260
-
261
- {
262
-
263
- symbol++;
264
-
265
- }
379
+ }
266
-
380
+
267
- }
381
+ }
268
-
269
-
270
-
271
- if (nums <= symbol) return "Invalid expression";
382
+
272
-
273
-
274
-
275
- if (valid_number)
383
+
276
-
277
- {
384
+
278
-
279
- for (int i = 0; i < list.Count; i++)
280
-
281
- {
282
-
283
- if ("[{".Contains(list[i])) list[i] = "(";
284
-
285
- if ("]}".Contains(list[i])) list[i] = ")";
286
-
287
- }
288
-
289
-
290
-
291
- return list;
385
+ stack.Add(list[i]);
292
386
 
293
387
  }
294
388
 
@@ -296,300 +390,182 @@
296
390
 
297
391
  {
298
392
 
299
- return "Invalid number";
393
+ for_calc.Add(list[i]);
300
-
394
+
301
- }
395
+ }
396
+
397
+
398
+
302
-
399
+ if (i == list.Count - 1)
400
+
401
+ {
402
+
403
+ if (stack.Count > 0)
404
+
405
+ {
406
+
407
+ stack.Reverse();
408
+
409
+
410
+
411
+ foreach (string e in stack)
412
+
413
+ {
414
+
415
+ for_calc.Add(e);
416
+
303
- }
417
+ }
418
+
304
-
419
+ }
420
+
305
-
421
+ }
422
+
306
-
423
+ }
424
+
425
+
426
+
307
- return "Missing end bracket";
427
+ return for_calc;
308
428
 
309
429
  }
310
430
 
311
431
 
312
432
 
313
- //逆ポーランド記法に変換
433
+ //逆ポーランド記法を解く
314
-
434
+
315
- static List<string> Translate(List<string> list)
435
+ static double Calc_Porland(List<string> list)
316
436
 
317
437
  {
318
438
 
319
439
  List<string> stack = new List<string>();
320
440
 
321
- List<string> for_calc = new List<string>();
322
-
323
-
324
-
325
- for (int i = 0; i < list.Count; i++)
326
-
327
- {
328
-
329
- if (list[i] == "(")
330
-
331
- {
332
-
333
- stack.Add(list[i]);
334
-
335
- }
336
-
337
- else if (list[i] == ")")
338
-
339
- {
340
-
341
- while (stack.Last() != "(")
342
-
343
- {
344
-
345
- for_calc.Add(stack.Last());
346
-
347
- stack.RemoveAt(stack.Count - 1);
348
-
349
- }
350
-
351
-
352
-
353
- stack.RemoveAt(stack.Count - 1);
354
-
355
- }
356
-
357
- else if ("+-*/%^".Contains(list[i]))
358
-
359
- {
360
-
361
- if (stack.Count > 0)
362
-
363
- {
364
-
365
- if ("+-".Contains(list[i]))
366
-
367
- {
368
-
369
- while ("+-*/%^".Contains(stack.Last()))
370
-
371
- {
372
-
373
- for_calc.Add(stack.Last());
374
-
375
- stack.RemoveAt(stack.Count - 1);
376
-
377
-
378
-
379
- if (stack.Count < 1) break;
380
-
381
- }
382
-
383
- }
384
-
385
- else if ("*/%".Contains(list[i]))
386
-
387
- {
388
-
389
- while ("*/%^".Contains(stack.Last()))
390
-
391
- {
392
-
393
- for_calc.Add(stack.Last());
394
-
395
- stack.RemoveAt(stack.Count - 1);
396
-
397
-
398
-
399
- if (stack.Count < 1) break;
400
-
401
- }
402
-
403
- }
404
-
405
- }
406
-
407
-
408
-
409
- stack.Add(list[i]);
410
-
411
- }
412
-
413
- else
414
-
415
- {
416
-
417
- for_calc.Add(list[i]);
418
-
419
- }
420
-
421
-
422
-
423
- if (i == list.Count - 1)
424
-
425
- {
426
-
427
- if (stack.Count > 0)
428
-
429
- {
430
-
431
- stack.Reverse();
432
-
433
-
434
-
435
- foreach (string e in stack)
436
-
437
- {
438
-
439
- for_calc.Add(e);
440
-
441
- }
442
-
443
- }
444
-
445
- }
446
-
447
- }
448
-
449
-
450
-
451
- return for_calc;
441
+
442
+
443
+ while (list.Any())
444
+
445
+ {
446
+
447
+ stack.Add(list[0]); list.RemoveAt(0);
448
+
449
+
450
+
451
+ if ("+-*/%^".Contains(stack.Last()))
452
+
453
+ {
454
+
455
+ List<string> tmp = stack.GetRange(stack.Count() - 3, 3);
456
+
457
+
458
+
459
+ stack.RemoveRange(stack.Count() - 3, 3);
460
+
461
+
462
+
463
+ double a = double.Parse(tmp[0]);
464
+
465
+ double b = double.Parse(tmp[1]);
466
+
467
+
468
+
469
+ double d = 0.0D;
470
+
471
+
472
+
473
+ switch (tmp[2])
474
+
475
+ {
476
+
477
+ case "+":
478
+
479
+ d = a + b;
480
+
481
+ break;
482
+
483
+ case "-":
484
+
485
+ d = a - b;
486
+
487
+ break;
488
+
489
+ case "*":
490
+
491
+ d = a * b;
492
+
493
+ break;
494
+
495
+ case "/":
496
+
497
+ d = a / b;
498
+
499
+ break;
500
+
501
+ case "%":
502
+
503
+ d = a % b;
504
+
505
+ break;
506
+
507
+ case "^":
508
+
509
+ d = Math.Pow(a, b);
510
+
511
+ break;
512
+
513
+ }
514
+
515
+
516
+
517
+ stack.Add(d.ToString());
518
+
519
+ }
520
+
521
+ }
522
+
523
+
524
+
525
+ return double.Parse(stack[0]);
452
526
 
453
527
  }
454
528
 
455
529
 
456
530
 
457
- //逆ポーランド記法を解く
458
-
459
- static double Calc_Porland(List<string> list)
531
+ static string Calculate(string str)
460
532
 
461
533
  {
462
534
 
535
+ var nums = Split_Nums(str);
536
+
537
+
538
+
539
+ var result = Check(nums);
540
+
541
+
542
+
543
+ if (result is String) return (string)result;
544
+
545
+
546
+
463
- List<string> stack = new List<string>();
547
+ double ans = Calc_Porland(Translate((List<string>)result));
464
-
465
-
466
-
467
- while (list.Any())
548
+
468
-
469
- {
549
+
470
-
471
- stack.Add(list[0]); list.RemoveAt(0);
550
+
472
-
473
-
474
-
475
- if ("+-*/%^".Contains(stack.Last()))
476
-
477
- {
478
-
479
- List<string> tmp = stack.GetRange(stack.Count() - 3, 3);
480
-
481
-
482
-
483
- stack.RemoveRange(stack.Count() - 3, 3);
484
-
485
-
486
-
487
- double a = double.Parse(tmp[0]);
488
-
489
- double b = double.Parse(tmp[1]);
490
-
491
-
492
-
493
- double d = 0.0D;
494
-
495
-
496
-
497
- switch (tmp[2])
498
-
499
- {
500
-
501
- case "+":
502
-
503
- d = a + b;
504
-
505
- break;
506
-
507
- case "-":
508
-
509
- d = a - b;
510
-
511
- break;
512
-
513
- case "*":
514
-
515
- d = a * b;
516
-
517
- break;
518
-
519
- case "/":
520
-
521
- d = a / b;
522
-
523
- break;
524
-
525
- case "%":
526
-
527
- d = a % b;
528
-
529
- break;
530
-
531
- case "^":
532
-
533
- d = Math.Pow(a, b);
534
-
535
- break;
536
-
537
- }
538
-
539
-
540
-
541
- stack.Add(d.ToString());
551
+ return ans.ToString();
542
-
543
- }
544
-
545
- }
546
-
547
-
548
-
549
- return double.Parse(stack[0]);
550
552
 
551
553
  }
552
554
 
553
555
 
554
556
 
555
- static string Calculate(string str)
557
+ static void Main(string[] args)
556
558
 
557
559
  {
558
560
 
559
- var nums = Split_Nums(str);
561
+ string input = "[{(1+2)*(3+4)}+{(5+6)*(7+8)}]*[{(9+10)*(11+12)}+{(13+14)*(15+16)}]*(-1)+0.1";
560
-
561
-
562
-
562
+
563
- var result = Check(nums);
563
+ Console.WriteLine(Calculate(input));
564
-
565
-
566
-
567
- if (result is String) return (string)result;
564
+
568
-
569
-
570
-
571
- double ans = Calc_Porland(Translate((List<string>)result));
572
-
573
-
574
-
575
- return ans.ToString();
565
+ Console.ReadKey();
576
566
 
577
567
  }
578
568
 
579
-
580
-
581
- static void Main(string[] args)
582
-
583
- {
584
-
585
- string input = "[{(1+2)*(3+4)}+{(5+6)*(7+8)}]*[{(9+10)*(11+12)}+{(13+14)*(15+16)}]*(-1)+0.1";
586
-
587
- Console.WriteLine(Calculate(input));
588
-
589
- Console.ReadKey();
590
-
591
- }
592
-
593
569
  }
594
570
 
595
571
  ```

24

修正

2017/07/16 18:31

投稿

退会済みユーザー
test CHANGED
@@ -14,7 +14,7 @@
14
14
 
15
15
  {
16
16
 
17
- static Object Split_Nums(string str)
17
+ static List<string> Split_Nums(string str)
18
18
 
19
19
  {
20
20
 
@@ -560,11 +560,7 @@
560
560
 
561
561
 
562
562
 
563
- if (nums is String) return (string)nums;
564
-
565
-
566
-
567
- var result = Check((List<string>)nums);
563
+ var result = Check(nums);
568
564
 
569
565
 
570
566
 

23

修正

2017/07/16 18:06

投稿

退会済みユーザー
test CHANGED
@@ -4,547 +4,595 @@
4
4
 
5
5
  using System.Collections.Generic;
6
6
 
7
- using System.Text.RegularExpressions;
8
-
9
7
  using System.Linq;
10
8
 
11
- namespace calc_porland
9
+ using System.Text.RegularExpressions;
10
+
11
+
12
+
13
+ class Program
12
14
 
13
15
  {
14
16
 
15
- class Program
16
-
17
- {
18
-
19
- //式分解
20
-
21
- static List<string> split_nums(string str)
22
-
23
- {
24
-
25
- str = str.Replace(" ", "");
26
-
27
- str = str.Replace("**", "^");
28
-
29
- str = str.Replace("f", "");
30
-
31
-
32
-
33
- Regex re = new Regex(@"\((?<nega>-\d+?(|.\d+?))\)");
34
-
35
-
36
-
37
- List<string> negative = new List<string>();
38
-
39
-
40
-
41
- MatchCollection mc = re.Matches(str);
42
-
43
-
44
-
45
- foreach(Match m in mc)
46
-
47
- {
48
-
49
- negative.Add(m.Groups["nega"].Value);
50
-
51
- }
52
-
53
-
54
-
55
- str = re.Replace(str, "f");
56
-
57
- List<string> for_translate = new List<string>();
58
-
59
- List<string> tmp = new List<string>();
60
-
61
- for (int i = 0; i < str.Length; i++)
62
-
63
- {
64
-
65
- if ("+-*/%^()[]{}".Contains(str[i]))
66
-
67
- {
68
-
69
- if (tmp.Count > 0)
70
-
71
- {
72
-
73
- for_translate.Add(string.Join("", tmp));
74
-
75
- tmp.Clear();
76
-
77
- }
78
-
79
- for_translate.Add(str[i].ToString());
80
-
81
- }
82
-
83
- else
84
-
85
- {
86
-
87
- tmp.Add(str[i].ToString());
88
-
89
- }
90
-
91
- if(i == str.Length - 1)
92
-
93
- {
94
-
95
- if(tmp.Count > 0)
96
-
97
- {
98
-
99
- for_translate.Add(string.Join("", tmp));
100
-
101
- }
102
-
103
- }
104
-
105
- }
106
-
107
- for(int i =0;i < for_translate.Count; i++)
108
-
109
- {
110
-
111
- if (for_translate[i] == "f")
112
-
113
- {
114
-
115
- for_translate[i] = negative[0];
116
-
117
- negative.RemoveAt(0);
118
-
119
- }
120
-
121
- }
122
-
123
-
124
-
125
- return for_translate;
126
-
127
- }
128
-
129
- //かっこが正しいか数値が正しいかを判定
130
-
131
- static Object check(List<string> list)
132
-
133
- {
134
-
135
- bool valid_nest = true;
136
-
137
- List<string> nest = new List<string>();
138
-
139
- Dictionary<string, string> p_dic = new Dictionary<string, string>()
140
-
141
- {
142
-
143
- { ")", "(" },
144
-
145
- { "]", "[" },
146
-
147
- { "}", "{" }
148
-
149
- };
150
-
151
- foreach(string e in list)
152
-
153
- {
154
-
155
- if ("([{".Contains(e))
156
-
157
- {
158
-
159
- nest.Add(e);
160
-
161
- }
162
-
163
- else if (")]}".Contains(e))
164
-
165
- {
166
-
167
- if(nest.Count < 1)
168
-
169
- {
170
-
171
- valid_nest = false;
172
-
173
- break;
174
-
175
- }
176
-
177
- if(nest.Last() != p_dic[e])
178
-
179
- {
180
-
181
- valid_nest = false;
182
-
183
- break;
184
-
185
- }
186
-
187
- nest.RemoveAt(nest.Count - 1);
188
-
189
- }
190
-
191
- }
192
-
193
- if (nest.Any()) valid_nest = false;
194
-
195
- if(valid_nest)
196
-
197
- {
198
-
199
- bool valid_number = true;
200
-
201
- int nums = 0;
202
-
203
- int symbol = 0;
204
-
205
- foreach(string e in list)
206
-
207
- {
208
-
209
- if ("+-*/%^()[]{}".Contains(e) == false)
210
-
211
- {
212
-
213
- double d = 0;
214
-
215
- if (double.TryParse(e, out d))
216
-
217
- {
218
-
219
- nums++;
220
-
221
- }
222
-
223
- else
224
-
225
- {
226
-
227
- valid_number = false;
228
-
229
- }
230
-
231
- }
232
-
233
- else if(("+-*/%^".Contains(e)))
234
-
235
- {
236
-
237
- symbol++;
238
-
239
- }
240
-
241
- }
242
-
243
- if (nums <= symbol) return "式が不正です";
244
-
245
- if(valid_number)
246
-
247
- {
248
-
249
- for (int i = 0; i < list.Count; i++)
250
-
251
- {
252
-
253
- if ("[{".Contains(list[i])) list[i] = "(";
254
-
255
- if ("]}".Contains(list[i])) list[i] = ")";
256
-
257
- }
258
-
259
- return list;
260
-
261
- }
262
-
263
- else
264
-
265
- {
266
-
267
- return "数値が不正です";
268
-
269
- }
270
-
271
- }
272
-
273
- return "かっこが不正です";
274
-
275
- }
276
-
277
- //逆ポーランド記法に変換
278
-
279
- static List<string> translate(List<string> list)
280
-
281
- {
282
-
283
- List<string> stack = new List<string>();
284
-
285
- List<string> for_calc = new List<string>();
286
-
287
- for(int i = 0; i < list.Count; i++)
288
-
289
- {
290
-
291
- if(list[i] == "(")
292
-
293
- {
294
-
295
- stack.Add(list[i]);
296
-
297
- }
298
-
299
- else if(list[i] == ")")
300
-
301
- {
302
-
303
- while (stack.Last() != "(")
304
-
305
- {
306
-
307
- for_calc.Add(stack.Last());
308
-
309
- stack.RemoveAt(stack.Count - 1);
310
-
311
- }
312
-
313
- stack.RemoveAt(stack.Count - 1);
314
-
315
- }
316
-
317
- else if ("+-*/%^".Contains(list[i]))
318
-
319
- {
320
-
321
- if(stack.Count > 0)
322
-
323
- {
324
-
325
- if ("+-".Contains(list[i]))
326
-
327
- {
328
-
329
- while ("*/%^".Contains(stack.Last()))
330
-
331
- {
332
-
333
- for_calc.Add(stack.Last());
334
-
335
- stack.RemoveAt(stack.Count - 1);
336
-
337
- if (stack.Count < 1) break;
338
-
339
- }
340
-
341
- }
342
-
343
- else if (list[i] == "*")
344
-
345
- {
346
-
347
- while ("/%^".Contains(stack.Last()))
348
-
349
- {
350
-
351
- for_calc.Add(stack.Last());
352
-
353
- stack.RemoveAt(stack.Count - 1);
354
-
355
- if (stack.Count < 1) break;
356
-
357
- }
358
-
359
- }
360
-
361
- else if ("/%".Contains(list[i]))
362
-
363
- {
364
-
365
- while (stack.Last() == "^")
366
-
367
- {
368
-
369
- for_calc.Add(stack.Last());
370
-
371
- stack.RemoveAt(stack.Count - 1);
372
-
373
- if (stack.Count < 1) break;
374
-
375
- }
376
-
377
- }
378
-
379
- }
380
-
381
- stack.Add(list[i]);
382
-
383
- }
384
-
385
- else
386
-
387
- {
388
-
389
- for_calc.Add(list[i]);
390
-
391
- }
392
-
393
- if(i == list.Count - 1)
394
-
395
- {
396
-
397
- if (stack.Count > 0)
398
-
399
- {
400
-
401
- stack.Reverse();
402
-
403
- foreach(string e in stack)
404
-
405
- {
406
-
407
- for_calc.Add(e);
408
-
409
- }
410
-
411
- }
412
-
413
- }
414
-
415
- }
416
-
417
- return for_calc;
418
-
419
- }
420
-
421
- //逆ポーランド記法を解く
422
-
423
- static double calc_porland(List<string> list)
424
-
425
- {
426
-
427
- List<double> stack = new List<double>();
428
-
429
- foreach(string e in list)
430
-
431
- {
432
-
433
- double d = 0;
434
-
435
- if(double.TryParse(e, out d))
436
-
437
- {
438
-
439
- stack.Add(d);
440
-
441
- }
442
-
443
- else
444
-
445
- {
446
-
447
- double a = stack.Last(); stack.RemoveAt(stack.Count - 1);
448
-
449
- double b = stack.Last(); stack.RemoveAt(stack.Count - 1);
450
-
451
- double acue = 0;
452
-
453
- switch (e)
454
-
455
- {
456
-
457
- case "+":
458
-
459
- acue = b + a;
460
-
461
- break;
462
-
463
- case "-":
464
-
465
- acue = b - a;
466
-
467
- break;
468
-
469
- case "*":
470
-
471
- acue = b * a;
472
-
473
- break;
474
-
475
- case "/":
476
-
477
- acue = b / a;
478
-
479
- break;
480
-
481
- case "%":
482
-
483
- acue = b % a;
484
-
485
- break;
486
-
487
- case "^":
488
-
489
- acue = Math.Pow(b, a);
490
-
491
- break;
492
-
493
- }
494
-
495
- stack.Add(acue);
496
-
497
- }
498
-
499
- }
500
-
501
- return stack[0];
502
-
503
- }
504
-
505
- //上のメソッドの結果を合体
506
-
507
- static string porland(string str)
508
-
509
- {
510
-
511
- List<string> for_check = split_nums(str);
512
-
513
- var result = check(for_check);
514
-
515
- if (result is String)
516
-
517
- {
518
-
519
- return (string)result;
520
-
521
- }
522
-
523
- else
524
-
525
- {
526
-
527
- return calc_porland(translate((List<string>)result)).ToString();
528
-
529
- }
530
-
531
- }
532
-
533
- static void Main(string[] args)
534
-
535
- {
536
-
537
- //string input = "[{(1+2)*(3+4)}+{(5+6)*(7+8)}]*[{(9+10)*(11+12)}+{(13+14)*(15+16)}]";
538
-
539
- string input = "[{(1+2)*(3+4)}+{(5+6)*(7+8)}]*[{(9+10)*(11+12)}+{(13+14)*(15+16)}]*(-1)+0.1";
540
-
541
- Console.WriteLine(porland(input));
542
-
543
- Console.ReadKey();
544
-
545
- }
546
-
547
- }
17
+ static Object Split_Nums(string str)
18
+
19
+ {
20
+
21
+ str = str.Replace(" ", "");
22
+
23
+ str = str.Replace("**", "^");
24
+
25
+ str = str.Replace(",", "");
26
+
27
+
28
+
29
+ Regex re = new Regex(@"\((?<nega>-\d+?(|.\d+?))\)");
30
+
31
+
32
+
33
+ List<string> negative = new List<string>();
34
+
35
+
36
+
37
+ MatchCollection mc = re.Matches(str);
38
+
39
+
40
+
41
+ foreach (Match m in mc)
42
+
43
+ {
44
+
45
+ negative.Add(m.Groups["nega"].Value);
46
+
47
+ }
48
+
49
+
50
+
51
+ str = re.Replace(str, "f");
52
+
53
+
54
+
55
+ List<string> for_translate = new List<string>();
56
+
57
+ List<string> tmp = new List<string>();
58
+
59
+
60
+
61
+ for (int i = 0; i < str.Length; i++)
62
+
63
+ {
64
+
65
+ if ("+-*/%^()[]{}".Contains(str[i]))
66
+
67
+ {
68
+
69
+ if (tmp.Count > 0)
70
+
71
+ {
72
+
73
+ for_translate.Add(string.Join("", tmp));
74
+
75
+ tmp.Clear();
76
+
77
+ }
78
+
79
+
80
+
81
+ for_translate.Add(str[i].ToString());
82
+
83
+ }
84
+
85
+ else
86
+
87
+ {
88
+
89
+ tmp.Add(str[i].ToString());
90
+
91
+ }
92
+
93
+
94
+
95
+ if (i == str.Length - 1)
96
+
97
+ {
98
+
99
+ if (tmp.Count > 0)
100
+
101
+ {
102
+
103
+ for_translate.Add(string.Join("", tmp));
104
+
105
+ }
106
+
107
+ }
108
+
109
+ }
110
+
111
+
112
+
113
+ for (int i = 0; i < for_translate.Count; i++)
114
+
115
+ {
116
+
117
+ if (for_translate[i] == "f")
118
+
119
+ {
120
+
121
+ for_translate[i] = negative[0];
122
+
123
+ negative.RemoveAt(0);
124
+
125
+ }
126
+
127
+ }
128
+
129
+
130
+
131
+ return for_translate;
132
+
133
+ }
134
+
135
+
136
+
137
+ //かっこが正しいか数値が正しいかを判定
138
+
139
+ static Object Check(List<string> list)
140
+
141
+ {
142
+
143
+ bool valid_nest = true;
144
+
145
+
146
+
147
+ List<string> nest = new List<string>();
148
+
149
+
150
+
151
+ Dictionary<string, string> p_dic = new Dictionary<string, string>()
152
+
153
+ {
154
+
155
+ { ")", "(" },
156
+
157
+ { "]", "[" },
158
+
159
+ { "}", "{" }
160
+
161
+ };
162
+
163
+
164
+
165
+ foreach (string e in list)
166
+
167
+ {
168
+
169
+ if ("([{".Contains(e))
170
+
171
+ {
172
+
173
+ nest.Add(e);
174
+
175
+ }
176
+
177
+ else if (")]}".Contains(e))
178
+
179
+ {
180
+
181
+ if (nest.Count < 1)
182
+
183
+ {
184
+
185
+ valid_nest = false;
186
+
187
+ break;
188
+
189
+ }
190
+
191
+
192
+
193
+ if (nest.Last() != p_dic[e])
194
+
195
+ {
196
+
197
+ valid_nest = false;
198
+
199
+ break;
200
+
201
+ }
202
+
203
+
204
+
205
+ nest.RemoveAt(nest.Count - 1);
206
+
207
+ }
208
+
209
+ }
210
+
211
+
212
+
213
+ if (nest.Any()) valid_nest = false;
214
+
215
+
216
+
217
+ if (valid_nest)
218
+
219
+ {
220
+
221
+ bool valid_number = true;
222
+
223
+ int nums = 0;
224
+
225
+ int symbol = 0;
226
+
227
+
228
+
229
+ foreach (string e in list)
230
+
231
+ {
232
+
233
+ if ("+-*/%^()[]{}".Contains(e) == false)
234
+
235
+ {
236
+
237
+ double d = 0;
238
+
239
+
240
+
241
+ if (double.TryParse(e, out d))
242
+
243
+ {
244
+
245
+ nums++;
246
+
247
+ }
248
+
249
+ else
250
+
251
+ {
252
+
253
+ valid_number = false;
254
+
255
+ }
256
+
257
+ }
258
+
259
+ else if (("+-*/%^".Contains(e)))
260
+
261
+ {
262
+
263
+ symbol++;
264
+
265
+ }
266
+
267
+ }
268
+
269
+
270
+
271
+ if (nums <= symbol) return "Invalid expression";
272
+
273
+
274
+
275
+ if (valid_number)
276
+
277
+ {
278
+
279
+ for (int i = 0; i < list.Count; i++)
280
+
281
+ {
282
+
283
+ if ("[{".Contains(list[i])) list[i] = "(";
284
+
285
+ if ("]}".Contains(list[i])) list[i] = ")";
286
+
287
+ }
288
+
289
+
290
+
291
+ return list;
292
+
293
+ }
294
+
295
+ else
296
+
297
+ {
298
+
299
+ return "Invalid number";
300
+
301
+ }
302
+
303
+ }
304
+
305
+
306
+
307
+ return "Missing end bracket";
308
+
309
+ }
310
+
311
+
312
+
313
+ //逆ポーランド記法に変換
314
+
315
+ static List<string> Translate(List<string> list)
316
+
317
+ {
318
+
319
+ List<string> stack = new List<string>();
320
+
321
+ List<string> for_calc = new List<string>();
322
+
323
+
324
+
325
+ for (int i = 0; i < list.Count; i++)
326
+
327
+ {
328
+
329
+ if (list[i] == "(")
330
+
331
+ {
332
+
333
+ stack.Add(list[i]);
334
+
335
+ }
336
+
337
+ else if (list[i] == ")")
338
+
339
+ {
340
+
341
+ while (stack.Last() != "(")
342
+
343
+ {
344
+
345
+ for_calc.Add(stack.Last());
346
+
347
+ stack.RemoveAt(stack.Count - 1);
348
+
349
+ }
350
+
351
+
352
+
353
+ stack.RemoveAt(stack.Count - 1);
354
+
355
+ }
356
+
357
+ else if ("+-*/%^".Contains(list[i]))
358
+
359
+ {
360
+
361
+ if (stack.Count > 0)
362
+
363
+ {
364
+
365
+ if ("+-".Contains(list[i]))
366
+
367
+ {
368
+
369
+ while ("+-*/%^".Contains(stack.Last()))
370
+
371
+ {
372
+
373
+ for_calc.Add(stack.Last());
374
+
375
+ stack.RemoveAt(stack.Count - 1);
376
+
377
+
378
+
379
+ if (stack.Count < 1) break;
380
+
381
+ }
382
+
383
+ }
384
+
385
+ else if ("*/%".Contains(list[i]))
386
+
387
+ {
388
+
389
+ while ("*/%^".Contains(stack.Last()))
390
+
391
+ {
392
+
393
+ for_calc.Add(stack.Last());
394
+
395
+ stack.RemoveAt(stack.Count - 1);
396
+
397
+
398
+
399
+ if (stack.Count < 1) break;
400
+
401
+ }
402
+
403
+ }
404
+
405
+ }
406
+
407
+
408
+
409
+ stack.Add(list[i]);
410
+
411
+ }
412
+
413
+ else
414
+
415
+ {
416
+
417
+ for_calc.Add(list[i]);
418
+
419
+ }
420
+
421
+
422
+
423
+ if (i == list.Count - 1)
424
+
425
+ {
426
+
427
+ if (stack.Count > 0)
428
+
429
+ {
430
+
431
+ stack.Reverse();
432
+
433
+
434
+
435
+ foreach (string e in stack)
436
+
437
+ {
438
+
439
+ for_calc.Add(e);
440
+
441
+ }
442
+
443
+ }
444
+
445
+ }
446
+
447
+ }
448
+
449
+
450
+
451
+ return for_calc;
452
+
453
+ }
454
+
455
+
456
+
457
+ //逆ポーランド記法を解く
458
+
459
+ static double Calc_Porland(List<string> list)
460
+
461
+ {
462
+
463
+ List<string> stack = new List<string>();
464
+
465
+
466
+
467
+ while (list.Any())
468
+
469
+ {
470
+
471
+ stack.Add(list[0]); list.RemoveAt(0);
472
+
473
+
474
+
475
+ if ("+-*/%^".Contains(stack.Last()))
476
+
477
+ {
478
+
479
+ List<string> tmp = stack.GetRange(stack.Count() - 3, 3);
480
+
481
+
482
+
483
+ stack.RemoveRange(stack.Count() - 3, 3);
484
+
485
+
486
+
487
+ double a = double.Parse(tmp[0]);
488
+
489
+ double b = double.Parse(tmp[1]);
490
+
491
+
492
+
493
+ double d = 0.0D;
494
+
495
+
496
+
497
+ switch (tmp[2])
498
+
499
+ {
500
+
501
+ case "+":
502
+
503
+ d = a + b;
504
+
505
+ break;
506
+
507
+ case "-":
508
+
509
+ d = a - b;
510
+
511
+ break;
512
+
513
+ case "*":
514
+
515
+ d = a * b;
516
+
517
+ break;
518
+
519
+ case "/":
520
+
521
+ d = a / b;
522
+
523
+ break;
524
+
525
+ case "%":
526
+
527
+ d = a % b;
528
+
529
+ break;
530
+
531
+ case "^":
532
+
533
+ d = Math.Pow(a, b);
534
+
535
+ break;
536
+
537
+ }
538
+
539
+
540
+
541
+ stack.Add(d.ToString());
542
+
543
+ }
544
+
545
+ }
546
+
547
+
548
+
549
+ return double.Parse(stack[0]);
550
+
551
+ }
552
+
553
+
554
+
555
+ static string Calculate(string str)
556
+
557
+ {
558
+
559
+ var nums = Split_Nums(str);
560
+
561
+
562
+
563
+ if (nums is String) return (string)nums;
564
+
565
+
566
+
567
+ var result = Check((List<string>)nums);
568
+
569
+
570
+
571
+ if (result is String) return (string)result;
572
+
573
+
574
+
575
+ double ans = Calc_Porland(Translate((List<string>)result));
576
+
577
+
578
+
579
+ return ans.ToString();
580
+
581
+ }
582
+
583
+
584
+
585
+ static void Main(string[] args)
586
+
587
+ {
588
+
589
+ string input = "[{(1+2)*(3+4)}+{(5+6)*(7+8)}]*[{(9+10)*(11+12)}+{(13+14)*(15+16)}]*(-1)+0.1";
590
+
591
+ Console.WriteLine(Calculate(input));
592
+
593
+ Console.ReadKey();
594
+
595
+ }
548
596
 
549
597
  }
550
598
 

22

テスト3

2017/07/16 17:56

投稿

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

21

テスト2

2017/07/11 12:48

投稿

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

20

テスト

2017/07/11 12:47

投稿

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

19

修正

2017/07/11 12:45

投稿

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

18

修正

2017/07/07 12:14

投稿

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

17

修正

2017/07/06 00:18

投稿

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

16

修正

2017/07/06 00:15

投稿

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

15

修正

2017/06/29 03:07

投稿

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

14

変更が反映されない?

2017/06/29 03:05

投稿

退会済みユーザー
test CHANGED
@@ -4,587 +4,547 @@
4
4
 
5
5
  using System.Collections.Generic;
6
6
 
7
+ using System.Text.RegularExpressions;
8
+
7
9
  using System.Linq;
8
10
 
9
-
10
-
11
11
  namespace calc_porland
12
12
 
13
13
  {
14
14
 
15
- class Program
16
-
17
- {
18
-
19
- //式分解
20
-
21
- static List<string> split_nums(string str)
22
-
23
- {
24
-
25
- str = str.Replace(" ", "");
26
-
27
- str = str.Replace("**", "^");
28
-
29
-
30
-
31
- List<string> for_translate = new List<string>();
32
-
33
- List<string> tmp = new List<string>();
34
-
35
-
36
-
37
- for (int i = 0; i < str.Length; i++)
38
-
39
- {
40
-
41
- if ("+-*/%^()[]{}".Contains(str[i]))
42
-
43
- {
44
-
45
- if (tmp.Count > 0)
46
-
47
- {
48
-
49
- for_translate.Add(string.Join("", tmp));
50
-
51
- tmp.Clear();
52
-
53
- }
54
-
55
-
56
-
57
- for_translate.Add(str[i].ToString());
58
-
59
- }
60
-
61
- else
62
-
63
- {
64
-
65
- tmp.Add(str[i].ToString());
66
-
67
- }
68
-
69
-
70
-
71
- if(i == str.Length - 1)
72
-
73
- {
74
-
75
- if(tmp.Count > 0)
76
-
77
- {
78
-
79
- for_translate.Add(string.Join("", tmp));
80
-
81
- }
82
-
83
- }
84
-
85
- }
86
-
87
-
88
-
89
- return for_translate;
90
-
91
- }
92
-
93
-
94
-
95
- //かっこが正しいか数値が正しいかを判定
96
-
97
- static Object check(List<string> list)
98
-
99
- {
100
-
101
- bool valid_nest = true;
102
-
103
-
104
-
105
- List<string> nest = new List<string>();
106
-
107
-
108
-
109
- Dictionary<string, string> p_dic = new Dictionary<string, string>()
110
-
111
- {
112
-
113
- { ")", "(" },
114
-
115
- { "]", "[" },
116
-
117
- { "}", "{" }
118
-
119
- };
120
-
121
-
122
-
123
- foreach(string e in list)
124
-
125
- {
126
-
127
- if ("([{".Contains(e))
128
-
129
- {
130
-
131
- nest.Add(e);
132
-
133
- }
134
-
135
- else if (")]}".Contains(e))
136
-
137
- {
138
-
139
- if(nest.Count < 1)
140
-
141
- {
142
-
143
- valid_nest = false;
144
-
145
- break;
146
-
147
- }
148
-
149
-
150
-
151
- if(nest.Last() != p_dic[e])
152
-
153
- {
154
-
155
- valid_nest = false;
156
-
157
- break;
158
-
159
- }
160
-
161
-
162
-
163
- nest.RemoveAt(nest.Count - 1);
164
-
165
- }
166
-
167
- }
168
-
169
-
170
-
171
- if (nest.Any()) valid_nest = false;
172
-
173
-
174
-
175
- if(valid_nest)
176
-
177
- {
178
-
179
- bool valid_number = true;
180
-
181
- int nums = 0;
182
-
183
- int symbol = 0;
184
-
185
-
186
-
187
- foreach(string e in list)
188
-
189
- {
190
-
191
- if ("+-*/%^()[]{}".Contains(e) == false)
192
-
193
- {
194
-
195
- double d = 0;
196
-
197
-
198
-
199
- if (double.TryParse(e, out d))
200
-
201
- {
202
-
203
- nums++;
204
-
205
- }
206
-
207
- else
208
-
209
- {
210
-
211
- valid_number = false;
212
-
213
- }
214
-
215
- }
216
-
217
- else if(("+-*/%^".Contains(e)))
218
-
219
- {
220
-
221
- symbol++;
222
-
223
- }
224
-
225
- }
226
-
227
-
228
-
229
- if (nums <= symbol) return "式が不正です";
230
-
231
-
232
-
233
- if(valid_number)
234
-
235
- {
236
-
237
- for (int i = 0; i < list.Count; i++)
238
-
239
- {
240
-
241
- if ("[{".Contains(list[i])) list[i] = "(";
242
-
243
- if ("]}".Contains(list[i])) list[i] = ")";
244
-
245
- }
246
-
247
-
248
-
249
- return list;
250
-
251
- }
252
-
253
- else
254
-
255
- {
256
-
257
- return "数値が不正です";
258
-
259
- }
260
-
261
- }
262
-
263
-
264
-
265
- return "かっこが不正です";
266
-
267
- }
268
-
269
-
270
-
271
- //逆ポーランド記法に変換
272
-
273
- static List<string> translate(List<string> list)
274
-
275
- {
276
-
277
- List<string> stack = new List<string>();
278
-
279
- List<string> for_calc = new List<string>();
280
-
281
-
282
-
283
- for(int i = 0; i < list.Count; i++)
284
-
285
- {
286
-
287
- if(list[i] == "(")
288
-
289
- {
290
-
291
- stack.Add(list[i]);
292
-
293
- }
294
-
295
- else if(list[i] == ")")
296
-
297
- {
298
-
299
- while (stack.Last() != "(")
300
-
301
- {
302
-
303
- for_calc.Add(stack.Last());
304
-
305
- stack.RemoveAt(stack.Count - 1);
306
-
307
- }
308
-
309
-
310
-
311
- stack.RemoveAt(stack.Count - 1);
312
-
313
- }
314
-
315
- else if ("+-*/%^".Contains(list[i]))
316
-
317
- {
318
-
319
- if(stack.Count > 0)
320
-
321
- {
322
-
323
- if ("+-".Contains(list[i]))
324
-
325
- {
326
-
327
- while ("*/%^".Contains(stack.Last()))
328
-
329
- {
330
-
331
- for_calc.Add(stack.Last());
332
-
333
- stack.RemoveAt(stack.Count - 1);
334
-
335
-
336
-
337
- if (stack.Count < 1) break;
338
-
339
- }
340
-
341
- }
342
-
343
- else if (list[i] == "*")
344
-
345
- {
346
-
347
- while ("/%^".Contains(stack.Last()))
348
-
349
- {
350
-
351
- for_calc.Add(stack.Last());
352
-
353
- stack.RemoveAt(stack.Count - 1);
354
-
355
-
356
-
357
- if (stack.Count < 1) break;
358
-
359
- }
360
-
361
- }
362
-
363
- else if ("/%".Contains(list[i]))
364
-
365
- {
366
-
367
- while (stack.Last() == "^")
368
-
369
- {
370
-
371
- for_calc.Add(stack.Last());
372
-
373
- stack.RemoveAt(stack.Count - 1);
374
-
375
-
376
-
377
- if (stack.Count < 1) break;
378
-
379
- }
380
-
381
- }
382
-
383
- }
384
-
385
-
386
-
387
- stack.Add(list[i]);
388
-
389
- }
390
-
391
- else
392
-
393
- {
394
-
395
- for_calc.Add(list[i]);
396
-
397
- }
398
-
399
-
400
-
401
- if(i == list.Count - 1)
402
-
403
- {
404
-
405
- if (stack.Count > 0)
406
-
407
- {
408
-
409
- stack.Reverse();
410
-
411
-
412
-
413
- foreach(string e in stack)
414
-
415
- {
416
-
417
- for_calc.Add(e);
418
-
419
- }
420
-
421
- }
422
-
423
- }
424
-
425
- }
426
-
427
-
428
-
429
- return for_calc;
430
-
431
- }
432
-
433
-
434
-
435
- //逆ポーランド記法を解く
436
-
437
- static double calc_porland(List<string> list)
438
-
439
- {
440
-
441
- List<double> stack = new List<double>();
442
-
443
-
444
-
445
- foreach(string e in list)
446
-
447
- {
448
-
449
- double d = 0;
450
-
451
-
452
-
453
- if(double.TryParse(e, out d))
454
-
455
- {
456
-
457
- stack.Add(d);
458
-
459
- }
460
-
461
- else
462
-
463
- {
464
-
465
- double a = stack.Last(); stack.RemoveAt(stack.Count - 1);
466
-
467
- double b = stack.Last(); stack.RemoveAt(stack.Count - 1);
468
-
469
-
470
-
471
- double acue = 0;
472
-
473
-
474
-
475
- switch (e)
476
-
477
- {
478
-
479
- case "+":
480
-
481
- acue = b + a;
482
-
483
- break;
484
-
485
- case "-":
486
-
487
- acue = b - a;
488
-
489
- break;
490
-
491
- case "*":
492
-
493
- acue = b * a;
494
-
495
- break;
496
-
497
- case "/":
498
-
499
- acue = b / a;
500
-
501
- break;
502
-
503
- case "%":
504
-
505
- acue = b % a;
506
-
507
- break;
508
-
509
- case "^":
510
-
511
- acue = Math.Pow(b, a);
512
-
513
- break;
514
-
515
- }
516
-
517
-
518
-
519
- stack.Add(acue);
520
-
521
- }
522
-
523
- }
524
-
525
-
526
-
527
- return stack[0];
528
-
529
- }
530
-
531
-
532
-
533
- //上のメソッドの結果を合体
534
-
535
- static string porland(string str)
536
-
537
- {
538
-
539
- List<string> for_check = split_nums(str);
540
-
541
-
542
-
543
- var result = check(for_check);
544
-
545
-
546
-
547
- if (result is String)
548
-
549
- {
550
-
551
- return (string)result;
552
-
553
- }
554
-
555
- else
556
-
557
- {
558
-
559
- return calc_porland(translate((List<string>)result)).ToString();
560
-
561
- }
562
-
563
- }
564
-
565
-
566
-
567
- static void Main(string[] args)
568
-
569
- {
570
-
571
-
572
-
573
- //string input = "[{(1+2)*(3+4)}+{(5+6)*(7+8)}]*[{(9+10)*(11+12)}+{(13+14)*(15+16)}]";
574
-
575
-
576
-
577
- string input = Console.ReadLine();
578
-
579
-
580
-
581
- Console.WriteLine(porland(input));
582
-
583
- Console.ReadKey();
584
-
585
- }
586
-
587
- }
15
+ class Program
16
+
17
+ {
18
+
19
+ //式分解
20
+
21
+ static List<string> split_nums(string str)
22
+
23
+ {
24
+
25
+ str = str.Replace(" ", "");
26
+
27
+ str = str.Replace("**", "^");
28
+
29
+ str = str.Replace("f", "");
30
+
31
+
32
+
33
+ Regex re = new Regex(@"\((?<nega>-\d+?(|.\d+?))\)");
34
+
35
+
36
+
37
+ List<string> negative = new List<string>();
38
+
39
+
40
+
41
+ MatchCollection mc = re.Matches(str);
42
+
43
+
44
+
45
+ foreach(Match m in mc)
46
+
47
+ {
48
+
49
+ negative.Add(m.Groups["nega"].Value);
50
+
51
+ }
52
+
53
+
54
+
55
+ str = re.Replace(str, "f");
56
+
57
+ List<string> for_translate = new List<string>();
58
+
59
+ List<string> tmp = new List<string>();
60
+
61
+ for (int i = 0; i < str.Length; i++)
62
+
63
+ {
64
+
65
+ if ("+-*/%^()[]{}".Contains(str[i]))
66
+
67
+ {
68
+
69
+ if (tmp.Count > 0)
70
+
71
+ {
72
+
73
+ for_translate.Add(string.Join("", tmp));
74
+
75
+ tmp.Clear();
76
+
77
+ }
78
+
79
+ for_translate.Add(str[i].ToString());
80
+
81
+ }
82
+
83
+ else
84
+
85
+ {
86
+
87
+ tmp.Add(str[i].ToString());
88
+
89
+ }
90
+
91
+ if(i == str.Length - 1)
92
+
93
+ {
94
+
95
+ if(tmp.Count > 0)
96
+
97
+ {
98
+
99
+ for_translate.Add(string.Join("", tmp));
100
+
101
+ }
102
+
103
+ }
104
+
105
+ }
106
+
107
+ for(int i =0;i < for_translate.Count; i++)
108
+
109
+ {
110
+
111
+ if (for_translate[i] == "f")
112
+
113
+ {
114
+
115
+ for_translate[i] = negative[0];
116
+
117
+ negative.RemoveAt(0);
118
+
119
+ }
120
+
121
+ }
122
+
123
+
124
+
125
+ return for_translate;
126
+
127
+ }
128
+
129
+ //かっこが正しいか数値が正しいかを判定
130
+
131
+ static Object check(List<string> list)
132
+
133
+ {
134
+
135
+ bool valid_nest = true;
136
+
137
+ List<string> nest = new List<string>();
138
+
139
+ Dictionary<string, string> p_dic = new Dictionary<string, string>()
140
+
141
+ {
142
+
143
+ { ")", "(" },
144
+
145
+ { "]", "[" },
146
+
147
+ { "}", "{" }
148
+
149
+ };
150
+
151
+ foreach(string e in list)
152
+
153
+ {
154
+
155
+ if ("([{".Contains(e))
156
+
157
+ {
158
+
159
+ nest.Add(e);
160
+
161
+ }
162
+
163
+ else if (")]}".Contains(e))
164
+
165
+ {
166
+
167
+ if(nest.Count < 1)
168
+
169
+ {
170
+
171
+ valid_nest = false;
172
+
173
+ break;
174
+
175
+ }
176
+
177
+ if(nest.Last() != p_dic[e])
178
+
179
+ {
180
+
181
+ valid_nest = false;
182
+
183
+ break;
184
+
185
+ }
186
+
187
+ nest.RemoveAt(nest.Count - 1);
188
+
189
+ }
190
+
191
+ }
192
+
193
+ if (nest.Any()) valid_nest = false;
194
+
195
+ if(valid_nest)
196
+
197
+ {
198
+
199
+ bool valid_number = true;
200
+
201
+ int nums = 0;
202
+
203
+ int symbol = 0;
204
+
205
+ foreach(string e in list)
206
+
207
+ {
208
+
209
+ if ("+-*/%^()[]{}".Contains(e) == false)
210
+
211
+ {
212
+
213
+ double d = 0;
214
+
215
+ if (double.TryParse(e, out d))
216
+
217
+ {
218
+
219
+ nums++;
220
+
221
+ }
222
+
223
+ else
224
+
225
+ {
226
+
227
+ valid_number = false;
228
+
229
+ }
230
+
231
+ }
232
+
233
+ else if(("+-*/%^".Contains(e)))
234
+
235
+ {
236
+
237
+ symbol++;
238
+
239
+ }
240
+
241
+ }
242
+
243
+ if (nums <= symbol) return "式が不正です";
244
+
245
+ if(valid_number)
246
+
247
+ {
248
+
249
+ for (int i = 0; i < list.Count; i++)
250
+
251
+ {
252
+
253
+ if ("[{".Contains(list[i])) list[i] = "(";
254
+
255
+ if ("]}".Contains(list[i])) list[i] = ")";
256
+
257
+ }
258
+
259
+ return list;
260
+
261
+ }
262
+
263
+ else
264
+
265
+ {
266
+
267
+ return "数値が不正です";
268
+
269
+ }
270
+
271
+ }
272
+
273
+ return "かっこが不正です";
274
+
275
+ }
276
+
277
+ //逆ポーランド記法に変換
278
+
279
+ static List<string> translate(List<string> list)
280
+
281
+ {
282
+
283
+ List<string> stack = new List<string>();
284
+
285
+ List<string> for_calc = new List<string>();
286
+
287
+ for(int i = 0; i < list.Count; i++)
288
+
289
+ {
290
+
291
+ if(list[i] == "(")
292
+
293
+ {
294
+
295
+ stack.Add(list[i]);
296
+
297
+ }
298
+
299
+ else if(list[i] == ")")
300
+
301
+ {
302
+
303
+ while (stack.Last() != "(")
304
+
305
+ {
306
+
307
+ for_calc.Add(stack.Last());
308
+
309
+ stack.RemoveAt(stack.Count - 1);
310
+
311
+ }
312
+
313
+ stack.RemoveAt(stack.Count - 1);
314
+
315
+ }
316
+
317
+ else if ("+-*/%^".Contains(list[i]))
318
+
319
+ {
320
+
321
+ if(stack.Count > 0)
322
+
323
+ {
324
+
325
+ if ("+-".Contains(list[i]))
326
+
327
+ {
328
+
329
+ while ("*/%^".Contains(stack.Last()))
330
+
331
+ {
332
+
333
+ for_calc.Add(stack.Last());
334
+
335
+ stack.RemoveAt(stack.Count - 1);
336
+
337
+ if (stack.Count < 1) break;
338
+
339
+ }
340
+
341
+ }
342
+
343
+ else if (list[i] == "*")
344
+
345
+ {
346
+
347
+ while ("/%^".Contains(stack.Last()))
348
+
349
+ {
350
+
351
+ for_calc.Add(stack.Last());
352
+
353
+ stack.RemoveAt(stack.Count - 1);
354
+
355
+ if (stack.Count < 1) break;
356
+
357
+ }
358
+
359
+ }
360
+
361
+ else if ("/%".Contains(list[i]))
362
+
363
+ {
364
+
365
+ while (stack.Last() == "^")
366
+
367
+ {
368
+
369
+ for_calc.Add(stack.Last());
370
+
371
+ stack.RemoveAt(stack.Count - 1);
372
+
373
+ if (stack.Count < 1) break;
374
+
375
+ }
376
+
377
+ }
378
+
379
+ }
380
+
381
+ stack.Add(list[i]);
382
+
383
+ }
384
+
385
+ else
386
+
387
+ {
388
+
389
+ for_calc.Add(list[i]);
390
+
391
+ }
392
+
393
+ if(i == list.Count - 1)
394
+
395
+ {
396
+
397
+ if (stack.Count > 0)
398
+
399
+ {
400
+
401
+ stack.Reverse();
402
+
403
+ foreach(string e in stack)
404
+
405
+ {
406
+
407
+ for_calc.Add(e);
408
+
409
+ }
410
+
411
+ }
412
+
413
+ }
414
+
415
+ }
416
+
417
+ return for_calc;
418
+
419
+ }
420
+
421
+ //逆ポーランド記法を解く
422
+
423
+ static double calc_porland(List<string> list)
424
+
425
+ {
426
+
427
+ List<double> stack = new List<double>();
428
+
429
+ foreach(string e in list)
430
+
431
+ {
432
+
433
+ double d = 0;
434
+
435
+ if(double.TryParse(e, out d))
436
+
437
+ {
438
+
439
+ stack.Add(d);
440
+
441
+ }
442
+
443
+ else
444
+
445
+ {
446
+
447
+ double a = stack.Last(); stack.RemoveAt(stack.Count - 1);
448
+
449
+ double b = stack.Last(); stack.RemoveAt(stack.Count - 1);
450
+
451
+ double acue = 0;
452
+
453
+ switch (e)
454
+
455
+ {
456
+
457
+ case "+":
458
+
459
+ acue = b + a;
460
+
461
+ break;
462
+
463
+ case "-":
464
+
465
+ acue = b - a;
466
+
467
+ break;
468
+
469
+ case "*":
470
+
471
+ acue = b * a;
472
+
473
+ break;
474
+
475
+ case "/":
476
+
477
+ acue = b / a;
478
+
479
+ break;
480
+
481
+ case "%":
482
+
483
+ acue = b % a;
484
+
485
+ break;
486
+
487
+ case "^":
488
+
489
+ acue = Math.Pow(b, a);
490
+
491
+ break;
492
+
493
+ }
494
+
495
+ stack.Add(acue);
496
+
497
+ }
498
+
499
+ }
500
+
501
+ return stack[0];
502
+
503
+ }
504
+
505
+ //上のメソッドの結果を合体
506
+
507
+ static string porland(string str)
508
+
509
+ {
510
+
511
+ List<string> for_check = split_nums(str);
512
+
513
+ var result = check(for_check);
514
+
515
+ if (result is String)
516
+
517
+ {
518
+
519
+ return (string)result;
520
+
521
+ }
522
+
523
+ else
524
+
525
+ {
526
+
527
+ return calc_porland(translate((List<string>)result)).ToString();
528
+
529
+ }
530
+
531
+ }
532
+
533
+ static void Main(string[] args)
534
+
535
+ {
536
+
537
+ //string input = "[{(1+2)*(3+4)}+{(5+6)*(7+8)}]*[{(9+10)*(11+12)}+{(13+14)*(15+16)}]";
538
+
539
+ string input = "[{(1+2)*(3+4)}+{(5+6)*(7+8)}]*[{(9+10)*(11+12)}+{(13+14)*(15+16)}]*(-1)+0.1";
540
+
541
+ Console.WriteLine(porland(input));
542
+
543
+ Console.ReadKey();
544
+
545
+ }
546
+
547
+ }
588
548
 
589
549
  }
590
550
 
@@ -602,14 +562,8 @@
602
562
 
603
563
  [逆ポーランド記法への変換2](http://www.gg.e-mansion.com/~kkatoh/program/novel2/novel208.html)
604
564
 
605
-
606
-
607
-
608
-
609
565
  素人ですが考えてみました。
610
566
 
611
567
  このプログラムが正確に動くかどうかは分かりません。
612
568
 
613
569
  B型単純式についてはよくわからなかったので何もしていません。
614
-
615
-

13

修正

2017/06/28 21:32

投稿

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

12

修正

2017/06/28 21:28

投稿

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

11

修正

2017/06/28 21:26

投稿

退会済みユーザー
test CHANGED
@@ -392,9 +392,7 @@
392
392
 
393
393
  {
394
394
 
395
- double d = 0;
396
-
397
- if(double.TryParse(list[i], out d)) for_calc.Add(list[i]);
395
+ for_calc.Add(list[i]);
398
396
 
399
397
  }
400
398
 
@@ -426,6 +424,8 @@
426
424
 
427
425
  }
428
426
 
427
+
428
+
429
429
  return for_calc;
430
430
 
431
431
  }
@@ -568,7 +568,13 @@
568
568
 
569
569
  {
570
570
 
571
+
572
+
571
- string input = "[{(1+2)*(3+4)}+{(5+6)*(7+8)}]*[{(9+10)*(11+12)}+{(13+14)*(15+16)}]";
573
+ //string input = "[{(1+2)*(3+4)}+{(5+6)*(7+8)}]*[{(9+10)*(11+12)}+{(13+14)*(15+16)}]";
574
+
575
+
576
+
577
+ string input = Console.ReadLine();
572
578
 
573
579
 
574
580
 

10

修正

2017/06/28 16:57

投稿

退会済みユーザー
test CHANGED
@@ -226,13 +226,7 @@
226
226
 
227
227
 
228
228
 
229
- if (nums <= symbol)
230
-
231
- {
232
-
233
- return "式が不正です";
229
+ if (nums <= symbol) return "式が不正です";
234
-
235
- }
236
230
 
237
231
 
238
232
 
@@ -388,6 +382,8 @@
388
382
 
389
383
  }
390
384
 
385
+
386
+
391
387
  stack.Add(list[i]);
392
388
 
393
389
  }
@@ -430,8 +426,6 @@
430
426
 
431
427
  }
432
428
 
433
-
434
-
435
429
  return for_calc;
436
430
 
437
431
  }

9

修正

2017/06/28 16:46

投稿

退会済みユーザー
test CHANGED
@@ -592,14 +592,16 @@
592
592
 
593
593
  参考にしたサイト
594
594
 
595
+ [【Ruby】【アルゴリズム】逆ポーランド記法](http://sekai.hateblo.jp/entry/2013/09/15/185314)
596
+
597
+ [逆ポーランド記法による計算式を計算する電卓](http://calc.exinfo.biz/)
598
+
599
+ [逆ポーランド記法変換ツール](http://tamanegi-core.net/memo/article/polish_notation_converter)
600
+
601
+ [rubyについて質問です。](https://teratail.com/questions/80654)のkatoy様の回答
602
+
595
603
  [逆ポーランド記法への変換2](http://www.gg.e-mansion.com/~kkatoh/program/novel2/novel208.html)
596
604
 
597
- [逆ポーランド記法変換ツール](http://tamanegi-core.net/memo/article/polish_notation_converter)
598
-
599
- [【Ruby】【アルゴリズム】逆ポーランド記法](http://sekai.hateblo.jp/entry/2013/09/15/185314)
600
-
601
- [rubyについて質問です。](https://teratail.com/questions/80654)のkatoy様の回答
602
-
603
605
 
604
606
 
605
607
 

8

修正

2017/06/28 16:44

投稿

退会済みユーザー
test CHANGED
@@ -154,6 +154,8 @@
154
154
 
155
155
  valid_nest = false;
156
156
 
157
+ break;
158
+
157
159
  }
158
160
 
159
161
 

7

修正

2017/06/28 16:38

投稿

退会済みユーザー
test CHANGED
@@ -72,7 +72,13 @@
72
72
 
73
73
  {
74
74
 
75
+ if(tmp.Count > 0)
76
+
77
+ {
78
+
75
- for_translate.Add(string.Join("", tmp));
79
+ for_translate.Add(string.Join("", tmp));
80
+
81
+ }
76
82
 
77
83
  }
78
84
 
@@ -160,12 +166,20 @@
160
166
 
161
167
 
162
168
 
169
+ if (nest.Any()) valid_nest = false;
170
+
171
+
172
+
163
173
  if(valid_nest)
164
174
 
165
175
  {
166
176
 
167
177
  bool valid_number = true;
168
178
 
179
+ int nums = 0;
180
+
181
+ int symbol = 0;
182
+
169
183
 
170
184
 
171
185
  foreach(string e in list)
@@ -184,7 +198,7 @@
184
198
 
185
199
  {
186
200
 
187
- //Do Nothing!
201
+ nums++;
188
202
 
189
203
  }
190
204
 
@@ -198,6 +212,24 @@
198
212
 
199
213
  }
200
214
 
215
+ else if(("+-*/%^".Contains(e)))
216
+
217
+ {
218
+
219
+ symbol++;
220
+
221
+ }
222
+
223
+ }
224
+
225
+
226
+
227
+ if (nums <= symbol)
228
+
229
+ {
230
+
231
+ return "式が不正です";
232
+
201
233
  }
202
234
 
203
235
 
@@ -206,7 +238,7 @@
206
238
 
207
239
  {
208
240
 
209
- for (int i = 0; i < list.Count - 1; i++)
241
+ for (int i = 0; i < list.Count; i++)
210
242
 
211
243
  {
212
244
 
@@ -378,17 +410,15 @@
378
410
 
379
411
  {
380
412
 
413
+ stack.Reverse();
414
+
415
+
416
+
381
- while (stack[0] != "(")
417
+ foreach(string e in stack)
382
418
 
383
419
  {
384
420
 
385
- if(stack.Last() != "") for_calc.Add(stack.Last());
386
-
387
- stack.RemoveAt(stack.Count - 1);
388
-
389
-
390
-
391
- if (stack.Count < 1) break;
421
+ for_calc.Add(e);
392
422
 
393
423
  }
394
424
 

6

修正

2017/06/28 16:35

投稿

退会済みユーザー
test CHANGED
@@ -560,11 +560,15 @@
560
560
 
561
561
  参考にしたサイト
562
562
 
563
+ [逆ポーランド記法への変換2](http://www.gg.e-mansion.com/~kkatoh/program/novel2/novel208.html)
564
+
565
+ [逆ポーランド記法変換ツール](http://tamanegi-core.net/memo/article/polish_notation_converter)
566
+
563
567
  [【Ruby】【アルゴリズム】逆ポーランド記法](http://sekai.hateblo.jp/entry/2013/09/15/185314)
564
568
 
565
569
  [rubyについて質問です。](https://teratail.com/questions/80654)のkatoy様の回答
566
570
 
567
- [逆ポーランド記法への変換2](http://www.gg.e-mansion.com/~kkatoh/program/novel2/novel208.html)
571
+
568
572
 
569
573
 
570
574
 

5

修正

2017/06/28 15:58

投稿

退会済みユーザー
test CHANGED
@@ -564,6 +564,8 @@
564
564
 
565
565
  [rubyについて質問です。](https://teratail.com/questions/80654)のkatoy様の回答
566
566
 
567
+ [逆ポーランド記法への変換2](http://www.gg.e-mansion.com/~kkatoh/program/novel2/novel208.html)
568
+
567
569
 
568
570
 
569
571
  素人ですが考えてみました。

4

修正

2017/06/28 15:57

投稿

退会済みユーザー
test CHANGED
@@ -562,6 +562,8 @@
562
562
 
563
563
  [【Ruby】【アルゴリズム】逆ポーランド記法](http://sekai.hateblo.jp/entry/2013/09/15/185314)
564
564
 
565
+ [rubyについて質問です。](https://teratail.com/questions/80654)のkatoy様の回答
566
+
565
567
 
566
568
 
567
569
  素人ですが考えてみました。

3

修正

2017/06/28 15:53

投稿

退会済みユーザー
test CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
 
10
10
 
11
- namespace polandtest1
11
+ namespace calc_porland
12
12
 
13
13
  {
14
14
 
@@ -16,107 +16,537 @@
16
16
 
17
17
  {
18
18
 
19
+ //式分解
20
+
21
+ static List<string> split_nums(string str)
22
+
23
+ {
24
+
25
+ str = str.Replace(" ", "");
26
+
27
+ str = str.Replace("**", "^");
28
+
29
+
30
+
31
+ List<string> for_translate = new List<string>();
32
+
33
+ List<string> tmp = new List<string>();
34
+
35
+
36
+
37
+ for (int i = 0; i < str.Length; i++)
38
+
39
+ {
40
+
41
+ if ("+-*/%^()[]{}".Contains(str[i]))
42
+
43
+ {
44
+
45
+ if (tmp.Count > 0)
46
+
47
+ {
48
+
49
+ for_translate.Add(string.Join("", tmp));
50
+
51
+ tmp.Clear();
52
+
53
+ }
54
+
55
+
56
+
57
+ for_translate.Add(str[i].ToString());
58
+
59
+ }
60
+
61
+ else
62
+
63
+ {
64
+
65
+ tmp.Add(str[i].ToString());
66
+
67
+ }
68
+
69
+
70
+
71
+ if(i == str.Length - 1)
72
+
73
+ {
74
+
75
+ for_translate.Add(string.Join("", tmp));
76
+
77
+ }
78
+
79
+ }
80
+
81
+
82
+
83
+ return for_translate;
84
+
85
+ }
86
+
87
+
88
+
89
+ //かっこが正しいか数値が正しいかを判定
90
+
91
+ static Object check(List<string> list)
92
+
93
+ {
94
+
95
+ bool valid_nest = true;
96
+
97
+
98
+
99
+ List<string> nest = new List<string>();
100
+
101
+
102
+
103
+ Dictionary<string, string> p_dic = new Dictionary<string, string>()
104
+
105
+ {
106
+
107
+ { ")", "(" },
108
+
109
+ { "]", "[" },
110
+
111
+ { "}", "{" }
112
+
113
+ };
114
+
115
+
116
+
117
+ foreach(string e in list)
118
+
119
+ {
120
+
121
+ if ("([{".Contains(e))
122
+
123
+ {
124
+
125
+ nest.Add(e);
126
+
127
+ }
128
+
129
+ else if (")]}".Contains(e))
130
+
131
+ {
132
+
133
+ if(nest.Count < 1)
134
+
135
+ {
136
+
137
+ valid_nest = false;
138
+
139
+ break;
140
+
141
+ }
142
+
143
+
144
+
145
+ if(nest.Last() != p_dic[e])
146
+
147
+ {
148
+
149
+ valid_nest = false;
150
+
151
+ }
152
+
153
+
154
+
155
+ nest.RemoveAt(nest.Count - 1);
156
+
157
+ }
158
+
159
+ }
160
+
161
+
162
+
163
+ if(valid_nest)
164
+
165
+ {
166
+
167
+ bool valid_number = true;
168
+
169
+
170
+
171
+ foreach(string e in list)
172
+
173
+ {
174
+
175
+ if ("+-*/%^()[]{}".Contains(e) == false)
176
+
177
+ {
178
+
179
+ double d = 0;
180
+
181
+
182
+
183
+ if (double.TryParse(e, out d))
184
+
185
+ {
186
+
187
+ //Do Nothing!
188
+
189
+ }
190
+
191
+ else
192
+
193
+ {
194
+
195
+ valid_number = false;
196
+
197
+ }
198
+
199
+ }
200
+
201
+ }
202
+
203
+
204
+
205
+ if(valid_number)
206
+
207
+ {
208
+
209
+ for (int i = 0; i < list.Count - 1; i++)
210
+
211
+ {
212
+
213
+ if ("[{".Contains(list[i])) list[i] = "(";
214
+
215
+ if ("]}".Contains(list[i])) list[i] = ")";
216
+
217
+ }
218
+
219
+
220
+
221
+ return list;
222
+
223
+ }
224
+
225
+ else
226
+
227
+ {
228
+
229
+ return "数値が不正です";
230
+
231
+ }
232
+
233
+ }
234
+
235
+
236
+
237
+ return "かっこが不正です";
238
+
239
+ }
240
+
241
+
242
+
243
+ //逆ポーランド記法に変換
244
+
245
+ static List<string> translate(List<string> list)
246
+
247
+ {
248
+
249
+ List<string> stack = new List<string>();
250
+
251
+ List<string> for_calc = new List<string>();
252
+
253
+
254
+
255
+ for(int i = 0; i < list.Count; i++)
256
+
257
+ {
258
+
259
+ if(list[i] == "(")
260
+
261
+ {
262
+
263
+ stack.Add(list[i]);
264
+
265
+ }
266
+
267
+ else if(list[i] == ")")
268
+
269
+ {
270
+
271
+ while (stack.Last() != "(")
272
+
273
+ {
274
+
275
+ for_calc.Add(stack.Last());
276
+
277
+ stack.RemoveAt(stack.Count - 1);
278
+
279
+ }
280
+
281
+
282
+
283
+ stack.RemoveAt(stack.Count - 1);
284
+
285
+ }
286
+
287
+ else if ("+-*/%^".Contains(list[i]))
288
+
289
+ {
290
+
291
+ if(stack.Count > 0)
292
+
293
+ {
294
+
295
+ if ("+-".Contains(list[i]))
296
+
297
+ {
298
+
299
+ while ("*/%^".Contains(stack.Last()))
300
+
301
+ {
302
+
303
+ for_calc.Add(stack.Last());
304
+
305
+ stack.RemoveAt(stack.Count - 1);
306
+
307
+
308
+
309
+ if (stack.Count < 1) break;
310
+
311
+ }
312
+
313
+ }
314
+
315
+ else if (list[i] == "*")
316
+
317
+ {
318
+
319
+ while ("/%^".Contains(stack.Last()))
320
+
321
+ {
322
+
323
+ for_calc.Add(stack.Last());
324
+
325
+ stack.RemoveAt(stack.Count - 1);
326
+
327
+
328
+
329
+ if (stack.Count < 1) break;
330
+
331
+ }
332
+
333
+ }
334
+
335
+ else if ("/%".Contains(list[i]))
336
+
337
+ {
338
+
339
+ while (stack.Last() == "^")
340
+
341
+ {
342
+
343
+ for_calc.Add(stack.Last());
344
+
345
+ stack.RemoveAt(stack.Count - 1);
346
+
347
+
348
+
349
+ if (stack.Count < 1) break;
350
+
351
+ }
352
+
353
+ }
354
+
355
+ }
356
+
357
+ stack.Add(list[i]);
358
+
359
+ }
360
+
361
+ else
362
+
363
+ {
364
+
365
+ double d = 0;
366
+
367
+ if(double.TryParse(list[i], out d)) for_calc.Add(list[i]);
368
+
369
+ }
370
+
371
+
372
+
373
+ if(i == list.Count - 1)
374
+
375
+ {
376
+
377
+ if (stack.Count > 0)
378
+
379
+ {
380
+
381
+ while (stack[0] != "(")
382
+
383
+ {
384
+
385
+ if(stack.Last() != "") for_calc.Add(stack.Last());
386
+
387
+ stack.RemoveAt(stack.Count - 1);
388
+
389
+
390
+
391
+ if (stack.Count < 1) break;
392
+
393
+ }
394
+
395
+ }
396
+
397
+ }
398
+
399
+ }
400
+
401
+
402
+
403
+ return for_calc;
404
+
405
+ }
406
+
407
+
408
+
409
+ //逆ポーランド記法を解く
410
+
411
+ static double calc_porland(List<string> list)
412
+
413
+ {
414
+
415
+ List<double> stack = new List<double>();
416
+
417
+
418
+
419
+ foreach(string e in list)
420
+
421
+ {
422
+
423
+ double d = 0;
424
+
425
+
426
+
427
+ if(double.TryParse(e, out d))
428
+
429
+ {
430
+
431
+ stack.Add(d);
432
+
433
+ }
434
+
435
+ else
436
+
437
+ {
438
+
439
+ double a = stack.Last(); stack.RemoveAt(stack.Count - 1);
440
+
441
+ double b = stack.Last(); stack.RemoveAt(stack.Count - 1);
442
+
443
+
444
+
445
+ double acue = 0;
446
+
447
+
448
+
449
+ switch (e)
450
+
451
+ {
452
+
453
+ case "+":
454
+
455
+ acue = b + a;
456
+
457
+ break;
458
+
459
+ case "-":
460
+
461
+ acue = b - a;
462
+
463
+ break;
464
+
465
+ case "*":
466
+
467
+ acue = b * a;
468
+
469
+ break;
470
+
471
+ case "/":
472
+
473
+ acue = b / a;
474
+
475
+ break;
476
+
477
+ case "%":
478
+
479
+ acue = b % a;
480
+
481
+ break;
482
+
483
+ case "^":
484
+
485
+ acue = Math.Pow(b, a);
486
+
487
+ break;
488
+
489
+ }
490
+
491
+
492
+
493
+ stack.Add(acue);
494
+
495
+ }
496
+
497
+ }
498
+
499
+
500
+
501
+ return stack[0];
502
+
503
+ }
504
+
505
+
506
+
507
+ //上のメソッドの結果を合体
508
+
509
+ static string porland(string str)
510
+
511
+ {
512
+
513
+ List<string> for_check = split_nums(str);
514
+
515
+
516
+
517
+ var result = check(for_check);
518
+
519
+
520
+
521
+ if (result is String)
522
+
523
+ {
524
+
525
+ return (string)result;
526
+
527
+ }
528
+
529
+ else
530
+
531
+ {
532
+
533
+ return calc_porland(translate((List<string>)result)).ToString();
534
+
535
+ }
536
+
537
+ }
538
+
539
+
540
+
19
541
  static void Main(string[] args)
20
542
 
21
543
  {
22
544
 
23
-
24
-
25
- Console.Write("式を入力してください:");
26
-
27
-
28
-
29
- string input = Console.ReadLine() ;
30
-
31
-
32
-
33
- List<string> strs = input.Split(' ').ToList();
545
+ string input = "[{(1+2)*(3+4)}+{(5+6)*(7+8)}]*[{(9+10)*(11+12)}+{(13+14)*(15+16)}]";
34
-
35
-
36
-
37
- List<double> stack = new List<double>();
546
+
38
-
39
-
40
-
41
- foreach(string e in strs)
547
+
42
-
43
- {
548
+
44
-
45
- double i;
46
-
47
-
48
-
49
- if(double.TryParse(e, out i))
50
-
51
- {
52
-
53
- stack.Add(i);
54
-
55
- }
56
-
57
- else
58
-
59
- {
60
-
61
- double acue = 0;
62
-
63
-
64
-
65
- double a = stack.Last(); stack.RemoveAt(stack.Count - 1);
66
-
67
- double b = stack.Last(); stack.RemoveAt(stack.Count - 1);
68
-
69
-
70
-
71
- switch (e)
72
-
73
- {
74
-
75
- case "+":
76
-
77
- acue = b + a;
78
-
79
- break;
80
-
81
- case "-":
82
-
83
- acue = b - a;
84
-
85
- break;
86
-
87
- case "*":
88
-
89
- acue = b * a;
90
-
91
- break;
92
-
93
- case "/":
94
-
95
- acue = b / a;
96
-
97
- break;
98
-
99
- }
100
-
101
-
102
-
103
- stack.Add(acue);
104
-
105
- }
106
-
107
- }
108
-
109
-
110
-
111
- foreach(double s in stack)
112
-
113
- {
114
-
115
- Console.WriteLine(s.ToString());
549
+ Console.WriteLine(porland(input));
116
-
117
- }
118
-
119
-
120
550
 
121
551
  Console.ReadKey();
122
552
 
@@ -128,48 +558,6 @@
128
558
 
129
559
  ```
130
560
 
131
- 入力値
132
-
133
- ```
134
-
135
- 1 2 + 3 4 + * 5 6 + 7 8 + * + 9 10 + 11 12 + * 13 14 + 15 16 + * + *
136
-
137
- ```
138
-
139
- 出力
140
-
141
- ```
142
-
143
- 236964
144
-
145
- ```
146
-
147
- 回答のチェックに使ったサイト
148
-
149
- [逆ポーランド記法変換ツール](http://tamanegi-core.net/memo/article/polish_notation_converter#introduction)
150
-
151
- [逆ポーランド記法による計算式を計算する電卓](http://calc.exinfo.biz/)
152
-
153
-
154
-
155
- 入力値
156
-
157
- ```
158
-
159
- 1,2+3,4+*5,6+7,8+*+9,10+11,12+*13,14+15,16+*+*
160
-
161
- ```
162
-
163
- 出力
164
-
165
- ```
166
-
167
- 236964
168
-
169
- ```
170
-
171
-
172
-
173
561
  参考にしたサイト
174
562
 
175
563
  [【Ruby】【アルゴリズム】逆ポーランド記法](http://sekai.hateblo.jp/entry/2013/09/15/185314)
@@ -181,3 +569,5 @@
181
569
  このプログラムが正確に動くかどうかは分かりません。
182
570
 
183
571
  B型単純式についてはよくわからなかったので何もしていません。
572
+
573
+

2

修正

2017/06/28 15:51

投稿

退会済みユーザー
test CHANGED
@@ -146,6 +146,8 @@
146
146
 
147
147
  回答のチェックに使ったサイト
148
148
 
149
+ [逆ポーランド記法変換ツール](http://tamanegi-core.net/memo/article/polish_notation_converter#introduction)
150
+
149
151
  [逆ポーランド記法による計算式を計算する電卓](http://calc.exinfo.biz/)
150
152
 
151
153
 

1

修正

2017/05/29 13:33

投稿

退会済みユーザー
test CHANGED
@@ -53,8 +53,6 @@
53
53
  stack.Add(i);
54
54
 
55
55
  }
56
-
57
-
58
56
 
59
57
  else
60
58