hpolzer

Palindromic numbers

May 7th, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 4.04 KB | None | 0 0
  1. (* 
  2.     This program tries to compute a palindromic number starting with a
  3.     number the user has to type in (use 65465241 for a test).
  4.     To compile run "fpc palzahl.pas".
  5.     Copyright (C) <December 27, 1995> Henning Polzer, h underscore polzer at gmx dot de
  6.  
  7.     This program is free software: you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation, either version 3 of the License, or
  10.     (at your option) any later version.
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19. *)
  20.  
  21.  
  22. PROGRAM palindromzahlen;
  23. CONST   max = 300;      (* Palindrom darf maximal "max" Stellen haben *)
  24.     maxstellen = 30;    (* Zahl, von der aus gesucht wird, kann maximal "ausgangswert" Stellen haben *)
  25. TYPE    Tganzzahl = integer;
  26.     Tfeld = ARRAY[1..max] OF Tganzzahl;
  27.  
  28. VAR ausgangswert:   string[maxstellen];
  29.     i, k, schritte: Tganzzahl;
  30.     verlassen:      boolean;
  31.  
  32.  
  33. PROCEDURE palindrom;
  34. VAR leer, v, nv, erg: Tfeld;
  35.  
  36.   PROCEDURE loesche_feld;
  37.   BEGIN
  38.     FOR i := 1 TO max DO
  39.     BEGIN
  40.       leer[i] := 0;
  41.       v[i] := 0;
  42.       nv[i] := 0;
  43.       erg[i] := 0
  44.     END (* for *)
  45.   END; (* loesche_feld *)
  46.  
  47.  
  48.   PROCEDURE eingabe;
  49.   VAR hilf, laenge, zahlwert, fehler: Tganzzahl;
  50.  
  51.   BEGIN
  52.     loesche_feld;
  53.     ausgangswert := '';
  54.  
  55.     REPEAT
  56.       write ('Zahl: ');
  57.       readln (ausgangswert);
  58.  
  59.       laenge := length (ausgangswert);
  60.       hilf := max;
  61.       fehler := 0;
  62.  
  63.       FOR i := laenge DOWNTO 1 DO
  64.       BEGIN
  65.         val (ausgangswert[i], zahlwert, fehler);
  66.         v[hilf] := zahlwert;
  67.         dec (hilf)
  68.       END (* for *)
  69.     UNTIL (length(ausgangswert) > 1) AND (fehler = 0)
  70.   END; (* eingabe *)
  71.  
  72.  
  73.   PROCEDURE kehre_um;
  74.   VAR beginn, hilf: Tganzzahl;
  75.  
  76.   BEGIN
  77.     beginn := 1;
  78.     WHILE (v[beginn] = 0) DO
  79.       inc (beginn);
  80.  
  81.     hilf := 0;
  82.     FOR i := beginn TO max DO
  83.     BEGIN
  84.       nv[max-hilf] := v[i];
  85.       inc(hilf)
  86.     END (* for *)
  87.   END; (* kehre_um *)
  88.  
  89.  
  90.   PROCEDURE tausche;
  91.   BEGIN
  92.     v := erg
  93.   END; (* tausche *)
  94.  
  95.  
  96.   PROCEDURE addiere;
  97.   VAR anfang, zaehler: Tganzzahl;
  98.  
  99.   BEGIN
  100.     anfang := 1;
  101.     WHILE (v[anfang] = 0) DO
  102.       inc (anfang);            
  103.  
  104.     FOR zaehler := max DOWNTO anfang DO
  105.     BEGIN
  106.       inc (erg[zaehler], v[zaehler] + nv[zaehler]);
  107.       IF erg[zaehler] >= 10 THEN
  108.       BEGIN
  109.         dec (erg[zaehler],10);
  110.         inc (erg[zaehler-1])
  111.       END (* if erg >=... *)
  112.     END (* for *)
  113.   END; (* addiere *)
  114.  
  115.  
  116.   FUNCTION gleich: boolean;
  117.   VAR anfang, hilf: Tganzzahl;
  118.       ent: boolean;
  119.  
  120.   BEGIN
  121.     ent := true;
  122.  
  123.     anfang := 1;
  124.     WHILE (v[anfang] = 0) DO
  125.       inc (anfang);
  126.  
  127.     IF anfang < 2 THEN verlassen := true ELSE
  128.     BEGIN (* Zahl nicht zu gross: Auf Uebereinstimmung testen *)
  129.       hilf := anfang;
  130.  
  131.       REPEAT
  132.         IF v[hilf] <> nv[hilf] THEN ent := false;
  133.         inc (hilf)
  134.       UNTIL (NOT ent) OR (hilf = max);
  135.  
  136.       gleich := ent
  137.     END (* if *)
  138.   END; (* gleich *)
  139.  
  140.  
  141.   PROCEDURE ausgabe;
  142.   VAR hilf: Tganzzahl;
  143.  
  144.   BEGIN
  145.     hilf := 1;
  146.     WHILE (v[hilf] = 0) DO
  147.       inc (hilf);
  148.  
  149.     writeln;
  150.     FOR k := hilf TO max DO
  151.       write (v[k]);
  152.  
  153.     write (' [', max-hilf+1, ' Stellen]')
  154.   END; (* ausgabe *)
  155.  
  156.  
  157.   PROCEDURE loeschen;
  158.   BEGIN
  159.     nv := leer;
  160.     erg := leer
  161.   END; (* loeschen *)
  162.  
  163.  
  164. BEGIN (* palindrom *)
  165.   verlassen := false;
  166.   schritte := 0;
  167.  
  168.   eingabe;
  169.   kehre_um;
  170.   REPEAT
  171.     addiere;
  172.     tausche;
  173.     loeschen;
  174.     kehre_um;
  175.     inc (schritte)
  176.   UNTIL gleich OR verlassen;
  177.  
  178.   writeln (schritte, ' Schritt(e) berechnet.');
  179.  
  180.   IF verlassen THEN write ('Palindromzahl nicht zu ermitteln.')
  181.     ELSE ausgabe;
  182.   writeln
  183. END; (* palindrom *)
  184.  
  185.  
  186. BEGIN (* Hauptprogramm *)
  187.   palindrom
  188. END.
Add Comment
Please, Sign In to add comment