Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- rbm.pas multiplies two numbers given at the command line using the "Russian
- peasant multiplication. To compile run "fpc rbm.pas".
- 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.
- }
- PROGRAM russische_bauernmultiplikation;
- CONST br = 7; { zur formatierten Ausgabe }
- TYPE ganzzahl = longint;
- VAR zahl1, zahl2: ganzzahl;
- fehlercode: integer; { muss integer sein }
- PROCEDURE rbm (a, b: ganzzahl);
- VAR halb, links, doppel, rechts, summe: ganzzahl;
- flag: boolean;
- BEGIN { rbm }
- links := a;
- rechts := b;
- summe := 0;
- flag := false;
- IF b < a THEN { kleineren Faktor nach links }
- BEGIN
- links := b;
- rechts := a;
- flag := true
- END; { if }
- write (links:br, ' ', rechts:br);
- IF odd (links) THEN summe := summe + rechts { Startwert ggf. einbeziehen }
- ELSE write ('<gestrichen');
- write (' Startwerte');
- IF flag = true THEN write (' (Positionen getauscht)');
- writeln;
- REPEAT
- halb := links DIV 2;
- doppel := rechts * 2;
- write (halb:br, ' ');
- IF NOT odd (halb) THEN write (doppel:br, '<gestrichen') { <-- links gerade }
- ELSE BEGIN { links ungerade: }
- write (doppel:br);
- summe := summe + doppel
- END; { else }
- writeln;
- links := halb;
- rechts := doppel
- UNTIL links < 2;
- writeln;
- IF flag = false THEN write (a, ' * ', b)
- ELSE write (b, ' * ', a);
- write (' = ', summe, ', stimmt mit konventioneller Berechnung');
- IF a * b <> summe THEN write (' nicht');
- writeln (' ueberein.')
- END; { rbm }
- BEGIN { Hauptprogramm }
- val (paramstr (1), zahl1, fehlercode); { Kommandozeilenparameter einlesen }
- val (paramstr (2), zahl2, fehlercode);
- IF (paramcount = 2) AND { genau zwei Zahlen > 0 eingeben }
- (fehlercode = 0) AND
- (zahl1 > 0) AND
- (zahl2 > 0) THEN rbm (zahl1, zahl2)
- ELSE writeln ('Aufruf: rbm zahl1 zahl2')
- END.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement