如何分解数字

如何分解数字

我想定义一个\myproduct带有两个参数的命令(即),以便当我调用它时\myproduct{125}{497},结果将是每个参数的 3 个新命令,如下所示:

\ci=1
\di=2
\ui=5
\dii=4
\cii=9
\uii=7

这样我就可以使用tikz图片中的每个图片作为坐标。

更准确地说,我想将两个数字分解为和100+20+5400+90+7并将每个数字保存在命令中。

只是为了举例

目的是画出这个

在此处输入图片描述

我正在改进代码以显示交叉点。也许我会把它发布在TeXample.net

实际代码如下。问题是它只适用于 3 位数字,包括前导零(即 98 会产生错误,但 098 不会)。

% !TeX spellcheck = en
% !TeX encoding = utf8
% !TeX program = pdflatex
% Author: Thiago de Melo
% www.rc.unesp.br/tmelo
\documentclass[12pt]{report}
\usepackage[a4paper,landscape,margin=2cm]{geometry}
\usepackage{tikz}

\newlength{\numberspace}
\setlength{\numberspace}{40mm}
\newlength{\linelength}
\setlength{\linelength}{110mm}

\definecolor{centscolor}{rgb}{1,0,0}
\definecolor{dozenscolor}{rgb}{0,1,0}
\definecolor{unitscolor}{rgb}{0,0,1}

