using System namespace Test class Calc private string firstNum private

  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
using System;
namespace Test
{
class Calc
{
private string firstNum;
private string secondNum;
private int[] arrFirstNum;
private int[] arrSecondNum;
private int[] result;
private int[,] resArr;
private int maxLenOfNum;
private int minLenOfNum;
public Calc(string fstNum, string scndNum)
{
firstNum = fstNum;
secondNum = scndNum;
arrFirstNum = new int[firstNum.Length];
arrSecondNum = new int[secondNum.Length];
LenCalc();
resArr = new int[minLenOfNum, firstNum.Length + secondNum.Length];
result = new int[firstNum.Length + secondNum.Length];
}
public bool CheckInput()
{
long check;
if ((!long.TryParse(firstNum, out check)) || (check <= 0) || (!long.TryParse(secondNum, out check)) || (check <= 0))
return false;
else
return true;
}
public void Sum()
{
int maxIndResArr = (firstNum.Length + secondNum.Length) - 1;
for (int i = maxIndResArr, k = 1; i >= 0; i--, k++)
{
for (int j = 0; j < minLenOfNum; j++)
result[i] += resArr[j, i];
if (result[i] >= 10)
{
int temp = result[i] % 10;
result[maxIndResArr - k] = result[maxIndResArr - k] + (result[i] / 10);
result[i] = temp;
}
}
}
public void Multiplication()
{
int maxIndResArr = (firstNum.Length + secondNum.Length) - 1;
int remOfDiv = 0;
int intPart = 0;
int lastDigitOfFirstNum = arrFirstNum.Length - 1;
for (int i = 0; i <= minLenOfNum - 1; i++)
{
int lastDigitOfSecondNum = arrSecondNum.Length - 1;
for (int j = maxIndResArr - i; j >= minLenOfNum - i; j--)
{
RemainderForMulti(out remOfDiv, out intPart, lastDigitOfFirstNum, lastDigitOfSecondNum);
resArr[i, j] += remOfDiv;
resArr[i, j - 1] += intPart;
remOfDiv = 0;
intPart = 0;
if (resArr[i, j] >= 10)
{
resArr[i, j - 1] += resArr[i, j] / 10;
resArr[i, j] %= 10;
}
lastDigitOfSecondNum--;
}
lastDigitOfFirstNum--;
}
}
public void RemainderForMulti(out int remOfDiv, out int intPart, int i, int j)
{
int temp = arrFirstNum[i] * arrSecondNum[j];
if (temp >= 10)
{
remOfDiv = temp % 10;
intPart = temp / 10;
}
else
{
remOfDiv = temp;
intPart = 0;
}
}
public void FillArrs()
{
if (long.Parse(firstNum) <= long.Parse(secondNum))
{
arrFirstNum = SplitByDigits(firstNum);
arrSecondNum = SplitByDigits(secondNum);
}
else
{
arrFirstNum = SplitByDigits(secondNum);
arrSecondNum = SplitByDigits(firstNum);
}
}
public void LenCalc()
{
if (firstNum.Length >= secondNum.Length)
{
maxLenOfNum = firstNum.Length;
minLenOfNum = secondNum.Length;
}
else
{
maxLenOfNum = secondNum.Length;
minLenOfNum = firstNum.Length;
}
}
public int[] SplitByDigits(string str)
{
int[] arr = new int[str.Length];
long num = long.Parse(str);
int i = str.Length - 1;
while (num != 0)
{
arr[i] = (int)num % 10;
num /= 10;
i--;
}
return arr;
}
public string Print2DArray()
{
int N = firstNum.Length + secondNum.Length;
string print = "";
for (int i = 0; i < minLenOfNum; i++)
{
for (int j = 0; j < N - i; j++)
print += resArr[i, j] + " ";
print += Environment.NewLine;
}
return print;
}
public string PrintArray()
{
string print = "";
foreach (int item in result)
print += item + " ";
return print;
}
}
}