MathGroup Archive 1997

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Customizing output format

  • To: mathgroup at smc.vnet.net
  • Subject: [mg9637] Re: Customizing output format
  • From: daiyanh at mindspring.com (Daitaro Hagihara)
  • Date: Mon, 17 Nov 1997 19:49:45 -0500
  • Organization: MindSpring Enterprises
  • Sender: owner-wri-mathgroup at wolfram.com

In article <63rteg$llc at smc.vnet.net>, John Kiehl <jkiehl at interport.net>
wrote:

> I would like to create a function that takes 2 integers as input and 
> displays their product in the style of a grade school student.  For 
> example:  f[123,456] would output:
> 
>      123
>      456
>      ___
>      738
>     615
>    492
> ________
>    56088
> 
> Of course I want the lines as well as the numbers and I would like 
> things flush right in the cell.  Thanks in advance.

This is just a start.

Format[multiply[a_Integer,b_Integer]:= Block[{dlist=Reverse[a
IntegerDigits[b]],
       sep=StringJoin[Table["-",{Length[IntegerDigits[a b]]}]]},
      Do[dlist[[i]]=If[dlist[[i]]==0,Null,
                       StringJoin[ToString[dlist[[i]]],Table["
",{i-1}]]],
         {i,Length[dlist]}];
      ColumnForm[Cases[Flatten[{a,b,sep,dlist,sep,a
b}],x_/;x=!=Null],Right]]]

Mathematica[17]? multiply[123,456]

  123
  456
-----
  738
 615 
492  
-----
56088

Also, with the following def's:

myPre[x_]:=(Hold[x]/.Literal[a_Integer X
b_Integer]->multiply[a,b])[[1]]; SetAttributes[myPre,HoldFirst];
$Pre=myPre;

you can do something like this:

Mathematica[30]? 123 X 456

  123
  456
-----
  738
 615 
492  
-----
56088

In a production system, the method of arithmetic (multiplication in this
case, or 'x') should be indicated in the display. The preprocessing
parser should be restricted in its scope so that overall speed hit is
kept at minimum, not to mention avoiding any side-effects.  Special
case handling such as multiplication by a single digit integer should
also be taken care of.  Finally, for better handling of chained
multiplications, as well as making In's and Out's to work correctly, a
more complex data structure is probably necessary.


  • Prev by Date: Fractal Plane Filling Curves
  • Next by Date: Re: [Q] gnuplot features in Mathematica 3.x
  • Previous by thread: Customizing output format
  • Next by thread: subsets (recursion homework)