44
55import java .util .ArrayList ;
66import java .util .List ;
7- import java . util . Map ;
8- import java . util . TreeMap ;
7+
8+ import static io . polypen . Util . isAbsoluteOne ;
99
1010public final class Polynomial {
1111 private final List <Fraction > coefficients ;
@@ -14,6 +14,10 @@ private Polynomial(List<Fraction> coefficients) {
1414 this .coefficients = coefficients ;
1515 }
1616
17+ public static Polynomial parse (String s ) {
18+ return new Polynomial (Parser .parse (s ));
19+ }
20+
1721 @ Override
1822 public String toString () {
1923 List <String > result = new ArrayList <>(coefficients .size ());
@@ -23,87 +27,18 @@ public String toString() {
2327 continue ;
2428 }
2529 String plus = (i == coefficients .size () - 1 && coefficient .compareTo (Fraction .ZERO ) > 0 ) ? "" : "+ " ;
26- String prettyCoefficient = coefficient .compareTo (Fraction .ZERO ) < 0 ? "- " : plus ;
30+ String prettySign = coefficient .compareTo (Fraction .ZERO ) < 0 ? "- " : plus ;
2731 if (i == 0 ) {
28- result .add (prettyCoefficient + coefficient .abs ());
32+ result .add (prettySign + coefficient .abs ());
2933 } else {
3034 String exponent = i == 1 ? "x" : "x^" + i ;
31- if (coefficient .isOne ()) {
32- result .add (plus + exponent );
33- } else if (coefficient .equals (Fraction .ONE .negate ())) {
34- result .add ("- " + exponent );
35+ if (isAbsoluteOne (coefficient )) {
36+ result .add (prettySign + exponent );
3537 } else {
36- result .add (prettyCoefficient + coefficient .abs () + " " + "x^" + i );
38+ result .add (prettySign + coefficient .abs () + " " + "x^" + i );
3739 }
3840 }
3941 }
4042 return String .join (" " , result );
4143 }
42-
43- public static Polynomial parse (String s ) {
44- List <SignedToken > strings = split (s .trim ());
45- TreeMap <Integer , Fraction > cof = new TreeMap <>();
46- for (SignedToken term : strings ) {
47- String [] tokens = term .token .split ("[a-z]" , 3 );
48- String coefficient = tokens [0 ].replace ("*" , "" ).trim ();
49- if (coefficient .isEmpty ()) {
50- coefficient = "1" ;
51- }
52- if (tokens .length == 1 ) {
53- cof .put (0 , Fraction .parse (coefficient ).multiply (term .sign .factor ));
54- } else {
55- String rawExponent = tokens [1 ].replace ("^" , "" ).trim ();
56- if (rawExponent .isEmpty ()) {
57- cof .put (1 , Fraction .parse (coefficient ).multiply (term .sign .factor ));
58- } else {
59- int exponent = Integer .parseInt (rawExponent );
60- cof .put (exponent , Fraction .parse (coefficient ).multiply (term .sign .factor ));
61- }
62- }
63- }
64- Integer highestExponent = cof .lastKey ();
65- List <Fraction > result = new ArrayList <>(highestExponent );
66- for (int i = 0 ; i <= highestExponent ; i ++) {
67- result .add (Fraction .ZERO );
68- }
69- for (Map .Entry <Integer , Fraction > e : cof .entrySet ()) {
70- Integer exponent = e .getKey ();
71- Fraction coefficient = e .getValue ();
72- result .set (exponent , coefficient );
73- }
74- return new Polynomial (result );
75- }
76-
77- enum Sign {
78- PLUS (1 ), MINUS (-1 );
79- final int factor ;
80-
81- Sign (int factor ) {
82- this .factor = factor ;
83- }
84- }
85-
86- record SignedToken (Sign sign , String token ) {
87- }
88-
89- static List <SignedToken > split (String s ) {
90- List <SignedToken > result = new ArrayList <>();
91- Sign sign = Sign .PLUS ;
92- int pos = -1 ;
93- for (int i = 0 ; i < s .length (); i ++) {
94- if (s .charAt (i ) == '-' ) {
95- result .add (new SignedToken (sign , s .substring (pos + 1 , i ).trim ()));
96- sign = Sign .MINUS ;
97- pos = i ;
98- } else if (s .charAt (i ) == '+' ) {
99- result .add (new SignedToken (sign , s .substring (pos + 1 , i ).trim ()));
100- sign = Sign .PLUS ;
101- pos = i ;
102- }
103- }
104- if (pos < s .length () - 1 ) {
105- result .add (new SignedToken (sign , s .substring (pos + 1 ).trim ()));
106- }
107- return result ;
108- }
10944}
0 commit comments