import java.util.*;
import java.lang.reflect.*;
import java.io.*;

public class Analyse{
    String str;
    String file;
    int len;
    int n; // nombre de sommets
    int d; // degre max des sommets
    Stack[] tab; // tableau des piles 

    FileOutputStream f_out;

    Analyse(String file_name,String s, int nb, int deg){
	str = s;
	len = str.length();
	n = nb;
	d = deg;
	tab = new Stack[n];
	file = file_name;

	//INITIALISATION
	int i;
	for (i=0;i<n;i++)
	    {tab[i] = new Stack();}
    }

    public int search_fst(){
	String aux = "";
	int i=0;
	int j;
	while ((i<len)&&(str.charAt(i) ==' '))
	    {i++;}
	j=i;
	while ((str.charAt(j) != '-')&&(str.charAt(j) != ' '))
	    {j++;}
	aux = str.substring(i,j);
	str = str.substring(j);
	len = str.length();
	//System.out.println(aux);
	//System.out.println(str);
	return(Integer.parseInt(aux));
    }

    public int search_scd(){
	String aux = "";
	int k=0;
	int l=0;
	int m=0;
	while ((k<len)&&(str.charAt(k) != '-'))
	    {k++;}
	m=k+1;
	while(str.charAt(m) == '-')
	    {m++;}
	while(str.charAt(m) == ' ')
	    {m++;}
	l=m;
	while(str.charAt(l) !=';')
	    {l++;}
	aux = str.substring(m,l);
	str = str.substring(l+1);
	len = str.length();
	//System.out.println(aux);
	//System.out.println(str);
	return(Integer.parseInt(aux));
    }

    //  public void search_vertice(){
    //ana.search_fst();
    //ana.search_scd();
    //}
    
    public void push_vertice(){
	int res1 = this.search_fst();
	int res2 = this.search_scd();
	System.out.println(res1);
	System.out.println(res2);
	(tab[res2]).push(new Integer(res1));
	(tab[res1]).push(new Integer(res2));
    }

    public boolean is_the_end(){
	int i=0;
	while ((i<len) && (str.charAt(i) == ' '))
	    {i++;}
	if (i==len) {return(true);}
	else {
	    if (str.charAt(i)=='F')
		{return(true);}
	    else {return(false);}
	}
    }

    public void flush_tab(){
	int i;
	int j;

	for (i=0;i<n;i++)
	    {
		System.out.println("Voisins de "+i);
		
		for (j=0;j<d;j++)
		    {
			if ((tab[i]).empty())
			    {System.out.println('#');}
			else
			    {System.out.println((tab[i].pop()));}
		    }
	    }
    }

    public void push_vertices(){
	try {
	    while(!(this.is_the_end()))
		{this.push_vertice();}
	}
	catch(Exception e)
	    {
		System.out.println("codage bizarre");
	    }
	
    }

    public void make_file(){
	int i,j,k,l;
	String aux;
	try{
	    f_out = new FileOutputStream(file);
	    //écriture de n et d
	    aux = n + " " + d + "\n";
	    for (j = 0;j<aux.length();j++)
		{f_out.write(aux.charAt(j));}
	    //écriture des piles de voisins
	    for (i = 0;i<n;i++)
		{
		    String aux2 = "";
		    for (k=0;k<d;k++)
			{
			    if ((tab[i]).empty())
				{aux2 += "# ";}
			    else
				{aux2 += ((tab[i]).pop()).toString()+" ";}
			}
		    aux2 += "\n";
  		    for (l=0;l<aux2.length();l++)
  			{f_out.write(aux2.charAt(l));}
  		}
	    f_out.close();
	}
	catch(IOException e)
	    {
		System.out.println("Pb de création du fichier");
		System.exit(0);
	    }
    }

//      public static void main(String[] args){
//  	Analyse ana;

//  	ana = new Analyse("toto","  3- 5;10-3; 5-4;     ",11,4);

//  	ana.push_vertices(); // empile les aretes
//  	ana.make_file(); // cree le fichier correspondant
//      }
    
    
}


