(* Exercice 1 *) let pile = Stack.create ();; Stack.push 17 pile;; Stack.pop pile ;; let tete pile = if Stack.is_empty pile then failwith "pile vide" else begin let a = Stack.pop pile in Stack.push a pile ; a end;; tete pile;; let creation_pile liste = let ma_pile = Stack.create () in let rec aux liste = match liste with | [] -> ma_pile | a::reste -> begin Stack.push a ma_pile ; aux reste end in aux liste ;; let pile_2 = creation_pile [1;2;3;4;5];; tete pile_2;; Stack.pop pile_2;; let taille pile = let pile_stock = Stack.create () in let rec compte pile nb = if Stack.is_empty pile then nb else begin let sommet = Stack.pop pile in Stack.push sommet pile_stock ; compte pile (nb+1) end in let nb = compte pile 0 in let rec reconstruit () = if Stack.is_empty pile_stock then () else begin Stack.push ( Stack.pop pile_stock ) pile ; reconstruit () ; end in reconstruit () ; nb;; (* Exercice 3 *) let associe k_1 k_2 = (k_1 = '(' && k_2 = ')' ) || (k_1 = '[' && k_2 = ']' ) || (k_1 = '{' && k_2 = '}' );; associe '{' '}';; let ouvrante = ['[' ; '(' ; '{' ];; let fermante = [ ']' ; ')' ; '}'];; let bon_parenthesage phrase = let pile_stock = Stack.create () in let booleen = ref true in let indice = ref 0 in let taille = String.length phrase in while ( !indice < taille ) && !booleen do let lettre = phrase.[ !indice] in if List.mem lettre ouvrante then Stack.push lettre pile_stock ; if List.mem lettre fermante then begin if Stack.is_empty pile_stock then booleen := false else let k = Stack.pop pile_stock in booleen := associe k lettre ; end ; incr indice ; done; Stack.is_empty pile_stock && !booleen ;; let phrase_exemple ="(1+2[x+2] -3 {5x+3})";; bon_parenthesage phrase_exemple ;; let phrase_exemple_2 ="(1+2[x+{ 2] -5x+3})";; bon_parenthesage phrase_exemple_2;;