Php algo td1

De The Linux Craftsman
Révision datée du 5 janvier 2022 à 19:04 par Jc.forton (discussion | contributions) (→‎Partie B)
(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)
Aller à la navigation Aller à la recherche

Sujet

Sujet_TD1.pdf

Partie A

Exercice 1

FONCTION code_mot_chiffre(mot)
VAR
   resultat : liste d'entiers
   trouve: booléen
   i, j : entier
   j <- 0

   POUR TOUT j DE mot FAIRE
      i <- 0
      trouve <- faux
      TANT QUE !trouve
         SI alphabet[i] = mot[j]
            resultat[j] <- alphabet[mot[j]]
            trouve <- vrai
         FIN SI
         i++
      FIN TANT QUE
   FIN POUR

   RETOURNER resultat
FIN FONCTION


Exercice 2

FONCTION code_chiffre_mot(code_chiffre)
VAR
   resultat : liste de caractères
   j : entier
   j <- 0

   POUR TOUT j DE code_chiffre FAIRE
      resultat[j] <- alphabet[code_chiffre[j]]
   FIN POUR

   RETOURNER resultat
FIN FONCTION


Exercice 3

FONCTION code_chiffre_chiffre(code_chiffre)
VAR
   resultat : tableau

   POUR TOUT j DE code_chiffre FAIRE
      resultat[j] <- (2 * code_chiffre[j] + 3)%26
   FIN POUR

   RETOURNER resultat
FIN FONCTION

Partie B

Exercice 4

function code_mot_chiffre($mot) {
	global $alphabet;
	$result = [];
	$i = 0;
	foreach ( $mot as $lettre_mot ) {
		for($i = 0; $i < sizeof($alphabet); $i ++) {
			if ($alphabet[$i] == $lettre_mot) {
				$result [] = $i;
				break;
			}
		}
	}
	return $result;
}


Exercice 5

function code_chiffre_mot($code) {
	global $alphabet;
	$result = [];
	foreach ( $code as $index) {
		$result[] = $alphabet[$index];
	}
	return $result;
}


Exercice 6

function code_chiffre_chiffre($code) {
	global $alphabet;
	$result = [];
	foreach ( $code as $index) {
		$result[] = (2 * $index + 3)%26;
	}
	return $result;
}


Exercice 7

<?php
$alphabet = array (
		"A",
		"B",
		"C",
		"D",
		"E",
		"F",
		"G",
		"H",
		"I",
		"J",
		"K",
		"L",
		"M",
		"N",
		"O",
		"P",
		"Q",
		"R",
		"S",
		"T",
		"U",
		"V",
		"W",
		"X",
		"Y",
		"Z" 
);

function code_mot_chiffre($mot) {
	global $alphabet;
	$result = [];
	$i = 0;
	foreach ( $mot as $lettre_mot ) {
		for($i = 0; $i < sizeof ( $alphabet ); $i ++) {
			if ($alphabet [$i] == $lettre_mot) {
				$result [] = $i;
				break;
			}
		}
	}
	return $result;
}

function code_chiffre_mot($code) {
	global $alphabet;
	$result = [];
	foreach ( $code as $index ) {
		$result [] = $alphabet [$index];
	}
	return $result;
}

function code_chiffre_chiffre($code) {
	global $alphabet;
	$result = [];
	foreach ( $code as $index ) {
		$result [] = (2 * $index + 3) % 26;
	}
	return $result;
}

function main($argv) {
	/* Récupération du mot a coder */
	if (sizeof ( $argv ) == 1) {
		echo "Spécifier un mot en paramètre !";
		exit ( 1 );
	}
	/* Lecture du mot a coder */
	$mot = $argv [1];
	/* Transformation du mot en tableau */
	$mot = str_split ( $mot );
	/* Codage du mot en chiffres */
	$code_chiffre1 = code_mot_chiffre ( $mot );
	/* Lecture du tableau de chiffre */
	echo "Transformation du mot en chiffre : \n";
	print_r ( $code_chiffre1 );
	/* Codage affine */
	$code_chiffre2 = code_chiffre_chiffre ( $code_chiffre1 );
	/* Lecture du tableau de chiffres codés */
	echo "Application de la fonction affine : \n";
	print_r ( $code_chiffre2 );
	/* Codage des chiffres au mot */
	$mot_code = code_chiffre_mot ( $code_chiffre2 );
	echo "Mot codé : \n";
	print_r( $mot_code );
	exit ( 0 );
}

main ($argv);
?>

Exécution

# php -f TD1.php AMI
Transformation du mot en chiffre :
Array
(
    [0] => 0
    [1] => 12
    [2] => 8
)
Application de la fonction affine :
Array
(
    [0] => 3
    [1] => 1
    [2] => 19
)
Mot codé :
Array
(
    [0] => D
    [1] => B
    [2] => T
)