Advertisement
hpolzer

Russian peasant multiplication

Mar 21st, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.71 KB | None | 0 0
  1. /*
  2.     rbm.c multiplies two numbers given at the command line using the "Russian
  3.     peasant multiplication“ and shows the steps of calculation.
  4.     To compile run "gcc -ansi -o rbm rbm.c".
  5.     Copyright (C) <September 22, 2013> Henning Polzer,
  6.     send comments and error reports to: h underscore polzer at gmx dot de.
  7.  
  8.     This program is free software; you can redistribute it and/or
  9.     modify it under the terms of the GNU General Public License
  10.     as published by the Free Software Foundation; either version 2
  11.     of the License, or (at your option) any later version.
  12.  
  13.     This program is distributed in the hope that it will be useful,
  14.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.     GNU General Public License for more details.
  17.  
  18.     You should have received a copy of the GNU General Public License
  19.     along with this program; if not, write to the Free Software
  20.     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  21. */
  22.  
  23.  
  24. #include <math.h>                           /* fabs (), floor () */
  25. #include <stdio.h>                          /* printf () */
  26. #include <stdlib.h>                         /* atol () */
  27.  
  28. typedef unsigned long ganzzahl;
  29.  
  30. void rbm (ganzzahl a, ganzzahl b)
  31. {
  32.     ganzzahl doppel, flag=0, halb, links=a, rechts=b, summe=0;
  33.  
  34.     if (b < a) {                        /* kleineren Faktor nach links */
  35.       links=b;
  36.       rechts=a;
  37.       flag=1;
  38.     }
  39.  
  40.     printf ("\n%9ld %9ld", links, rechts);          /* Startwerte ausgeben */
  41.     if (links%2==1) summe += rechts;                /* Startwert ggf. addieren */
  42.       else printf ("<gestrichen");      /* Links gerade? Rechts streichen (s. u.)*/
  43.  
  44.     printf ("\t\tStartwerte");
  45.     if (flag==1) printf (" (Positionen getauscht)");    /* weil kleiner Wert links */
  46.  
  47.     printf ("\n");
  48.  
  49.     do {
  50.       halb=floor(links/2);
  51.       doppel=rechts*2;
  52.       printf ("%9ld ", halb);
  53.       if (halb%2==0) printf ("%9ld<gestrichen", doppel);    /* <-- links gerade (s. o.)*/
  54.         else {
  55.               printf ("%9ld", doppel);      /* Links ungerade: Rechts addieren*/
  56.               summe+=doppel;
  57.         } /* else */
  58.        printf ("\n");
  59.        links=halb;              /* Wert anpassen fuer naechsten Schritt */
  60.        rechts=doppel;
  61.     } while (links>1);                      /* 1 Untergrenze: Ende */
  62.  
  63.     printf ("\n");
  64.     if (flag==0) printf ("\t%ld * %ld", a, b);
  65.       else printf ("\t%ld * %ld", b, a);
  66.     /* pruefen, ob Rechenergebnis stimmt: */
  67.     printf (" = %ld stimmt mit konventioneller Berechnung", summe);
  68.     if (a*b!=summe) printf (" nicht");
  69.     printf (" ueberein.\n");
  70. } /* rbm */
  71.  
  72.  
  73. int main (int argc, char *argv[])
  74. {
  75.     ganzzahl zahl1, zahl2;
  76.     if ((argc==3) &&                /* genau zwei ganze Zahlen > 0 eingeben */
  77.        ((zahl1=fabs(atol(argv[1])))!=0) &&
  78.        ((zahl2=fabs(atol(argv[2])))!=0)) rbm (zahl1, zahl2);
  79.       else printf ("Richtiger Aufruf: rbm zahl>0 zahl>0\n");
  80. } /* main */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement