-
Notifications
You must be signed in to change notification settings - Fork 0
/
computor.js
executable file
·64 lines (59 loc) · 1.78 KB
/
computor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const Equation = require('./equation_class')
const readInput = (str) => {
let x_reg = /(= )?([\+-] )?((\d+.)?\d+) \* X\^(\d+)/ig;
let hallado;
let equ = new Equation();
let eq = 0;
while ((hallado = x_reg.exec(str)) !== null) {
let degree = parseInt(hallado[5]);
let coef = parseFloat(hallado[3]);
if (hallado[2] === '- ')
coef *= -1;
if (hallado[1] === '= ' || eq === 1)
{
coef *= -1;
eq = 1;
}
equ.addMonomial(coef, degree);
}
return equ;
};
const printSolution = (equation) =>
{
if (equation.degree === 0)
{
if (equation.solution === true)
console.log("There are infinite number of solutions, each real number is a solution.")
else
console.log("There is no solution.")
}
else if (equation.degree === 1)
console.log("The solution is:\n" + equation.solution);
else if (equation.degree === 2 && equation.discriminant)
console.log(equation.solution[0] + '\n' + equation.solution[1]);
else if (equation.degree === 2 && equation.discriminant === 0)
console.log(equation.solution === -0 ? 0 : equation.solution);
}
const discriminantInfo = (equation) =>
{
if (equation.degree != 2)
return;
if (equation.discriminant === 0)
console.log("Discriminant is zero, the solution is:");
else if (equation.discriminant > 0)
console.log("Discriminant is strictly positive, the two solutions are:");
else
console.log("Discriminant is strictly negative, this equation has two complex solutions:");
}
if (process.argv.length != 3) // process.argv es el array de los argv
return console.log("Invalid arguments");
let equ = readInput(process.argv[2]);
equ.print();
console.log("Polynomial degree: " + equ.degree);
if (equ.degree > 2)
{
console.log("The polynomial degree is strictly greater than 2, I can't solve.");
return;
}
discriminantInfo(equ);
printSolution(equ);