import com.perisic.ring.*;
import java.io.*;
/**
A small programm for testing this class.
*/

public class PolynomialIOExample {
  public static Ring inputRing() {
       while( true ) {
       System.out.println("Input the coefficient Ring (One of "+
            "C, Z, Q, R or F2). Default: Z");  
       String str = "";
       BufferedReader reader = new BufferedReader(
                              new InputStreamReader(System.in));
        try {
                str = reader.readLine(); 
           } catch (IOException e) { 
                     System.out.println("Invalid input: "+str); 
                      }
                      
        if( str.length() == 0 ) {
            return Ring.Z;
        } else if( str.equals("R") ) {
                return Ring.R;
        } else if( str.equals("F2") ) {
                return Ring.F2;
                }
        else if ( str.equals("Q") ) {
                return Ring.Q;
                }
        else if ( str.equals("C") ) {
                return Ring.C;
                }
        else if( str.equals("Z") ) {
                return Ring.Z;
                }
        System.out.println("Invalid Input, Try again.");
        }
       }
         
  public static void main ( String [] args ) { 
             Ring P = new QuotientField(new PolynomialRing(
                 inputRing(),
                 "a,b"));
//               "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z");
            // PolynomialRing P = new PolynomialRing(
            //        RationalField.Q,"a,b,c,d");
          
            RingElt p;
            RingElt gcd = P.zero();
            String lastString = "";
            boolean ende = false;
            do {
                p = null;
                String str = "0";
                System.out.println("Enter a polynomial of "+P+" (Enter $ for Exit, & for clear)"); 
                BufferedReader reader = new BufferedReader(
                              new InputStreamReader(System.in));
                try {
                    str = reader.readLine();
                     
                 } catch (IOException e) { 
                     System.out.println("Invalid input: "+str); 
                      }
                 p = null;
                if( str.equals("&") ) { lastString = ""; p = null; }
                else if( str.equals("$") ) { ende = true; } 
                else {
                  lastString = lastString + str; 
                  System.out.println("Processing "+lastString+"... "); 
                  p = null;   
                  try { 
                   p = P.map(lastString);
                   } catch ( RingException e ) {
                     System.out.println("Couldn't map "+str+" into "+P+
                        " because of "+e);
                     System.out.println("> "+lastString);
                        }
                   }
                if( p != null ) {        
                System.out.println("input = " + p);
                lastString = "("+p.toString()+")";
              //  gcd = P.gcd(gcd,p); 
              //  System.out.println("gcd of this and the previous gcd = " + gcd);
                }
                } while (  ende == false);

         }
         
}

