CRC #30

Closed
s426110 wants to merge 12 commits from s426110/DALGLI0:CRC into master
6 changed files with 256 additions and 40 deletions

View File

@ -1,21 +0,0 @@
## Zadanie
Napisać algorytm, który dla danego `n ∈ ` znajdzie wszystkie:
1. elementy odwracalne
2. dzielniki zera
3. elementy nilpotentne
4. elementy idempotentne
w pierścieniu `{/n, +, ⋅}`.
Termin: 31.05
### Przykłady:
> Input: `4`
> Output: `[[1,3], [0,2], [0,2], [0,1]]`
> Input: `6`
> Output: `[[1,5], [0,2,3,4], [0], [0,1,3,4]]`

View File

@ -1,19 +0,0 @@
## Zadanie
Napisać program, który dla danego pierścienia współczynników `R = /n, n ∈ ` oraz wielomianów `f,g ∈ R[x] ` zmiennej `x ` znajdzie:
1. iloczyn `f⋅g ∈ R[x]`
2. klasę reszty `f ∈ R[x]/(g)`
3. największy wspólny dzielnik `nwd(f,g)` korzystając z algorytmu Euklidesa.
**Uwaga**: wielomiany są podawane jako ciąg współczynników **od wyrazu wolnego, do współczynnika wiodącego**.
Termin: 07.06
### Przykłady:
> Input: `2, [1,1,1,0,1], [0,1,1]` (i.e. `f = 1 + x + x² + x⁴, g = x² + x`)
> Output: `[[0,1,0,0,1,1,1], [1,1], [1,1]]`
> Input: `6, [2,1,0,2,1,3], [1,0,0,5]`
> Output: `[[3,1,0,5,0,1,4,5,5], [5,2,1], DivisionError]`

16
CRC.java Normal file
View File

@ -0,0 +1,16 @@
public class CRC {
public static void main(String[] args) {
try {
if (args.length == 1){
Encoding tmp = new Encoding(args[0]);
System.out.println(args[0] + tmp.getFcsChars());
} else if (args.length == 3){
String tmp = args[0] + Character.toString((char) Integer.parseInt(args[1])) + Character.toString((char) Integer.parseInt(args[2]));
Decoding d = new Decoding(tmp);
System.out.println(d.getCheck());
}
} catch (ArrayIndexOutOfBoundsException e){
System.out.println("Za mało argumentów");
}
}
}

54
Decoding.java Normal file
View File

@ -0,0 +1,54 @@
import java.util.ArrayList;
import java.util.Collections;
public class Decoding {
private Boolean check;
public Decoding(String arg){
Polynomials poly = new Polynomials(2);
ArrayList<Byte> message = new ArrayList<>();
ArrayList<Integer> g = new ArrayList<>();
ArrayList<Integer> l = new ArrayList<>();
ArrayList<Integer> tmp = new ArrayList<>();
ArrayList<Integer> tmp2 = new ArrayList<>();
for (int i = 0; i < arg.length(); ++i){
message.add((byte)arg.charAt(i));
}
for (int i = 0; i < 16; ++i){
if (i == 12 || i == 5 || i==0){
g.add(1);
} else{
g.add(0);
}
l.add(1);
}
g.add(1);
for (int i = 0; i < 16 ; ++i){
tmp.add(0);
}
for (byte b : message){
String s2 = String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0');
for (int i = 0; i < 8; ++i){
tmp2.add(Character.getNumericValue(s2.charAt(i)));
}
}
Collections.reverse(tmp2);
tmp.addAll(tmp2);
tmp2.clear();
for (int i = 0; i < arg.length() * 8; ++i) tmp2.add(0);
for (int i = 0; i < 16; ++i) tmp2.add(1);
tmp = poly.divModN(poly.add(tmp, tmp2),g);
if (tmp.size() == 0) {
check = true;
} else
check = false;
}
public Boolean getCheck() {
return check;
}
}

65
Encoding.java Normal file
View File

