2016-09-09 16 views
5

に変換し、次のように私は現在、テキストファイルを持っている:テキストファイルを読み込むと、多項式

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

のjavaに読み込むファイルが3倍^ 9^5 + 6Xとして表示されなければなりません。 2行目は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

多項式は、値が5 4となり、5x^4となるように見えます。これは、ペアの最初のペアか、ペアの2番目のペアか、ペアのメンバーでないかを追跡する必要があることを意味します。最初の場合は値を出力するだけですが、2番目の場合は "x ^" +値が必要です。

あなたは、多項式クラスのコンストラクタで次の操作を行うことができます:

  1. String polynomial = ""を作成します。

  2. boolean isOnSecondを追跡しながら渡された行をループします。

  3. !isOnSecondの場合、値を読み込み、isOnSecondをtrueに設定します。

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

は、ご返信いただきありがとうございます!これをさらに拡大できますか?文字列多項式を作成するとどういう意味ですか?つまり、配列にデータを読み込んでいますか?私はブール値isOnSecondにも慣れていません。あなたは例を挙げて、それをキックスタートさせるのに役立つでしょうか?私はお詫び申し上げます、私はまだ何が起こっているのか把握しようとしています。しかし、ありがとうございました –

+0

私はあなたを見ていくつかの例のコードを追加しました。 –

+1

ファイルのレイアウトを保証できるならば、それをバイナリファイルとして保存して、より速くアクセスできるようにバイトを繰り返し処理することもできます。 – Krythic

関連する問題