Skip to main content
added a note and examples
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

#JavaScript (ES6), 94 bytes

Not particularly short, but fun. Adding parentheses all over the place...

s=>([e,o]=s.split` `,[...o].map(x=>e=e.split(x).join((a+=')')+x+(b+='(')),a=b=''),eval(b+e+a))

###Test cases

let f =

s=>([e,o]=s.split` `,[...o].map(x=>e=e.split(x).join((a+=')')+x+(b+='(')),a=b=''),eval(b+e+a))

console.log(f("6.3*7.8 +-*/"))              // 49.14
console.log(f("2.2*3.3+9.9/8.8-1.1 */+-"))  // 7.285
console.log(f("2.2*3.3+9.9/8.8-1.1 +*/-"))  // 2.2
console.log(f("10/2+5-1 +-/*"))             // 1.6666
console.log(f("2147480/90+10*5 +/-*"))      // 107374
console.log(f("3*55-5/8/4+1 -/+*"))         // 7.6875

###Historical note

A similar method was used in early FORTRAN compilers. Here is a link from archive.org to a relevant article written by Donald E. Knuth in a 1962 book called Computers and automation.

###Examples

Let's consider the expression 2.2*3.3+9.9/8.8-1.1.

With operator precedence */+-, it will expand to:

((((2.2)*(3.3)))+(((9.9))/((8.8))))-((((1.1))))

With operator precedence +*/-, it will now expand to:

((((2.2))*((3.3)+(9.9)))/(((8.8))))-((((1.1))))

Removing all redundant parentheses, we get:

((2.2*3.3)+(9.9/8.8))-1.1 = 7.285

and:

((2.2*(3.3+9.9))/8.8)-1.1 = 2.2

#JavaScript (ES6), 94 bytes

Not particularly short, but fun. Adding parentheses all over the place...

s=>([e,o]=s.split` `,[...o].map(x=>e=e.split(x).join((a+=')')+x+(b+='(')),a=b=''),eval(b+e+a))

###Test cases

let f =

s=>([e,o]=s.split` `,[...o].map(x=>e=e.split(x).join((a+=')')+x+(b+='(')),a=b=''),eval(b+e+a))

console.log(f("6.3*7.8 +-*/"))              // 49.14
console.log(f("2.2*3.3+9.9/8.8-1.1 */+-"))  // 7.285
console.log(f("2.2*3.3+9.9/8.8-1.1 +*/-"))  // 2.2
console.log(f("10/2+5-1 +-/*"))             // 1.6666
console.log(f("2147480/90+10*5 +/-*"))      // 107374
console.log(f("3*55-5/8/4+1 -/+*"))         // 7.6875

#JavaScript (ES6), 94 bytes

Not particularly short, but fun. Adding parentheses all over the place...

s=>([e,o]=s.split` `,[...o].map(x=>e=e.split(x).join((a+=')')+x+(b+='(')),a=b=''),eval(b+e+a))

###Test cases

let f =

s=>([e,o]=s.split` `,[...o].map(x=>e=e.split(x).join((a+=')')+x+(b+='(')),a=b=''),eval(b+e+a))

console.log(f("6.3*7.8 +-*/"))              // 49.14
console.log(f("2.2*3.3+9.9/8.8-1.1 */+-"))  // 7.285
console.log(f("2.2*3.3+9.9/8.8-1.1 +*/-"))  // 2.2
console.log(f("10/2+5-1 +-/*"))             // 1.6666
console.log(f("2147480/90+10*5 +/-*"))      // 107374
console.log(f("3*55-5/8/4+1 -/+*"))         // 7.6875

###Historical note

A similar method was used in early FORTRAN compilers. Here is a link from archive.org to a relevant article written by Donald E. Knuth in a 1962 book called Computers and automation.

###Examples

Let's consider the expression 2.2*3.3+9.9/8.8-1.1.

With operator precedence */+-, it will expand to:

((((2.2)*(3.3)))+(((9.9))/((8.8))))-((((1.1))))

With operator precedence +*/-, it will now expand to:

((((2.2))*((3.3)+(9.9)))/(((8.8))))-((((1.1))))

Removing all redundant parentheses, we get:

((2.2*3.3)+(9.9/8.8))-1.1 = 7.285

and:

((2.2*(3.3+9.9))/8.8)-1.1 = 2.2
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

#JavaScript (ES6), 94 bytes

Not particularly short, but fun. Adding parentheses all over the place...

s=>([e,o]=s.split` `,[...o].map(x=>e=e.split(x).join((a+=')')+x+(b+='(')),a=b=''),eval(b+e+a))

###Test cases

let f =

s=>([e,o]=s.split` `,[...o].map(x=>e=e.split(x).join((a+=')')+x+(b+='(')),a=b=''),eval(b+e+a))

console.log(f("6.3*7.8 +-*/"))              // 49.14
console.log(f("2.2*3.3+9.9/8.8-1.1 */+-"))  // 7.285
console.log(f("2.2*3.3+9.9/8.8-1.1 +*/-"))  // 2.2
console.log(f("10/2+5-1 +-/*"))             // 1.6666
console.log(f("2147480/90+10*5 +/-*"))      // 107374
console.log(f("3*55-5/8/4+1 -/+*"))         // 7.6875