@ -0,0 +1,65 @@
import java.util.ArrayList;
import java.util.Collections;
public class Encoding {
private ArrayList<Integer> fcs;
public Encoding(String args) {
ArrayList<Byte> message = new ArrayList<>();
ArrayList<Integer> g = new ArrayList<>();
fcs = new ArrayList<>();
Polynomials poly = new Polynomials(2);
for (int i = 0; i < args.length(); ++i){
message.add((byte)args.charAt(i));
}
for (int i = 0; i < 16; ++i){
if (i == 12 || i == 5 || i==0){
g.add(1);
} else{
g.add(0);
}
}
g.add(1);
ArrayList<Integer> tmp = new ArrayList<>();
ArrayList<Integer> tmp2 = new ArrayList<>();
for (int i = 0; i < 16 ; ++i){
tmp.add(0);
}
for (byte b : message){
String s2 = String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0');
for (int i = 0; i < 8; ++i){
tmp2.add(Character.getNumericValue(s2.charAt(i)));
}
}
Collections.reverse(tmp2);
tmp.addAll(tmp2);
tmp2.clear();
for (int i = 0; i < args.length() * 8; ++i) tmp2.add(0);
for (int i = 0; i < 16; ++i) tmp2.add(1);
fcs = poly.divModN(poly.add(tmp, tmp2), g);
while(fcs.size() < 16) fcs.add(0);
Collections.reverse(fcs);
}
public ArrayList<Integer> getFcs() {
return fcs;
}
public String getFcsChars(){
int a = 0;
int b = 0;
for (int i = 0; i < 8; ++i){
if (fcs.get(i) == 1){
a += Math.pow(2, 7-i);
}
if (fcs.get(i + 8) == 1){
b += Math.pow(2, 7-i);
}
}
return " " +(byte)a +" "+ (byte)b;
}
}

121
Polynomials.java Normal file
View File

@ -0,0 +1,121 @@
import java.util.ArrayList;
public class Polynomials {
int n;
public Polynomials(int n) {
this.n = n;
}
private ArrayList<Integer> normalize(ArrayList<Integer> w){
for (int i = w.size()-1; i >= 0; --i){
if (w.get(i).equals(0)) {
w.remove(i);
}else {
return w;
}
}
return w;
}
public ArrayList<Integer> multiply(ArrayList<Integer> f, ArrayList<Integer> g){
ArrayList<Integer> result = new ArrayList<>();
int tmp;
for (int i = 0; i < f.size(); ++i){
for (int j = 0; j < g.size(); ++j){
tmp = f.get(i) * g.get(j);
if (i + j < result.size()){
tmp += result.get(i+j);
result.set(i + j, tmp % n);
} else {
while ((i + j) >= (result.size())){
result.add(0);
}
result.set(i + j, tmp % n);
}
}
}
return (result);
}
private int getZero(int a, int b) throws ArithmeticException{
for (int i = 0; i <= n; ++i){
if ((a + (-(b * i))) == 0) return i;
}
throw new ArithmeticException("not reversible");
}
public ArrayList<Integer> multiplyByConst(ArrayList<Integer> f, Integer a){
ArrayList<Integer> result = new ArrayList<>();
for (Integer i : f){
result.add((i * a) % n);
}
return result;
}
public ArrayList<Integer> subtract(ArrayList<Integer> f, ArrayList<Integer> g){
ArrayList<Integer> result = new ArrayList<>();
for (int i = 0; i < f.size(); ++i){
if (i < g.size()){
result.add(Math.floorMod(f.get(i) - g.get(i), n));
} else{
result.add(f.get(i));
}
}
return result;
}
public ArrayList<Integer> add(ArrayList<Integer> f, ArrayList<Integer> g){
ArrayList<Integer> result = new ArrayList<>();
if (f.size() >= g.size()) {
for (int i = 0; i < f.size(); ++i) {
if (i < g.size()) {
result.add(Math.floorMod(f.get(i) + g.get(i), n));
} else {
result.add(f.get(i));
}
}
} else{
for (int i = 0; i < g.size(); ++i) {
if (i < f.size()) {
result.add(Math.floorMod(f.get(i) + g.get(i), n));
} else {
result.add(g.get(i));
}
}
}
return result;
}
public ArrayList<Integer> divModN(ArrayList<Integer> f, ArrayList<Integer> g) throws ArithmeticException{
ArrayList<Integer> quotient = new ArrayList<>();
ArrayList<Integer> remainder = new ArrayList<>(f);
ArrayList<Integer> tmpList;
int a = f.size() - 1;
int b = g.size() - 1;
for (int i = 0; i < a - b + 1; ++i){
quotient.add(0);
}
int q = quotient.size()-1;
int tmp;
while (a >= b) {
tmp = getZero(remainder.get(a), g.get(b));
quotient.set(q--, tmp);
tmpList = multiplyByConst(g, tmp);
while(tmpList.size() <= a) tmpList.add(0,0);
remainder = subtract(remainder, tmpList);
--a;
}
return normalize(remainder);
}
public ArrayList<Integer> gcc (ArrayList<Integer> f, ArrayList<Integer> g){
if (g.size() == 0) return f;
if (f.size() > g.size()){
return gcc(g, divModN(f, g));
} else return gcc(g, divModN(g, f));
}
}