首页主机资讯java怎么实现多项式相加

java怎么实现多项式相加

时间2023-12-09 08:29:03发布访客分类主机资讯浏览924
导读:在 Java 中,可以使用数组或链表来表示多项式,然后通过遍历多项式的每一项,将相同指数的项相加得到最终结果。 以下是一种使用链表表示多项式的实现方式: public class PolynomialNode { int coeff...

在 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

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: java怎么实现多项式相加
本文地址: https://pptw.com/jishu/574452.html
vba怎么筛选出满足条件的数据 gitlab怎么克隆项目到本地

游客 回复需填写必要信息