Java ADT——一元二次多项式

1、一元二次多项式抽象数据类型类 QUAdratic.java

package com.practice1_2;

/**
 * 一元二次多项式ADT类
 */
public class QUAdratic {
    private int a;
    private int b;
    private int c;

    public QUAdratic(){
        this.a = 0;
        this.b = 0;
        this.c = 0;
    }

    public QUAdratic(int a, int b, int c){
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public int getA() {
        return a;
    }

    public int getB() {
        return b;
    }

    public int getC() {
        return c;
    }

    public void setA(int a) {
        this.a = a;
    }

    public void setB(int b) {
        this.b = b;
    }

    public void setC(int c) {
        this.c = c;
    }
}

 

2、一元二次多项式操作接口 QUAbstratic.java

package com.practice1_2;

/**
 * 一元二次多项式操作接口
 */
public interface QUAbstratic {
    //初始化一个一元二次多项式
    QUAdratic InitQuadreatic(int a, int b, int c);
    //两个多项式相加,返回一个新的多项式
    QUAdratic Add(QUAdratic q1, QUAdratic q2);
    //给定一个多项式和一个x值,计算多项式结果
    int Eval(QUAdratic q, int x);
    //当多项式结果为0时,求解方程
    float[] Root(QUAdratic q);
    //输出多项式
    void Print(QUAdratic q);
}

 

3、应用一元二次多项式ADT和接口,实现相应功能

package com.practice1_2;


/**
 * 使用一元二次多项式ADT解决一些应用问题
 * 1、给定一个 x 值,计算多项式结果
 * 2、计算多项式方程结果
 */
public class QUAbtraticAppli implements QUAbstratic{

    public static void main(String[] args){
        QUAbtraticAppli app = new QUAbtraticAppli();
        app.test();
    }

    private void test(){
        QUAdratic q1 = InitQuadreatic(1,4,3);
        QUAdratic q2 = InitQuadreatic(-2,5,-8);
        QUAdratic total = Add(q1,q2);
        Print(q1);
        Print(q2);
        Print(total);
        System.out.println("x为2时的结果为:" + Eval(total, 2));
        judgeRoot(total);
    }

    private void judgeRoot(QUAdratic q){
        float[] result = Root(q);
        if(result[0] == -1){
            System.out.println("该方程无解");
            return;
        }
        if(result[1] == 0){
            System.out.println("该方程有唯一解: " + result[1]);
            return;
        }
        System.out.println("该方程有两个解: ");
        System.out.println("第一个解:x1 = " + result[1]);
        System.out.println("第二个解: x2 = " + result[2]);
    }

    @Override
    public QUAdratic InitQuadreatic(int a, int b, int c) {
        return new QUAdratic(a,b,c);
    }

    @Override
    public QUAdratic Add(QUAdratic q1, QUAdratic q2) {
        QUAdratic total = new QUAdratic();
        total.setA(q1.getA() + q2.getA());
        total.setB(q1.getB() + q2.getB());
        total.setC(q1.getC() + q2.getC());
        return total;
    }

    @Override
    public int Eval(QUAdratic q, int x) {
        return q.getA() * (int)Math.pow(x,2) + q.getB() * x + q.getC();
    }

    @Override
    public float[] Root(QUAdratic q) {
        float[] result = new float[3];
        float derta = (float)Math.pow(q.getB(),2) - 4*q.getA()*q.getC();
        if(derta < 0){
            result[0] = -1;
            result[1] = 0;
            result[2] = 0;
            return result;
        }
        else if(derta == 0){
            result[0] = 0;
            result[1] = -1 * q.getB() / (2 * q.getA());
            result[2] = 0;
            return result;
        }
        else {
            result[0] = 1;
            result[1] = (-1 * q.getB() + (float)Math.sqrt(derta)) / (2 * q.getA());
            result[2] = (-1 * q.getB() - (float)Math.sqrt(derta)) / (2 * q.getA());
            return result;
        }
    }

    @Override
    public void Print(QUAdratic q) {
        if(q.getA() != 0){
            if(q.getA() != -1 && q.getA() != 1)
                System.out.print(q.getA() + "x^2");
            else {
                if(q.getA() == 1)
                    System.out.print("x^2");
                else
                    System.out.print("-x^2");
            }
        }
        if(q.getB() != 0){
            if(q.getB() >0) {
                if(q.getB() != 1)
                    System.out.print("+" + q.getB() + "x");
                else
                    System.out.print("+x");
            }
            else {
                if(q.getB() != -1)
                    System.out.print(q.getB() + "x");
                else
                    System.out.print("-x");
            }
        }
        if(q.getC() != 0){
            if(q.getC() >0)
                System.out.print("+" + q.getC());
            else
                System.out.print(q.getC());
        }
        System.out.println();
    }
}

 

4、运行结果:

x^2+4x+3
-2x^2+5x-8
-x^2+9x-5
x为2时的结果为:9
该方程有两个解: 
第一个解:x1 = 0.5948751
第二个解: x2 = 8.405125

Process finished with exit code 0

 

 

你可能感兴趣的:(Java)