This repository has been archived by the owner on Nov 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RPN.cs
250 lines (235 loc) · 9.31 KB
/
RPN.cs
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using System;
using System.Collections.Generic;
using System.Windows;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Calculator
{
class RPN
{
static public double Calculate(string input)
{
try { return double.Parse(GetExpression(input)); }
catch (Exception) { return Counting(GetExpression(input)); }
}
static private string GetExpression(string input)
{
try
{
string output = string.Empty;
string fun = string.Empty;
Stack<char> operStack = new Stack<char>();
char k = ' '; string p = "";
for (int i = 0; i < input.Length; i++)
{
if (IsOperator(input[i]) || Char.IsDigit(input[i]))
{
if (k == ' ')
k = input[i];
else
if (input[i] == '-' && !Char.IsDigit(k))
p += " 0 ";
k = input[i];
}
p += input[i];
}
input = p;
for (int i = 0; i < input.Length; i++)
{
if (IsDelimeter(input[i]))
continue;
if (Char.IsDigit(input[i]))
{
while (!IsDelimeter(input[i]) && !IsOperator(input[i]))
{
output += input[i];
i++;
if (i == input.Length) break;
}
output += " ";
i--;
}
else
if (IsOperator(input[i]))
{
if (input[i] == '(')
operStack.Push(input[i]);
else if (input[i] == ')')
{
char s = operStack.Pop();
while (s != '(')
{
output += s.ToString() + ' ';
s = operStack.Pop();
}
}
else
{
if (operStack.Count > 0)
if (GetPriority(input[i]) <= GetPriority(operStack.Peek()))
output += operStack.Pop().ToString() + " ";
operStack.Push(char.Parse(input[i].ToString()));
}
}
else if (input[i] == '\u03C0')
output += " \u03C0 ";
else if (input[i] == 'e')
output += " e ";
else
{
fun = String.Empty;
while (input[i] != '(')
{
fun += input[i];
i++;
if (i == input.Length) break;
}
i++;
if (IsFunction(fun))
{
String param = String.Empty;
while (input[i] != ')')
{
param += input[i];
i++;
if (i == input.Length) break;
}
double d;
try { d = double.Parse(param); }
catch (Exception) { d = Counting(GetExpression(param)); }
output += doFunc(fun, d);
}
}
}
while (operStack.Count > 0)
output += operStack.Pop() + " ";
return output;
}
catch (Exception) { throw new SyntaxException(); }
}
static private double Counting(string input)
{
try
{
double result = 0;
double b = 0;
Stack<double> temp = new Stack<double>();
try { return double.Parse(input); }
catch (Exception)
{
for (int i = 0; i < input.Length; i++)
{
if (Char.IsDigit(input[i]))
{
string a = string.Empty;
while (!IsDelimeter(input[i]) && !IsOperator(input[i]))
{
a += input[i];
i++;
if (i == input.Length) break;
}
temp.Push(double.Parse(a));
i--;
}
else if (input[i] == '\u03C0')
temp.Push(Math.PI);
else if (input[i] == 'e')
temp.Push(Math.E);
else if (IsOperator(input[i]))
{
double a = temp.Pop();
try
{ b = temp.Pop(); }
catch (Exception) { b = 0; }
switch (input[i])
{
case '!': result = factorial((int)a); break;
case 'P': result = factorial((int)b) / factorial((int)(b - a)); break;
case 'C': result = factorial((int)b) / (factorial((int)a) * factorial((int)(b - a))); break;
case '^': result = Math.Pow(b, a); break;
case '%': result = b % a; break;
case '+': result = b + a; break;
case '-': result = b - a; break;
case '*': result = b * a; break;
case '/': if (a == 0) throw new DividedByZeroException(); else result = b / a; break;
}
temp.Push(result);
}
}
try { return temp.Peek(); }
catch (Exception) { throw new SyntaxException(); }
}
}
catch (Exception)
{
throw new SyntaxException();
}
}
static private bool IsDelimeter(char c)
{
if ((" =".IndexOf(c) != -1))
return true;
return false;
}
static private bool IsOperator(char с)
{
if (("+-/*^()PC!%".IndexOf(с) != -1))
return true;
return false;
}
static private bool IsFunction(String s)
{
String[] func = { "sin", "cos", "tg", "asin", "acos", "atg", "sqrt", "ln", "lg" };
if (Array.Exists(func, e => e == s))
return true;
return false;
}
static private String doFunc(String fun, double param)
{
try
{
switch (fun)
{
case "cos": return Math.Cos(param).ToString();
case "sin": return Math.Sin(param).ToString();
case "tg": if (Math.Abs(param % (2 * Math.PI)) == (Math.PI / 2)) throw new TgException(param); else return Math.Tan(param).ToString();
case "asin": if (param < -1 || param > 1) throw new ArcSinCosException(param); else return Math.Asin(param).ToString();
case "acos": if (param < -1 || param > 1) throw new ArcSinCosException(param); else return Math.Acos(param).ToString();
case "atg": return Math.Atan(param).ToString();
case "sqrt": if (param < 0) throw new SqrtException(param); else return Math.Sqrt(param).ToString();
case "ln": if (param <= 0) throw new LogException(param); else return Math.Log(param).ToString();
case "lg": if (param <= 0) throw new LogException(param); else return Math.Log10(param).ToString();
default: return "";
}
}
catch(Exception){
throw new SyntaxException();
}
}
static private byte GetPriority(char s)
{
switch (s)
{
case '(': return 0;
case ')': return 1;
case '+': return 2;
case '-': return 3;
case '!': return 4;
case '%': return 4;
case '*': return 4;
case '/': return 4;
case '^': return 5;
default: return 4;
}
}
private static int factorial(int x)
{
int i = 1;
for (int s = 1; s <= x; s++)
i = i * s;
if (x < 0) throw new NegativeFactorialException(x);
return i;
}
}
}