-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
137 lines (128 loc) · 4.07 KB
/
Program.cs
File metadata and controls
137 lines (128 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Console.Write("Введите математическую операцию ");
string input = Console.ReadLine();
Console.WriteLine();
List<object> rpn = RPN(input.Replace(" ", string.Empty));
Console.WriteLine("ОПЗ: " + string.Join(" ", rpn) + "\n");
Console.WriteLine("Числа: " + string.Join(" ", GetNums(rpn)) + "\n");
Console.WriteLine("Операторы: " + string.Join(" ", GetOperators(rpn)) + "\n");
Console.WriteLine("Ответ: " + Calculate(rpn)[0]);
}
static int Priority(char operators)
{
if (operators == '(' || operators == ')') return 0;
else if (operators == '+' || operators == '-') return 1;
else return 2;
}
static bool IsOperator(char symbol)
{
string operators = "+-*/()";
if (operators.Contains(symbol)) return true;
else return false;
}
static double TheOperation(char oper, int numer1, int number2)
{
if (oper == '+') return numer1 + number2;
else if (oper == '-') return numer1 - number2;
else if (oper == '*') return numer1 * number2;
else return numer1 / number2;
}
static List<int> GetNums(List<object> expression)
{
List<int> res = new List<int>();
foreach (var i in expression)
{
if (!(i is char))
res.Add(Convert.ToInt32(i));
}
return res;
}
static List<char> GetOperators(List<object> expression)
{
List<char> res = new List<char>();
foreach (var i in expression)
{
if (i is char)
res.Add(Convert.ToChar(i));
}
return res;
}
public static List<object> RPN(string input)
{
List<object> output = new List<object>();
Stack<char> operators = new Stack<char>();
string number = string.Empty;
for (int i = 0; i < input.Length; i++)
{
if (char.IsDigit(input[i]))
{
number += input[i];
}
if (IsOperator(input[i]))
{
output.Add(number);
number = string.Empty;
if (input[i] == '(')
{
operators.Push(input[i]);
}
else if (input[i] == ')')
{
while (operators.Peek() != '(')
{
output.Add(operators.Peek());
operators.Pop();
}
operators.Pop();
}
else
{
if (operators.Count > 0)
{
if (Priority(input[i]) <= Priority(operators.Peek()))
{
output.Add(operators.Peek());
operators.Pop();
}
operators.Push(input[i]);
}
else
{
operators.Push(input[i]);
}
}
}
}
output.Add(number);
while (operators.Count != 0)
{
output.Add(operators.Peek());
operators.Pop();
}
while (output.Contains(string.Empty)) output.Remove(string.Empty);
return output;
}
static List<object> Calculate(List<object> expression)
{
int i = 0;
while (expression.Count > 1)
{
if (i > expression.Count)
i = 0;
if (expression[i] is char && IsOperator((char)expression[i]))
{
expression[i - 2] = TheOperation((char)expression[i], Convert.ToInt32(expression[i - 2]), Convert.ToInt32(expression[i - 1]));
expression.RemoveAt(i);
expression.RemoveAt(i - 1);
i = 0;
}
i++;
}
return expression;
}
}