java怎么实现多项式相加

64次阅读
没有评论

共计 2524 个字符,预计需要花费 7 分钟才能阅读完成。

在 Java 中,可以使用数组或链表来表示多项式,然后通过遍历多项式的每一项,将相同指数的项相加得到最终结果。

以下是一种使用链表表示多项式的实现方式:

public class PolynomialNode {int coefficient;
    int exponent;
    PolynomialNode next;
    
    public PolynomialNode(int coefficient, int exponent) {this.coefficient = coefficient;
        this.exponent = exponent;
        this.next = null;
    }
}

public class Polynomial {private PolynomialNode head;
    
    public Polynomial() {this.head = null;
    }
    
    public void addTerm(int coefficient, int exponent) {PolynomialNode newNode = new PolynomialNode(coefficient, exponent);
        
        if (head == null) {head = newNode;} else {PolynomialNode current = head;
            PolynomialNode previous = null;
            
            while (current != null && current.exponent > exponent) {
                previous = current;
                current = current.next;
            }
            
            if (current != null && current.exponent == exponent) {current.coefficient += coefficient;} else {
                newNode.next = current;
                
                if (previous != null) {previous.next = newNode;} else {head = newNode;}
            }
        }
    }
    
    public Polynomial add(Polynomial polynomial) {Polynomial result = new Polynomial();
        
        PolynomialNode node1 = this.head;
        PolynomialNode node2 = polynomial.head;
        
        while (node1 != null && node2 != null) {if (node1.exponent > node2.exponent) {result.addTerm(node1.coefficient, node1.exponent);
                node1 = node1.next;
            } else if (node1.exponent < node2.exponent) {result.addTerm(node2.coefficient, node2.exponent);
                node2 = node2.next;
            } else {int sum = node1.coefficient + node2.coefficient;
                
                if (sum != 0) {result.addTerm(sum, node1.exponent);
                }
                
                node1 = node1.next;
                node2 = node2.next;
            }
        }
        
        while (node1 != null) {result.addTerm(node1.coefficient, node1.exponent);
            node1 = node1.next;
        }
        
        while (node2 != null) {result.addTerm(node2.coefficient, node2.exponent);
            node2 = node2.next;
        }
        
        return result;
    }
    
    public String toString() {StringBuilder sb = new StringBuilder();
        
        PolynomialNode current = head;
        boolean isFirstTerm = true;
        
        while (current != null) {if (current.coefficient != 0) {if (current.coefficient > 0 && !isFirstTerm) {sb.append("+");
                }
                
                sb.append(current.coefficient);
                
                if (current.exponent > 1) {sb.append("x^").append(current.exponent);
                } else if (current.exponent == 1) {sb.append("x");
                }
                
                isFirstTerm = false;
            }
            
            current = current.next;
        }
        
        return sb.toString();}
}

使用示例:

public class Main {public static void main(String[] args) {Polynomial polynomial1 = new Polynomial();
        polynomial1.addTerm(3, 4);
        polynomial1.addTerm(2, 3);
        polynomial1.addTerm(5, 2);
        polynomial1.addTerm(1, 0);
        
        Polynomial polynomial2 = new Polynomial();
        polynomial2.addTerm(1, 3);
        polynomial2.addTerm(4, 2);
        polynomial2.addTerm(2, 1);
        polynomial2.addTerm(3, 0);
        
        Polynomial result = polynomial1.add(polynomial2);
        
        System.out.println("Polynomial 1: " + polynomial1);
        System.out.println("Polynomial 2: " + polynomial2);
        System.out.println("Result: " + result);
    }
}

输出结果:

Polynomial 1: 3x^4+2x^3+5x^2+1
Polynomial 2: x^3+4x^2+2x+3
Result: 3x^4+3x^3+9x^2+2x+4

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-09发表,共计2524字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)