Cod sursa(job #1361547)

Utilizator Cosmin_DragosDeliu Cosmin Dragos Cosmin_Dragos Data 25 februarie 2015 22:07:47
Problema Convertor Scor 0
Compilator java Status done
Runda rosedu_cdl_2015 Marime 4.41 kb
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

/**
 * Aceasta clasa este folosita pentru parsarea liniilor unui fisier
 * @author cosmin
 *
 */


public class Main {
	
	String filePath = "convertor.in";
	BufferedReader reader = null;
	Queue<String> wordBuffer = new LinkedList<String>();
	//this.filePath = filePath;
	//this.reader = null;
	//this.wordBuffer = new LinkedList<String>();

	/**
	 * Opens the file for reading.
	 * <p>
	 * This operation must be performed before calling any other methods on this object.
	 */
	public void open() {
		if (reader != null) {
			throw new IllegalStateException("File already opened for reading");
		}
		try {
			reader = new BufferedReader(new FileReader(filePath));
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * Closes the file opened for reading.
	 * <p>
	 * Should be called after finishing reading.
	 */
	public void close() {
		wordBuffer.clear();
		if (reader != null) {
			try {
				reader.close();
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
	}

	/**
	 * Parses the underlying file and returns the next word.
	 *
	 * @return a word as a <code>String<code>, or <code>null<code> if the end of the file has been
	 * reached.
	 */
	public String getNextWord() {

		if (!wordBuffer.isEmpty()) {
			return wordBuffer.poll();
		}

		// refill word buffer
		String[] nextWords = new String[0];
		while (nextWords.length == 0) {
			nextWords = parseNextLine();
			if (nextWords == null) {
				return null;
			}
		}
		String[] nextfinalWords = new String[nextWords.length];
		String[] auxWord;
		
		for (int i=0;i<nextWords.length;i++) {
			if ((nextWords[i] != null) && (nextWords[i].startsWith("\"") == true)) {
				auxWord = nextWords[i].split("\"");
				nextfinalWords[i] = auxWord[1];
			} else {
				nextfinalWords[i] = nextWords[i];
			}
		}
		wordBuffer.addAll(Arrays.asList(nextfinalWords));
		return wordBuffer.poll();
	}
	/**
	 * Aceasta metoda este folosita pentru parsarea unei linii
	 * @return returneaza linia impartita in functie de criteriile corespunzatoare
	 */
	private String[] parseNextLine() {
		String line;
		try {
			line = reader.readLine();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		if (line == null) {
			return null;
		}
		String[] line_aux;
		String[] final_line;
		line_aux = line.split("[^a-zA-Z0-9\"']+");
		final_line = new String [line_aux.length];
		int nr=0,i=1,j;
		boolean c1,c2;
		while (i < line_aux.length) {
			c1 = line_aux[i].startsWith("\"");
			c2 = line_aux[i].endsWith("\"");
			if (((c1 == true) && (c2 == true)) || ((c1 == false) && (c2 == false))) {
				final_line[nr] = line_aux[i];
				nr++;
				i++;
			} else {
				final_line[nr] = line_aux[i] + " ";

				i++;
				for (j=i;j<line_aux.length;j++) {
					boolean c3 = line_aux[j].endsWith("\"");

					if (c3 == true) {
						final_line[nr] = final_line[nr] + line_aux[j];
						i=j+1;
						nr++;
						break;
					} else {
						final_line[nr] = final_line[nr] + line_aux[j] + " ";
					}
				}

			}
		}
		return final_line;
	}

	public static void main(String[] args) {
		
		Hashtable<String,List<String>> h = new Hashtable<String,List<String>>();
		List<String> lista_chei = new ArrayList<String>();

		String cheie, valoare;
		int nr_chei = 0, nr_valori = 0, total_valori = 0;

		Main.open();
		cheie = Main.getNextWord();
		valoare = Main.getNextWord();

		while ((cheie != null) && (valoare != null)) {
			List<String> lista_auxiliara = new ArrayList<String>();
			if (lista_chei.contains(cheie) == false) {
				lista_chei.add(cheie);
				lista_auxiliara.add(valoare);
				h.put(cheie, lista_auxiliara);
				nr_chei++;
				total_valori++;
			} else {
				lista_auxiliara = h.get(cheie);
				lista_auxiliara.add(valoare);
				h.put(cheie, lista_auxiliara);
				total_valori++;
			}
			cheie = Main.getNextWord();
			valoare = Main.getNextWord();
		}
		
		nr_valori = total_valori/nr_chei;
		
		for (int i=0;i<nr_chei;i++) {
			System.out.print(lista_chei.get(i) + ",");
			
		}
		System.out.println();
		
		for (int i=0;i<nr_valori;i++) {
			for (int j=0;j<nr_chei;j++) {
				List<String> lista_auxiliara = new ArrayList<String>();
				lista_auxiliara = h.get(lista_chei.get(j));
				System.out.print(lista_auxiliara.get(i) + ",");
			}
			System.out.println();
		}
		
	}

}