-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoly.java
More file actions
160 lines (152 loc) · 4.98 KB
/
Poly.java
File metadata and controls
160 lines (152 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
class Poly {
class Term {
private int coef;
private int expo;
private Term next;
private Term(int coef, int expo, Term next) {
this.coef = coef;
this.expo = expo;
this.next = next;
}
}
private Term first;
private Term last;
public Poly() {
Term head = new Term(0, Integer.MAX_VALUE, null);
first = head;
last = head;
}
public boolean isZero() {
return first.next == null;
}
public Poly minus() {
Poly result = new Poly();
Term temp = first.next;
while (temp != null) {
result.last.next = new Term(-temp.coef, temp.expo, null);
result.last = result.last.next;
temp = temp.next;
}
return result;
}
public Poly plus(Poly that) {
Poly result = new Poly();
Term left = first.next;
Term right = that.first.next;
while (left != null && right != null) {
if (left.expo > right.expo) {
result.last.next = new Term(left.coef, left.expo, null);
result.last = result.last.next;
left = left.next;
}
else if (right.expo > left.expo) {
result.last.next = new Term(right.coef, right.expo, null);
result.last = result.last.next;
right = right.next;
}
else if (left.coef + right.coef != 0) {
result.last.next = new Term(left.coef + right.coef, left.expo, null);
result.last = result.last.next;
left = left.next;
right = right.next;
}
else {
left = left.next;
right = right.next;
}
}
if (left != null) {
result.last.next = left;
}
else if (right != null){
result.last.next = right;
}
return result;
}
public Poly plus(int coef, int expo) {
if (coef == 0 || expo < 0 || expo >= last.expo) {
throw new IllegalArgumentException("Cannot add this term to the polynomial.");
}
else {
last.next = new Term(coef, expo, null);
last = last.next;
}
return this;
}
public String toString() {
StringBuilder out = new StringBuilder();
Term temp = first.next;
if (isZero()) {
return "0";
}
else {
out.append(temp.coef + "x");
appendExpo(out, temp.expo);
temp = temp.next;
}
if (temp.next != null && temp.next.coef >= 0) {
out.append(" + ");
}
else if (temp.next != null) {
out.append(" - " );
}
while(temp != null) {
if (temp.coef > 0) {
out.append(temp.coef + "x");
appendExpo(out, temp.expo);
}
else if (temp.coef < 0){
out.append((-temp.coef) + "x");
appendExpo(out, temp.expo);
}
if (temp.next != null && temp.next.coef >= 0) {
out.append(" + ");
}
else if (temp.next != null) {
out.append(" - " );
}
temp = temp.next;
}
return out.toString();
}
// APPEND EXPO. Append superscript digits for EXPO to BUILDER. EXPO is greater
// than or equal to zero.
private void appendExpo(StringBuilder builder, int expo)
{
if (expo == 0)
{
builder.append('⁰');
}
else
{
appendingExpo(builder, expo);
}
}
// APPENDING EXPO. Do all the work for APPEND EXPO.
private void appendingExpo(StringBuilder builder, int expo)
{
if (expo > 0)
{
appendingExpo(builder, expo / 10);
builder.append("⁰¹²³⁴⁵⁶⁷⁸⁹".charAt(expo % 10));
}
}
}
class Driver {
public static void main(String[] args) {
Poly p = new Poly().plus(3,5).plus(2,4).plus(2,3).plus(-1,2).plus(5,0);
Poly q = new Poly().plus(7,4).plus(1,2).plus(-4,1).plus(-3,0);
Poly z = new Poly();
System.out.println(p); // 3x⁵ + 2x⁴ + 2x³ - 1x² + 5x⁰
System.out.println(q); // 7x⁴ + 1x² - 4x¹ - 3x⁰
System.out.println(z); // 0
System.out.println(p.minus()); // -3x⁵ - 2x⁴ - 2x³ + 1x² - 5x⁰
System.out.println(q.minus()); // -7x⁴ - 1x² + 4x¹ + 3x⁰
System.out.println(z.minus()); // 0
System.out.println(p.plus(q)); // 3x⁵ + 9x⁴ + 2x³ - 4x¹ + 2x⁰
System.out.println(p.plus(z)); // 3x⁵ + 2x⁴ + 2x³ - 1x² + 5x⁰
System.out.println(p.plus(q.minus())); // 3x⁵ - 5x⁴ + 2x³ - 2x² + 4x¹ + 8x⁰
System.out.println(p.plus(q).plus(z));
System.out.println(p.plus(z).plus(q));
}
}