代码之家  ›  专栏  ›  技术社区  ›  calebeja9

球拍编程:如何在2个空格字符后添加新行?

  •  1
  • calebeja9  · 技术社区  · 6 年前

    我目前正在使用Racket博士进行编程,我需要做的任务是向用户提示输入文件。使用输入文件中的整数值,程序将执行两个计算n平方和的函数(两个lhs和两个rhs),并将结果输出到提示的输出文件;左侧列出两个lhs中的值,右侧列出两个rhs中的值。

    例如:假设目录中有一个名为'data'的文件,其中第一行有整数25,第二行有7,第三行有9。输入“data”作为输入文件和“testing”作为输出文件的用户,将在目录中创建名为“testing”的输出文件,其值和格式如下:

    (results from two-lhs)     (results from two-rhs)
    5525 5525
    140 140
    285 285
    

    这是我当前的代码,有我理解的注释:

    #lang racket
    
    (define squared ;helper function for two lhs
      (lambda (x) (* x x)))
    
    
    
    (define Two-LHS 
      (lambda (n)
        (cond((= n 0) 0)
             (else 
              (+ (squared n) (Two-LHS(- n 1)))))))
    
    
    
    (define Two-RHS 
      (lambda (n)
        (cond ((= n 0) 0)
              (else
               (/ (* n (+ n 1) (+ (* n 2) 1)) 6)))))
    
    
    (define in ;function that reads in the input file from user
      (lambda ()
        (let((pin(open-input-file (symbol->string (read))))) ;prompts the user for input file. pin = the input-port
          (let f ((x(read pin))) ;f is a procedure that reads the input port?
            (if(eof-object? x) ; x reads the value inside pin and if x happens to be end of file object
               (begin          ; then closes the input-port
                 (close-input-port pin)
                 '())
               (cons (Two-LHS x)(cons (Two-RHS x)(f(read pin))))) ;else using the x, executes two lhs and rhs until x reaches
            ))))                                                  ; end of file to close the port
    
    (define write-lst 
      (lambda (lst outp) ;lst = input file, outp = output file
        (if(eq? lst '()) ; if input file contains an empty list
           (close-output-port outp) ; the output-port will be closed
           (begin                   ; else execute begin
             (write (car lst) outp) ; which writes the first element of the list to the output file
             (display #\space outp) ; will add whitespace after each element to the output file.
             (newline outp) ; was thinking this would add newline on the output file after each iteration, but need a way to add newline after every 2 whitespace. 
             (write-lst (cdr lst) outp))))) ;recurses back to write-lst function with the next element in the list without
                                            ;the first element until it becomes an empty list so that output-port could close.
    
    (define out ;will be renamed to two-sum, since this is the function that will write to the output file.
      (lambda (lst) ;lst = input file
        (let((pout(open-output-file (symbol->string (read))))) ; prompts the user for the output file, pout = the output-port
          (write-lst lst pout); uses write-list function to write out to output file
          )))
    (out (in))
    

    我运行代码得到的输出文件是:

    5525 
    5525 
    140 
    140 
    285 
    285 
    

    如何使输出文件正确格式化? 任何正确方向的帮助都将不胜感激! 谢谢您。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Óscar López    6 年前

    我们可以租球拍 fprintf 使事情更简单的过程,并一次遍历列表中的两个元素-假设它有偶数个元素:

    (define write-lst
      (lambda (lst outp)
        (if (null? lst)
            (close-output-port outp)
            (begin
              (fprintf outp "~a ~a~n" (car lst) (cadr lst))
              (write-lst (cddr lst) outp)))))
    

    技巧就在这里,格式字符串: "~a ~a~n" . 它说明:打印一个对象、一个空白、另一个对象和一条新行。我们传递当前元素 (car lst) 第二个元素 (cadr lst) -事实上,我们可以使用 first second 更容易理解的程序。最后,在递归中,我们前进两个元素: (cddr lst) .