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

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 ^” +值。

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

  • 创建String polynomial = ""
  • 在跟踪boolean isOnSecond的同时循环通过传入的行。
  • 如果为!isOnSecond,则附加读取的值并将isOnSecond设置为true。
  • 其他isOnSecond == true附加"x^" + value并将isOnSecond设置为false。
  • 检查字符串是否具有另一个值,并且是否确实附加了“+”并继续循环,否则自该行结束以来不执行任何操作。

  • 这将为您提供看起来像所需输出的字符串。
    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
        }
    }
    

    09-27 18:45