/**
 * PolyMain.java  -  Polynomials
 * 
 * @author Bary W Pollack
 * @version 01/05/01 - 01:00 - Created
 */
 
public class PolyMain {

    public static final String sInFile = "PolyData.txt";

    /* These four overloads of sop() make it easier to output...
     *   public static void sop(String s)  { System.out.println(s); }
     *   public static void sop(Term t)    { sop("" + t); }
     *   public static void sop(Poly p)    { sop("" + p); }
     *   public static void sop(boolean b) { sop("" + b); }
     */

    /*==============================================================*/
    /*  The main driver - loops 'til an exponent < 0                */
    /*==============================================================*/

    public static void main(String[] args) {
        header();

        Poly p1 = new Poly(),
             p2 = new Poly();
        int nExp;
        int nExpression = 0;

        Poly.open(sInFile);

        while ((nExp = Poly.readExp()) >= 0) {
            Poly pDiff, pPwr;
            p1.readPoly();
            p2.readPoly();                             // cin >> p1 >> p2;
            pDiff = (p1.mpy(3)).sub(p2.mpy(2));        // pDiff = 3 * p1 - 2 * p2;
            pPwr  = pDiff.expo(nExp);                  // pPwr  = pDiff ^ nExp;
            System.out.println(
                      "Expression number " + ++nExpression + ":"          + LS
                    + "Exp:              " + nExp                         + LS
                    + "P1:               " + p1                           + LS
                    + "P2:               " + p2                           + LS
                    + "3*P1-2*P2:        " + pDiff                        + LS
                    + "(3*P1-2*P2)^" + leftJust(nExp, 2) + "    " + pPwr  + LS);
        };

        Poly.close();
        trailer();
    }

    /**
     * LS is Java's LineSeparator string  (equivalent of newline or endl)
     */

    public static final String LS = System.getProperty("line.separator");

    /**
     * leftJust is a formating routine, a bit similar to setw in C++
     * 
     * @param nValue the value to be converted
     * @param nCols the width of the field (String) in characters
     * @return nValue left-justified in nCols columns
     */

    private static String leftJust(int nValue, int nCols) {
        String s = "" + nValue;
        while (s.length() < nCols)
            s = s + " ";
        return s;
    }
    
    /*==============================================================*/
    /*  Standard prolog and epilog titles                           */
    /*==============================================================*/

    private static void header() {
        String[] sHeading = {
            "========================",
            "=  Simple Polynomials  =",
            "========================",
            " ",
            "Authors: Pollack, Bary W - 1234 - Sect 001",
            "         Someone, Else   - 2345 - Sect 001",
            "Date:    January 26, 2001  (date assignment is due)",
            "Course:  CIS 110L - Data Structures & Algorithms - Assignment L1",
            " ",
            "This program reads a sequence of data representing an exponent",
            "followed by a pair of polynomials, P1 and P2.  The difference,",
            "(3*P1 - 2*P2) is formed and then raised to the power of the exponent.",
            "The values of P1, P2 and the exponentiated difference are displayed.",
            "Input continues until an exponent less than zero is encountered.",
            " "
        };
    
        for (int i = 0; i < sHeading.length; i++)
            System.out.println(sHeading[i]);
    }

    private static void trailer() {
        System.out.println();
        System.out.println("That's all, folks!");
        System.out.println();
    }

}

/*==============================================================*/
