-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
216 lines (192 loc) · 6.28 KB
/
Program.cs
File metadata and controls
216 lines (192 loc) · 6.28 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System;
using System.Collections.Generic;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace Solution
{
class Program
{
internal class Token
{
}
class Number : Token
{
public double Symbol { get; }
public Number(double num)
{
Symbol = num;
}
}
class Operation : Token
{
public char Symbol { get; }
public int Priority { get; }
public Operation(char symbol)
{
Symbol = symbol;
Priority = GetPriority(symbol);
}
private static int GetPriority(char symbol)
{
switch (symbol)
{
case '(': return 0;
case ')': return 0;
case '+': return 1;
case '-': return 1;
case '*': return 2;
case '/': return 2;
default: return 3;
}
}
}
class Paranthesis : Token
{
public char Symbol { get; }
public bool isClosing { get; }
public Paranthesis(char symbol)
{
Symbol = symbol;
isClosing = symbol == ')';
}
}
class RPN
{
static void Main(string[] args)
{
Console.Write("Введите математическую операцию ");
string input = Console.ReadLine();
input.Replace(" ", string.Empty);
var tokens = Tokenize(input);
var rpn = toRPN(tokens);
Console.WriteLine($"Результат: {Calculate(rpn)}");
}
}
static List<Token> Tokenize(string input)
{
List<Token> tokens = new List<Token>();
string number = string.Empty;
foreach (var c in input)
{
if (char.IsDigit(c))
{
number += c;
}
else if (c == ',' || c == '.')
{
number += ",";
}
else if (c == '+' || c == '-' || c == '*' || c == '/')
{
if (number != string.Empty)
{
tokens.Add(new Number(double.Parse(number)));
number = string.Empty;
}
tokens.Add(new Operation(c));
}
else if (c == '(' || c == ')')
{
if (number != string.Empty)
{
tokens.Add(new Number(double.Parse(number)));
number = string.Empty;
}
tokens.Add(new Paranthesis(c));
}
}
if (number != string.Empty)
{
tokens.Add(new Number(double.Parse(number)));
}
return tokens;
}
static List<Token> toRPN(List<Token> tokens)
{
List<Token> rpnOutput = new List<Token>();
Stack<Token> operators = new Stack<Token>();
string number = string.Empty;
foreach (Token token in tokens)
{
if (operators.Count == 0 && !(token is Number))
{
operators.Push(token);
continue;
}
if (token is Operation)
{
if (operators.Peek() is Paranthesis)
{
operators.Push(token);
continue;
}
Operation first = (Operation)token;
Operation second = (Operation)operators.Peek();
if (first.Priority > second.Priority)
{
operators.Push(token);
}
else if (first.Priority <= second.Priority)
{
while (operators.Count > 0 && !(token is Paranthesis))
{
rpnOutput.Add(operators.Pop());
}
operators.Push(token);
}
}
else if (token is Paranthesis)
{
if (((Paranthesis)token).isClosing)
{
while (!(operators.Peek() is Paranthesis))
{
rpnOutput.Add(operators.Pop());
}
operators.Pop();
}
else
{
operators.Push(token);
}
}
else if (token is Number)
{
rpnOutput.Add(token);
}
}
while (operators.Count > 0)
{
rpnOutput.Add(operators.Pop());
}
return rpnOutput;
}
static double TheOperation(Operation oper, double number1, double number2)
{
if (oper.Symbol == '+') return number1 + number2;
else if (oper.Symbol == '-') return number2 - number1;
else if (oper.Symbol == '*') return number1 * number2;
else return number2 / number1;
}
public static double Calculate(List<Token> rpnCalc)
{
Stack<double> tempCalc = new Stack<double>();
double result = 0;
for (int i = 0; i < rpnCalc.Count; i++)
{
if (rpnCalc[i] is Number num)
{
tempCalc.Push(num.Symbol);
}
else
{
double number1 = tempCalc.Pop();
double number2 = tempCalc.Pop();
var oper = (Operation)rpnCalc[i];
result = TheOperation(oper,number1, number2);
tempCalc.Push(result);
}
}
return tempCalc.Peek();
}
}
}