-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculatorLogic.cs
277 lines (273 loc) · 8.61 KB
/
CalculatorLogic.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
using Calculator.Exceptions;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Calculator
{
enum Operands
{
Addition = '+',
Percent = '%',
Multiply = '*',
Div = '/',
Substract = '-',
Sqrt = '√',
Squaring = '^'
}
internal class CalculatorLogic
{
public CalculatorLogic()
{
}
private const byte _maxLengthCurrentNumber = 49;
private bool _displayCurrentNumber_readOnly = false;
private bool _abilityChangeOperand = false;
private string _currentNumber = "0";
private string _secondNumber = "0";
private string _result = null;
private bool _firstNullSym = false;
private string _memory;
private bool _memoryIsSet = false;
private bool _currentNumberIsSet = false;
private Operands _currentOperand;
public bool DisplayCurrentNumber_readOnly
{
get { return _displayCurrentNumber_readOnly; }
set { _displayCurrentNumber_readOnly = value; }
}
public bool AbilityChangeOperand
{
get { return _abilityChangeOperand; }
set { _abilityChangeOperand = value; }
}
public bool CurrentNumberIsSet
{
get { return _currentNumberIsSet; }
set { _currentNumberIsSet = value; }
}
public string Memory
{
get { return _memory; }
set { _memory = value; }
}
public bool MemoryIsSet
{
get { return _memoryIsSet; }
set { _memoryIsSet = value; }
}
public Operands CurrentOperand
{
get { return _currentOperand; }
set { _currentOperand = value; }
}
public bool OperandPerformed { get; set; }
public string Result
{
get { return _result; }
set { _result = value; }
}
public bool FirstNullSym
{
get { return _firstNullSym; }
set { _firstNullSym = value; }
}
public string CurrentNumber
{
get { return _currentNumber; }
set
{
if (value.Length > _maxLengthCurrentNumber) return;
if (value == "")
{
_currentNumber = "0";
}
else if (IsValidNumber(value))
{
_currentNumber = value;
}
}
}
public string SecondNumber
{
get { return _secondNumber; }
set
{
_secondNumber = value;
}
}
public string Del_CurrentNumber()
{
if (_currentNumber.Length == 1)
{
return "0";
}
else if (_currentNumber.Length > 1)
{
_currentNumber = _currentNumber.Remove(_currentNumber.Length - 1, 1);
}
return _currentNumber;
}
public static bool IsValidNumber(string value)
{
return (String.IsNullOrEmpty(value) || (value.FirstOrDefault() == '-' && value.Length == 1) || Double.TryParse(value, out _));
}
private static double DoubleParse(string value)
{
return Double.Parse(value);
}
private static string DoubleToString(double value)
{
return value.ToString();
}
public string CompilationResutString()
{
string curNum = CurrentNumber;
string secNum = SecondNumber;
string res = Result;
Operands operand = CurrentOperand;
string ResultString = secNum + ' ' + (char)operand + ' ';
if (curNum.Length > 0 && curNum.First() == '-')
{
ResultString += $"({curNum})";
}
else
{
ResultString += curNum;
}
ResultString += '=';
return ResultString;
}
public static string NormalizeNumber(string value)
{
if (value.Last() == ',')
{
value = value.Remove(value.Length - 1, 1);
}
if (value.First() == '-')
{
bool compilanceCondition = false;
for (int i = 1; i < value.Length; i++)
{
if (value[i] == '0' || value[i] == ',')
{
}
else
{
compilanceCondition = true;
break;
}
}
if (compilanceCondition == false)
{
value = "0";
}
}
return value;
}
private void CheckCalculateBinaryOperandException(double value)
{
if (Double.IsInfinity(value))
{
CurrentNumber = "";
throw new CalculatorDoubleInfinityException();
}
}
public string CalculateBinaryOperand()
{
if (!OperandPerformed || SecondNumber.Length <= 0)
{
return null;
}
if (String.IsNullOrEmpty(CurrentNumber))
{
CurrentNumber = SecondNumber;
}
double DSecondNumber = 0;
double DCurrentNumber = 0;
try
{
DSecondNumber = DoubleParse(SecondNumber);
DCurrentNumber = DoubleParse(CurrentNumber);
}
catch
{
throw new CalculatorDoubleParseException();
}
double resultOpertion = 0;
switch (CurrentOperand)
{
case Operands.Addition:
resultOpertion = DSecondNumber + DCurrentNumber;
CheckCalculateBinaryOperandException(resultOpertion);
break;
case Operands.Substract:
resultOpertion = DSecondNumber - DCurrentNumber;
CheckCalculateBinaryOperandException(resultOpertion);
break;
case Operands.Div:
if (DCurrentNumber == 0)
{
CurrentNumber = "";
throw new CalculatorZeroDivideException();
}
resultOpertion = DSecondNumber / DCurrentNumber;
CheckCalculateBinaryOperandException(resultOpertion);
break;
case Operands.Multiply:
resultOpertion = DSecondNumber * DCurrentNumber;
CheckCalculateBinaryOperandException(resultOpertion);
break;
default:
return null;
}
Result = DoubleToString(resultOpertion);
OperandPerformed = false;
return Result;
}
private void CheckCalculateUnaryOperandException(double value)
{
if (Double.IsInfinity(value))
{
throw new CalculatorDoubleInfinityException();
}
}
public string CalculateUnaryOperand()
{
if (CurrentNumber.Length == 0) return null;
double resultOperation;
double DCurrentNumber;
try
{
DCurrentNumber = Double.Parse(CurrentNumber);
}
catch
{
throw new CalculatorDoubleParseException();
}
switch (CurrentOperand)
{
case Operands.Sqrt:
resultOperation = Math.Sqrt(DCurrentNumber);
CheckCalculateUnaryOperandException(resultOperation);
if (DCurrentNumber.CompareTo(0) < 0)
{
throw new CalculatorNegativeRootException();
}
break;
case Operands.Squaring:
resultOperation = Math.Pow(DCurrentNumber, 2);
CheckCalculateUnaryOperandException(resultOperation);
break;
default:
return null;
}
OperandPerformed = false;
Result = DoubleToString(resultOperation);
return Result;
}
}
}