
Ich verwende \usepackage{pseudocode}
LaTeX zum Schreiben von Pseudocode.
Wie schreibt manschaltenAnweisung unter Verwendung dieses Pakets?
Ich schreibe mehr als 90 % Pseudocode mit dem PaketPseudocode. In der Funktion Validate möchte ich schreibenschaltenStellungnahme.
\PROCEDURE{Validate}{location1,location2}
l1 \GETS 0 \\
l2 \GETS 0 \\
validate \GETS true \\
\IF board[location1] \neq 0 \THEN
\BEGIN
l1 \GETS board[location1]
l2 \GETS board[location2]
\IF l1 + l2 is odd and l2 \neq 0 \THEN
validate \GETS false
\ELSE
\BEGIN
%\SWITCH here i want to write switch statement
%\BEGIN
%\END
\END
\END
\RETURN {validate}\\
\ENDPROCEDURE
Ich kann die Switch-Anweisung in Pseudocode mithilfe anderer Pakete wie algorithmicx und algorithm hinzufügen. Es gibt eine Möglichkeit, die Switch-Anweisung mithilfe von hinzuzufügenPseudocode Paket?
Antwort1
Ich bin nicht wirklich sicher, ob es das ist, wonach Sie suchen, aber es ist ein erster Versuch:
\documentclass{article}
\usepackage{amsmath,pseudocode}% http://ctan.org/pkg/{amsmath,pseudocode}
\newcommand{\algvar}[1]{\text{\ttfamily\upshape#1}}
\makeatletter
\newcommand{\SWITCH}{\mbox{\bfseries switch }}
\renewcommand{\CASE}{\mbox{ \bfseries case }}
\newcommand{\CASEELSE}{\mbox{ \bfseries else }}
\makeatother
\begin{document}
\begin{pseudocode}{Validate}{\algvar{location1}, \algvar{location2}}
\PROCEDURE{Validate}{\algvar{location1}, \algvar{location2}}
l_1 \GETS 0 \\
l_2 \GETS 0 \\
\algvar{validate} \GETS \TRUE \\
\IF \algvar{board}[\algvar{location1}] \neq 0 \THEN
\BEGIN
l_1 \GETS \algvar{board}[\algvar{location1}] \\
l_2 \GETS \algvar{board}[\algvar{location2}] \\
\IF (l_1 + l_2 \text{ is odd}) \AND (l_2 \neq 0) \THEN
\algvar{validate} \GETS \FALSE
\ELSE
\BEGIN
\SWITCH i
\BEGIN
\CASE 1 \text{ Something 1} \\
\CASE 2 \text{ Something 2} \\
\CASE 3 \text{ Something 3} \\
\CASE 4 \text{ Something 4} \\
\CASEELSE \text{ Something else}
\END
\END
\END \\
\RETURN {\algvar{validate}} \\
\ENDPROCEDURE
\end{pseudocode}
\end{document}
Die Konstruktionen vonpseudocode
basiert auf array
s und ist daher leicht modifizierbar. Es hängt jedoch davon ab, was Sie hinsichtlich des Aussehens eines \SWITCH
Befehls, hinsichtlich Platzierung, Ausrichtung und Rechtfertigung erreichen möchten.