A calculator as a list of numbers and operators
JavaScript (ES6) 53
A function taking an array as input.
Run the snippet in Firefox to test.
f=a=>a.map(t=>t<'0'?o=t:v=eval(v+o+t)|0,v=0,o='+')&&v
// TEST
out=x=>O.innerHTML=x;
input = [5,8,25,"*",9,6,2,"-",104,"/",4,7,"+",6,"%",14];
out(input.join(' ')+' -> '+f(input));
function go() {
i=I.value.split(/ +/),out(I.value+' -> '+f(i))
}
<pre id=O></pre>
Your test:<input id=I><button onclick='go()'>GO</button>
Pyth - 24 23 22 20 bytes
2 bytes saved thanks to @issacg and 1 thanks to @orlp!
Uses reduce with base case of 0
and checks for '
being in repr to detect string vs. int.
u.xsv++GbH&=bHG+\+QZ
Does not work online because I use full eval which is disabled online for security reasons. Takes input from stdin in a list as such: 5, 8, 25, "*", 9, 6, 2, "-", 104, "/", 4, 7, "+", 6
.
Julia, 85 83 bytes
s->(o=0;p="+";for i=split(s) isdigit(i)?o=eval(parse("ifloor($o$p$i)")):(p=i)end;o)
This creates an unnamed function that accepts a string as input and returns an integer.
Ungolfed:
function f(s::String)
# Assign the starting output value o and operator p
o = 0
p = "+"
# Split the input string into an array on spaces
for i = split(s)
if isdigit(i)
# Assign o using string interpolation
o = eval(parse("ifloor($o $p $i)"))
else
# Assign p to the new operator
p = i
end
end
end
Fixed issue and saved 2 bytes thanks to Glen O.