-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathHelper.cs
More file actions
213 lines (183 loc) · 5.14 KB
/
MathHelper.cs
File metadata and controls
213 lines (183 loc) · 5.14 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
/*
* Created by SharpDevelop.
* User: kafeinaltor
* Date: 20.03.2020
* Time: 20:44
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Text.RegularExpressions;
using System.Linq;
namespace StringMath
{
/// <summary>
/// Description of MathHelper.
/// </summary>
public static class MathHelper
{
public static int Compare(this string num1, string num2)
{
string sinum1 = num1.Simplify();
string sinum2 = num2.Simplify();
int s1 = sinum1.Sign();
int s2 = sinum2.Sign();
if (sinum1 == sinum2)
return 0;
if (s1 > s2)
return 1;
if (s1 < s2)
return -1;
if (s1 == 0 && s2 == 0)
return 0;
string usnum1 = sinum1.WoSign();
string usnum2 = sinum2.WoSign();
var arr = usnum1.EqualizeLength(usnum2);
usnum1 = arr[0];
usnum2 = arr[1];
int len1 = usnum1.Length;
bool isgreater = false;
for (int i = 0; i < len1; i++) {
if (usnum1[i] != usnum2[i]) {
isgreater = usnum1[i] > usnum2[i];
break;
}
}
return s1 == 1 ? isgreater ? 1 : -1 : !isgreater ? 1 : -1;
}
public static int GetDecimalLength(this string num)
{
var arr = num.Split('.');
return arr.Length > 1 ? arr[1].Length : 0;
}
public static string WoSign(this string num)
{
num.CheckIfNumber();
const string signPattern = @"^(\+|-)*";
Match m = Regex.Match(num, signPattern);
if (m != null)
return Regex.Replace(num, signPattern, "");
else
return num;
}
public static int Sign(this string num)
{
num.CheckIfNumber();
if (num.Replace("-", "").Replace("+", "").Replace(".", "").All(x => x == '0'))
return 0;
const string signPattern = @"^(\+|-)*";
Match m = Regex.Match(num, signPattern);
if (m != null) {
return m.Value.ToCharArray().Count(x => x == '-') % 2 == 0 ? 1 : -1;
} else
return 1;
}
public static bool IsNumber(this string num)
{
const string numberPattern = @"^(\+|-)*[0-9]+(\.[0-9]+)?$";
return Regex.IsMatch(num, numberPattern);
}
public static string Simplify(this string num)
{
num.CheckIfNumber();
string pattern = @"(?:[1-9][0-9]*)?0?(?:\.[0-9]*[1-9])?";
var mc = Regex.Matches(num, pattern).Cast<Match>();
var selected = mc.First(x=> x.Value.Length == mc.Max(a=>a.Value.Length));
return (num.Sign() > 0 ? "" : "-") + selected;
}
public static void CheckIfNumber(this string num)
{
if (!num.IsNumber())
throw new Exception(num + " is not a valid number");
}
public static string Change(this string num, int i, string newStr)
{
return num.Insert(i, newStr).Remove(i + newStr.Length, 1);
}
public static string[] EqualizeLength(this string num1, string num2)
{
if (num1.Sign() == -1 || num2.Sign() == -1)
throw new Exception();
var arr1 = num1.Split('.');
var arr2 = num2.Split('.');
var intPart1 = arr1[0];
var intPart2 = arr2[0];
var decPart1 = arr1.Length > 1 ? arr1[1] : "";
var decPart2 = arr2.Length > 1 ? arr2[1] : "";
int max = Math.Max(decPart1.Length, decPart2.Length);
int decLen1 = decPart1.Length;
int decLen2 = decPart2.Length;
for (int j = 0; j < max - decLen1; j++) {
decPart1 += "0";
}
for (int j = 0; j < max - decLen2; j++) {
decPart2 += "0";
}
int intLen1 = intPart1.Length;
int intLen2 = intPart2.Length;
max = Math.Max(intLen1, intLen2);
for (int j = 0; j < max - intLen1; j++) {
intPart1 = intPart1.Insert(0, "0");
}
for (int j = 0; j < max - intLen2; j++) {
intPart2 = intPart2.Insert(0, "0");
}
return new string[] {
intPart1 + (decPart1 != "" ? "." + decPart1 : ""),
intPart2 + (decPart2 != "" ? "." + decPart2 : "")
};
}
public static int FindDivider(this string num1, string num2)
{
for (int i = 1; i <= 10; i++) {
string res = num2.Multiply(i.ToString());
int comp = res.Compare(num1);
if (comp == 1)
return i - 1;
if (comp == 0)
return i;
}
throw new Exception("Divider must be in between 0-9");
}
public static string DivSubstring(this string num1, int start, int count)
{
if (start >= num1.Length) {
return "".InsertZerosToEnd(count);
}
if (start + count > num1.Length) {
string zeros = "".InsertZerosToEnd(start + count - num1.Length);
string substr = num1.Substring(start, num1.Length - start);
return substr + zeros;
}
return num1.Substring(start, count);
}
public static string BorrowingFromNeighbour(this string num1, string num2, int a)
{
for (int i = a - 1; i >= 0; i--) {
string t1 = num1[i].ToString();
string t2 = num2[i].ToString();
if (t1 == ".")
continue;
int x = int.Parse(t1);
int y = int.Parse(t2);
if (x > 0)
return num1.Change(i, (x - 1).ToString());
num1 = num1.Change(i, "9");
}
return num1;
}
public static string InsertZerosToEnd(this string num, int numOfZeros)
{
for (int i = 0; i < numOfZeros; i++) {
num += "0";
}
return num;
}
public static string FixNumber(this string woSignNum)
{
if (woSignNum[0] == '.')
woSignNum = woSignNum.Insert(0, "0");
return woSignNum;
}
}
}