(* Détails du fichier

Description : TP Pascal noté n°1
Auteur : INGOUFF Christian
Date : 9/12/11
Version : 1.0

*)

program TPNote;

(*
Fichier : noRepeat
Description : Retourne la chaîne sans répétitions de lettres
Auteur : INGOUFF Christian
Date : 9/12/11 (1.0)
Version : 1.0
Variables : string1 : chaîne de caractères
Retour : Chaîne sans répétitions de lettres
Note : Ne marche pas.
*)

function noRepeat(string1:string):string;
var
i, j, lg : integer;
same : boolean;

begin
	lg := length(noRepeat);
	noRepeat := '';
	for i := 1 to lg-1 do
	begin
		j := i+1;
		while j <= lg do
		begin
			same := string1[i] = string1[j];
			if same then
				j := lg + 1
			else
				j := j + 1;
		end;
		if not same then
			noRepeat := string1[i] + noRepeat(string1);
	end;
end;

(*
Fichier : carEnCommun
Description : Exercice 1
Auteur : INGOUFF Christian
Date : 9/12/11 (1.0)
Version : 1.0
Variables : string1 : chaîne de caractères
	    string2 : chaîne de caractères
Retour : Les lettres en commun
*)

function carEnCommun(string1,string2:string):string;

var
i, j, lg1, lg2 : integer;

begin
	lg1 := length(string1);
	lg2 := length(string2);	
	carEnCommun := '';
	for i := 1 to lg1 do
	begin
		j := 1;
		while j <= lg2 do
			if string1[i] = string2[j] then
			begin
				carEnCommun := carEnCommun + string1[i];
				j := lg2 + 1;
			end
			else
				j := j+1;
	end;
	carEnCommun := noRepeat(carEnCommun);
(* NoRepeat ne marche pas dans cette version là. Les lettres en commun sont répétées. *)
end;

(*
Fichier : cryptic
Description : Exercice 2
Auteur : INGOUFF Christian
Date : 9/12/11 (1.0)
Version : 1.0
Variables : string1 : chaîne de caractères
	    offset (entier) : décalage alphabétique
	    i (entier) : pour la position du caractère
	    acc (chaîne) : accumulateur
Retour : La chaîne (string1) cryptée.
*)

function cryptic(string1:string ; offset:integer ; i:integer ; acc:string):string;
var
switchint : integer;
switchstr : string;
begin
	if i <= length(string1) then
	begin
		switchint := ord(string1[i]) + offset;
		switchstr := chr(switchint);
		cryptic := cryptic(string1,offset,i+1,acc+switchstr);
	end
	else cryptic := acc;
end;

function crypticCall(string1 : string ; offset : integer):string;
begin
	crypticCall := cryptic(string1,offset,1,'');
end;

(*
Fichier : uberfibonacci
Description : Exercice 3
Auteur : INGOUFF Christian
Date : 9/12/11 (1.0)
Version : 1.0
Variables : n : n dans f(n), entier naturel
	    f0 : correspond à f(n-3)
	    f1 : correspond à f(n-2)
	    f2 : correspond à f(n-1)
Retour : La valeur de f(n) pour un n défini.
*)

function uberfibonacci(n,f0,f1,f2:integer):integer;
(* Je vais supposer que n appartient aux entiers naturels, donc que les résultats vont être des entiers naturels. Nisrine ne serait pas contente, mais je m'égare. *)
begin
	if n < 2 then
	begin
		uberfibonacci := n;
	end
	else if n = 2 then
	begin
		uberfibonacci := f2;
	end
	else
	begin
		uberfibonacci := uberfibonacci(n-1,f1,f2,f0+f1+f2);
	end;
end;

function uberfibonacciCall(n:integer):integer;
begin
	uberfibonacciCall := uberfibonacci(n,0,1,2);
end;

(* Désolé, je ne vois pas trop comment coder le menu avec une fonction. Je vais donc coder une procédure. *)

procedure menu(choice:integer);
var
string1,string2 : string;
resultStr : string;
int1 : integer;
resultInt : integer;

begin
	case choice of
		0 :	writeln('Merci d''avoir utilisé/corrigé ce programme ! A bientôt.');
		1 :	begin
				writeln('Bonjour ! Cette fonction vous permettra de connaître les caractères en commun entre 2 chaînes que vous définirez.');
				writeln('Entrez la première chaîne.');
				readln(string1);
				writeln('Entrez la deuxième chaîne.');
				readln(string2);
				resultStr := carEnCommun(string1,string2);
				writeln('Les caractères en commun sont : ',resultStr);
				readln;
			end;
		2 :	begin
				writeln('Bonjour ! Cette fonction vous permettra de crypter une chaîne de caractères selon un décalage alphabétique que vous définirez afin de la rendre illisible.');
				writeln('Entrez la chaîne à crypter.');
				readln(string1);
				writeln('Entrez le décalage alphabétique.');
				readln(int1);
				resultStr := crypticCall(string1,int1);
				writeln('Votre message crypté est le suivant :');
				writeln(resultStr);
				readln;
			end;
		3 :	begin
				writeln('Bonjour ! Cette fonction vous permettra de calculer la valeur de f(n), pour un n défini.');
				repeat
					writeln('Entrez n.');
					readln(int1);
					if int1 < 0 then
						writeln('Cette fonction est définie pour les entiers positifs !');
				until int1 >= 0;
				resultInt := uberfibonacciCall(int1);
				writeln('f(n) = ',resultInt);
				readln;
			end;
(*		4 :	begin
				writeln('Bonjour ! Cette procédure vous permettra de connaître le mot le plus court et le plus long d''une chaîne définie.');
				writeln('Entrez une phrase.');
				readln(string1);
				shortlong(string1,short,long);
				writeln('Le mot le plus long est "',long,'" et le mot le plus court est "',short,'".');
				readln;
			end;			*)
	else
		writeln('Fonction non reconnue !');
	end;
end;

var
choice : integer;

begin
	choice := -1;
	while choice <> 0 do
	begin
		writeln('Choisissez un exercice :');
		writeln('1 pour l''exercice 1');
		writeln('2 pour l''exercice 2');
		writeln('3 pour l''exercice 3');
		writeln('0 pour quitter');
		readln(choice);
		menu(choice);
	end;
	readln;
end.
