0033【Edabit ★☆☆☆☆☆】【字符串表达式计算器】Miserable Parody of a Calculator

0033【Edabit ★☆☆☆☆☆】【字符串表达式计算器】Miserable Parody of a Calculator

algebra math strings

Instructions

Create a function that will handle simple math expressions. The input is an expression in the form of a string.

Examples
javascript 复制代码
calculator("23+4") // 27
calculator("45-15") // 30
calculator("13+2-5*2") // 5
calculator("49/7*2-3") // 11
Notes
  • There will be no brackets in the input line.
  • No need to calculate mathematical functions (sin, cos, ln...).
  • There are no gaps in the expression.
Solutions
javascript 复制代码
function calculator(str) {
    return eval(str)
}
TestCases
javascript 复制代码
let Test = (function(){
    return {
        assertEquals:function(actual,expected){
            if(actual !== expected){
                let errorMsg = `actual is ${actual},${expected} is expected`;
                throw new Error(errorMsg);
            }
        }
    }
})();

Test.assertEquals(calculator("23+4"), 27)
Test.assertEquals(calculator("45-15"), 30)
Test.assertEquals(calculator("13+2-5*2"), 5)
Test.assertEquals(calculator("49/7*2-3"), 11)
Test.assertEquals(calculator("2+2*2"), 6)
Test.assertEquals(calculator("5/2"), 2.5)
Test.assertEquals(calculator("-34/4"), -8.5)
Test.assertEquals(calculator("0+43-0+56*0"), 43)
Test.assertEquals(calculator("-14*2-37-0"), -65)
Test.assertEquals(calculator("0*0"), 0)
Test.assertEquals(calculator("4+2+3-5*2-8/4-12-0+3-14"), -26)
Test.assertEquals(calculator("9/2+9/4"), 6.75)
Test.assertEquals(calculator("56*27*18*12/2*0"), 0)
Test.assertEquals(calculator("34/4*0*45*7"), 0)
相关推荐
陈平安Java and C39 分钟前
MyBatisPlus
java
秋野酱1 小时前
如何在 Spring Boot 中实现自定义属性
java·数据库·spring boot
Bunny02122 小时前
SpringMVC笔记
java·redis·笔记
feng_blog66882 小时前
【docker-1】快速入门docker
java·docker·eureka
枫叶落雨2224 小时前
04JavaWeb——Maven-SpringBootWeb入门
java·maven
m0_748232394 小时前
SpringMVC新版本踩坑[已解决]
java
码农小灰4 小时前
Spring MVC中HandlerInterceptor和Filter的区别
java·spring·mvc
乔木剑衣5 小时前
Java集合学习:HashMap的原理
java·学习·哈希算法·集合
古蓬莱掌管玉米的神5 小时前
vue3语法watch与watchEffect
前端·javascript
林涧泣5 小时前
【Uniapp-Vue3】uni-icons的安装和使用
前端·vue.js·uni-app