%!
% str1 str2 strcat -> concatstring
% utility to concatenate strings
/strcat {
5 dict begin
/str2 exch def
/str1 exch def
/str1len str1 length def
/str2len str2 length def
/newstring str1len str2len add string def
newstring 0 str1 putinterval
newstring str1len str2 putinterval
newstring
end
} def
/fib {
gsave
10 dict begin
dup dup dup
type cvlit str cvs (integertype) ne { (Argument to fib is not integer\n)
show }
{
1 lt exch 10 gt or
{(Incoming argument for fib out of range\n) show }
{
% duplicate top of stack so it can be tested against
% zero. If value is one pop the stack and then push 0 and 1 onto
% stack and return,
dup 1 eq {
pop %pop incoming argument
0 1
}
{
%subtract 1 from value at top of stack for next recursive
% call to fibb i.e. a call of fibb(i-1)
1 sub fib
% now pick up two values returned on stack from level below and manipulate them
dup
3 1 roll
add
}
ifelse
}
ifelse % end of inrange check
}
ifelse % end of "is integer" check
end
grestore
} def
/Helvetica findfont 12 scalefont setfont
/str 50 string def
/xpos 250 def
/ypos 470 def
xpos ypos moveto
1 1 10 {
dup dup
xpos exch 20 mul ypos exch sub moveto
str cvs (The value of fib ) exch strcat ( is ) str cvs strcat show
% clean up the stack by deleting fibb(n-1) value -- the convert fibb(n)
% value to a string
fib exch pop str cvs show
} for
xpos 200 moveto
% check that out of range value gives error message and then quits
0 fib
xpos 180 moveto
% test error message for non-integer argument
3.142 fib
showpage
1 Response
Write a comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.