Remove unnecessary parentheses
Mathematica, 105 97 91 bytes
-6 bytes thanks to Roman!
a=StringReplace;ToString@ToExpression@a[#,{"*"->"**","+"->"~~"}]~a~{" ** "->"*","~~"->"+"}&
Replaces +
and *
with ~~
(StringExpression
) and **
(NonCommutativeMultiply
) respectively, evaluates it, stringifies it, and replaces the operators back.
JavaScript (ES6) 163 178
Edit 15 bytes saved thx @IsmaelMiguel
a=>eval(`s=[]${_=';for(b=0;a!=b;a=b.replace(/'}\\(([^()]*)\\)(?=(.?))/,(x,y,z,p)=>~y.indexOf('+')?-s.push(b[p-1]=='*'|z=='*'?x:y):y))b=a;${_}-\\d+/,x=>s[~x]))b=a`)
Less golfed
a=>{
for(s=[],b='';
a!=b;
a=b.replace(/\(([^()]*)\)(?=(.?))/,(x,y,z,p)=>y.indexOf('+')<0?y:-s.push(b[p-1]=='*'|z=='*'?x:y)))
b=a;
for(b=0;
a!=b;
a=b.replace(/-\d+/,x=>s[~x]))
b=a;
return a
}
Test
f=a=>eval(`s=[]${_=';for(b=0;a!=b;a=b.replace(/'}\\(([^()]*)\\)(?=(.?))/,(x,y,z,p)=>~y.indexOf('+')
?-s.push(b[p-1]=='*'|z=='*'?x:y)
:y))b=a;${_}-\\d+/,x=>s[~x]))b=a`)
console.log=x=>O.textContent+=x+'\n'
test=`(4*12)+11 ==> 4*12+11
(1+2)*3 ==> (1+2)*3
3*(4*5) ==> 3*4*5
((((523)))) ==> 523
(1+1) ==> 1+1
1*(2*(3+4)*5)*6 ==> 1*2*(3+4)*5*6
1*(2+3) ==> 1*(2+3)
0*(1+0) ==> 0*(1+0)
(((2+92+82)*46*70*(24*62)+(94+25))+6) ==> (2+92+82)*46*70*24*62+94+25+6`
test.split`\n`.forEach(r=>{
var t,k,x
[t,,k]=r.match(/\S+/g)
x=f(t)
console.log((x==k?'OK ':'KO ')+t+' -> '+x+(x==k?'':' expected '+k))
})
<pre id=O></pre>
Python3 + PEG implementation in Python, 271 bytes
import peg
e=lambda o,m=0:o.choice and str(o)or(m and o[1][1]and"("+e(o[1])+")"or e(o[1]))if hasattr(o,"choice")else o[1]and e(o[0],1)+"".join(str(x[0])+e(x[1],1)for x in o[1])or e(o[0])
print(e(peg.compile_grammar('e=f("+"f)*f=p("*"p)*p="("e")"/[0-9]+').parse(input())))
A while back I made a PEG implementation in Python. I guess I can use that here.
Parses the expression into a tree, and only keeps parenthesis if the child is addition, and the parent is multiplication.