2016-09-09 3 views
5

Umwandlung habe ich zur Zeit eine Textdatei wie folgt:eine Textdatei lesen und sie in Polynome

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

Die Datei, wenn sie in Java sollte wie 3x^5 + 6x^9 angezeigt werden. Die zweite Zeile würde als 4x^4 + 6x^7 + 2 gelesen werden. Das Programm kann das nicht anzeigen, da ich nicht weiß, wie man diese Zahlen in diese Form umwandelt. Ich bekomme gerade die Zahlen mit Leerzeichen dazwischen, wenn ich das Programm starte.

Hier ist, was ich versucht habe:

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


} 

Ich möchte zuerst diese Ziffern als Polynome angezeigt werden, dann will ich schließlich Operationen mit ihnen durchzuführen. Kann mir jemand helfen?

Antwort

1

Es scheint, dass die Polynome Werte haben, die als Paare eingehen, d. H. 5 4 wird 5x^4. Das bedeutet, dass Sie nachverfolgen müssen, ob es sich um das erste eines Paares, das zweite eines Paares oder nicht um ein Paar handelt. Der erste Fall, in dem Sie nur den Wert drucken müssen, aber der zweite muss einen "x ^" + Wert haben.

Sie könnten die folgenden in Ihrem Polynom Klassenkonstruktors tun:

  1. String polynomial = "" erstellen.

  2. Schleife durch die eingefahrene Leitung, während ein boolean isOnSecond verfolgt wird.

  3. Wenn !isOnSecond, Wert anhängen und isOnSecond auf true setzen.

  4. Sonst isOnSecond == true anhängen "x^" + value und isOnSecond auf false setzen.

  5. Überprüfen Sie, ob der String einen anderen Wert hat, und wenn er ein "+" anfügt und die Schleife hält, machen Sie sonst nichts, da die Zeile fertig ist.

Dadurch erhalten Sie Strings, die wie die Ausgabe aussehen, die Sie benötigen.


Beispielcode in Ihrem Polynomial Konstruktor:

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

Vielen Dank für Ihre Antwort! Könnten Sie das möglicherweise erweitern? Was meinst du mit einem String-Polynom? Dh wann lese ich die Daten in ein Array? Ich bin auch mit booleschen IsOnSecond nicht vertraut. Kannst du ein Beispiel geben und mir helfen, es zu starten? Ich entschuldige mich, ich versuche immer noch zu verstehen, was los ist. Vielen Dank, obwohl –

+0

Ich habe einige Beispiel-Code für Sie zum Betrachten hinzugefügt. –

+1

Wenn Sie das Layout der Datei garantieren können, können Sie sie auch als Binärdatei speichern und für einen schnelleren Zugriff einfach über die Bytes iterieren. – Krythic