/**
 Auther : Eslam Mohamed Erman .

 Program : Solving Second Order Equation.

 How to use it ?
 
 Creat flie Called ,eq.txt, then write the equation like that
 EqSecOrder [ a b c ].
 
 Example :
      1. Creat The ,eq.txt, file
      2. Write
                 EqSecOrder [ 1 3 2 ]
   Solution (O/P):
             => 6 tokens
   **The EQUATION ( a x^2 + b x + c = 0 )
      a == 1.0,  b == 3.0,  c == 2.0
  **The Roots (r1,r2)
     r1=-b+sqrt(b^2-4ac), r1 == -1.0
     r2=-b-sqrt(b^2-4ac), r2 == -2.0
   => 0 tokens
     The End Of File :).

 */
import java.lang.System;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.lang.Math;
import java.lang.String;

public class SecOrder{
	public static void main(String args[]) throws IOException {
		FileReader file =new FileReader("eq.txt");
		BufferedReader data = new BufferedReader(file);
		int numTokens;
		do {
			boolean bl=true;
			String tok1,tok2,aa,bb,cc;
			double a,b,c,m,r1,r2;
			
			System.out.print("=> ");
			System.out.flush();
			
			//The string tokenizer class allows an application to break a string into tokens.
			StringTokenizer st = new StringTokenizer(data.readLine());
			numTokens = st.countTokens();
			System.out.println(numTokens+" tokens");
			if (numTokens == 6){
				
				tok1=st.nextToken();
				tok2=st.nextToken();
				
				boolean bl1= tok1.equals("EqSecOrder");
				boolean bl2=tok2.equals("[");
				if (bl1==bl && bl2==bl)
				{
					aa=st.nextToken();
					bb=st.nextToken();
					cc=st.nextToken();
					
					{
						//To convert String to double number.
						a =Double.parseDouble(aa);
						b=Double.parseDouble(bb);
						c=Double.parseDouble(cc);
						
						System.out.println("**The EQUATION ( a x^2 + b x + c = 0 )");
						System.out.println("  a == "+a+",  b == "+b+",  c == "+c);
						if(a ==0.0 || b==0.0 || c== 0.0){
							
							System.out.println("Error : a || b || C == 0 \n");
						}
						else{
							m =(Math.pow(b,2)- 4*a*c) ;
							if(m>0){
								System.out.println("**The Roots (r1,r2)");
								r1=(-b+Math.sqrt(m))/2;
								r2=(-b-Math.sqrt(m))/2;
								System.out.println("  r1=-b+sqrt(b^2-4ac), r1 == "+r1);
								System.out.println("  r2=-b-sqrt(b^2-4ac), r2 == "+r2);
							}
							else System.out.println("Error :The program Can't solve the Imaginary so let  (b^2-4*a*c > 0)");
						}
						
					}
					
					
				}
				else System.out.println("Error :Check that the 1st token is ,EqSecOrder, & the 2nd is ,[,");
			}
			else if (numTokens==0)
				System.out.println("The End Of File :)");
			else	System.out.println("Error : Check that ur Equation must be like , EqSecOrder [ a b c ] ,");
			
		} while(numTokens!=0);
	}
}


