ThirdLab

  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
using System;
using System.Collections.Generic;
namespace Labs
{
class ThirdLab
{
private static string firstNum = Console.ReadLine();
private static string secondNum = Console.ReadLine();
private int[] arrFirstNum = new int[firstNum.Length];
private int[] arrSecondNum = new int[secondNum.Length];
private int[] result = new int[firstNum.Length + secondNum.Length];
private int maxLenOfNum;
private int minLenOfNum;
public ThirdLab()
{
if (!ValidInput())
{
Console.WriteLine("\nInvalid input.");
goto End;
}
LenCalc();
FillArrs();
int[,] resArr = new int[minLenOfNum, firstNum.Length + secondNum.Length];
Multiplication(resArr);
Sum(resArr);
Print2DArray(resArr);
Console.WriteLine();
PrintArray(result);
End:
Console.ReadKey();
}
private bool ValidInput()
{
if ((long.Parse(firstNum) < 1) || (long.Parse(secondNum) < 1))
return false;
else
return true;
}
private void Sum(int[,] resArr)
{
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;
}
}
}
private void Multiplication(int[,] resArr)
{
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--;
}
}
private 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;
}
}
private void FillArrs()
{
if (long.Parse(firstNum) <= long.Parse(secondNum))
{
arrFirstNum = SplitByDigits(firstNum);
arrSecondNum = SplitByDigits(secondNum);
}
else
{
arrFirstNum = SplitByDigits(secondNum);
arrSecondNum = SplitByDigits(firstNum);
}
}
private void LenCalc()
{
if (firstNum.Length >= secondNum.Length)
{
maxLenOfNum = firstNum.Length;
minLenOfNum = secondNum.Length;
}
else
{
maxLenOfNum = secondNum.Length;
minLenOfNum = firstNum.Length;
}
}
private 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;
}
private void Print2DArray(int[,] arr)
{
int N = firstNum.Length + secondNum.Length;
Console.WriteLine();
for (int i = 0; i < minLenOfNum; i++)
{
for (int j = 0; j < N - i; j++)
Console.Write(arr[i, j] + " ");
Console.WriteLine();
}
}
private void PrintArray(int[] arr)
{
foreach (int item in arr)
Console.Write(item + " ");
}
}
}