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

时间:2016-09-09 02:08:57

标签: java 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;
}


}

我想首先将这些数字显示为多项式,然后我最终想要用它们执行操作。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

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

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

  1. 创建String polynomial = ""

  2. 在跟踪boolean isOnSecond的同时循环传入的行。

  3. 如果!isOnSecond,请附上读取值并将isOnSecond设置为true。

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

  5. 检查字符串是否有另一个值,如果它确实附加了&#34; +&#34;并保持循环,否则不做任何事情,因为线已经完成。

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

    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
        }
    }