Code télégram

Fermé
salam - 19 déc. 2005 à 13:46
 Utilisateur anonyme - 19 déc. 2005 à 14:56
bonjour;
je cherche un programme en c ,c++ ou java qui consiste à traiter les télégrammes
sachant qu'on a comme entrée un fichier texte teleg.txt
et comme sortie un télegramme avec un compteur qui compte le nombre de mot écrits ds le prog.
ainsi un mot ne peut contenir que 12 caractère (et si y'a plus en tranque le mot en 2)
et une ligne ne peut contenir que 80 cara.
merci bcp

1 réponse

Utilisateur anonyme
19 déc. 2005 à 14:56
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

/*
 * Created on Dec 19, 2005
 *
 */

/**
 * @author Fery.P
 *
 */
public class Telegram {
	public static final int MAX_CHAR_PER_LINE = 80;
	public static final int MAX_CHAR_PER_WORD = 12;

	private int wordsCount;
	private StringBuffer text;
	private StringBuffer currentLine;
	private BufferedReader reader;

	public Telegram(String fileName) throws TelegException {
		super();
		wordsCount = 0;
		text = new StringBuffer();
		try {
			parseFile(fileName);
		} catch (IOException e) {
			throw new TelegException(TelegException.EXC_CANNOT_OPEN_FILE);
		} finally {
			try {
				reader.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void parseFile(String fileName) throws IOException {
		reader = new BufferedReader(new FileReader(fileName));
		StringTokenizer tok = null;
		String line = "";
		String wordTemp = "";
		currentLine = new StringBuffer();

		while ((line = reader.readLine()) != null) {
			System.out.println(line);
			tok = new StringTokenizer(line, " ");
			while (tok.hasMoreElements()) {
				String word = (String)tok.nextElement();
				while (word.length() > MAX_CHAR_PER_WORD) {
					wordTemp = word.substring(0, MAX_CHAR_PER_WORD);
					append(wordTemp);
					word = word.substring(MAX_CHAR_PER_WORD);
				}
				append(word);
			}

		}
	}

	private void append(String word6char) {
		int calculatedLineLength = currentLine.length() + word6char.length();
		if (calculatedLineLength > MAX_CHAR_PER_LINE) {
			int diff = calculatedLineLength - MAX_CHAR_PER_LINE;
			currentLine.append(word6char.substring(0, word6char.length() - diff)).append("\n\r");
			wordsCount++;
			text.append(currentLine);
			currentLine.delete(0, currentLine.length());
			currentLine.append(word6char.substring(word6char.length() - diff)).append(" ");
			wordsCount++;
		} else if (calculatedLineLength == MAX_CHAR_PER_LINE) {
			currentLine.append(word6char).append("\n\r");
			text.append(currentLine);
			wordsCount++;
			currentLine.delete(0, currentLine.length());
		} else {
			currentLine.append(word6char).append(" ");
			wordsCount++;
		}

	}

	class TelegException extends Exception {
		public static final String EXC_CANNOT_OPEN_FILE = "Impossible d'ouvrir le fichier";
		public static final String EXC_DEFAULT_MESSAGE = "Un problème indéterminé est survenu";
		private String msg;

		public TelegException() {
			this(EXC_DEFAULT_MESSAGE);
		}

		public TelegException(String msg) {
			this.msg = msg;
		}

		public String getMessage() {
			return msg;
		}
	}

	private String getText() {
		return text.toString();
	}

	public int getWordsCount() {
		return wordsCount;
	}


	public static void main(String[] args) {
		try {
			String filePath = "d:/teleg.txt";
			Telegram tel = new Telegram(filePath);
			System.out.println("Le fichier '"+filePath+"' contient "+tel.getWordsCount()+" mots");
			System.out.println(tel.getText());
		} catch (TelegException e) {
			e.printStackTrace();
		}
	}
}


A contrôler, je l'ai pondu en vitesse. Pas le temps de tester.

;-)
HackTrack
0