1
0
forked from kalmar/DALGLI0

Zadanie 2

Program można uruchomić za pomocą polecenia java Main "6" "[1,2,3,0,1]" "[2,4,0,0,2]"
This commit is contained in:
Cezary Baryłka 2018-07-08 22:24:39 +00:00
parent 084753cc47
commit aea8d85191
4 changed files with 122 additions and 0 deletions

BIN
zad2/Main.class Normal file

Binary file not shown.

34
zad2/Main.java Normal file
View File

@ -0,0 +1,34 @@
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Proszę podać parametry np. java Main \"6\" \"[1,2,3,0,1]\" \"[2,4,0,0,2]\"");
return;
}
int modulo = Integer.parseInt(args[0]);
int[] polyF = stringToArray(args[1]);
int[] polyG = stringToArray(args[2]);
String multiple = Arrays.toString(PolynomialOperations.multiple(polyF, polyG, modulo));
int[] divisionArray = PolynomialOperations.divide(polyF, polyG, modulo);
String division;
if (divisionArray.length == 0)
division = "[0]";
else
division = Arrays.toString(divisionArray);
String nwd = Arrays.toString(PolynomialOperations.NWD(polyF, polyG, modulo));
if (nwd.equals("null"))
nwd = "DivisionError";
String outputString = "[" + multiple + ", " + division + ", " + nwd + "]";
System.out.println(outputString);
}
private static int[] stringToArray(String str) {
return Arrays.stream(str.substring(1, str.length() - 1).split(","))
.map(String::trim).mapToInt(Integer::parseInt).toArray();
}
}

Binary file not shown.

View File

@ -0,0 +1,88 @@
import java.util.Arrays;
public class PolynomialOperations {
public static int[] multiple(int[] f, int[] g, int n) {
int[] result = new int[f.length + g.length - 1];
for (int i = 0; i < f.length; i++)
for (int j = 0; j < g.length; j++)
result[i + j] += f[i] * g[j];
for (int i = 0; i < result.length; i++)
result[i] = result[i] % n;
return result;
}
private static int findFactor(int a, int b, int mod) {
for (int i = 0; i < mod; i++)
if (a == (b * i) % mod)
return i;
return Integer.MIN_VALUE;
}
private static int[] shr(int[] p, int shift) {
if (shift <= 0)
return p;
int[] shiftedPoly = new int[p.length];
for (int i = 0; i < p.length - shift; i++)
shiftedPoly[i] = p[i + shift];
return shiftedPoly;
}
private static void polyMultiply(int[] p, int n) {
Arrays.stream(p).forEach(value -> value = value * n);
}
private static int[] polySubtract(int[] p, int[] s) {
int result[] = Arrays.copyOf(p, p.length);
for (int i = 0; i < p.length; ++i) {
result[i] = p[i] - s[i];
}
return result;
}
private static int[] createNewPoly(int[] p, int size) {
int[] tmpTab = new int[size];
for (int j = 0; j < p.length; j++) {
tmpTab[tmpTab.length - 1 - j] = p[p.length - 1 - j];
}
return tmpTab;
}
private static void applyModuloToTab(int p[], int mod) {
Arrays.stream(p).forEach(value -> value = value % mod);
}
public static int[] divide(int[] n, int[] d, int mod) {
if (n.length < d.length) {
throw new IllegalArgumentException("Numerator and denominator vectors must have the same size");
}
int nd = n.length - 1;
int dd = d.length - 1;
int index = 0;
int[] tmpTab = createNewPoly(d, n.length);
while (nd >= dd) {
int factor = findFactor(n[nd], d[dd], mod);
tmpTab = shr(tmpTab, index);
polyMultiply(tmpTab, factor);
applyModuloToTab(tmpTab, mod);
tmpTab = polySubtract(n, tmpTab);
nd--;
index++;
}
return Arrays.copyOf(n, index - 1);
}
public static int[] NWD(int[] a, int[] b, int mod) {
if (a.length <= 0 || b.length <= 0)
return null;
while (b.length != 0) {
int[] c = divide(a, b, mod);
a = b;
b = c;
}
return a;
}
}