Utilisateur:Ambigraphe/Algorithmique sous LaTeX

Une page de Wikipédia, l'encyclopédie libre.

Ce code LaTeX peut être enregistré sous le nom algopack.sty et utilisé à l'intérieur d'un fichier .tex à l'aide de la commande \usepackage{algopack} pour écrire des algorithmes et programmes à l'aide d'une syntaxe unifiée.

% CC-BY-SA Christophe Boilley, 2011

% Vous êtes libres de partager, distribuer et transmettre ce fichier,

% de l'adapter et de le modifier.

% Vous devez conserver ces lignes indiquant l'auteur du fichier et

% la licence CC-BY-SA qui doit être transférée sur les fichiers dérivés.

\ProvidesPackage{algopack}

\usepackage[latin1]{inputenc}

\usepackage[T1]{fontenc}

\usepackage[frenchb]{babel}

\usepackage{tikz}

\usepackage{pifont}

\newenvironment{algo}{

\newcommand{\programme}[3]{\begin{tabular}[t]{@{}l}

Algorithme {\it ##1} \\ ##2 ##3 \end{tabular}}

\newcommand{\ind}{\quad}

\newcommand{\ap}{'}

\newcommand{\sepdec}{,}

\newcommand{\typeentier}{\mbox{entier}}

\newcommand{\typeentierlong}{\mbox{entier long}}

\newcommand{\typefloat}{\mbox{décimal}}

\newcommand{\constantes}[1]{Constantes\\ \ind$\begin{array}{l@{ = }l} ##1 \end{array}$\\}

\newcommand{\variables}[1]{Variables\\ \ind$\begin{array}{l@{\ :\ }l} ##1 \end{array}$\\}

\renewcommand{\;}{\ ;\quad}

\newcommand{\affecter}[2]{$##1 \longrightarrow ##2$}

\newcommand{\effacer}{tout effacer}

\newcommand{\pause}[1]{\afficher{##1} (pause)}

\newcommand{\afficher}[1]{afficher $##1$}

\newcommand{\placer}[3]{placer $##3$ en ligne $##1$ et colonne $##2$}

\newcommand{\texte}[1]{ $\og ##1 \fg$ }

\newcommand{\ccat}{,\ }

\newcommand{\saisir}[1]{saisir $##1$}

\newcommand{\demander}[2]{\afficher{\texte{##1}}\\ \saisir{##2}}

\newcommand{\touche}{$touche$}

\newcommand{\fois}{\times}

\newcommand{\produit}[2]{##1 \fois ##2}

\newcommand{\carre}[1]{##1^2}

\newcommand{\racine}[1]{\sqrt{##1}}

\renewcommand{\sin}[1]{\mathrm{sin}\left(##1\right)}

\renewcommand{\cos}[1]{\mathrm{cos}\left(##1\right)}

\renewcommand{\exp}[1]{\mathrm{exp}\left(##1\right)}

\renewcommand{\ln}[1]{\mathrm{ln}\left(##1\right)}

\newcommand{\abs}[1]{\mathrm{abs}\left(##1\right)}

\newcommand{\partent}[1]{\mathrm{E}(##1)}

\newcommand{\partfrac}[1]{\{##1\}}

\renewcommand{\div}[2]{##1$ divisé par $##2}

\newcommand{\reste}[2]{$reste de $##1$ par $##2}

\newcommand{\alea}{$nombre aléatoire entre 0 et 1$}

\newcommand{\entaleat}[2]{$entier aléatoire entre $##1$ et $##2}

\newcommand{\egal}{=}

\newcommand{\different}{\neq}

\newcommand{\vrai}{$vrai$}

\newcommand{\faux}{$faux$}

\newcommand{\ou}{$ ou $}

\newcommand{\et}{$ et $}

\newcommand{\non}{$non $}

\newcommand{\estpair}[1]{##1$ est pair$}

\newcommand{\divise}[2]{##1$ divise $##2}

\newcommand{\nondivise}[2]{##1$ ne divise pas $##2}

\newcommand{\bloc}[1]{\begin{tabular}[t]{|l}##1\\ \hline \end{tabular}}

\newcommand{\sialors}[2]{si $##1$ alors\\ \bloc{##2}}

\newcommand{\sialorsbis}[2]{si $##1$ alors ##2}

\newcommand{\sias}[3]{si $##1$ alors\\ \bloc{##2} \\ sinon \\ \bloc{##3}}

\newcommand{\siasbis}[3]{si $##1$ alors \bloc{##2} \\ sinon \bloc{##3}}

\newcommand{\tantque}[2]{tant que $##1$\\ \bloc{##2}}

\newcommand{\repeter}[2]{répéter \\ \bloc{##1} \\ jusqu'à $##2$}

\newcommand{\pour}[4]{pour $##1$ variant de $##2$ à $##3$ \\ \bloc{##4}}

\newcommand{\terme}[2]{##1(##2)}

\newcommand{\affecterterme}[3]{\affecter{##1}{\terme{##2}{##3}}}

\newcommand{\afficherliste}[1]{\afficher{##1}}

\newcommand{\sousprog}[1]{exécuter ##1}

\sffamily }{}

\newenvironment{pascal}{

\newcommand{\programme}[3]{\begin{tabular}[t]{@{}l} program ##1;\\ ##2 begin\\ ##3 readln;\\ end.\end{tabular}}

\newcommand{\ind}{\hspace{1em}}

\newcommand{\ap}{''}

\newcommand{\sepdec}{.}

\newcommand{\constantes}[1]{const\\ \ind\begin{tabular}{l@{ = }l} ##1 \end{tabular}\\}

\newcommand{\variables}[1]{var\\ \ind\begin{tabular}{l@{ : }l} ##1 \end{tabular}\\}

\newcommand{\typeentier}{integer;}

\newcommand{\typeentierlong}{longint;}

\newcommand{\typefloat}{real}

\newcommand{\types}[1]{type\\ \ind\begin{tabular}{l@{ = }l} ##1 \end{tabular}\\}%

\renewcommand{\;}{\ }

\newcommand{\affecter}[2]{##2 := ##1;}

\newcommand{\effacer}{}

\newcommand{\pause}[1]{\afficher{##1} readln;}

\newcommand{\afficher}[1]{writeln(##1);}

\newcommand{\texte}[1]{'##1'}

\newcommand{\ccat}{,}

\newcommand{\saisir}[1]{readln(##1);}

\newcommand{\demander}[2]{\afficher{\texte{##1}} \\ \saisir{##2}}

\newcommand{\fois}{*}

\newcommand{\produit}[2]{##1\fois ##2}

\newcommand{\carre}[1]{sqr(##1)}

\newcommand{\racine}[1]{sqrt(##1)}

\renewcommand{\sin}[1]{sin(##1)}

\renewcommand{\cos}[1]{cos(##1)}

\renewcommand{\exp}[1]{exp(##1)}

\renewcommand{\ln}[1]{ln(##1)}

\newcommand{\abs}[1]{abs ##1}

\newcommand{\partent}[1]{trunc(##1)}

\newcommand{\partfrac}[1]{##1-\partent{##1}}

\renewcommand{\div}[2]{##1 div ##2}

\newcommand{\reste}[2]{##1 mod ##2}

\newcommand{\alea}{random}

\newcommand{\entaleat}[2]{##1+random(##2-##1+1)}

\newcommand{\egal}{=}

\renewcommand{\le}{<=}

\renewcommand{\ge}[2]{>=}

\newcommand{\different}{<>}

\newcommand{\vrai}{true}

\newcommand{\faux}{false}

\newcommand{\ou}{\ or\ }

\newcommand{\et}{\ and\ }

\newcommand{\non}{not }

\newcommand{\estpair}[1]{##1 mod 2 = 0}

\newcommand{\divise}[2]{\egale{\reste{##1}{##2}}{0}}

\newcommand{\nondivise}[2]{\reste{##1}{##2}\different 0}

\newcommand{\bloc}[1]{\begin{tabular}[b]{@{}l}begin\\ \ind \begin{tabular}[t]{@{}l} ##1 \end{tabular}\\ end; \end{tabular}}

\newcommand{\blocbis}[1]{\begin{tabular}[b]{@{}l} begin\\ \ind \begin{tabular}[t]{@{}l} ##1 \end{tabular}\\ end \end{tabular}}

\newcommand{\sialors}[2]{\begin{tabular}[b]{@{}l} if ##1 then\\ \ind \bloc{##2}\end{tabular}}

\newcommand{\sialorsbis}[2]{\begin{tabular}[b]{@{}l} if ##1 then ##2 \end{tabular}}

\newcommand{\sias}[3]{\begin{tabular}[b]{@{}l} if ##1 then\\ \ind \blocbis{##2} \\ else\\ \ind \bloc{##3} \end{tabular}}

\newcommand{\siasbis}[3]{\begin{tabular}[b]{@{}l} if ##1 then\\ \ind ##2 \\ else\\ \ind ##3 \end{tabular}}

\newcommand{\tantque}[2]{\begin{tabular}[b]{@{}l} while ##1 do\\ \ind\bloc{##2} \end{tabular}}

\newcommand{\repeter}[2]{\begin{tabular}[b]{@{}l} repeat\\ \ind \begin{tabular}[t]{@{}l} ##1 \end{tabular}\\ until ##2; \end{tabular}}

\newcommand{\pour}[4]{\begin{tabular}[b]{@{}l}\texttt{for} \affecter*{##1}{##2} \texttt{to} ##3 do \\ \ind \bloc{##4} \end{tabular}}

\newcommand{\terme}[2]{##1[##2]}

\newcommand{\affecterterme}[3]{\affecter{##1}{\terme{##2}{##3}}}

\newcommand{\afficherliste}[1]{\pour*{iterateur}{low(##1)}{high(##1)}{\afficher{\terme{##1}{iterateur}}}}

\newcommand{\sousprog}[1]{##1;}

\ttfamily

}{}

\newenvironment{TI}{\ttfamily

\newcommand{\programme}[3]{\begin{tabular}[t]{@{}l} PROGRAM:\MakeUppercase{##1}\\ ##2 ##3 \end{tabular}}

\newcommand{\ap}{'}

\newcommand{\sepdec}{.}

\newcommand{\variables}[1]{}

\newcommand{\types}[1]{}

\renewcommand{\;}{}

\newcommand{\affecter}[2]{:##1-\makebox[0pt][r]{\guilsinglright}##2}

\newcommand{\constantes}[1]{\begin{tabular}{@{\hspace{2ex}=}l@{\hspace{-5ex}:}l} ##1 \end{tabular}}

\newcommand{\effacer}{:\EffEcr}

\newcommand{\pause}[1]{:Pause ##1}

\newcommand{\afficher}[1]{:Disp ##1}

\newcommand{\placer}[3]{:Output(##1,##2,##3)}

\newcommand{\texte}[1]{"\MakeUppercase{##1}"}

\newcommand{\ccat}{,}

\newcommand{\saisir}[1]{:Prompt ##1}

\newcommand{\demander}[2]{:Input \texte{##1},##2}

\newcommand{\touche}{codetouche}

\newcommand{\fois}{*}

\newcommand{\produit}[2]{##1\fois ##2}

\newcommand{\carre}[1]{##1\makebox[0pt][l]{$^2$}{\ }}

\newcommand{\partent}[1]{partEnt(##1)}

\newcommand{\partfrac}[1]{partDéc(##1)}

\newcommand{\racine}[1]{\makebox[0pt][l]{$\sqrt$}{\ }(##1)}

\renewcommand{\sin}[1]{sin(##1)}

\renewcommand{\cos}[1]{cos(##1)}

\renewcommand{\exp}[1]{e\textasciicircum(##1)}

\renewcommand{\ln}[1]{ln(##1)}

\newcommand{\abs}[1]{abs(##1)}

\renewcommand{\div}[2]{\partEnt{##1/##2}}

\newcommand{\reste}[2]{##2\fois \partFrac{##1/##2}}

\newcommand{\alea}{NbrAléat}

\newcommand{\entaleat}[2]{entAléat(##1,##2)}

\newcommand{\egal}{=}

\renewcommand{\le}{\smash{\raisebox{-0.2ex}{\begin{tabular}{@{}c@{}} < \\[-2.1ex] - \end{tabular}}}}

\renewcommand{\ge}{\smash{\raisebox{-0.2ex}{\begin{tabular}{@{}c@{}} > \\[-2.1ex] - \end{tabular}}}}

\newcommand{\different}{\makebox[0pt][l]{=}/}

\newcommand{\vrai}{1}

\newcommand{\faux}{0}

\newcommand{\ou}{\ ou\ }

\newcommand{\et}{\ et\ }

\newcommand{\non}{non}

\newcommand{\estpair}[1]{\partfrac{##1/2}=0}

\newcommand{\divise}[2]{\partfrac{##1/##2}\egal 0}

\newcommand{\nondivise}[2]{\partfrac{##1/##2}\different 0}

\newcommand{\bloc}[1]{##1}

\newcommand{\sialors}[2]{:If ##1 \\ :Then \\ ##2 \\ :End}

\newcommand{\sialorsbis}[2]{:If ##1 \\ ##2}

\newcommand{\sias}[3]{:If ##1 \\ :Then \\ ##2 \\ :Else \\ ##3 \\ :End}

\newcommand{\siasbis}[3]{\sias{##1}{##2}{##3}}

\newcommand{\tantque}[2]{:While ##1\\ ##2 \\ :End}

\newcommand{\repeter}[2]{:Repeat ##2\\ ##1 \\ :End}

\newcommand{\pour}[4]{:For(##1,##2,##3)\\ ##4 \\ :End}

\newcommand{\nomliste}[1]{{\small L}##1}

\newcommand{\terme}[2]{\nomliste{##1}(##2)}

\newcommand{\affecterterme}[3]{\affecter{##1}{\terme{##2}{##3}}}

\newcommand{\afficherliste}[1]{\pause{##1}}

\newcommand{\sousprog}[1]{:prgm##1}

}{}

\newenvironment{TIen}{

\begin{TI}

\renewcommand{\touche}{getKey}

\renewcommand{\ou}{\ or\ }

\renewcommand{\et}{\ and\ }

\renewcommand{\non}{not}

\renewcommand{\effacer}{:ClrHome}

\renewcommand{\partent}[1]{int(##1)}

\renewcommand{\partfrac}[1]{fpart(##1)}

\renewcommand{\alea}{rand}

\renewcommand{\entaleat}[2]{randint(##1,##2)}\\

}{\end{TI}}

\newenvironment{Casio}{

\newcommand{\finligne}{\Pisymbol{psy}{191}}

\newcommand{\programme}[3]{\begin{tabular}[t]{@{}l} PROGRAM:\MakeUppercase{##1}\\ ##2 ##3 \end{tabular}}

\newcommand{\variables}[1]{}

\newcommand{\types}[1]{}

\renewcommand{\;}{\\}

\newcommand{\ap}{\ }

\newcommand{\sepdec}{.}

\newcommand{\affecter}[2]{##1-\makebox[0pt][r]{\guilsinglright}##2\finligne}

\newcommand{\constantes}[1]{\begin{tabular}{@{\hspace{2ex}=}l@{\hspace{-4ex}}l@{\finligne}l} ##1 \end{tabular}}

\newcommand{\effacer}{ClrText\finligne}

\newcommand{\pause}[1]{##1\begin{tikzpicture} \draw[fill=black] (0,0) -- (1ex,0) -- (1ex,1ex)-- cycle; \end{tikzpicture}\finligne}

\newcommand{\afficher}[1]{##1\finligne}

\newcommand{\placer}[3]{Locate ##2,##1,##3\finligne}

\newcommand{\texte}[1]{"\MakeUppercase{##1}"}

\newcommand{\ccat}{,}

\newcommand{\saisir}[1]{\affecter{?}{##1}}

\newcommand{\demander}[2]{\texte{##1}\saisir{##2}}

\newcommand{\touche}{Getkey}

\newcommand{\fois}{*}

\newcommand{\produit}[2]{##1 \fois ##2}

\newcommand{\carre}[1]{##1\makebox[0pt][l]{$^2$}{\ }}

\newcommand{\racine}[1]{\makebox[0pt][l]{$\sqrt$}{\ }(##1)}

\renewcommand{\sin}[1]{sin (##1)}

\renewcommand{\cos}[1]{cos (##1)}

\renewcommand{\exp}[1]{e\textasciicircum(##1)}

\renewcommand{\ln}[1]{ln (##1)}

\newcommand{\abs}[1]{Abs (##1)}

\newcommand{\partent}[1]{Int (##1)}

\newcommand{\partfrac}[1]{Frac (##1)}

\renewcommand{\div}[2]{\partent{##1/##2}}

\newcommand{\reste}[2]{##2\fois \partfrac{##1/##2}}

\newcommand{\alea}{Ran\#\ }

\newcommand{\entaleat}[2]{\partent{(##2-##1+1)\alea}+##1}

\newcommand{\egal}{=}

\renewcommand{\le}{\smash{\raisebox{-0.2ex}{\begin{tabular}{@{}c@{}} < \\[-2.1ex] - \end{tabular}}}}

\renewcommand{\ge}{\smash{\raisebox{-0.2ex}{\begin{tabular}{@{}c@{}} > \\[-2.1ex] - \end{tabular}}}}

\newcommand{\different}{\makebox[0pt][l]{=}\textbackslash}

\newcommand{\vrai}{1}

\newcommand{\faux}{0}

\newcommand{\ou}{\ Or\ }

\newcommand{\et}{\ And\ }

\newcommand{\non}{Not\ }

\newcommand{\divise}[2]{\partfrac{##1/##2}\egal 0}

\newcommand{\nondivise}[2]{\partfrac{##1/##2}\different 0}

\newcommand{\estpair}[1]{\partfrac{##1/2}=0}

\newcommand{\bloc}[1]{##1}

\newcommand{\sialors}[2]{If ##1\finligne \\ Then ##2 \\ IfEnd\finligne}

\newcommand{\sialorsbis}[2]{##1 $\Rightarrow$ ##2}

\newcommand{\sias}[3]{If ##1\finligne \\ Then ##2 \\ Else ##3 \\ IfEnd\finligne}

\newcommand{\siasbis}[3]{\sias{##1}{##2}{##3}}

\newcommand{\tantque}[2]{While ##1\finligne\\ ##2 \\ WhileEnd\finligne}

\newcommand{\repeter}[2]{Do\finligne\\ ##1 \\ LpWhile \non{##2}\finligne}

\newcommand{\pour}[4]{For \affecter{##1}{##2} To ##3\finligne\\ ##4 \\ Next\finligne}

\newcommand{\nomliste}[1]{List ##1}

\newcommand{\terme}[2]{\nomliste{##1}[##2]}

\newcommand{\affecterterme}[3]{\affecter{##1}{\terme{##2}{##3}}\finligne}

\newcommand{\afficherliste}[1]{\pause{##1}}

\newcommand{\sousprog}[1]{Prog \texte{##1}\finligne}

\ttfamily

}{}

\newenvironment{python}{

\newcommand{\ind}{\hspace{1em}}

\newcommand{\programme}[3]{\begin{tabular}[t]{@{}l} import math random \\ ##3 \end{tabular}}

\newcommand{\ap}{'}

\newcommand{\sepdec}{.}

\renewcommand{\;}{\\}

\newcommand{\affecter}[2]{##2 = ##1}

\newcommand{\effacer}{}

\newcommand{\pause}[1]{\afficher{##1}\\ raw_input()}

\newcommand{\afficher}[1]{print ##1}

%\newcommand{\placer}[3]{}

\newcommand{\texte}[1]{u"##1"}

\newcommand{\ccat}{,}

\newcommand{\demander}[2]{\affecter{input(\texte{##1})}{##2}}

\newcommand{\saisir}[1]{\demander{}{##1}}

%\newcommand{\touche}{getkey()}

\newcommand{\fois}{*}

\newcommand{\produit}[2]{##1 \fois ##2}

\newcommand{\carre}[1]{##1**2}

\newcommand{\racine}[1]{math.sqrt(##1)}

\renewcommand{\sin}[1]{math.sin(##1)}

\renewcommand{\cos}[1]{math.cos(##1)}

\renewcommand{\exp}[1]{math.exp(##1)}

\renewcommand{\ln}[1]{math.log(##1)}

\newcommand{\abs}[1]{abs(##1)}

\newcommand{\partent}[1]{math.floor(##1)}

\newcommand{\partfrac}[1]{##1-math.floor(##1)}

\renewcommand{\div}[2]{##1 // ##2}

\newcommand{\reste}[2]{##1 \% ##2}

\newcommand{\alea}{random.random()}

\newcommand{\entaleat}[2]{random.randint(##1,##2)}

\newcommand{\egal}{ == }

\renewcommand{\le}{ <= }

\renewcommand{\ge}{ >= }

\newcommand{\different}{ != }

\newcommand{\vrai}{true}

\newcommand{\faux}{false}

\newcommand{\ou}{\ or\ }

\newcommand{\et}{\ and\ }

\newcommand{\non}{not\ }

\newcommand{\divise}[2]{\reste{##1}{##2}\egal 0}

\newcommand{\nondivise}[2]{\reste{##1}{##2}\different 0}

\newcommand{\estpair}[1]{\partfrac{##1/2}=0}

\newcommand{\bloc}[1]{\begin{tabular}[t]{@{}l}##1\end{tabular}}

\newcommand{\sialors}[2]{if ##1: \\ \ind\bloc{##2}}

\newcommand{\sialorsbis}[2]{\sialors{##1}{##2}}

\newcommand{\sias}[3]{if ##1: \\ \ind\bloc{##2} \\ \texttt{else:}\\ \ind\bloc{##3}}

\newcommand{\siasbis}[3]{\sias{##1}{##2}{##3}}

\newcommand{\tantque}[2]{while ##1:\\ \ind\bloc{##2}}

\newcommand{\repeter}[2]{##1 while not (##2):\\ \ind\bloc{##1}}

\newcommand{\pour}[4]{for ##1 in range(##2,##3+1)\\ \ind\bloc{##4}}

\newcommand{\terme}[2]{##1[##2-1]}

\newcommand{\affecterterme}[3]{\affecter{##1}{\terme{##2}{##3}}}

\newcommand{\afficherliste}[1]{\afficher{##1}}

\newcommand{\sousprog}[1]{##1()}

\newcommand{\fonction}[2]{def ##1(##2):}

\ttfamily

}{}