从 Int 文本文件创建多项式。

发布于 2025-01-04 04:08:03 字数 4508 浏览 0 评论 0原文

我正在读取 .txt 文件来创建多项式。我在实际打印多项式时遇到麻烦(在将它们放入链接列表之后)。我不太确定如何“链接”链表和多项式方法...

文本文件:

P1 = 3 5 1 -1 0 8
P2 = 5 6 2 -1 1 7 0 -4
p3 = p1 + p2
p4 = p3 - p1

代码:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;

public class Polynomial {
    public Term first;
    public Term last;
    private int[] coef; // coefficients
    private int deg; // degree of polynomial (0 for the zero polynomial)

    // a * x^b
    public Polynomial(int a, int b) {
        coef = new int[b + 1];
        coef[b] = a;
        deg = degree();
    }

    // return the degree of this polynomial (0 for the zero polynomial)
    public int degree() {
        int d = 0;
        for (int i = 0; i < coef.length; i++)
            if (coef[i] != 0)
                d = i;
        return d;
    }

    // return c = a + b
    public Polynomial plus(Polynomial b) {
        Polynomial a = this;
        Polynomial c = new Polynomial(0, Math.max(a.deg, b.deg));
        for (int i = 0; i <= a.deg; i++)
            c.coef[i] += a.coef[i];
        for (int i = 0; i <= b.deg; i++)
            c.coef[i] += b.coef[i];
        c.deg = c.degree();
        return c;
    }

    // return (a - b)
    public Polynomial minus(Polynomial b) {
        Polynomial a = this;
        Polynomial c = new Polynomial(0, Math.max(a.deg, b.deg));
        for (int i = 0; i <= a.deg; i++)
            c.coef[i] += a.coef[i];
        for (int i = 0; i <= b.deg; i++)
            c.coef[i] -= b.coef[i];
        c.deg = c.degree();
        return c;
    }

    // convert to string representation
    public String toString() {
        if (deg == 0)
            return "" + coef[0];
        if (deg == 1)
            return coef[1] + "x + " + coef[0];
        String s = coef[deg] + "x^" + deg;
        for (int i = deg - 1; i >= 0; i--) {
            if (coef[i] == 0)
                continue;
            else if (coef[i] > 0)
                s = s + " + " + (coef[i]);
            else if (coef[i] < 0)
                s = s + " - " + (-coef[i]);
            if (i == 1)
                s = s + "x";
            else if (i > 1)
                s = s + "x^" + i;
        }
        return s;
    }

    // test client
    public static void main(String[] args) throws IOException {

        // Welcome message
        System.out
                .println("Welcome! The following program processes single-variable polynomials represented as linked lists.\n"
                        + "Test Data will appear below, and is also saved to a text file (userSpecification.txt) \n"
                        + "-------------------------------" + "\n");

        String content = new String();
        String name = new String();
        File file = new File("polynomialTest.txt");
        LinkedList<String> list = new LinkedList<String>();

        try {
            Scanner sc = new Scanner(new FileInputStream(file));
            while (sc.hasNext()) {
                name = sc.next();
                content = sc.nextLine();

                // ..just checking things as they come in.
                System.out.println("name " + name + "  content " + content);

                list.add(content);

            }

            sc.close();
        } catch (FileNotFoundException fnfe) {
            fnfe.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("\nProgram terminated Safely...");
        }

        Iterator<String> i = list.iterator();
        while (i.hasNext()) {
            System.out.println(name + i.next() + "\n");

        }

    private class Term {
        int coef;
        int expo;
        Term next;

        Term(int coef, int expo, Term n) {
            this.coef = coef;
            this.expo = expo;
            this.next = n;
        }
    }
}

所需的输出:

P1 = 5X^3 – 4X + 8 
P2 = 6X^5 -2X^2 +7X -4 
P3 = 6X^5 +5X^3 -2X^2 +3X +4 
P4 = 6X^5 -2X^2 +7X -4

立即输出:

Project #2
Welcome! The following program processes single-variable polynomials represented as linked lists.
-------------------------------

P1 = 3 5 1 -1 0 8

P2 = 5 6 2 -1 1 7 0 -4

p3 = p1 + p2

p4 = p3 - p1

[P1 = 3 5 1 -1 0 8, P2 = 5 6 2 -1 1 7 0 -4, p3 = p1 + p2, p4 = p3 - p1]

I'm reading in a .txt file to create polynomials. I am having trouble actually printing the polynomials (after they've been put into the linked list). I'm not really sure how to go about 'linking' the linked list and the polynomial method...

Text File:

P1 = 3 5 1 -1 0 8
P2 = 5 6 2 -1 1 7 0 -4
p3 = p1 + p2
p4 = p3 - p1

Code:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;

public class Polynomial {
    public Term first;
    public Term last;
    private int[] coef; // coefficients
    private int deg; // degree of polynomial (0 for the zero polynomial)

    // a * x^b
    public Polynomial(int a, int b) {
        coef = new int[b + 1];
        coef[b] = a;
        deg = degree();
    }

