2012-02-11 5 views
1

다항식을 만들려면 .txt 파일을 읽는 중입니다. 다항식을 실제로 인쇄하는 데 문제가 있습니다 (링크 된 목록에 넣은 후). 나는 ... '연결'링크 된 목록과 다항식 방법에 대해 이동하는 방법을 정말 잘 모르겠어요Int 텍스트 파일에서 다항식 만들기.

텍스트 파일 :

P1 = 3 5 1 -1 0 8 
P2 = 5 6 2 -1 1 7 0 -4 
p3 = p1 + p2 
p4 = p3 - p1 

코드 :

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.util.Iterator; 
import java.util.LinkedList; 
import java.util.Scanner; 

public class Polynomial { 
    public Term first; 
    public Term last; 
    private int[] coef; // coefficients 
    private int deg; // degree of polynomial (0 for the zero polynomial) 

    // a * x^b 
    public Polynomial(int a, int b) { 
     coef = new int[b + 1]; 
     coef[b] = a; 
     deg = degree(); 
    } 

    // return the degree of this polynomial (0 for the zero polynomial) 
    public int degree() { 
     int d = 0; 
     for (int i = 0; i < coef.length; i++) 
      if (coef[i] != 0) 
       d = i; 
     return d; 
    } 

    // return c = a + b 
    public Polynomial plus(Polynomial b) { 
     Polynomial a = this; 
     Polynomial c = new Polynomial(0, Math.max(a.deg, b.deg)); 
     for (int i = 0; i <= a.deg; i++) 
      c.coef[i] += a.coef[i]; 
     for (int i = 0; i <= b.deg; i++) 
      c.coef[i] += b.coef[i]; 
     c.deg = c.degree(); 
     return c; 
    } 

    // return (a - b) 
    public Polynomial minus(Polynomial b) { 
     Polynomial a = this; 
     Polynomial c = new Polynomial(0, Math.max(a.deg, b.deg)); 
     for (int i = 0; i <= a.deg; i++) 
      c.coef[i] += a.coef[i]; 
     for (int i = 0; i <= b.deg; i++) 
      c.coef[i] -= b.coef[i]; 
     c.deg = c.degree(); 
     return c; 
    } 

    // convert to string representation 
    public String toString() { 
     if (deg == 0) 
      return "" + coef[0]; 
     if (deg == 1) 
      return coef[1] + "x + " + coef[0]; 
     String s = coef[deg] + "x^" + deg; 
     for (int i = deg - 1; i >= 0; i--) { 
      if (coef[i] == 0) 
       continue; 
      else if (coef[i] > 0) 
       s = s + " + " + (coef[i]); 
      else if (coef[i] < 0) 
       s = s + " - " + (-coef[i]); 
      if (i == 1) 
       s = s + "x"; 
      else if (i > 1) 
       s = s + "x^" + i; 
     } 
     return s; 
    } 

    // test client 
    public static void main(String[] args) throws IOException { 

     // Welcome message 
     System.out 
       .println("Welcome! The following program processes single-variable polynomials represented as linked lists.\n" 
         + "Test Data will appear below, and is also saved to a text file (userSpecification.txt) \n" 
         + "-------------------------------" + "\n"); 

     String content = new String(); 
     String name = new String(); 
     File file = new File("polynomialTest.txt"); 
     LinkedList<String> list = new LinkedList<String>(); 

     try { 
      Scanner sc = new Scanner(new FileInputStream(file)); 
      while (sc.hasNext()) { 
       name = sc.next(); 
       content = sc.nextLine(); 

       // ..just checking things as they come in. 
       System.out.println("name " + name + " content " + content); 

       list.add(content); 

      } 

      sc.close(); 
     } catch (FileNotFoundException fnfe) { 
      fnfe.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      System.out.println("\nProgram terminated Safely..."); 
     } 

     Iterator<String> i = list.iterator(); 
     while (i.hasNext()) { 
      System.out.println(name + i.next() + "\n"); 

     } 

    private class Term { 
     int coef; 
     int expo; 
     Term next; 

     Term(int coef, int expo, Term n) { 
      this.coef = coef; 
      this.expo = expo; 
      this.next = n; 
     } 
    } 
} 

원하는 출력 :

지금
P1 = 5X^3 – 4X + 8 
P2 = 6X^5 -2X^2 +7X -4 
P3 = 6X^5 +5X^3 -2X^2 +3X +4 
P4 = 6X^5 -2X^2 +7X -4 

출력 :

Project #2 
Welcome! The following program processes single-variable polynomials represented as linked lists. 
------------------------------- 

P1 = 3 5 1 -1 0 8 

P2 = 5 6 2 -1 1 7 0 -4 

p3 = p1 + p2 

p4 = p3 - p1 

[P1 = 3 5 1 -1 0 8, P2 = 5 6 2 -1 1 7 0 -4, p3 = p1 + p2, p4 = p3 - p1] 
+0

P1 = 5X^3 - X + 8이어야하며, 첫 번째 등식이 잘못되었거나 입력 데이터가 잘못되었습니다. – eternaln00b

+0

감사합니다. 실제로 다항식을 표시 할 때 예상 출력과 같이 숫자를 걱정하지 않습니다. 잘 잡아라, 나는 그것을 바꿀 것이다! – tommy1370

답변

1

LinkedList의 개념과 프로그램에서의 사용에 대해 이해하고 있습니까? 코드에서 수집 한 내용 (더 많은 답변을 원한다면 매우 간결하고 프로그램 실행에 절대적으로 필요한 코드 만 포함해야 함)에서이 Term 클래스를 사용하면 LinkedList을 구현할 수 있습니다. 기본적으로 Term 클래스를 보유하고이 클래스를 많이 사용하지 않고 대신 모든 코드를 자체적으로 돌보는이 Polynomial 클래스 내에 있습니다. 당신이해야 할 일은 Term 그 자체를 다항식을 표현하는 노드의 모음으로 생각하는 것입니다. main 함수에서 변수를 읽고 Term 인스턴스로 만들고 더 많은 노드로 Term 인스턴스화 (다항식 당 하나씩) 인스턴스에 추가해야합니다. Term (이제 전체 다항식)을 함께 추가 할 때 수행해야하는 작업은 동일한 지수로 모든 것을 순환시켜 추가하는 것입니다.