Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. var fileName = "";
  2.  
  3. const fs = require('fs');
  4.  
  5. function readFormula(fileName) {
  6.  
  7. let text = fs.readFileSync(fileName, 'utf8').split("\n")// an array containing lines of text extracted from the file.
  8.  
  9. let qtVariables = 0;
  10. let qtClauses = 0;
  11. let lineOfClauses = "";
  12.  
  13. for (let i = 0; i < text.length; i++) {
  14.  
  15. let firstChar = text[i].charAt(0);
  16.  
  17. if (firstChar == "c"){
  18. //does NOTHING
  19. } else if(firstChar == "p") {
  20. // coloca cada elemento da linha em pos.
  21. let pos = text[i].split(" ");
  22.  
  23. // determina como inteiro a quantidade de variaveis e clausulas.
  24.  
  25. qtVariables = parseInt(pos[2]);
  26. qtClauses = parseInt(pos[3]);
  27.  
  28. } else {
  29. lineOfClauses = lineOfClauses.concat(" ", text[i]);
  30. };
  31.  
  32. let clauseLine = lineOfClauses.split("0");
  33. };
  34.  
  35.  
  36.  
  37. let clauses = readClauses(clauseLine)
  38. let variables = readVariables(clauses)
  39.  
  40. // In the following line, text is passed as an argument so that the function
  41. // is able to extract the problem specification.
  42.  
  43. let specOk = checkProblemSpecification(lineOfClauses, qtClauses, qtVariables)
  44.  
  45. let result = { 'clauses': [], 'variables': [] }
  46. if (specOk) {
  47. result.clauses = clauses
  48. result.variables = variables
  49. }
  50. return result
  51. }
  52.  
  53. function readClauses (clauseLine){
  54.  
  55. let clauses = [];
  56.  
  57. for (let i = 0; i < clauseLine.length; i++) {
  58.  
  59.  
  60. let eachVariable = clauseLine[i].split(" ");
  61.  
  62.  
  63. for (let j = 0; j < eachVariable.length; j++){
  64.  
  65. if (eachVariable[j] != ""){
  66. eachVariable.push(parseInt(eachVariable[j], 10));
  67. };
  68.  
  69. };
  70.  
  71. clauses.push(eachVariable);
  72. };
  73.  
  74. return clauses;
  75.  
  76. }
  77.  
  78. function readVariables(qntVariables){
  79.  
  80. let allZeros = [];
  81.  
  82. for (let i = 0; i < qntVariables; i++){
  83.  
  84. allZeros.push("0");
  85.  
  86. };
  87.  
  88. return allZeros;
  89.  
  90. }
  91.  
  92. function checkProblemSpecification(lineOfClauses, qtClauses, qtVariables) {
  93.  
  94. lineOfClauses = lineOfClauses.replace(/-/g, "");
  95.  
  96. let highest = highestValue(lineOfClauses);
  97.  
  98. let splitClauses = lineOfClauses.split(" 0");
  99.  
  100. splitClauses.pop();
  101.  
  102. let specOk = false;
  103.  
  104. if (qtClauses == splitClauses.length){
  105.  
  106. if (qtVariables == highest){
  107.  
  108. specOk = true;
  109.  
  110. } else {
  111. specOk = false;
  112. };
  113. } else {
  114.  
  115. specOk = false;
  116. };
  117.  
  118. return specOk;
  119.  
  120. }
  121.  
  122. function highestValue(lineOfClauses) {
  123.  
  124. var arr = lineOfClauses.split(" ").map(Number);
  125.  
  126. var largest = arr[0];
  127.  
  128. for (var i = 1; i < arr.length; i++) {
  129. if (arr[i] > largest) {
  130. largest = arr[i];
  131. };
  132. } ;
  133.  
  134. return largest;
  135. }
  136.  
  137. function nextAssignment(variables) {
  138.  
  139. let newVector = variables;
  140.  
  141. if(newVector[newVector.length-1] == 0){
  142.  
  143. newVector[newVector.length-1] = 1;
  144.  
  145.  
  146. } else {
  147.  
  148. let checar;
  149. let contador = 1;
  150. let tempArray[];
  151.  
  152. tempArray = newVector;
  153.  
  154. tempArray.pop();
  155.  
  156. while (checar = false) {
  157.  
  158. if (tempArray[tempArray.length - 1] = 1) {
  159.  
  160. tempArray.pop();
  161. contador++;
  162. } else {
  163. checar = true;
  164. }
  165.  
  166. }
  167.  
  168. newVector = tempArray;
  169. newVector[newVector.length - 1] = 1;
  170.  
  171. while (contador > 0) {
  172. newVector.push("0");
  173. contador--;
  174. };
  175.  
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement