% --------------------------------------%--------------------------------------
% ---------------------------- Identification part ----------------------------
% --------------------------------------%--------------------------------------

\RequirePackage{rcs}

\RCS$Date: 1998/11/18 13:37:02 $
\RCS$RCSfile: aanokcalc.sty,v $
\RCS$Revision: 0.2 $
\RCS$State: Exp $
\RCS$Author: anders $

\input{aausercs}


% --------------------------------------%--------------------------------------
% ------------------------------ Package loading ------------------------------
% --------------------------------------%--------------------------------------

\RequirePackage{ifthen}


% --------------------------------------%--------------------------------------
% --------------------------------- Main code ---------------------------------
% --------------------------------------%--------------------------------------

\newcommand{\setval}[3][{\ }]{\@setval#2,#3:
  \expandafter\def\csname @c@#2\endcsname{#1}}
\def\@setval#1,#2,#3:{%
  \@ifundefined{the@n@#1}{%
    \newcounter{@n@#1}\newcounter{@d@#1}}{}%
  \setcounter{@n@#1}{#2}%
  \ifthenelse{\value{@n@#1}<0}{%
    \setcounter{@d@#1}{-#3}}{\setcounter{@d@#1}{#3}}}

\newcommand{\giveval}[2]{%
  \@ifundefined{the@n@#1}{\newcounter{@n@#1}\newcounter{@d@#1}}{}%
  \setcounter{@n@#1}{\value{@n@#2}}\setcounter{@d@#1}{\value{@d@#2}}}

\newcommand{\setnval}[3][{\ }]{%
  \@ifundefined{the@n@#2}{\newcounter{@n@#2}}{}%
  \setcounter{@n@#2}{#3}%
  \expandafter\def\csname @c@#2\endcsname{#1}}
  
\newcommand{\givenval}[2]{%
  \@ifundefined{the@n@#1}{\newcounter{@n@#1}}{}%
  \setcounter{@n@#1}{\value{@n@#2}}}

\newcounter{@d@tmp@@}
\newcommand{\printval}{%
  \@ifnextchar*{\@printval}{\@mayprintval}}
\def\@printval*{\@@printval}
\newcommand{\@mayprintval}[1]{%
  \ifthenelse{\value{@n@#1}=0}{\ifthenelse{\value{@d@#1}=0}{}{
      \@@printval{#1}}}{
    \@@printval{#1}}}
\newcommand{\@@printval}[1]{%
  \ensuremath{%
    \ifthenelse{\value{@n@#1}=0}{%
      \ifthenelse{\value{@d@#1}<0}{-}{}}{}%
    \csname the@n@#1\endcsname{,}%
    \ifthenelse{\value{@d@#1}<0}{%
      \setcounter{@d@tmp@@}{-\value{@d@#1}}%
      }{%
      \setcounter{@d@tmp@@}{\value{@d@#1}}}%
    \ifthenelse{\value{@d@tmp@@}<10}{0}{}%
    \the@d@tmp@@}}

\newcommand{\printconst}[1]{\@printconst#1:}
\def\@printconst#1,#2:{\ensuremath{#1{,}#2}}

\newcommand{\printnval}{%
  \@ifnextchar*{\@printnval}{\@mayprintnval}}
\def\@printnval*{\@@printnval}
\newcommand{\@mayprintnval}[1]{%
  \ifthenelse{\value{@n@#1}=0}{}{\@@printnval{#1}}}
\newcommand{\@@printnval}[1]{\ensuremath{\csname the@n@#1\endcsname}}

\newcommand{\printcom}[1]{\csname @c@#1\endcsname}

\newcommand{\sumval}[2]{%
  \@ifundefined{the@n@#1}{%
    \newcounter{@n@#1}\newcounter{@d@#1}}{}%
  \setcounter{@n@#1}{0}\setcounter{@d@#1}{0}%
  \@sumval#1:#2,:}
\def\@sumval#1:#2,{%
  \addtocounter{@d@#1}{\value{@d@#2}}%
  \ifthenelse{\value{@d@#1}>99}{%
    \addtocounter{@d@#1}{-100}%
    \addtocounter{@n@#1}{1}}{}%
  \ifthenelse{\value{@d@#1}<-99}{%
    \addtocounter{@d@#1}{100}%
    \addtocounter{@n@#1}{-1}}{}%
  \addtocounter{@n@#1}{\value{@n@#2}}%
  \@moresum#1:}
\def\@moresum#1:{
  \@ifnextchar:{\@donesum}{\@sumval#1:}}
\def\@donesum:{}

\newcommand{\subval}[2]{%
  \sumval{#1}{#2}%
  \setcounter{@n@#1}{-\value{@n@#1}}%
  \setcounter{@d@#1}{-\value{@d@#1}}}

\newcounter{@i@tmp@@}
\newcommand{\mulval}[3]{%
  \@ifundefined{the@n@#1}{%
    \newcounter{@n@#1}\newcounter{@d@#1}}{}%
  \setcounter{@n@#1}{0}\setcounter{@d@#1}{0}%
  \ifthenelse{\value{@n@#2}<0}{%
    \setcounter{@i@tmp@@}{-\value{@n@#2}}%
    }{%
    \setcounter{@i@tmp@@}{\value{@n@#2}}}%
  \whiledo{\value{@i@tmp@@}>0}{%
    \giveval{@i@val}{#1}%
    \sumval{#1}{@i@val,#3}%
    \addtocounter{@i@tmp@@}{-1}}
  \ifthenelse{\value{@n@#2}<0}{%
    \setcounter{@n@#1}{-\value{@n@#1}}%
    \setcounter{@d@#1}{-\value{@d@#1}}}{}}
