Jump to content

Pascal : Nesaprotama izvade funkcijā


Grauznis

Recommended Posts

Uzdevums : Sastādīt programmu, kas atrod dotā naturālā skaitļa ciparu summu; pēc tam – iegūtā
skaitļa ciparu summu utt. Līdz palicis viens cipars.

 

Mana programma:

Program skaitlis;
Var
  a : longint;
Function sk( x : longint) : longint;
Var
  t :longint;
Begin
 t := 0;
 Repeat
   t := t + ( x mod 10);
   x:= x div 10;
 until x = 0;
 if (t < 10) then sk := t else sk(t);
End;
Begin
  readln(a);
  writeln(sk(a));
End.                  

Programma drabojas skaitļiem, kā 25 , bet ievadot skaitli 976 izvada 0. Vai kāds varētu paskaidrot kāpēc tā?

Pareizo rezultātu izdevās izvadīt ar šadu programmu :

Program skaitlis;
Var
  a : longint;
Function sk( x : longint) : longint;
Var
  t :longint;
Begin
 t := 0;
 Repeat
   t := t + ( x mod 10);
   x:= x div 10;
 until x = 0;
 if (t < 10) then begin
   writeln(t);
   halt;

   end else sk(t);
End;
Begin
  readln(a);
  sk(a);
End.

Kāpēc pirmā programma nedarbojas nedorbojas?

Link to post
Share on other sites
AndrisBB

Neko nezinu par paskalu, bet man izskatas ka tu te neko neatgriez, ja izpildas ELSE

 if (t < 10) then sk := t else sk(t);

Manuprat vajadzetu kka ta:

if (t < 10) then sk := t else sk := sk(t);

....

 

oi, sorry izskatas ka pa lenu domaju un rakstu :D

Edited by AndrisBB
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...