From 60b29ca3d886a3a81e36a10761e697980f71f695 Mon Sep 17 00:00:00 2001 From: Kacper Kruczek Date: Sun, 1 Jul 2018 15:27:15 +0200 Subject: [PATCH] 03 mono MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit zeby ograniczyć ilość obliczeń --- 03-rozwiazanie.js | 5 +++-- crc.js | 30 +++++++++++++++--------------- polynomial.js | 9 ++++++++- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/03-rozwiazanie.js b/03-rozwiazanie.js index 26f800f..23fdf1a 100644 --- a/03-rozwiazanie.js +++ b/03-rozwiazanie.js @@ -1,6 +1,7 @@ const crc = require("./crc.js"); -let message = process.argv[3]; -let flag = process.argv[2]; +const message = process.argv[3]; +const flag = process.argv[2]; + switch (flag) { case '-e': console.log(crc.encode(message).encoded); diff --git a/crc.js b/crc.js index d5f2180..75a90ce 100644 --- a/crc.js +++ b/crc.js @@ -1,9 +1,8 @@ -let Polynomial = require('./polynomial.js'); +const Polynomial = require('./polynomial.js'); -let L = new Polynomial.Class(2, new Array(16).fill(1)); -let X = new Polynomial.Class(2, [0, 1]); -let X16 = new Polynomial.Class(2, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]); //jako ze można to od razu wymnożyć -let G = new Polynomial.Class(2, [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1]); //jako ze można to od razu wymnożyć +const L = new Polynomial.Class(2, new Array(16).fill(1)); +const X16 = new Polynomial.Class(2, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]); //jako ze można to od razu wymnożyć +const G = new Polynomial.Class(2, [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1]); //jako ze można to od razu wymnożyć const to_bin = a => { var result = ""; @@ -17,7 +16,7 @@ const to_ascii = a => { a = a.join(''); a = parseInt(a, 2); //nie znalazłem innego sposobu w js na osiągniecie tego efektu.. - // if (a < 126) //ponieważ wieksze liczby nie należą do typowego Ascii + // if (a < 126) //ponieważ wieksze liczby nie należą do typowego Ascii//porzucone ze wzgledu na decode return String.fromCharCode(a); // else { // return "0x" + a.toString(16); @@ -26,6 +25,12 @@ const to_ascii = a => { } +const mod8format = array => { + while (array.length % 8 != 0) { + array.push(0); + } + return array; +} const fcs = m => { let bits = m.map(to_bin); //message in binary @@ -35,7 +40,7 @@ const fcs = m => { fcs = Polynomial.add(fcs, Polynomial.multiply( - Polynomial.power(X, m.length * 8), + Polynomial.Mono(m.length * 8, 1, 2), L ) ) @@ -45,19 +50,14 @@ const fcs = m => { return fcs.coefficients; } -function mod8format(lst) { - while (lst.length % 8 != 0) { - lst.push(0); - } - return lst; -} -function check(m) { + +const check = m => { let bits = m.map(to_bin); //message in binary bits = bits.join('').split('').reverse(); //reverse binary decoded message - let fcs = Polynomial.power(X, bits.length); + let fcs = Polynomial.Mono(bits.length, 1, 2); let C = new Polynomial.Class(2, bits); diff --git a/polynomial.js b/polynomial.js index b7aef67..70626b6 100644 --- a/polynomial.js +++ b/polynomial.js @@ -170,4 +170,11 @@ function gcd(p1, p2) { } -exports.gcd = gcd; \ No newline at end of file +exports.gcd = gcd; + +function Mono(n, c, mod) { + let coef = new Array(n).fill(0); + coef.push(c); + return new Polynomial(mod, coef); +} +exports.Mono = Mono; \ No newline at end of file