SHARE
TWEET

Untitled

a guest May 21st, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs');
  2.  
  3.  
  4. //console.log(macierz1.toString());
  5. //console.log(macierz2.toString());
  6.  
  7. function convertToArray(string) {
  8.     let data = [];
  9.  
  10.     let lines = string.split("\r\n");
  11.     for (let i = 0; i < lines.length; i++) {
  12.         let line = lines[i].split(" ");
  13.  
  14.         let dataline = [];
  15.         for (let k = 0; k < line.length; k++) {
  16.             dataline[k]=parseInt(line[k]);
  17.         }
  18.         data[i] = dataline;
  19.     }
  20.  
  21.     return data;
  22. }
  23. function printArray(matrix) {
  24.     for (let i = 0; i < matrix.length; i++) {
  25.         for (let k = 0; k < matrix[i].length; k++) {
  26.             let abc = matrix[i][k];
  27.             console.log(abc);
  28.         }
  29.     }
  30. }
  31. function multiply(macierz1, macierz2) {
  32.     if (!Array.isArray(macierz1) || !Array.isArray(macierz2) || !macierz1.length || !macierz2.length) {
  33.         console.log('Argumenty powinny być w formacie dwuwymiarowym');
  34.     }
  35.  
  36.     let x = macierz1.length,
  37.         z = macierz1[0].length,
  38.         y = macierz2[0].length;
  39.  
  40.     if (macierz2.length !== z) {
  41.         console.log('Liczba kolumn w pierwszej macierzy powinna być taka sama jak liczba wierszy w drugiej');
  42.     }
  43.  
  44.     let productRow = Array.apply(null, new Array(y)).map(Number.prototype.valueOf, 0);
  45.     let product = new Array(x);
  46.     for (let p = 0; p < x; p++) {
  47.         product[p] = productRow.slice();
  48.     }
  49.     for (let i = 0; i < x; i++) {
  50.         for (let j = 0; j < y; j++) {
  51.             for (let k = 0; k < z; k++) {
  52.                 product[i][j] += macierz1[i][k] * macierz2[k][j];
  53.             }
  54.         }
  55.     }
  56.  
  57.     return product;
  58.    
  59.    
  60. }
  61.  
  62. function convertToString(matrix) {
  63.     let String = '';
  64.     for (let i = 0; i < matrix.length; i++) {
  65.         for (let k = 0; k < matrix[i].length; k++) {
  66.             let abc = matrix[i][k];
  67.             String += ' ' + abc;
  68.         }
  69.         String += "\n";
  70.     }
  71.     return String;
  72. }
  73.  
  74. let macierz1 = fs.readFileSync(__dirname + "/macierz1.txt");
  75. let macierz2 = fs.readFileSync(__dirname + "/macierz2.txt");
  76.  
  77. //console.log('macierz1:');
  78. let m1 = convertToArray(macierz1.toString());
  79. //printArray(m1);
  80.  
  81. //console.log('macierz2:');
  82. let m2 = convertToArray(macierz2.toString());
  83. //printArray(m2);
  84.  
  85. let mProduct = multiply(m1,m2);
  86. //printArray(mProduct);
  87.  
  88. let macierzPlik = convertToString(mProduct);
  89.  
  90. fs.writeFile("WynikTekstu.txt",macierzPlik,function (error){});
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top