读取文本文件并将它们转换为多项式

Reading a text file and converting them into polynomials

我目前有一个文本文件如下:

3 5 6 9 
3 4 6 7 2
3 5 7 2 5 3

读入java的文件应该显示为3x^5 + 6x^9。第二行将被读作 4x^4 + 6x^7 + 2。无法让我的程序显示它,因为我不知道如何将这些数字转换成那种形式。当我 运行 程序时,我目前只得到它们之间有空格的数字。

这是我尝试过的:

import java.io.File;  
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class Driver {

public static void main(String[] args) {
    try {
        @SuppressWarnings("resource")
        Scanner myfile = new Scanner(new File("poly.dat"));

        Polynomial[] mypolynomial;

        mypolynomial = new Polynomial[10];

        int index = 0;
        if (myfile.hasNext() == true) { //ignore this part
            myfile.nextLine();
        } else {
            System.out.println("Error: File is empty");
            return;
        }
        while (myfile.hasNextLine()) {
            mypolynomial[index] = new Polynomial(myfile.nextLine());
            index++;
        }
        String menu = "Please choose a Polynomial \n";
        for (int i = 0; i < index; i++) {
            menu = menu + i + " " + mypolynomial[i].getNumber() + "\n";
        }
        String choicemenu = "What do you want to do ? \n " + "A - Display a Polynomial \n "
                + "B - Add two Polynomial \n " + "C - Subtract two Polynoimal \n "
                + "D - Multiply two Polynomial \n ";
        String action = JOptionPane.showInputDialog(choicemenu);
        if (action.equals("A")) {
            int choice = Integer.parseInt(JOptionPane.showInputDialog(menu));
            JOptionPane.showMessageDialog(null, mypolynomial[choice]);
        }
    } catch (FileNotFoundException e) {
        System.out.println(" OOOPS - something wrong - maybe the file name is wrong");
    }
}
}

public class Polynomial { //Testing the program

String poly;

public Polynomial(String p)
{
    poly = p;
}

public String getNumber() {
        return poly;
}

public void setNumber(String p)
{
    poly=p;
}

public String toString()
{
    String result = "The Polynomial is " + poly;
    return result;
}


}

我想首先将这些数字显示为多项式,然后我最终想对它们进行运算。谁能帮帮我?

多项式的值似乎成对出现,即 5 4 变为 5x^4。这意味着您必须跟踪它是一对中的第一个、一对中的第二个还是不是一对中的成员。第一种情况你只需要打印值,但第二种情况你需要有一个 "x^"+ 值。

您可以在多项式 class 构造函数中执行以下操作:

  1. 创建String polynomial = "".

  2. 循环遍历传入的行,同时跟踪 boolean isOnSecond

  3. 如果 !isOnSecond,追加读入的值并将 isOnSecond 设置为真。

  4. 否则 isOnSecond == true 附加 "x^" + value 并将 isOnSecond 设置为 false。

  5. 检查字符串是否有另一个值,如果它确实附加了一个“+”并继续循环,否则什么都不做,因为该行已完成。

这将为您提供看起来像您需要的输出的字符串。


Polynomial 构造函数中的示例代码:

public Polynomial(String p) {
    // First step is to create a scanner of the String
    // passed into the constructor.
    Scanner scanner = new Scanner(p);

    // Next step is to initialize the String poly
    // to an empty String so we can append to it.
    poly = "";

    // Next we need to include a way of keeping track of
    // whether the value we just read was a first or second
    // member of a pair. We can do that with a boolean
    // initialized to false since the first use will be
    // when it is on the first of a pair.
    boolean isOnSecond = false;

    // Now we need to start looping through the values in
    // p which are separated by white space. Scanner has
    // a method for that, scanner.next().
    while(scanner.hasNext()) {
        String currentValue = scanner.next();

        // Now is where the boolean comes into play.
        if(isOnSecond) { // second of a pair
            // the second of a pair needs to have "x^" before its value
            poly = poly + "x^" + currentValue;

            // Here we need to check if there is another value coming after
            // and if there is append a " + " to poly
            if(scanner.hasNext() {
                poly = poly + " + ";
            }
        }
        else { // !isOnSecond, so is first of a pair
            // Only need to append the currentValue here
            poly = poly + currentValue;
        }
        isOnSecond = !isOnSecond; // toggles isOnSecond
    }
}