%%------------------------------------------------------------------------------------------------------------
%%                                              ALGORITHME 1.1
%%------------------------------------------------------------------------------------------------------------
%%
%% Copyright Pierre Chatelier
%%       e-mail: pierre.chatelier@club-internet.fr
%%   page perso: http://perso.club-internet.fr/ktd
%%
%% Démonstration d'utilisation de mon package algorithme 1.1
%%
%%------------------------------------------------------
\documentclass[a4paper]{article}
\usepackage[french]{babel}       %francisation
\usepackage[latin1]{inputenc}    %pour pouvoir taper les accents directement dans ce fichier
\usepackage[french]{algorithme} %on pourrait ausi utiliser [english] pour la langue des mots-clés

\usepackage{vmargin}
\setmarginsrb{2cm}{2cm}{2cm}{2cm}{2cm}{0cm}{2cm}{0cm}
%1 est la marge gauche
%2 est la marge en haut
%3 est la marge droite
%4 est la marge en bas
%5 fixe la hauteur de l'entête
%6 fixe la distance entre l'entête et le texte
%7 fixe la hauteur du pied de page
%8 fixe la distance entre le texte et le pied de page


\begin{document}

\author{Pierre Chatelier\\
        \emph{e-mail: pierre.chatelier@club-internet.fr}\\
        \emph{http://perso.club-internet.fr/ktd}\\
       }
\title{Démonstration de l'utilisation du package``algorithme 1.1''}
\maketitle

\tableofcontents
\clearpage

%Table des algorithmes
\par La table des algorithmes est insérée ci-dessous par la commande:
\begin{verbatim}
\listofalgorithms
\end{verbatim}

\listofalgorithms

\clearpage

%Localisation
\section{Localisation automatique des mots-clés}
\noindent
Selon que \begin{verbatim}\usepackage[french]{algorithme}\end{verbatim} ou \begin{verbatim}\usepackage[english]{algorithme}\end{verbatim} ait été employé, les mots-clés seront dans la langue choisie. La langue est héritée du package \texttt{babel} si elle n'est pas spécifiée, et il s'agit du par défaut du Français.

%Comment écrire un algorithme
\section{Environnement algorithme}
Pour écrire un algorithme, on ouvre l'environnement adéquat. Il est automatiquement encapsulé dans une boîte.\\~\\
\noindent
\begin{tabular}{ll}
\begin{minipage}{.4\linewidth}
\begin{verbatim}

\begin{algorithme}
Je suis un algorithme\\
\end{algorithme}

\end{verbatim}
\end{minipage}
&
\begin{minipage}{.5\linewidth}
\begin{algorithme}
Je suis un algorithme\\
\end{algorithme}
\end{minipage}
\end{tabular}\\

\section{Légende: $\backslash$caption}
Pour ajouter une légende, et voir l'algorithme apparaître dans la table des algorithmes, on utilise le \emph{$\backslash$caption}. La légende est inscrite en dessous.\\~\\
\noindent
\begin{tabular}{ll}
\begin{minipage}{.4\linewidth}
\begin{verbatim}

\begin{algorithme}
Encore un algorithme\\
\caption{J'aime les légendes}
\end{algorithme}

\end{verbatim}
\end{minipage}
&
\begin{minipage}{.5\linewidth}
\begin{algorithme}
Encore un algorithme\\%
\caption{J'aime les légendes}
\end{algorithme}
\end{minipage}
\end{tabular}\\

%Typage
\section{Typage: $\backslash$Type}
On peut préciser le type des variables.\\~\\
\noindent
\begin{tabular}{ll}
\begin{minipage}{.4\linewidth}
\begin{verbatim}

\begin{algorithme}
\Type{x}{bool}; \\
\Type{y}{integer}; \\
x $\leftarrow$ \True; \\
y $\leftarrow$ 0;
\caption{Typage}
\end{algorithme}

\end{verbatim}
\end{minipage}
&
\begin{minipage}{.5\linewidth}
\begin{algorithme}
\Type{x}{bool}; \\
\Type{y}{integer}; \\
x $\leftarrow$ \True; \\
y $\leftarrow$ 0;
\caption{Typage}
\end{algorithme}
\end{minipage}\\
\end{tabular}

%Commentaires
\section{Commentaires: $\backslash$Rem}
On peut ajouter des commentaires.\\~\\
\noindent
\begin{tabular}{ll}
\begin{minipage}{.4\linewidth}
\begin{verbatim}

\begin{algorithme}
\Rem{$x$ est initialisé à 0}\\
x $\leftarrow$ 0;
\caption{Commentaires}
\end{algorithme}

\end{verbatim}
\end{minipage}
&
\begin{minipage}{.5\linewidth}
\begin{algorithme}
\Rem{$x$ est initialisé à 0}\\
x $\leftarrow$ 0;
\caption{Commentaires}
\end{algorithme}
\end{minipage}
\end{tabular}\\

%Tant que
\section{Boucle tant que: $\backslash$While ou $\backslash$WhileDo}
Boucle "Tant que"\\
\noindent
\begin{tabular}{ll}
\begin{minipage}{.4\linewidth}
\begin{verbatim}

\begin{algorithme}
\Type{x}{entier}; \\
x $\leftarrow$ 1; \\
\While{x $\leq$ 5}
    {x $\leftarrow$ x+1;}
\caption{Tant que}
\end{algorithme}

\end{verbatim}
\end{minipage}
&
\begin{minipage}{.5\linewidth}
\begin{algorithme}
\Type{x}{entier}; \\
x $\leftarrow$ 1; \\
\While{x $\leq$ 5}
    {x $\leftarrow$ x+1;}
\caption{Tant que}
\end{algorithme}
\end{minipage}
\end{tabular}

%Tant que (version courte)
\section{Boucle ``Tant que (version courte)'': $\backslash$While* ou $\backslash$WhileDo*}
\noindent
\begin{tabular}{ll}
\begin{minipage}{.4\linewidth}
\begin{verbatim}

\begin{algorithme}
\Type{x}{entier}; \\
x $\leftarrow$ 0; \\
\While*{x $\leq$ 5}
{x $\leftarrow$ x+1}
\caption{Tant que
(version courte)}
\end{algorithme}

\end{verbatim}
\end{minipage}
&
\begin{minipage}{.5\linewidth}
\begin{algorithme}
\Type{x}{entier}; \\
x $\leftarrow$ 0; \\
\WhileDo*{x $\leq$ 5}{x $\leftarrow$ x+1}
\caption{Tant que (version courte)}
\end{algorithme}
\end{minipage}
\end{tabular}

Pratiquement toutes les structures de contrôle de ce package ont une forme courte (forme étoilée dans le fichier source \LaTeXe) (Une exception : le Selon que).

%Répéter jusqu'a
\section{Boucle ``Répéter\ldots jusqu'à'': $\backslash$Repeat ou $\backslash$RepeatUntil}
\noindent
\begin{tabular}{ll}
\begin{minipage}{.4\linewidth}
\begin{verbatim}

\begin{algorithme}
\Type{x}{entier}; \\
x $\leftarrow$ 0; \\
\Repeat{x $\leftarrow$ x+1;}
{x = 6}
\caption{Répéter \ldots jusqu'à}
\end{algorithme}

\end{verbatim}
\end{minipage}
&
\begin{minipage}{.5\linewidth}
\begin{algorithme}
\Type{x}{entier}; \\
x $\leftarrow$ 0; \\
\Repeat{x $\leftarrow$ x$+1$;}{x = 6}
\caption{Répéter \ldots jusqu'à}
\end{algorithme}
\end{minipage}
\end{tabular}

%boucle Pour
\section{Boucle ``Pour'' standard: $\backslash$ForFromTo ou $\backslash$ForFromToDo}
\noindent
\begin{tabular}{ll}
\begin{minipage}{.4\linewidth}
\begin{verbatim}

\begin{algorithme}
\Type{x}{entier}; \\
\ForFromTo{x}{0}{5}
    {x $\leftarrow$ x+1}
\caption{boucle Pour standard}
\end{algorithme}

\end{verbatim}
\end{minipage}
&
\begin{minipage}{.5\linewidth}
\begin{algorithme}
\Type{x}{entier}; \\
\ForFromTo{x}{0}{5}{x $\leftarrow$ x+1}
\caption{boucle Pour standard}
\end{algorithme}
\end{minipage}
\end{tabular}

%boucle Pour en style C/ C-style For loop
\section{Boucle ``Pour'' de style C: $\backslash$For ou $\backslash$ForDo}
\noindent
\begin{tabular}{ll}
\begin{minipage}{.4\linewidth}
\begin{verbatim}

\begin{algorithme}
\Type{x}{entier}; \\
\For{(x=0 ; x $\leq$ 5; x++)}
    {\Rem{Rien à faire, du coup}}
\caption{boucle Pour en style C}
\end{algorithme}

\end{verbatim}
\end{minipage}
&
\begin{minipage}{.5\linewidth}
\begin{algorithme}
\Type{x}{entier}; \\
\For{(x=0 ; x $\leq$ 5; x++)}{\Rem{Rien à faire, du coup}}
\caption{boucle Pour en style C}
\end{algorithme}
\end{minipage}
\end{tabular}

%Si ... alors / If ... then
\section{Si\ldots Alors: $\backslash$If ou $\backslash$IfThen}
\noindent
\begin{tabular}{ll}
\begin{minipage}{.4\linewidth}
\begin{verbatim}

\begin{algorithme}
\Type{x}{entier}; \\
x $\leftarrow$ 0; \\
\If{x = 0}{x $\leftarrow$ 1;}
\caption{Si \ldots alors \ldots}
\end{algorithme}

\end{verbatim}
\end{minipage}
&
\begin{minipage}{.5\linewidth}
\begin{algorithme}
\Type{x}{entier}; \\
x $\leftarrow$ 0; \\
\If{x = 0}{x $\leftarrow$ 1;}
\caption{Si \ldots alors \ldots}
\end{algorithme}
\end{minipage}
\end{tabular}

%Si ... alors ... sinon / If ... then ... else
\section{Si\ldots Alors\ldots Sinon: $\backslash$IfThenElse}
\noindent
\begin{tabular}{ll}
\begin{minipage}{.4\linewidth}
\begin{verbatim}

\begin{algorithme}
\Type{x}{entier}; \\
x $\leftarrow$ 0; \\
\IfThenElse{x = 0}
    {x $\leftarrow$ 1;}
    {x $\leftarrow$ 2;}
\caption{Si \ldots alors
\ldots sinon}
\end{algorithme}

\end{verbatim}
\end{minipage}
&
\begin{minipage}{.5\linewidth}
\begin{algorithme}
\Type{x}{entier}; \\
x $\leftarrow$ 0; \\
\IfThenElse{x = 0}{x $\leftarrow$ 1;}{x $\leftarrow$ 2;}
\caption{Si \ldots alors \ldots sinon}
\end{algorithme}
\end{minipage}
\end{tabular}

%Utilisation de quelques mots-clefs
\section{Quelques mots-clés: $\backslash$True, $\backslash$False, $\backslash$Or, $\backslash$And, $\backslash$Not}
\noindent
\begin{tabular}{ll}
\begin{minipage}{.4\linewidth}
\begin{verbatim}

\begin{algorithme}
\Type{x}{bool}; \\
\If{condition1 \And
    (Condition2 \Or
    \Not(condition3))}
    {x $\leftarrow$ \False}
\caption{Utilisation de quelques
mots-clefs}
\end{algorithme}

\end{verbatim}
\end{minipage}
&
\begin{minipage}{.5\linewidth}
\begin{algorithme}
\Type{x}{bool}; \\
\If{condition1 \And (Condition2 \Or \Not(condition3))}
        {x $\leftarrow$ \False}
\caption{Utilisation de quelques mots-clefs}
\end{algorithme}
\end{minipage}
\end{tabular}

%Selon que
\section{Structure ``Selon que'': $\backslash$Switch}
\noindent
\begin{tabular}{ll}
\begin{minipage}{.4\linewidth}
\begin{verbatim}

\begin{algorithme}
\Type{x}{entier}; \\
x $\leftarrow$ 0; \\
\Switch{
    x = 0 : x $\leftarrow$ 1;\\
    x = 1 : x $\leftarrow$ 11;\\
    x = 2 : x $\leftarrow$ 111;
}
\caption{Selon que}
\end{algorithme}

\end{verbatim}
\end{minipage}
&
\begin{minipage}{.5\linewidth}
\begin{algorithme}
\Type{x}{entier}; \\
x $\leftarrow$ 0; \\
\Switch{
    x = 0 : x $\leftarrow$ 1;\\
    x = 1 : x $\leftarrow$ 11;\\
    x = 2 : x $\leftarrow$ 111;
}
\caption{Selon que}
\end{algorithme}
\end{minipage}
\end{tabular}

%Selon que raté
\subsection{Selon que raté}
\noindent
\begin{tabular}{ll}
\begin{minipage}{.4\linewidth}
\begin{verbatim}

\begin{algorithme}
\Type{x}{entier}; \\
x $\leftarrow$ 0; \\
\Type{y}{entier}; \\
y $\leftarrow$ 0; \\
\Switch{
    x = 0 : x $\leftarrow$ 1;\\
            y $\leftarrow$ 2;\\
    x = 1 : x $\leftarrow$ 11;\\
            y $\leftarrow$ 22;\\
    x = 2 : x $\leftarrow$ 111;\\
            y $\leftarrow$ 222;\\
}
\caption{Selon que raté}
\end{algorithme}

\end{verbatim}
\end{minipage}
&
\begin{minipage}{.5\linewidth}
\begin{algorithme}
\Type{x}{entier}; \\
x $\leftarrow$ 0; \\
\Type{y}{entier}; \\
y $\leftarrow$ 0; \\
\Switch{
    x = 0 : x $\leftarrow$ 1;\\   y $\leftarrow$ 2;\\
    x = 1 : x $\leftarrow$ 11;\\  y $\leftarrow$ 22;\\
    x = 2 : x $\leftarrow$ 111;\\ y $\leftarrow$ 222;\\
}
\caption{Selon que raté}
\end{algorithme}
\end{minipage}
\end{tabular}

%Selon que corrigé
\subsection{Selon que corrigé}
\noindent
\begin{tabular}{ll}
\begin{minipage}{.4\linewidth}
\begin{verbatim}

\begin{algorithme}
\Type{x}{entier}; \\
x $\leftarrow$ 0; \\
\Type{y}{entier}; \\
y $\leftarrow$ 0; \\
\Switch{
  x=0:\Block{
        x $\leftarrow$ 1;\\
        y $\leftarrow$ 2;\\
       }\\
  x=1:\Block{
        x $\leftarrow$ 11;\\
        y $\leftarrow$ 22;\\
       }\\
  x=2:\Block{
        x $\leftarrow$ 111;\\
        y $\leftarrow$ 222;\\
       }
}
\caption{Selon que corrigé}
\end{algorithme}

\end{verbatim}
\end{minipage}
&
\begin{minipage}{.5\linewidth}
\begin{algorithme}
\Type{x}{entier}; \\
x $\leftarrow$ 0; \\
\Type{y}{entier}; \\
y $\leftarrow$ 0; \\
\Switch{
    x = 0 : \Block{
                x $\leftarrow$ 1;\\
                y $\leftarrow$ 2;\\
            }\\
    x = 1 : \Block{
                x $\leftarrow$ 11;\\
                y $\leftarrow$ 22;\\
            }\\
    x = 2 : \Block{
                x $\leftarrow$ 111;\\
                y $\leftarrow$ 222;\\
            }
}
\caption{Selon que corrigé}
\end{algorithme}
\end{minipage}
\end{tabular}

%fonction
\section{Fonction: $\backslash$Function}
\noindent
\begin{tabular}{ll}
\begin{minipage}{.4\linewidth}
\begin{verbatim}

\begin{algorithme}
\Function{maFonction}
    {x: \textbf{entier}}{entier}
{
    x $\leftarrow$ x*x*x;\\
    \Return x;
}
\caption{fonction}
\end{algorithme}

\end{verbatim}
\end{minipage}
&
\begin{minipage}{.5\linewidth}
\begin{algorithme}
\Function{maFonction}{x: \textbf{entier}}{entier}
{
    x $\leftarrow$ x*x*x;\\
    \Return x;
}
\caption{fonction}
\end{algorithme}
\end{minipage}
\end{tabular}

%Procedure
\section{Procédure}
\noindent
\begin{tabular}{ll}
\begin{minipage}{.4\linewidth}
\begin{verbatim}

\begin{algorithme}
\Procedure{maProcedure}{x}
{
    x $\leftarrow$ 0;
}
\caption{procédure}
\end{algorithme}

\end{verbatim}
\end{minipage}
&
\begin{minipage}{.5\linewidth}
\begin{algorithme}
\Procedure{maProcedure}{x}
{
    x $\leftarrow$ 0;
}
\caption{procédure}
\end{algorithme}
\end{minipage}
\end{tabular}

%Imbrication
\clearpage
\section{Imbrication des commandes}
L'intérêt de ce package est bien sûr de pouvoir imbriquer les commandes.

\begin{tabular}{ll}
\begin{minipage}{.4\linewidth}
\begin{verbatim}

\begin{algorithme}
\Function{max}
    {\Type{x, y, z}{entier}}
    {entier}
{
  \Type{result}{entier}\\
  \IfThenElse{x $\leq$ y}
  {
    \IfThenElse{y $\leq$ z}
      {result $\leftarrow$ z;}
      {result $\leftarrow$ y;}
  }
  {
    \IfThenElse{x $\leq$ z}
      {result $\leftarrow$ z;}
      {result $\leftarrow$ x;}
  }\\
  \Return{result};
}
\caption{Un algorithme complet}
\end{algorithme}

\end{verbatim}
\end{minipage}
&
\begin{minipage}{.5\linewidth}
\begin{algorithme}
\Function{max}
    {\Type{x, y, z}{entier}}
    {entier}
{
  \Type{result}{entier}\\
  \IfThenElse{x $\leq$ y}
  {
    \IfThenElse{y $\leq$ z}
      {result $\leftarrow$ z;}
      {result $\leftarrow$ y;}
  }
  {
    \IfThenElse{x $\leq$ z}
      {result $\leftarrow$ z;}
      {result $\leftarrow$ x;}
  }\\
  \Return{result};
}
\caption{Un algorithme complet}
\end{algorithme}
\end{minipage}
\end{tabular}

\end{document}

