Exercices

Pour la prochaine fois, préparer les deux exercices du TD4 et ceux du TD5 (pas besoin d'annoter).

Vous pouvez vous aider de ce rappel de cours.

Correction du TD4

Exercice 1

public class BasiqueTrad {
	public static void main(String[] args) {
		int ic=0;
		int[] mem=new int[1000];
		Stack<Integer> p=new Stack<Integer>();
		while (true) {
			switch (ic++) {
				case 0: System.out.println("Entrée main"); break;
				case 1: p.push(ic); ic=300; break;
				case 2: p.push(ic); ic=100; break;
				case 3: System.out.println("Sortie main"); break;
				case 4: System.exit(0);
				
				case 100: System.out.println("Entrée h"); break;
				case 101: p.push(ic); ic=200; break;
				case 102: System.out.println("Sortie h"); break;
				case 103: ic=p.pop(); break;
				
				case 200: System.out.println("Entrée g"); break;
				case 201: p.push(ic); ic=300; break;
				case 202: System.out.println("Sortie g"); break;
				case 203: ic=p.pop(); break;
				
				case 300: System.out.println("Milieu f"); break;
				case 301: ic=p.pop(); break;
			}
		}
	}
}

Exercice 2

public class Partiel2018Trad {
	public static void main(String[] args) {
		int ic=0;
		int[] mem=new int[1000];
		Stack<Integer> p=new Stack<Integer>();
		while (true) {
			switch (ic++) {
				case 0: mem[0] = 0; break;
				case 1: mem[2] = 20; break;
				case 2: mem[3] = 20; break;
				case 3: p.push(ic); ic = 300; break;
				case 4: if (mem[0]>4) ic += 3; break;
				case 5: p.push(ic); ic = 100; break;
				case 6: p.push(ic); ic = 300; break;
				case 7: ic -= 4; break;
				case 8: System.exit(0);
				
				case 100: System.out.println(" début tirs"); break;
				case 101: mem[1] = 1; break;
				case 102: if (mem[1]>5) ic += 3; break;
				case 103: p.push(ic); ic = 200; break;
				case 105: mem[1]++; break;
				case 106: ic -= 4; break;
				case 107: System.out.println(" fin tirs"); break;
				case 108: ic = p.pop(); break;
				
				case 200: System.out.println("  cible " + mem[1]); break;
				case 201: mem[3]--; break;
				case 202: ic=p.pop(); break;
				
				case 300: mem[0]++; break;
				case 301: System.out.println("dans le tour " + (mem[0])); break;
				case 302: mem[2] -= 4; break;
				case 303: ic = p.pop(); break;
			}
		}
	}
}

Adapté de la solution de Maryline Z.