\newcount\total
\def\myproduct#1#2{\xmyproduct#1#2}
\def\xmyproduct#1#2#3#4#5#6{%
\def\numberi{#1#2#3}
\def\numberii{#4#5#6}
\def\centsi{#1}%
\def\dozensi{#2}%
\def\unitsi{#3}%
\def\centsii{#4}%
\def\dozensii{#5}%
\def\unitsii{#6}%
\total=#1#2#3
\multiply \total by #4#5#6
}

%%%%%%%%% 
% insert numbers with 3 digits including 0

\begin{document}\pagestyle{empty}%
\myproduct{645}{298}%
\centering
%
\begin{tikzpicture}[rotate=-45,thick]
\expandafter\ifnum\centsi>0 
\foreach \c in {1,...,\centsi}{%
\draw[centscolor,xshift=3*\c mm] (0,0)--+(0,\linelength);
}
\expandafter\ifnum\centsi>1 
\draw[centscolor,yshift=-3mm,|-|] (3mm,0) --node[rotate=-45,below=2mm] {$\centsi$}  (3*\centsi mm,0); 
\else
\draw[centscolor] (3mm,0) node[rotate=-45,below=5mm] {$\centsi$}; 
\fi\else \fi

\expandafter\ifnum\dozensi>0 
\foreach \d in {1,...,\dozensi}{%
\draw[dozenscolor,xshift=3*\d mm] (\numberspace,0)--+(0,\linelength);
}
\expandafter\ifnum\dozensi>1
\draw[dozenscolor,yshift=-3mm,|-|] (\numberspace+3mm,0) --node[rotate=-45,below=2mm] {$\dozensi$}  (\numberspace +3*\dozensi mm,0); 
\else
\draw[dozenscolor] (\numberspace+3mm,0) node[rotate=-45,below=5mm] {$\dozensi$}; 
\fi\else \fi

\expandafter\ifnum\unitsi>0 
\foreach \u in {1,...,\unitsi}{%
\draw[unitscolor,xshift=3*\u mm] ( 2\numberspace,0)--+(0,\linelength);
}
\expandafter\ifnum\unitsi>1 
\draw[unitscolor,yshift=-3mm,|-|] (2\numberspace+3mm,0) --node[rotate=-45,below=2mm] {$\unitsi$}  (2\numberspace + 3*\unitsi mm,0);
\else
\draw[unitscolor] (2\numberspace+3mm,0) node[rotate=-45,below=5mm] {$\unitsi$}; 
\fi \else \fi 

%%%

\expandafter\ifnum\centsii>0 
\foreach \c in {1,...,\centsii}{%
\draw[centscolor,yshift=3*\c mm] (0,0)--+(\linelength,0);
}
\expandafter\ifnum\centsii>1 
\draw[centscolor,xshift=-3mm,|-|] (0,3mm) --node[rotate=45,above=2mm] {$\centsii$}  (0,3*\centsii mm); 
\else
\draw[centscolor] (0,3mm) node[rotate=45,above=5mm] {$\centsii$}; 
\fi \else \fi


\expandafter\ifnum\dozensii>0 
\foreach \d in {1,...,\dozensii}{%
\draw[dozenscolor,yshift=3*\d mm] (0,\numberspace)--+(\linelength,0);
}
\expandafter\ifnum\dozensii>1
\draw[dozenscolor,xshift=-3mm,|-|] (0,\numberspace+3mm) --node[rotate=45,above=2mm] {$\dozensii$}  (0,\numberspace +3*\dozensii mm); 
\else
\draw[dozenscolor] (0,\numberspace + 3mm) node[rotate=45,above=5mm] {$\dozensii$}; 
\fi \else \fi

\expandafter\ifnum\unitsii>0 
\foreach \u in {1,...,\unitsii}{%
\draw[unitscolor,yshift=3*\u mm] (0,2\numberspace)--+(\linelength,0);
}
\expandafter\ifnum\unitsii>1 
\draw[unitscolor,xshift=-3mm,|-|] (0,2\numberspace+3mm) --node[rotate=45,above=2mm] {$\unitsii$}  (0,2\numberspace + 3*\unitsii mm);
\else
\draw[unitscolor] (0,2\numberspace+3mm) node[rotate=45,above=5mm] {$\unitsii$}; 
\fi \else \fi


\begin{scope}[xshift=-1cm,yshift=-1cm]
\node[inner sep=0pt]  at (0,0)[left=1cm]       {\large $\times$};
\node[inner sep=10pt] at (0,0)[below left] {\large $\numberi\searrow$};
\node[inner sep=10pt] at (0,0)[above left] {\large $\numberii\nearrow$};
\end{scope}
\node[inner sep=0pt] at (\linelength,\linelength)[right=1cm] {\large $=\the\total$};
\end{tikzpicture}

\end{document}

答案1

\def\myproduct#1#2{\xmyproduct#1#2}
\def\xmyproduct#1#2#3#4#5#6{%
\def\ci{#1}%
\def\di{#2}%
\def\ui{#3}%
\def\cii{#4}%
\def\dii{#5}%
\def\uii{#6}%
}

\myproduct{125}{497}

答案2

另一种方法可能是这样的:

  • 0 没有问题
  • 也与数字的长度有关。

在此处输入图片描述

\documentclass{article}
\RequirePackage[svgnames]{xcolor}
\usepackage{tikz,xparse,xstring,fp}


\colorlet{color0}{red}
\colorlet{color1}{green}
\colorlet{color2}{blue}
\colorlet{color3}{orange}
\colorlet{color4}{red!60}
\colorlet{color5}{blue!60!cyan!60}
\colorlet{color6}{cyan!60!yellow!60}
\colorlet{color7}{red!60!cyan!60}
\colorlet{color8}{red!60!blue!60}
\colorlet{color9}{orange!60!cyan!60}

\DeclareDocumentCommand{\split}{%
    s % reverse order
    m % number of digit in one slice
    m % the number to split
    o % the string containing the output
    }{%
    \xdef\Entree{#3}%
    \let\Sortie\empty%
    \IfBooleanTF #1
    {% sans étoile
        \loop
        \StrRight{\Entree}{#2}[\tmp]%
        \edef\Sortie{\Sortie,\tmp}%
        \StrGobbleRight{\Entree}{#2}[\Entree]%
        \unless\ifx\Entree\empty
        \repeat
        \StrGobbleLeft{\Sortie}{1}[\Sortie]%
    }
    {% avec étoile dans l'ordre inverse
        \loop
        \StrRight{\Entree}{#2}[\tmp]%
        \edef\Sortie{\tmp,\Sortie}%
        \StrGobbleRight{\Entree}{#2}[\Entree]%
        \unless\ifx\Entree\empty
        \repeat
        \StrGobbleRight{\Sortie}{1}[\Sortie]%
    }
    \IfNoValueTF{#4}{\Sortie}{\edef#4{\Sortie}}
    }

\xdef\Sp{3pt}
\xdef\LghX{5cm}
\xdef\LghY{5cm}

\newcommand{\Lattice}[4][]{%
\begin{scope}[#1]
\ifnum#2=0
    \draw[color\j] (-\Sp,-4pt) -- (1\Sp,-4pt)
        node[midway,below=3pt,transform shape] (X\j) {};    
\else
    \begin{scope}[xshift=-(.5+.5*#2)*\Sp]
    \foreach \i in {1,...,#2} {%
        \draw[color\j] (\i*\Sp,0) -- (\i*\Sp,#4); }
    \ifnum#2=1
    \draw[color\j] (\Sp,-4pt) -- (#2*\Sp,-4pt)
        node[midway,below,transform shape] (X\j) {};    
    \else
    \draw[|-|,color\j] (\Sp,-4pt) -- (#2*\Sp,-4pt)
        node[midway,below=3pt,transform shape] (X\j) {};    
    \fi
    \end{scope}
\fi
\end{scope}
}

\newcommand{\product}[2]{%
\split{1}{#1}[\Liste]
\foreach \x [count=\j from 0]in \Liste {%
\Lattice[rotate=45,shift={(1.5*\j,0)}]{\x}{\j}{\LghX} ;
\node[color\j] at (X\j) {\footnotesize\x} ;
    }

\split{1}{#2}[\Liste]
\foreach \x [count=\j from 0]in \Liste {%
\Lattice[rotate=-45,shift={(-1-1.5*\j,-1)}]{\x}{\j}{\LghY} ;
\node[color\j] at (X\j) {\footnotesize\x} ;
    }   
}


\begin{document}


\begin{tikzpicture}
\product{286}{845}  
\end{tikzpicture}

\xdef\LghX{3.5cm}
\xdef\LghY{6cm}

\begin{tikzpicture}
\product{1807}{45}  
\end{tikzpicture}

\end{document}

相关内容