@@ -17,43 +17,47 @@ private Polynomial(List<Fraction> coefficients) {
1717 @ Override
1818 public String toString () {
1919 List <String > result = new ArrayList <>(coefficients .size ());
20- for (int i = 0 ; i < coefficients .size (); i ++ ) {
20+ for (int i = coefficients .size () - 1 ; i >= 0 ; i -- ) {
2121 Fraction coefficient = coefficients .get (i );
2222 if (coefficient .isZero ()) {
2323 continue ;
2424 }
25+ String plus = (i == coefficients .size () - 1 && coefficient .compareTo (Fraction .ZERO ) > 0 ) ? "" : "+ " ;
26+ String prettyCoefficient = coefficient .compareTo (Fraction .ZERO ) < 0 ? "- " : plus ;
2527 if (i == 0 ) {
26- result .add (coefficient .toString ());
28+ result .add (prettyCoefficient + coefficient .abs ());
2729 } else {
2830 String exponent = i == 1 ? "x" : "x^" + i ;
2931 if (coefficient .isOne ()) {
30- result .add (exponent );
32+ result .add (plus + exponent );
33+ } else if (coefficient .equals (Fraction .ONE .negate ())) {
34+ result .add ("- " + exponent );
3135 } else {
32- result .add (coefficient + " " + "x^" + i );
36+ result .add (prettyCoefficient + coefficient . abs () + " " + "x^" + i );
3337 }
3438 }
3539 }
36- return String .join (" + " , result );
40+ return String .join (" " , result );
3741 }
3842
3943 public static Polynomial parse (String s ) {
40- String [] strings = s . split ( " \\ w*[+-] \\ w*" , 0 );
44+ List < SignedToken > strings = split ( s . trim () );
4145 TreeMap <Integer , Fraction > cof = new TreeMap <>();
42- for (String term : strings ) {
43- String [] tokens = term .split ("[a-z]" , 3 );
46+ for (SignedToken term : strings ) {
47+ String [] tokens = term .token . split ("[a-z]" , 3 );
4448 String coefficient = tokens [0 ].replace ("*" , "" ).trim ();
4549 if (coefficient .isEmpty ()) {
4650 coefficient = "1" ;
4751 }
4852 if (tokens .length == 1 ) {
49- cof .put (0 , Fraction .parse (coefficient ));
53+ cof .put (0 , Fraction .parse (coefficient ). multiply ( term . sign . factor ) );
5054 } else {
5155 String rawExponent = tokens [1 ].replace ("^" , "" ).trim ();
5256 if (rawExponent .isEmpty ()) {
53- cof .put (1 , Fraction .parse (coefficient ));
57+ cof .put (1 , Fraction .parse (coefficient ). multiply ( term . sign . factor ) );
5458 } else {
5559 int exponent = Integer .parseInt (rawExponent );
56- cof .put (exponent , Fraction .parse (coefficient ));
60+ cof .put (exponent , Fraction .parse (coefficient ). multiply ( term . sign . factor ) );
5761 }
5862 }
5963 }
@@ -69,4 +73,37 @@ public static Polynomial parse(String s) {
6973 }
7074 return new Polynomial (result );
7175 }
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+ }
72109}
0 commit comments