[질문] 자꾸 에러가 나는데 잘못된것좀 고쳐주세요....
채꽃
다항식 덧셈 뺄셈 곱셈인데.. 자꾸 곱셈에서 에러가 납니다..
뭐가 잘못 됐는지 좀 가르쳐 주세요...
class Node {
int coef;
int exp;
Node link;
public Node(int x, int y) {
coef = y;
exp = x;
link = null;
}
}
public class Poly {
private Node head;
private Node last;
public Poly() {
head = null;
last = null;
}
public void appendTerm(int x, int y) {
Node newNode = new Node(x, y);
if (head == null) {
head = newNode;
last = newNode;
}
else {
last.link = newNode;
last = newNode;
}
}
public Poly polyAdd(Poly a) {
Node p = this.head;
Node q = a.head;
Poly res = new Poly();
while (p != null && q != null) {
if (p.exp == q.exp) {
if (p.coef + q.coef == 0) {
p = p.link;
q = q.link;
}
else {
res.appendTerm(p.exp, p.coef + q.coef);
p = p.link;
q = q.link;
}
}
else if (p.exp q.exp) {
res.appendTerm(q.exp, q.coef);
q = q.link;
}
else {
res.appendTerm(p.exp, p.coef);
p = p.link;
}
}
while (p != null)
{
res.appendTerm(p.exp, p.coef);
p = p.link;
}
while (q != null)
{
res.appendTerm(q.exp, q.coef);
q = q.link;
}
return res;
}
public Poly polyDiff(Poly a) {
Node p = this.head;
Node q = a.head;
Poly res = new Poly();
while (p != null && q != null) {
if (p.exp == q.exp) {
if (p.coef + q.coef == 0) {
p = p.link;
q = q.link;
}
else {
res.appendTerm(p.exp, p.coef - q.coef);
p = p.link;
q = q.link;
}
}
else if (p.exp q.exp) {
res.appendTerm(q.exp, -q.coef);
q = q.link;
}
else {
res.appendTerm(p.exp, p.coef);
p = p.link;
}
}
while (p != null) {
res.appendTerm(p.exp, p.coef);
p = p.link;
}
while (q != null)
{
res.appendTerm(q.exp, q.coef);
q = q.link;
}
return res;
}
public Poly polyMul(Poly a) {
Node p = this.head;
Node q = a.head;
Node x;
Poly res = new Poly();
Node temp = res.head;
while (p != null) {
while (q != null) {
x.exp = p.exp + q.exp;
if (temp == null) {
while (q != null) {
res.appendTerm(p.exp + q.exp, p.coef * q.coef);
q = q.link;
}
}
else {
while (temp !=null) {
if (temp == x.exp) {
res = temp;
&; res.coef += p.coef * q.coef;
temp = temp.link;
}
else if (temp x.exp) {
temp = temp.link;
}
else {
res = temp;
res.appendTerm(p.exp + q.exp, p.coef * q.coef);
temp = temp.link;
}
}
temp = res.head;
}
q = q.link;
}
q = a.head;
p = p.link;
}
return res;
}
public void print() {
Node temp = head;
while (temp != null) {
if (!temp.equals(head)) {
if (temp.coef = 0)
System.out.print( + );
else
System.out.print( - );
}
if (head == temp)
System.out.print(temp.coef + x^ + temp.exp);
else if (temp.exp == 0) {
System.out.print(temp.coef);
}
else {
if (temp.coef = 0)
System.out.print(temp.coef + x^ + temp.exp);
else
System.out.print(-temp.coef + x^ + temp.exp);
}
temp = temp.link;
}
System.out.println();
}
public static void main(String args[])
{
Poly p1 = new Poly();
p1.appendTerm(3, 5);
p1.appendTerm(2, 1);
p1.appendTerm(0, 7);
System.out.print(A = );
p1.print();
Poly p2 = new Poly();
p2.appendTerm(4, 2);
p2.appendTerm(2, -8);
p2.appendTerm(1, 4);
System.out.print(B = );
p2.print();
Poly p3 = p1.polyAdd(p2);
System.out.print(A + B = );
p3.print();
Poly p4 = p1.polyDiff(p2);
System.out.print(A - B = );
p4.print();
Poly p5 = p1.polyMul(p2);
System.out.print(A * B = );
p5.print();
}
}
-
돌삥
말머리 달아주세요 ^^