Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
Formula
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SAADI Nesrine
Formula
Commits
f2e698b6
Commit
f2e698b6
authored
Oct 5, 2023
by
SAADI Nesrine
Browse files
Options
Downloads
Patches
Plain Diff
Exo 1 TP4
parent
2d67564c
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/main/java/formula/Calculator.java
+29
-38
29 additions, 38 deletions
src/main/java/formula/Calculator.java
src/main/java/formula/Product.java
+5
-6
5 additions, 6 deletions
src/main/java/formula/Product.java
src/main/java/formula/Sum.java
+1
-4
1 addition, 4 deletions
src/main/java/formula/Sum.java
with
35 additions
and
48 deletions
src/main/java/formula/Calculator.java
+
29
−
38
View file @
f2e698b6
...
@@ -3,51 +3,42 @@ package formula;
...
@@ -3,51 +3,42 @@ package formula;
import
java.util.Stack
;
import
java.util.Stack
;
public
class
Calculator
{
public
class
Calculator
{
String
[]
formule
;
AbstractFormulaFactory
factory
;
Stack
<
Formula
>
stackFormula
;
public
Calculator
(
AbstractFormulaFactory
factory
){
public
Calculator
(
String
...
formule
){
this
.
factory
=
factory
;
this
.
formule
=
formule
;
calculate
();
System
.
out
.
println
(
stackFormula
.
peek
().
asValue
());
}
}
Formula
analyze
(
String
...
tokens
){
boolean
isDouble
(
String
string
){
Stack
<
Formula
>
stack
=
new
Stack
<>();
try
{
for
(
String
token
:
tokens
){
Double
.
parseDouble
(
string
);
analyzeToken
(
token
,
stack
);
return
true
;
}
}
catch
(
NumberFormatException
e
){
return
false
;}
return
stack
.
pop
();
}
}
void
calculateProduct
(){
void
analyzeToken
(
String
token
,
Stack
<
Formula
>
stack
){
Formula
[]
variables
=
new
Formula
[
2
];
if
(
token
==
"*"
)
variables
[
0
]
=
stackFormula
.
pop
();
analyzeProduct
(
stack
);
variables
[
1
]
=
stackFormula
.
pop
();
else
if
(
token
==
"+"
)
analyzeSum
(
stack
);
else
analyzeInt
(
token
,
stack
);
}
Formula
product
=
new
Product
(
variables
);
void
analyzeSum
(
Stack
<
Formula
>
stack
){
Formula
var1
=
stack
.
pop
();
Formula
var2
=
stack
.
pop
();
stack
.
push
(
factory
.
createSum
(
var1
,
var2
));
};
void
analyzeProduct
(
Stack
<
Formula
>
stack
){
Formula
var1
=
stack
.
pop
();
Formula
var2
=
stack
.
pop
();
stack
.
push
(
factory
.
createProduct
(
var1
,
var2
));
};
stackFormula
.
push
(
product
);
void
analyzeInt
(
String
token
,
Stack
<
Formula
>
stack
){
stack
.
push
(
factory
.
createConstant
(
Double
.
parseDouble
(
token
)));
};
}
}
void
calculateSum
(){
Formula
[]
variables
=
new
Formula
[
2
];
variables
[
0
]
=
stackFormula
.
pop
();
variables
[
1
]
=
stackFormula
.
pop
();
Formula
sum
=
new
Sum
(
variables
);
stackFormula
.
push
(
sum
);
}
public
void
calculate
(){
for
(
String
s
:
formule
)
{
if
(
isDouble
(
s
))
{
Formula
var
=
new
Constant
(
Double
.
parseDouble
(
s
));
stackFormula
.
push
(
var
);}
if
(
s
.
equals
(
"*"
))
calculateProduct
();
if
(
s
.
equals
(
"+"
))
calculateSum
();
}
}
}
This diff is collapsed.
Click to expand it.
src/main/java/formula/Product.java
+
5
−
6
View file @
f2e698b6
package
formula
;
package
formula
;
public
class
Product
extends
VariadicOperator
{
public
class
Product
implements
Operator
{
public
Product
(
Formula
[]
formulas
){
super
(
formulas
);
}
@Override
@Override
public
String
symbol
()
{
public
String
symbol
()
{
return
"*"
;
return
"*"
;
...
@@ -16,7 +14,8 @@ public class Product extends VariadicOperator{
...
@@ -16,7 +14,8 @@ public class Product extends VariadicOperator{
}
}
@Override
@Override
public
double
cumulativeValue
(
double
accumulator
,
double
value
)
{
public
double
cumulativeValue
(
double
value
,
double
accumulator
)
{
return
accumulator
*
value
;
return
value
*
accumulator
;
}
}
}
}
This diff is collapsed.
Click to expand it.
src/main/java/formula/Sum.java
+
1
−
4
View file @
f2e698b6
package
formula
;
package
formula
;
public
class
Sum
ext
en
d
s
Variadic
Operator
{
public
class
Sum
implem
en
t
s
Operator
{
public
Sum
(
Formula
...
formulas
){
super
(
formulas
);
}
@Override
@Override
public
String
symbol
()
{
public
String
symbol
()
{
return
"+"
;
return
"+"
;
...
...
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