-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoduleII_formuleBienFormee.js
152 lines (127 loc) · 4.49 KB
/
moduleII_formuleBienFormee.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const readline = require( 'readline' ).createInterface(process.stdin , process.stdout) ;
function verifierOpBin(expr) {
let elementPb = []; //element de l'expression ou se situe le problème
var length = expr.length;
let OpBinO = ['/','*','-','+','(','=']
let OpBinF = ['/','*','+',')','=']
let OpBin = ['/','*','+','-','=']
var taille = OpBinO.length;
for (var j = 0; j < length; j++) {
for (var i = 0; i < taille; i++) {
if ( expr[j] == ')' && expr[j-1] == OpBinO[i] ) {
elementPb.push(expr[j-1]);
}else if (expr[j] == '(' && expr[j+1] == OpBinF[i]){
elementPb.push(expr[j+1]);
}else if (expr[j] == OpBinO[i] && expr[j+1] == OpBinF[i]){
elementPb.push(expr[j+1]);
}else if (expr[j] == OpBinO[i] && expr[j-1] == OpBin[i]){
elementPb.push(expr[j-1]);
}else if (expr[j] == ')' && expr[j+1] == OpBin[i] && j+1 == expr.length-1){
elementPb.push(expr[j+1]);
}else if (expr[j] == '=' && expr[j+1] == OpBinF[let OpBinF = ['/','*','+',')','='] OpBinF = ['/','*','+',')','=']i]){
elementPb.push(expr[j+1]);
}else if (j == expr.length-1 && expr[j] == OpBin[i]){
elementPb.push(expr[j]);
}
}
}
return elementPb;
}
function verifierPr(expr) {
let T = []
let R = []
var length = expr.length;
for (var i = 0; i < length; i++) {
if ( expr[i] == '('){
T.push(expr[i])
}
}
for (var j = 0; j < length; j++) {
if ( expr[j] == ')' && T.length > 0){
T.shift();
}
else if (expr[j] == ')' && T.length === 0) {
R.push(expr[j])
}
}
let totaLength = T.length + R.length
if( totaLength === 0){
return T;
}else if (T.length === 0) {
return R;
}else if (R.length === 0){
return T;
}
}
readline.question("Quelle est votre expression?", function (str = '') {
let strg = str.toString().replace( / /g, "" );
var rgx = "/[^a-z]+/gi"; // j'arrive pas à gerer le regex
let text = ['Il y a un problème au niveau de :'];
if (!strg.match(rgx)) {
let list = strg.split('');
let vPr = verifierPr(list);
let vOpBin = verifierOpBin(list);
if (vPr.length === 0 && vOpBin.length === 0) {
console.log('Oh! il n\'ya pas de problème! Bravo!');
}else if (vPr.length === 0) {
console.log(text.toString(),vOpBin.toString());
}else if (vOpBin.length === 0 ){
console.log(text.toString(), vPr.toString());
}else if (vPr.length === 1 && vOpBin.length === 1){
console.log(text.toString(), vPr.toString() ,"et",vOpBin.toString());
}else {
console.log(text.toString(),vPr.toString(),vOpBin.toString());
}
}
else {
console.log("expression non gérée");
}
readline.close();
});
T.push(expr[i])
}
}
for (var j = 0; j < length; j++) {
if ( expr[j] == ')' && T.length > 0){
T.shift();
}
else if (expr[j] == ')' && T.length === 0) {
R.push(expr[j])
}
}
let totaLength = T.length + R.length
if( totaLength === 0){
return T;
}else if (T.length === 0) {
return R;
}else if (R.length === 0){
return T;
}
}
readline.question("Quelle est votre expression?", function (str = '') {
let strg = str.toString().replace( / /g, "" );
var rgx = "/[^a-z]+/gi"; // j'arrive pas à gerer le regex
let text = ['Il y a un problème au niveau de :'];
if (!strg.match(rgx)) {
let list = strg.split('');
let vPr = verifierPr(list);
let vOpBin = verifierOpBin(list);
if (vPr.length === 0 && vOpBin.length === 0) {
console.log('Oh! il n\'ya pas de problème! Bravo!');
}else if (vPr.length === 0) {
console.log(text.toString(),vOpBin.toString());
}else if (vOpBin.length === 0 ){
console.log(text.toString(), vPr.toString());
}else if (vPr.length === 1 && vOpBin.length === 1){
console.log(text.toString(), vPr.toString() ,"et",vOpBin.toString());
}else {
console.log(text.toString(),vPr.toString(),vOpBin.toString());
}
}
else {
console.log("expression non gérée");
}
readline.close();
});