首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >球拍中的堆栈计算器

球拍中的堆栈计算器
EN

Code Review用户
提问于 2018-02-18 18:33:31
回答 1查看 774关注 0票数 1

我已经实现了一个基于这个编程任务的堆栈计算器。我想知道是否一个更有经验的骗子能给我反馈,并告诉我,如果我错过了什么在球拍,使解决方案更优雅。

代码语言:javascript
复制
#lang racket

(define (inc n)
  (+ n 1))

(define (print-value s)
  (begin
    (display s)
    (display " ")))

(define (exec l (stack empty) (opcount 1))
  "executes a stack based program."
  (cond
    [(empty? l) (void)]

    ;; push number onto stack
    [(number? (string->number (first l)))
     (exec (rest l)
           (cons (string->number (first l)) stack)
                 (inc opcount))]

    ;; pop number from stack
    [(equal? (first l) ".")
     (begin
       (print-value (first stack))
       (exec (rest l) (rest stack)
       (inc opcount)))]

    ;; mathmatical operators
    [(equal? (first l) "+")
     (exec (rest l) (cons
                     (+ (first stack) (second stack))
                     (rest (rest stack))) (inc opcount))]
    [(equal? (first l) "-")
     (exec (rest l) (cons
                     (- (first stack) (second stack))
                     (rest (rest stack))) (inc opcount))]
    [(equal? (first l) "*")
     (exec (rest l) (cons
                     (* (first stack) (second stack))
                     (rest (rest stack))) (inc opcount))]
    [(equal? (first l) "/")
     (exec (rest l) (cons
                     (/ (first stack) (second stack))
                     (rest (rest stack))) (inc opcount))]

    ;; duplication operator
    [(equal? (string-downcase (first l)) "dup")
     (exec (rest l)
           (cons (first stack) stack) (inc opcount))]
    [else
     (~a "Error: operation " opcount " invalid")]))

(define program (string-split "64 DUP * ."))

(exec program)
EN

回答 1

Code Review用户

回答已采纳

发布于 2018-02-19 07:55:26

Bug

-/运算符从典型的RPN顺序向后解释它们的操作数。也就是说,我希望"10 7 -"应该生成3,但实际上它会生成-3

Recursion

您的代码非常重复,因为每个操作符的处理程序必须获取操作符((first l)),执行操作,然后进行递归调用((exec (rest l) … (inc opcount)))。应该定义一个将操作符应用于堆栈的助手函数。

代码语言:javascript
复制
(define (apply-op op stack)
  (cond
    [(number? (string->number op))
     (cons (string->number op) stack)]
    [(equal? op ".")
     (begin
       (display (first stack))
       (display " ")
       (rest stack))]
    [(equal? op "+")
     (cons (+ (second stack) (first stack)) (rest (rest stack)))]
    [(equal? op "-")
     (cons (- (second stack) (first stack)) (rest (rest stack)))]
    [(equal? op "*")
     (cons (* (second stack) (first stack)) (rest (rest stack)))]
    [(equal? op "/")
     (cons (/ (second stack) (first stack)) (rest (rest stack)))]
    [(equal? (string-downcase op) "dup")
     (cons (first stack) stack)]
    [else
     (~a "operation '" op "' invalid")]))

然后,exec函数只需调用它并驱动递归。

代码语言:javascript
复制
(define (exec ops (stack empty) (opcount 1))
  "executes a stack based program."
  (cond
    [(empty? ops) (void)]
    [else
      (let ([next-stack (apply-op (first ops) stack)])
           (if (string? next-stack)
               (~a "Error at operator " opcount ": " next-stack)
               (exec (rest ops) next-stack (+ opcount 1))))]))
票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/187817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档