Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
3
3IF2235
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ettayeb Yassine
3IF2235
Commits
37f3c71e
Commit
37f3c71e
authored
3 weeks ago
by
Ettayeb Yassine
Browse files
Options
Downloads
Patches
Plain Diff
use of valueOf() and isNaN()
parent
e7a7c76d
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
main.js
+64
-25
64 additions, 25 deletions
main.js
with
64 additions
and
25 deletions
main.js
+
64
−
25
View file @
37f3c71e
// comptage d'instances
let
instancesCreated
=
0
;
let
instancesDestroyed
=
0
;
// classe de base pour toutes les expressions
class
Expression
{
constructor
()
{
instancesCreated
++
;
}
// méthode d'affichage
toString
()
{
return
""
;
}
// méthode de dérivation
derive
(
variableName
)
{
return
new
Nombre
(
0
);
}
// méthode de simplification
simplify
()
{
return
this
;
}
valueOf
()
{
return
NaN
;
}
destroy
()
{
instancesDestroyed
++
;
}
}
// classe représentant un nombre constant
class
Nombre
extends
Expression
{
constructor
(
value
)
{
super
();
...
...
@@ -39,18 +39,20 @@ class Nombre extends Expression {
return
this
.
value
.
toString
();
}
// dérivée d'un n
b cst =
0
// dérivée d'un n
ombre constant est toujours
0
derive
(
variableName
)
{
return
new
Nombre
(
0
);
}
// nb est déjà simplifié
simplify
()
{
return
this
;
}
valueOf
()
{
return
this
.
value
;
}
}
// classe représentant une variable
class
Variable
extends
Expression
{
constructor
(
name
)
{
super
();
...
...
@@ -61,7 +63,7 @@ class Variable extends Expression {
return
this
.
name
;
}
// dérivée d'une var est 1 si c'est la variable par rapport à laquelle
//
la
dérivée d'une var
iable
est 1 si c'est la variable par rapport à laquelle
// on dérive, 0 sinon
derive
(
variableName
)
{
return
this
.
name
===
variableName
?
new
Nombre
(
1
)
:
new
Nombre
(
0
);
...
...
@@ -70,9 +72,12 @@ class Variable extends Expression {
simplify
()
{
return
this
;
}
valueOf
()
{
return
NaN
;
}
}
// classe de base pour toutes les opérations
class
Operation
extends
Expression
{
constructor
(
leftOperand
,
rightOperand
)
{
super
();
...
...
@@ -98,27 +103,37 @@ class Addition extends Operation {
);
}
// simplification addition
simplify
()
{
const
leftSimplified
=
this
.
leftOperand
.
simplify
();
const
rightSimplified
=
this
.
rightOperand
.
simplify
();
// si les deux opérandes
sont des nombres, on peut
calcule
r
le résultat
if
(
leftSimplified
instanceof
Nombre
&&
rightSimplified
instanceof
Nombre
)
{
return
new
Nombre
(
leftSimplified
.
value
+
rightSimplified
.
value
);
// si les deux opérandes
peuvent être évalués numériquement, on
calcule le résultat
if
(
!
isNaN
(
leftSimplified
.
valueOf
())
&&
!
isNaN
(
rightSimplified
.
valueOf
())
)
{
return
new
Nombre
(
leftSimplified
.
value
Of
()
+
rightSimplified
.
value
Of
()
);
}
// si l'un des opérandes est 0, on retourne l'autre
if
(
leftSimplified
instanceof
Nombre
&&
leftSimplified
.
value
===
0
)
{
if
(
!
isNaN
(
leftSimplified
.
valueOf
())
&&
leftSimplified
.
value
Of
()
===
0
)
{
return
rightSimplified
;
}
if
(
rightSimplified
instanceof
Nombre
&&
rightSimplified
.
value
===
0
)
{
if
(
!
isNaN
(
rightSimplified
.
valueOf
())
&&
rightSimplified
.
value
Of
()
===
0
)
{
return
leftSimplified
;
}
// sinon, on crée une nouvelle addition avec les opérandes simplifiés
return
new
Addition
(
leftSimplified
,
rightSimplified
);
}
// une addition peut être évaluée si ses deux opérandes peuvent l'être
valueOf
()
{
const
leftValue
=
this
.
leftOperand
.
valueOf
();
const
rightValue
=
this
.
rightOperand
.
valueOf
();
if
(
isNaN
(
leftValue
)
||
isNaN
(
rightValue
))
{
return
NaN
;
}
return
leftValue
+
rightValue
;
}
}
class
Multiplication
extends
Operation
{
...
...
@@ -141,34 +156,49 @@ class Multiplication extends Operation {
);
}
// simplification d'une multiplication
simplify
()
{
const
leftSimplified
=
this
.
leftOperand
.
simplify
();
const
rightSimplified
=
this
.
rightOperand
.
simplify
();
// si les deux opérandes
sont des nombres, on peut
calcule
r
le résultat
if
(
leftSimplified
instanceof
Nombre
&&
rightSimplified
instanceof
Nombre
)
{
return
new
Nombre
(
leftSimplified
.
value
*
rightSimplified
.
value
);
// si les deux opérandes
peuvent être évalués numériquement, on
calcule le résultat
if
(
!
isNaN
(
leftSimplified
.
valueOf
())
&&
!
isNaN
(
rightSimplified
.
valueOf
())
)
{
return
new
Nombre
(
leftSimplified
.
value
Of
()
*
rightSimplified
.
value
Of
()
);
}
// si l'un des opérandes est 0, le résultat est 0
if
((
leftSimplified
instanceof
Nombre
&&
leftSimplified
.
value
===
0
)
||
(
rightSimplified
instanceof
Nombre
&&
rightSimplified
.
value
===
0
))
{
if
((
!
isNaN
(
leftSimplified
.
valueOf
())
&&
leftSimplified
.
value
Of
()
===
0
)
||
(
!
isNaN
(
rightSimplified
.
valueOf
())
&&
rightSimplified
.
value
Of
()
===
0
))
{
return
new
Nombre
(
0
);
}
// si l'un des opérandes est 1, on retourne l'autre
if
(
leftSimplified
instanceof
Nombre
&&
leftSimplified
.
value
===
1
)
{
if
(
!
isNaN
(
leftSimplified
.
valueOf
())
&&
leftSimplified
.
value
Of
()
===
1
)
{
return
rightSimplified
;
}
if
(
rightSimplified
instanceof
Nombre
&&
rightSimplified
.
value
===
1
)
{
if
(
!
isNaN
(
rightSimplified
.
valueOf
())
&&
rightSimplified
.
value
Of
()
===
1
)
{
return
leftSimplified
;
}
// sinon, on crée une nouvelle multiplication avec les opérandes simplifiés
return
new
Multiplication
(
leftSimplified
,
rightSimplified
);
}
// multiplication peut être évaluée si ses deux opérandes peuvent l'être
valueOf
()
{
const
leftValue
=
this
.
leftOperand
.
valueOf
();
const
rightValue
=
this
.
rightOperand
.
valueOf
();
if
(
isNaN
(
leftValue
)
||
isNaN
(
rightValue
))
{
return
NaN
;
}
return
leftValue
*
rightValue
;
}
}
function
test
()
{
const
x
=
new
Variable
(
"
x
"
);
const
y
=
new
Variable
(
"
y
"
);
...
...
@@ -198,7 +228,16 @@ function test() {
// simplification de la dérivée par rapport à y
const
simplifiedDerivedY
=
derivedY
.
simplify
();
console
.
log
(
"
Dérivée simplifiée par rapport à y:
"
,
simplifiedDerivedY
.
toString
());
// Tests de valueOf()
console
.
log
(
"
\n
Tests de valueOf():
"
);
console
.
log
(
"
Valeur de 2:
"
,
deux
.
valueOf
());
console
.
log
(
"
Valeur de x:
"
,
x
.
valueOf
());
console
.
log
(
"
Valeur de 2+3:
"
,
new
Addition
(
deux
,
trois
).
valueOf
());
console
.
log
(
"
Valeur de 2*3:
"
,
new
Multiplication
(
deux
,
trois
).
valueOf
());
console
.
log
(
"
Valeur de x+y:
"
,
new
Addition
(
x
,
y
).
valueOf
());
}
// Exécution du test
test
();
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment