Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- rbm.c multiplies two numbers given at the command line using the "Russian
- peasant multiplication“ and shows the steps of calculation.
- To compile run "gcc -ansi -o rbm rbm.c".
- Copyright (C) <September 22, 2013> Henning Polzer,
- send comments and error reports to: h underscore polzer at gmx dot de.
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License
- as published by the Free Software Foundation; either version 2
- of the License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- #include <math.h> /* fabs (), floor () */
- #include <stdio.h> /* printf () */
- #include <stdlib.h> /* atol () */
- typedef unsigned long ganzzahl;
- void rbm (ganzzahl a, ganzzahl b)
- {
- ganzzahl doppel, flag=0, halb, links=a, rechts=b, summe=0;
- if (b < a) { /* kleineren Faktor nach links */
- links=b;
- rechts=a;
- flag=1;
- }
- printf ("\n%9ld %9ld", links, rechts); /* Startwerte ausgeben */
- if (links%2==1) summe += rechts; /* Startwert ggf. addieren */
- else printf ("<gestrichen"); /* Links gerade? Rechts streichen (s. u.)*/
- printf ("\t\tStartwerte");
- if (flag==1) printf (" (Positionen getauscht)"); /* weil kleiner Wert links */
- printf ("\n");
- do {
- halb=floor(links/2);
- doppel=rechts*2;
- printf ("%9ld ", halb);
- if (halb%2==0) printf ("%9ld<gestrichen", doppel); /* <-- links gerade (s. o.)*/
- else {
- printf ("%9ld", doppel); /* Links ungerade: Rechts addieren*/
- summe+=doppel;
- } /* else */
- printf ("\n");
- links=halb; /* Wert anpassen fuer naechsten Schritt */
- rechts=doppel;
- } while (links>1); /* 1 Untergrenze: Ende */
- printf ("\n");
- if (flag==0) printf ("\t%ld * %ld", a, b);
- else printf ("\t%ld * %ld", b, a);
- /* pruefen, ob Rechenergebnis stimmt: */
- printf (" = %ld stimmt mit konventioneller Berechnung", summe);
- if (a*b!=summe) printf (" nicht");
- printf (" ueberein.\n");
- } /* rbm */
- int main (int argc, char *argv[])
- {
- ganzzahl zahl1, zahl2;
- if ((argc==3) && /* genau zwei ganze Zahlen > 0 eingeben */
- ((zahl1=fabs(atol(argv[1])))!=0) &&
- ((zahl2=fabs(atol(argv[2])))!=0)) rbm (zahl1, zahl2);
- else printf ("Richtiger Aufruf: rbm zahl>0 zahl>0\n");
- } /* main */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement