%%%%
% Prog de calculs "simples"
%%%%
\setKVdefault[ClesProg]{%
  Ecart=2em,%
  Direct,%
  SansCalcul=false,%
  Application=false,
  Details=false,
  Enonce=false,
  Nom={},
  CouleurCadre=black,%
  CouleurFond=gray!10,%
  Largeur={.95\linewidth},%
  Epaisseur=.75pt,%
  Pointilles=0,
  ThemePerso=false,
}

\newcounter{NBprog}%
\setcounter{NBprog}{0}%

\newlength{\PointillesClesProg}%

\newcommand\ProgCalcul[2][]{%
  % #1 : cl\'es
  % #2 : \'etapes
  \useKVdefault[ClesProg]%
  \setKV[ClesProg]{#1}%
  \ifboolKV[ClesProg]{ThemePerso}{}{%
    \tcbset{ProgCalcul/.style={%
        boxsep=1mm,
        bottom=.75mm,
        middle=2mm,
        boxrule={\useKV[ClesProg]{Epaisseur}},
        text width={\useKV[ClesProg]{Largeur}},
        colframe={\useKV[ClesProg]{CouleurCadre}},
        colback={\useKV[ClesProg]{CouleurFond}},
        halign upper=center
      }%
    }%
  }%
  \ifboolKV[ClesProg]{Application}{%
    %    % by Thomas Dehon and cp
    \setsepchar[*]{§*,}
    % \setsepchar[*]{,* }%
    \ignoreemptyitems%
    \readlist*\ListeTotale{#2}%
    \xdef\PfCPCfoo{\ListeTotale[1]}%
    \xdef\PfCPCfaa{\ListeTotale[2]}%
    %% 
    \setsepchar{,}%    \ignoreemptyitems%
    \readlist*\ListeEtapes{\PfCPCfoo}%
    \setsepchar[*]{,* }\ignoreemptyitems%
    \readlist*\ListeProg{\PfCPCfaa}%
      \begin{tcolorbox}[%
        ProgCalcul,%
        ]
        \ifthenelse{\equal{\useKV[ClesProg]{Nom}}{}}%
        {}%
        {%
          {\color{\useKV[ClesProg]{CouleurCadre}}{\bfseries\useKV[ClesProg]{Nom}}}%
          \tcblower
        }%
        \ifboolKV[ClesProg]{SansCalcul}{%
          \begin{enumerate}
          \item Choisir un nombre~\pointilles~$\ListeProg[1]$%
            \foreachitem\etape\in\ListeEtapes{%
            \item \etape~\pointilles~$\ListeProg[3,\etapecnt]$
            }%
          \end{enumerate}
        }{\begin{enumerate}
          \item Choisir un nombre~\pointilles~\xdef\NbDepart{\ListeProg[1]}\num{\NbDepart}
            \foreachitem\etape\in\ListeEtapes{%
            \item \etape~\pointilles~\edef\Test{\ListeProg[2,\etapecnt]}%
              \expandarg%
              \StrSubstitute{\Test}{^}{\empty\dots{}^}[\tempa]%
              \StrSubstitute{\tempa}{**}{^}[\tempab]%
              \StrSubstitute{\tempab}{*}{\times}[\tempac]%
              \StrSubstitute{\tempac}{/}{\div}[\tempad]%
              $\ifboolKV[ClesProg]{Details}{\xintifboolexpr{\NbDepart<0}{(\num{\NbDepart})}{\num{\NbDepart}}\tempad=}{}\xdef\NbDepart{\fpeval{(\NbDepart)\ListeProg[2,\etapecnt]}}\num{\NbDepart}$%
            }%
          \end{enumerate}
        }
      \end{tcolorbox}
  }{%
    \ifboolKV[ClesProg]{Enonce}{%
      % by Thomas Dehon
      \setsepchar[*]{,* }%
      \ignoreemptyitems%
      \readlist*\ListeEtapes{#2}% 
      \begin{tcolorbox}[%
        ProgCalcul,%
        ]
        \ifthenelse{\equal{\useKV[ClesProg]{Nom}}{}}%
        {}%
        {%
          {\color{\useKV[ClesProg]{CouleurCadre}}{\bfseries\useKV[ClesProg]{Nom}}}%
          \tcblower
        }%
        \begin{enumerate}
          \foreachitem\etape\in\ListeEtapes{%
          \item \etape 
            \ifthenelse{\equal{\useKV[ClesProg]{Pointilles}}{0}}%
            {}%
            {%
              \setlength{\PointillesClesProg}{\useKV[ClesProg]{Pointilles}}
              \hfill \pointilles[\PointillesClesProg]%
            }%
          }
        \end{enumerate}
      \end{tcolorbox}
    }{%
      \setsepchar[*]{,* }\ignoreemptyitems%
      \readlist*\ListeProg{#2}%
      \stepcounter{NBprog}%
      \xdef\NbDepart{\ListeProg[1]}%
      \ifboolKV[ClesProg]{SansCalcul}{%
        $\NbDepart$\foreachitem\compteur\in\ListeProg[2]{%
          \hspace{0.2em}\tikzmark{A-\theNBprog-\compteurcnt}\hspace{\useKV[ClesProg]{Ecart}}\tikzmark{B-\theNBprog-\compteurcnt}\hspace{0.2em}$\ListeProg[3,\compteurcnt]$%
        }%
        \begin{tikzpicture}[remember picture, overlay]
          \foreachitem\compteur\in\ListeProg[2]{%
            \edef\Test{\ListeProg[2,\compteurcnt]}
            \expandarg%
            \StrSubstitute{\Test}{^}{\empty\dots{}^}[\tempa]%
            \StrSubstitute{\tempa}{**}{^}[\tempab]%
            \StrSubstitute{\tempab}{*}{\times}[\tempac]%
            \StrSubstitute{\tempac}{/}{\div}[\tempad]%
            \draw[-stealth,transform canvas={yshift=0.25em}] (pic cs:A-\theNBprog-\compteurcnt) --
            node[above]{\scriptsize$\tempad$}(pic cs:B-\theNBprog-\compteurcnt);
          }
        \end{tikzpicture}  
      }{%
        \num{\NbDepart}\foreachitem\compteur\in\ListeProg[2]{%
          \hspace{0.2em}\tikzmark{A-\theNBprog-\compteurcnt}\hspace{\useKV[ClesProg]{Ecart}}\tikzmark{B-\theNBprog-\compteurcnt}\xdef\NbDepart{\fpeval{(\NbDepart)\ListeProg[2,\compteurcnt]}}\hspace{0.2em}\num{\NbDepart}%
        }%
        \ifboolKV[ClesProg]{Direct}{%
          \begin{tikzpicture}[remember picture, overlay]
            \foreachitem\compteur\in\ListeProg[2]{%
              \edef\Test{\ListeProg[2,\compteurcnt]}
              \expandarg%
              \StrSubstitute{\Test}{^}{\empty\dots{}^}[\tempa]%
              \StrSubstitute{\tempa}{**}{^}[\tempab]%
              \StrSubstitute{\tempab}{*}{\times}[\tempac]%
              \StrSubstitute{\tempac}{/}{\div}[\tempad]%
              \draw[-stealth,transform canvas={yshift=0.25em}] (pic cs:A-\theNBprog-\compteurcnt) --
              node[above]{\scriptsize$\tempad$}(pic
              cs:B-\theNBprog-\compteurcnt);
            }
          \end{tikzpicture}
        }{%
          \begin{tikzpicture}[remember picture, overlay]
            \foreachitem\compteur\in\ListeProg[2]{%
              \edef\Test{\ListeProg[2,\compteurcnt]}
              \expandarg%
              \StrSubstitute{\Test}{^2}{\empty\sqrt{\dots{}}}[\tempa]%
              \StrSubstitute{\tempa}{**}{^}[\tempab]%
              \StrSubstitute{\tempab}{*}{\div}[\tempac]%
              \StrSubstitute{\tempac}{/}{\times}[\tempad]%
              \StrSubstitute{\tempad}{-}{+}[\tempae]%
              \StrSubstitute{\tempae}{++}{-}[\tempaf]%
              \draw[-stealth,transform canvas={yshift=0.25em}] (pic cs:B-\theNBprog-\compteurcnt) --  node[above]{\scriptsize$\tempaf$}(pic cs:A-\theNBprog-\compteurcnt);
            }
          \end{tikzpicture}
        }%
      }%
    }%
  }%
}%