    // return the degree of this polynomial (0 for the zero polynomial)
    public int degree() {
        int d = 0;
        for (int i = 0; i < coef.length; i++)
            if (coef[i] != 0)
                d = i;
        return d;
    }

    // return c = a + b
    public Polynomial plus(Polynomial b) {
        Polynomial a = this;
        Polynomial c = new Polynomial(0, Math.max(a.deg, b.deg));
        for (int i = 0; i <= a.deg; i++)
            c.coef[i] += a.coef[i];
        for (int i = 0; i <= b.deg; i++)
            c.coef[i] += b.coef[i];
        c.deg = c.degree();
        return c;
    }

    // return (a - b)
    public Polynomial minus(Polynomial b) {
        Polynomial a = this;
        Polynomial c = new Polynomial(0, Math.max(a.deg, b.deg));
        for (int i = 0; i <= a.deg; i++)
            c.coef[i] += a.coef[i];
        for (int i = 0; i <= b.deg; i++)
            c.coef[i] -= b.coef[i];
        c.deg = c.degree();
        return c;
    }

    // convert to string representation
    public String toString() {
        if (deg == 0)
            return "" + coef[0];
        if (deg == 1)
            return coef[1] + "x + " + coef[0];
        String s = coef[deg] + "x^" + deg;
        for (int i = deg - 1; i >= 0; i--) {
            if (coef[i] == 0)
                continue;
            else if (coef[i] > 0)
                s = s + " + " + (coef[i]);
            else if (coef[i] < 0)
                s = s + " - " + (-coef[i]);
            if (i == 1)
                s = s + "x";
            else if (i > 1)
                s = s + "x^" + i;
        }
        return s;
    }

    // test client
    public static void main(String[] args) throws IOException {

        // Welcome message
        System.out
                .println("Welcome! The following program processes single-variable polynomials represented as linked lists.\n"
                        + "Test Data will appear below, and is also saved to a text file (userSpecification.txt) \n"
                        + "-------------------------------" + "\n");

        String content = new String();
        String name = new String();
        File file = new File("polynomialTest.txt");
        LinkedList<String> list = new LinkedList<String>();

        try {
            Scanner sc = new Scanner(new FileInputStream(file));
            while (sc.hasNext()) {
                name = sc.next();
                content = sc.nextLine();

                // ..just checking things as they come in.
                System.out.println("name " + name + "  content " + content);

                list.add(content);

            }

            sc.close();
        } catch (FileNotFoundException fnfe) {
            fnfe.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("\nProgram terminated Safely...");
        }

        Iterator<String> i = list.iterator();
        while (i.hasNext()) {
            System.out.println(name + i.next() + "\n");

        }

    private class Term {
        int coef;
        int expo;
        Term next;

        Term(int coef, int expo, Term n) {
            this.coef = coef;
            this.expo = expo;
            this.next = n;
        }
    }
}

Desired output:

P1 = 5X^3 – 4X + 8 
P2 = 6X^5 -2X^2 +7X -4 
P3 = 6X^5 +5X^3 -2X^2 +3X +4 
P4 = 6X^5 -2X^2 +7X -4

Output right now:

Project #2
Welcome! The following program processes single-variable polynomials represented as linked lists.
-------------------------------

P1 = 3 5 1 -1 0 8

P2 = 5 6 2 -1 1 7 0 -4

p3 = p1 + p2

p4 = p3 - p1

[P1 = 3 5 1 -1 0 8, P2 = 5 6 2 -1 1 7 0 -4, p3 = p1 + p2, p4 = p3 - p1]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吹梦到西洲 2025-01-11 04:08:03

您了解 LinkedList 的概念及其在程序中的使用吗?根据我从您的代码中收集到的内容(如果您想要更多答案,代码应该非常简洁,并且仅包含程序运行绝对需要的代码),您有这个 Term 类,它是一个 LinkedList 的实现,在这个 Polynomial 类中,它基本上只是保存 Term 类,并没有做太多事情,而是采用自行处理所有代码。您应该做的是将 Term 本身视为表示多项式表达式的节点的集合。在您的 main 函数中,您应该读取变量,将它们放入 Term 实例化中,并将它们添加到根 Term 实例化(一个每个多项式表达式)作为更多节点。当您将Term(现在是完整的多项式表达式)相加时,您所要做的就是循环遍历并将所有具有相同指数的项相加。

do you understand the concept of a LinkedList and its use in your program? From what I gathered from your code (which, if you want more answers, should be very concise and contain only the code that is absolutely required for your program to run), you have this Term class, which is an implementation of a LinkedList, within this Polynomial class which is basically just holding the Term class and not doing much with it and instead is taking care of all of your code by itself. What you should do is think of the Term itself as a collection of nodes representing the polynomial expression. In your main function you should be reading in the variables, making them into Term instantiations, and adding them on to a root Term instantiation (one per polynomial expression) as more nodes. When you are adding Terms (now full polynomial expressions) together, all you have to do is cycle through and add together all of the ones with the same exponent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文