2

我正在学习golfscript,我想读入一个包含数字的文件并在打印出一行之前进行平方。例如,我有一个如下文件

1
2
3
4

然后我需要打印出来

1 4 9 16

我怎样才能做到这一点?

4

1 回答 1

2

以下代码为您提供了一个想法,即如何完成任务。

;                             # Discard the input (golfscript takes 
                              # input from STDIN)

"#{File.read('input.txt')}"   # Read contents of file into a string
                              # (see your previous question 
                              # http://stackoverflow.com/q/17826704)

[~]                           # Evaluate the input into an array

{2?}%                         # Apply the operators `2?` to each element
                              # ( "x 2 ?" simply calculates x^2 )

" "*                          # Join the array elements with spaces

                              # By default the remaining items on the stack
                              # are printed when the program ends

您可以在此处找到每个操作员的详细信息。

于 2013-07-25T06:32:56.197 回答