using System namespace CalcLastVersion class BigNumber private int arr

  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
using System;
namespace CalcLastVersion
{
class BigNumber
{
private int[] arrIntNum;
private double[] arrDoubleNum;
private string num;
public BigNumber()
{
}
public BigNumber(string num)
{
this.num = num;
arrIntNum = new int[this.num.Length];
arrDoubleNum = new double[this.num.Length];
}
public static BigNumber operator *(BigNumber firstNum, BigNumber secondNum)
{
BigNumber result = new BigNumber();
firstNum.IntParse();
secondNum.IntParse();
result.arrIntNum = result.SolutionForMulti(firstNum, secondNum);
return result;
}
public static string operator +(BigNumber firstNum, int secondNum)
{
int[] result = new int[firstNum.arrIntNum.Length + 1];
string print = "";
for(int i = firstNum.arrIntNum.Length - 1; i >= 0; i--)
{
result[i + 1] = firstNum.arrIntNum[i];
}
result[result.Length - 1] += secondNum;
for (int i = result.Length - 1; i >= 0; i-- )
{
if (result[i] >= 10)
{
int temp = result[i];
result[i] %= 10;
result[i - 1] += temp / 10;
}
}
foreach (int item in result)
print += item + " ";
return print;
}
public static string operator /(BigNumber firstNum, double secondNum)
{
firstNum.DoubleParse();
return "";
}
private void IntParse()
{
long num;
if ((!long.TryParse(this.num, out num)) || (num <= 0))
throw new Exception("Bad input");
else
{
num = long.Parse(this.num);
int i = this.num.Length - 1;
while (num != 0)
{
arrIntNum[i] = (int)num % 10;
num /= 10;
i--;
}
}
}
private void DoubleParse()
{
double num;
if ((!double.TryParse(this.num, out num)) || (num <= 0))
throw new Exception("Bad input");
else
{
num = double.Parse(this.num);
int i = this.num.Length - 1;
while (num != 0)
{
arrDoubleNum[i] = num % 10;
num /= 10;
i--;
}
}
}
private int[] OneDigitMulti(BigNumber firstNum, BigNumber secondNum, int j, int cnt)
{
int resLen = firstNum.arrIntNum.Length + secondNum.arrIntNum.Length - 1;
int[] result = new int[firstNum.arrIntNum.Length + secondNum.arrIntNum.Length];
for (int i = firstNum.arrIntNum.Length - 1, k = 0; i >= 0; i--, k++)
{
int remOfDiv = 0;
int intPart = 0;
int temp = 0;
if ((secondNum.arrIntNum[j] * firstNum.arrIntNum[i]) > 9)
{
temp = secondNum.arrIntNum[j] * firstNum.arrIntNum[i];
remOfDiv = temp % 10;
intPart = temp / 10;
}
else
{
temp = secondNum.arrIntNum[j] * firstNum.arrIntNum[i];
remOfDiv = temp;
intPart = 0;
}
result[resLen - k - cnt] += remOfDiv;
result[resLen - k - (1 + cnt)] += intPart;
if (result[resLen - k - cnt] > 9)
{
result[resLen - k - (1 + cnt)] += result[resLen - k - cnt] / 10;
result[resLen - k - cnt] %= 10;
}
}
return result;
}
private int[] SumArrays(int[] firstArr, int[] secondArr)
{
int[] result = new int[firstArr.Length];
for (int i = result.Length - 1; i >= 0; i--)
{
result[i] += firstArr[i] + secondArr[i];
if (result[i] >= 10)
{
int temp = result[i] % 10;
result[i - 1] = result[i - 1] + (result[i] / 10);
result[i] = temp;
}
}
return result;
}
private int[] SolutionForMulti(BigNumber firstNum, BigNumber secondNum)
{
int cnt = 0;
int[] resSum = new int[firstNum.arrIntNum.Length + secondNum.arrIntNum.Length];
int[] resVal = new int[firstNum.arrIntNum.Length + secondNum.arrIntNum.Length];
for (int i = secondNum.arrIntNum.Length - 1; i >= 0; i--)
{
resSum = SumArrays(resSum, resVal);
resVal = OneDigitMulti(firstNum, secondNum, i, cnt);
cnt++;
}
resSum = SumArrays(resSum, resVal);
return resSum;
}
private string Division(BigNumber firstNum, double secondNum)
{
string result = "";
int[] temp = new int[firstNum.arrDoubleNum.Length];
for (int i = 0; i < firstNum.arrDoubleNum.Length; i++)
{
if((firstNum.arrDoubleNum[i] / secondNum) != 0)
{
}
}
return result;
}
}
}