微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在不使用PostScript滚动的情况下将堆栈顶部的数字移到底部?

如何解决如何在不使用PostScript滚动的情况下将堆栈顶部的数字移到底部?

我想在字典中定义一些东西,将其命名为top,这样它就可以将堆栈顶部的数字移到底部,并将所有其他数字保留在适当的位置。我想不使用roll运算符就这样做。我已经尝试了好几个小时,但是对于PostScript还是陌生的,并且感到沮丧,因为这看起来像是一个简单的任务,我可以用任何其他语言迅速解决

这是我到目前为止所了解的,但不知道为什么它不起作用:

/top {
  1 dict begin
  count 1 gt 
    {
      /first exch def
      /second exch def
      first top second
    }
} def

解决方法

您可以将所有内容存储在一个数组中,然后getgetinterval您想要的片段。

/first { 0 get } def
/rest { 1  1 index length 1 sub  getinterval } def

% a b c d ... n  --  b c d ... n a
/top {
  1 dict begin
    count array astore /stack exch def
    stack rest aload pop
    stack first
  end
} def

这也可能是更多因素。

<<
    /first { 0 get }
    /rest  { 1  1 index length 1 sub  getinterval }
    /aa    { array astore }
    /:=    { exch def }
    /spill { aload pop }
>> currentdict copy

% a b c d ... n  --  b c d ... n a
/top {
  1 dict begin
    count aa /stack :=
    stack rest spill
    stack first
  end
} def

/spill可以用几种方式编写

/spill { {} forall } def

甚至

% assumes array does not contain executable names or arrays
/spill { cvx exec } def

编辑:我可能误解了这个问题,并显示了想要的相反内容。上面的代码执行count -1 roll。要count 1 roll将顶部元素移到底部,

/head { 0  1 index length 1 sub  getinterval } def
/last { dup length 1 sub  get } def

% a b c ... x y z  --  z a b c .. x y
/top {
  1 dict begin
    count array astore /stack exch def
    stack last
    stack head aload pop
  end
} def

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。