定义一个回文运算符,在postscript中以相反的顺序复制堆栈上的值



定义一个回文运算符,以相反的顺序复制堆栈上的值。

这是我到目前为止所做的,它没有做我想让它做的

/palindrome {1 dict begincount 1 gt{/first exch def/second exch deftemp1 = firsttemp2 = lastfirst = lastlast = temp1}} def

您在那里所写的大部分内容在PostScript:中都没有任何意义

/palindrome
{
1 dict begin
count 1 gt 
{
/first exch def
/second exch def
%% The following four lines are not valid PostScript
temp1 = first
temp2 = last
first = last
last = temp1
%% There is no '=' assignment operator in PostScript, in PS the = operator
%% pops one object from the stack and writes a text representation to stdout.
%% You have not defined any of the keys temp1, temp2 or last
%% in any dictionary. If executed I would expect this program to throw an
%% 'undefined' error in 'temp1'
}
%% Given the 'count 1 gt' at the opening brace, I would expect this executable
%% array to be followed by a conditional, such as 'if'. Since it isn't this just
%% leaves the executable array '{....}' on the stack
} def

因此,总的来说,我希望这个PostScript函数将布尔值推送到操作数堆栈,true或false取决于执行时堆栈上是否至少有2个对象,然后将可执行数组推到操作数堆栈并退出。

如果我这样做,我会将堆栈存储到一个数组中,然后将数组卸载回堆栈,然后从头到尾迭代数组。类似于:

%!
/palindrome
{
count array astore
aload
dup length 1 sub -1 0 {
1 index exch get
exch
} for
pop
} def
(line 1)
2
3
(before palindromen) print
pstack
palindrome
(after palindromen) print
pstack

也可以(我在这里有一个工作示例(通过使用for循环和操作堆栈,在不定义任何额外存储对象(字典或数组(的情况下一次性完成这项工作。对我来说,这似乎是一个更优雅的解决方案,留给读者练习:-(

最新更新