File tree Expand file tree Collapse file tree 4 files changed +35
-15
lines changed
Expand file tree Collapse file tree 4 files changed +35
-15
lines changed Original file line number Diff line number Diff line change 1+ package io .polypen ;
2+
3+ import java .util .List ;
4+
5+ public class Expressions {
6+ public sealed interface Expression permits Product {
7+ String eval ();
8+ }
9+
10+ public record Product (List <String > factors ) implements Expression {
11+ @ Override
12+ public String eval () {
13+ List <Polynomial > polynomials = factors .stream ().map (Polynomial ::parse ).toList ();
14+ Polynomial result = Polynomial .ONE ;
15+ for (Polynomial p : polynomials ) {
16+ result = result .multiply (p );
17+ }
18+ return result .toString ();
19+ }
20+ }
21+ }
Original file line number Diff line number Diff line change 11package io .polypen ;
22
3- import java .util .List ;
3+ import io .polypen .Expressions .Expression ;
4+
45import java .util .Scanner ;
56
67public class Main {
78
8- public static Polynomial evalProduct (String s ) {
9- List <Polynomial > polynomials = Parser .parseProduct (s ).stream ().map (Polynomial ::parse ).toList ();
10- Polynomial result = Polynomial .ONE ;
11- for (Polynomial p : polynomials ) {
12- result = result .multiply (p );
13- }
14- return result ;
15- }
16-
179 public static void main (String [] args ) {
1810 Scanner in = new Scanner (System .in );
1911 StringBuilder sb = new StringBuilder ();
2012 while (in .hasNextLine ()) {
2113 String line = in .nextLine ();
2214 sb .append (line );
2315 }
24- System .out .println (evalProduct (sb .toString ()));
16+ Expression expression = Parser .parseProduct (sb .toString ());
17+ System .out .println (expression .eval ());
2518 }
2619}
Original file line number Diff line number Diff line change 11package io .polypen ;
22
3+ import io .polypen .Expressions .Expression ;
4+ import io .polypen .Expressions .Product ;
35import org .apache .commons .numbers .fraction .Fraction ;
46
57import java .util .ArrayList ;
@@ -43,7 +45,7 @@ static List<Fraction> parsePolynomial(String s) {
4345 return result ;
4446 }
4547
46- static List < String > parseProduct (String s ) {
48+ static Expression parseProduct (String s ) {
4749 List <String > result = new ArrayList <>();
4850 StringBuilder sb = new StringBuilder ();
4951 int nestingLevel = -1 ;
@@ -74,7 +76,7 @@ static List<String> parseProduct(String s) {
7476 if (!sb .isEmpty ()) {
7577 result .add (sb .toString ());
7678 }
77- return result ;
79+ return new Product ( result ) ;
7880 }
7981
8082 enum Sign {
Original file line number Diff line number Diff line change 11package io .polypen ;
22
3+ import io .polypen .Expressions .Expression ;
34import org .apache .commons .numbers .fraction .Fraction ;
45import org .junit .jupiter .api .Test ;
56
67import java .util .List ;
78
89import static io .polypen .Polynomial .parse ;
910import static org .junit .jupiter .api .Assertions .assertEquals ;
11+ import static org .junit .jupiter .api .Assertions .assertInstanceOf ;
1012
1113class PolynomialTest {
1214
@@ -39,7 +41,9 @@ void monomialMultiplication() {
3941
4042 @ Test
4143 void parseProduct () {
42- List <String > strings = Parser .parseProduct ("(a + 1) * (a - 1)" );
43- assertEquals (List .of ("a + 1" , "a - 1" ), strings );
44+ Expression expression = Parser .parseProduct ("(a + 1) * (a - 1)" );
45+ assertInstanceOf (Expressions .Product .class , expression );
46+ Expressions .Product product = (Expressions .Product ) expression ;
47+ assertEquals (List .of ("a + 1" , "a - 1" ), product .factors ());
4448 }
4549}
You can’t perform that action at this time.
0 commit comments