编程介的小学生 2019-12-31 01:49 采纳率: 0.2%
浏览 56

Simple Desk Calculator

Problem Description
Your task are to write a program that imitates a simple desk calculator. Your calculator must be able to accept an infix expression which at least includes (, ), +, -, *, /,% . If the expression is legal, output its value, else ouput an error message.

Input
There are several test cases, each occupies one line that contains an infix expression. Proceed until the end of the file.

Output
For each test case, in one line the value of that expression ( accurate up to two decimal places), or an error message “ERROR IN INFIX NOTATION”.

Sample Input
4.99+5.99+6.99*1.06
(3*5*(4*8)%2)
1+2(
2/0
2.5%2
2%2.5

Sample Output
18.39
0.00
ERROR IN INFIX NOTATION
ERROR IN INFIX NOTATION
ERROR IN INFIX NOTATION
ERROR IN INFIX NOTATION

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-27 12:50
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    # Simple Desk Calculator
    #
    # This function takes an infix expression as input and returns its value.
    # The expression should include parentheses, addition (+), subtraction (-),
    # multiplication (*), division (/), and modulo (%) operations.
    # It outputs the result or an error message if the expression is invalid.
    
    simple_desk_calculator <- function(expr) {
      # Function to evaluate the expression
      evaluate_expression <- function(expression) {
        tryCatch({
          # Split the expression into tokens
          tokens <- strsplit(expression, "")[[1]]
          
          # Check for valid operators
          if (length(tokens) < 3 || !all(token == "+" | "-" | "*" | "/" | "%")) {
            return("ERROR IN INFIX NOTATION")
          }
          
          # Calculate the value based on precedence
          left_operand <- parseNum(tokens[1])
          operator <- tokens[2]
          right_operand <- parseNum(tokens[3])
          
          match(operator, c("+", "-", "*", "/", "%"))
          
          # Evaluate the expression recursively
          eval(left_operand, right_operand)
        }, error = function(e) {
          return(e$message)
        })
      }
      
      # Split the input expression into tokens
      tokens <- strsplit(expr, "")[[1]]
      
      # Try evaluating the expression
      tryCatch({
        evaluate_expression(expr)
      }, error = function(e) {
        return(e$message)
      })
    }
    
    # Example usage:
    # exprs <- c("(3*5*(4*8)%2)", "1+2(", "2/0", "2.5%2", "2%2.5")
    # results <- lapply(exprs, simple_desk_calculator)
    # print(results)
    

    This code defines a simple_desk_calculator function that evaluates an infix expression using recursion and tokenization. The function first checks if the expression is valid by ensuring it has three tokens and uses only allowed operators. Then, it calculates the value of the expression according to the order of operations. If the expression is not valid, it returns an error message. Finally, we demonstrate how to use this function with example expressions and print their results.

    评论

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上