首页 > 解决方案 > 球拍编程:如何在 2 个空格字符后添加新行?

问题描述

我目前正在使用 Dr Racket 进行编程,我需要做的任务是提示用户输入文件。使用输入文件中的整数值,程序将执行计算 N 平方和的两个函数(two-lhs 和 two-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 

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

标签: stringschemeracket

解决方案


我们可以利用 Racket 的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)——实际上,我们可以只使用firstsecond过程,这样更容易理解。最后,在递归中,我们推进了两个元素:(cddr lst).


推荐阅读