方案编程I / O并删除

时间:2015-04-26 09:51:57

标签: scheme

我正在做一个计划项目并且在编码方面存在一些问题。 对于我的项目,我们必须保留一个类名单(作为列表实现)并能够执行不同的操作。我有两个问题:

  1. 我的写入名单功能打开了传递的文件名,但是没有将列表写入文件,我不知道为什么?当n = 2时,你可以在执行任务函数中找到这个函数。

  2. 我的删除功能...当我去测试它时,我收到错误: ;程序#[编译程序13(“列表”#x3)#x14#x11a2714]已被调用4个参数;它只需要2个参数。 我的删除功能叫做removestu

  3. 这是我的代码:

        (define performtask
      (lambda (n roster)
      (cond ((= n 0) (begin
                       (display "\n\tResetting roster...\n\n")
                       (menu '())
                      ))
            ((= n 1) (begin
                        (display "\n\tLoad roster from file: ")
                        (read (open-input-file (read-line)))
                        (menu roster)
                       ))
            ((= n 2) (begin
                        (display "\n\tStore roster to file: ")
                        (write roster (open-output-file (read-line)))
                        (menu roster)
                       ))
            ((= n 3) (begin
                        (display "\n\tDisplaying roster, sorted by ID:\n")
                        (printroster (select-sort roster))
                        (menu roster)
                     ))
            ((= n 4) (begin
                        (display "\n\tDisplaying roster, sorted by ID:\n")
                        (printroster (select-sort-name roster))
                        (menu roster)
                     ))
            ((= n 5) (begin
                        (display "\n\tDisplaying roster, sorted by ID:\n")
                        (printroster (select-sort-grade roster))
                        (menu roster)
                     ))
            ((= n 6) (begin
                        (display "\n\tEnter student name or ID: ")
                        (studentinfo roster (read-line))
                        (menu roster)
                     ))
            ((= n 7) (begin
                        (display "\n\tAdd a student to the class roster:\n\n")
                        (cond ((null? roster) (menu (read-3-items 0 '())))
                        (else (menu (list (read-3-items 0 '()) roster))))
                     ))
            ((= n 8) (begin
                        (display "\n\tEnter student name or ID: ")
                        (removestu roster (read-line))
                        (menu roster)
                     ))
    
    
            ((= n 9) (begin
                        (display "\n\tExiting program...\n\n")
                          #t
                     ))
            (else (begin
                        (display "\n\tTask no. ")
                        (display n)
                        (display " does not exist.\n\n")
                        (menu roster)
                     )
                )
          )
       )
    )
    
    (define studentinfo
       (lambda (lst value)
          (cond ((null? lst) (display "\n\tStudent is not found in roster.\n"))
                    ((equal? (car (car lst)) value) (printrecord (car lst)))
                        ((equal? (car (cdr (car lst))) value) (printrecord (car lst)))
                             (else (studentinfo (cdr lst) value))
          )
       )
    )
    
    
    (define printroster
       (lambda (billy)
          (cond ((null? billy) (newline))
                    (else (begin
                                 (printrecord (car billy))
                                      (printroster (cdr billy))
                                        )
                              )
          )
       )
    )
    
    (define printrecord
       (lambda (lst)
          (begin
             (display "\tID=")
             (display (car lst))
             (display ", Name=")
             (display (car (cdr lst)))
             (display ", Grade=")
             (display (car (cdr (cdr lst))))
             (newline)
          )
       )
    )
    
    (define select-sort
       (lambda (roster)
          (cond ((null? roster) '())
                (else (cons (smallest roster (car roster)) (select-sort (remove roster (smallest roster (car roster))))))
          )
       )
    )
    
    (define select-sort-name
       (lambda (roster)
          (cond ((null? roster) '())
                (else (cons (smallest-name roster (car roster)) (select-sort (remove roster (smallest-name roster (car ro\
    ster))))))
          )
       )
    )
    
    (define select-sort-grade
       (lambda (roster)
          (cond ((null? roster) '())
                    (else (cons (smallest-grade roster (car roster)) (select-sort (remove roster (smallest-grade roster (\
    car roster))))))
          )
       )
    )
    
    (define smallest
       (lambda (roster record)
          (cond ((null? roster) record)
                    ((< (car (car roster)) (car record)) (smallest (cdr roster) (car roster)))
                        (else (smallest (cdr roster) record))
          )
       )
    )
    
    (define smallest-name
       (lambda (roster record)
          (cond ((null? roster) record)
                ((< (car (cdr (car roster))) (car (cdr record))) (smallest-name (cdr roster) (car roster)))
                (else (smallest-name (cdr roster) record))
          )
       )
    )
    
    (define smallest-grade
       (lambda (roster record)
          (cond ((null? roster) record)
                ((< (cdr (cdr (car roster))) (cdr (cdr record))) (smallest-grade (cdr roster) (car roster)))
                (else (smallest-grade (cdr roster) record))
          )
       )
    )
    
    (define removestu
      (lambda (roster item)
          (cond ((null? roster) '())
                ((equal? item (car roster)) (cdr roster))
                (else (cons (car roster) removestu (cdr roster) item))
          )
       )
    )
    
    (define read-3-items
       (lambda (n l)
          (cond ((= n 0) (begin
                           (display "\tStudent ID: ")
                           (read-3-items 1 (list (read)))
                                ))
                    ((= n 1) (begin
                               (display "\n\tStudent name: ")
                               (read-3-items 2 (list (car l) (read-line)))
                                            ))
     ((= n 2) (begin
                                   (display "\n\tGrade: ")
                                   (list (car l) (car (cdr l)) (read))
                                        ))
          )
       )
    )
    
    
    (define menu
       (lambda (roster)
          (begin
             (display "\n\tClass roster management system\n")
              (display "\t============================\n")
               (display "\t   MENU\n")
                (display "\t============================\n")
                 (display "\t0. Reset roster\n")
                  (display "\t1. Load roster from file\n")
                   (display "\t2. Store roster to file\n")
                    (display "\t3. Display roster sorted by ID\n")
                     (display "\t4. Display roster sorted by name\n")
                      (display "\t5. Display roster sorted by grade\n")
                       (display "\t6. Display student info\n")
                        (display "\t7. Add a student to roster\n")
    (display "\t8. Remove a student from roster\n")
                          (display "\t9. Exit\n\n")
                           (display "\tEnter your choice: ")
                            (performtask (read) roster)
          )
       )
       )
    

1 个答案:

答案 0 :(得分:1)

  1. (write roster (open-output-file (read-line)))似乎没问题。但是你不能关闭从open-output-file获得的端口。也许您应该使用with-output-to-file代替。

  2. 使用4个参数cons调用
  3. (cons (car roster) removestu (cdr roster) item)。成对只需要两个!

  4. 我注意到在阅读文件时你不会使用它。尾部调用为(menu roster),并且(read (open-input-file (read-line)))的结果被读取并返回,并且由于未使用,因此被删除。