自定义LinkedList实现以存储多项式的项

时间:2018-11-18 06:44:14

标签: java singly-linked-list polynomials

对于一项家庭作业,我正在努力上,您需要创建一个自定义的LinkedList数据结构来保存多项式的项。目前,我的构造函数向数据结构中添加了术语时遇到了问题,因为它需要接受“ 5.2 3 2.1 2”之类的字符串(相当于5.2 ^ 3 + 2.1 ^ 2)并将其存储在我的自定义LinkedList中。一些要求包括:项不能具有零的系数,指数必须是整数,系数可以是整数或双精度数。当我使用IDE的调试器跟踪程序时,我看到的是由于某种原因,有效系数被捕获在我标有“#”的子句中,并且引用了列表的开头(术语优先)变量)似乎没有从与其正确链接的输入字符串中获取其他变量。非常感谢您能提供的任何帮助。有很多必需的方法,但这是我遇到的问题的相关代码:

import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Polynomial 
{
// instance variables
private Term first;
private int numTerms;

/**
 * Constructor for objects of class Polynomial
 */
public Polynomial(String s)
{
    Pattern whiteSpace = Pattern.compile(" ");
    String[] poly = whiteSpace.split(s);
    double coefficient;
    int exponent;

    if(poly.length % 2 == 1) {
        throw new IllegalArgumentException();
    }

    first = new Term(); // dummy variable so that checking the first term specially is unnecessary

    for(int term = 0; term < poly.length; term += 2) {
        if(poly[term].matches("[\\-][0-9]||[0-9]||[0-9][\\.][0-9]||[\\-][0-9][\\.][0-9]")) {
            coefficient = Double.parseDouble(poly[term]);
            if(poly[term + 1].matches("[0-9]")) {
                exponent = Integer.parseInt(poly[term++]);
            } else {
                throw new IllegalArgumentException(); //#
            }

            numTerms++;
            this.addTerm(coefficient, exponent);
        }
    }
}
public void addTerm(double coef, int exp)
{
    if(coef == 0) {
        throw new IllegalArgumentException();
    }
    Term pointer = first; 
    while(pointer.next != null) { 
        if(exp == pointer.next.exponent) { 
            if(coef + pointer.next.coefficient == 0) {
                pointer.next = pointer.next.next; 
                numTerms--; 
            } else {
                pointer.next.coefficient += coef; 
                break; 
            }

        } else if(pointer.next.exponent < exp) { 
            Term newTerm = new Term(coef, exp, pointer.next.next); 
            pointer.next = newTerm; 
            numTerms++; 
            break; 
        }
        pointer = pointer.next;
    }
}
private class Term {
    double coefficient;
    int exponent;
    Term next;

    Term() {
        next = null;
    }

    Term(double coef, int exp, Term nextTerm) {
        coefficient = coef;
        exponent = exp;
        next = nextTerm;
    }
}`

1 个答案:

答案 0 :(得分:0)

您不需要regex来检查有效性ParseXXX()可以解决此问题。如果解析失败,则会引发异常,然后您可以简单地throw IllegalArgumentException

        for (int term = 0; term < poly.length - 1; term += 2) {
            try {
                coefficient = Double.parseDouble(poly[term]);
                exponent = Integer.parseInt(poly[term + 1]);
                numTerms++;
                this.addTerm(coefficient, exponent);
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException();
            }
        }

此外,使用regex =“ [0-9]”表示仅一位数的指数。您可能需要将其更改为“ [0-9] +”