Implement a simple integer operation scriptable calculator.
Concept
The accumulator starts at 0 and has operations performed on it. At the end of the program output the value of the accumulator.
Operations:
+
adds1
to the accumulator-
subtracts1
from the accumulator*
multiplies the accumulator by2
/
divides the accumulator by2
Sample script
The input ++**--/
should give the output 3
.
Example implementation
def calc(s)
i = 0
s.chars.each do |o|
case o
when '+'
i += 1
when '-'
i -= 1
when '*'
i *= 2
when '/'
i /= 2
end
end
return i
end
Rules
- This is code-golf, so lowest answer in bytes wins, but is not selected.
- Creative implementations are encouraged.
- Standard loopholes are prohibited.
- You get the program via stdin or arguments, and you can output the answer via return value or stdout.
- Have fun.
- Division truncates down because it is integer division.
- The program
-/
returns-1
.
Test cases
*///*-*+-+
-1
/*+/*+++/*///*/+-+//*+-+-/----*-*-+++*+**+/*--///+*-/+//*//-+++--++/-**--/+--/*-/+*//*+-*-*/*+*+/+*-
-17
+++-+--/-*/---++/-+*-//+/++-*--+*+/*/*/++--++-+//++--*/***-*+++--+-*//-*/+*/+-*++**+--*/*//-*--**-/-*+**-/*-**/*+*-*/--+/+/+//-+*/---///+**////-*//+-+-/+--/**///*+//+++/+*++**++//**+**+-*/+/*/*++-/+**+--+*++++/-*-/*+--/++*/-++/-**++++/-/+/--*/-/+---**//*///-//*+-*----+//--/-/+*/-+++-+*-*+*+-/-//*-//+/*-+//+/+/*-/-/+//+**/-****/-**-//+/+-+/*-+*++*/-/++*/-//*--+*--/-+-+/+/**/-***+/-/++-++*+*-+*+*-+-//+/-++*+/*//*-+/+*/-+/-/*/-/-+*+**/*//*+/+---+*+++*+/+-**/-+-/+*---/-*+/-++*//*/-+-*+--**/-////*/--/*--//-**/*++*+/*+/-+/--**/*-+*+/+-*+*+--*///+-++/+//+*/-+/**--//*/+++/*+*////+-*-//--*+/*/-+**/*//+*+-//+--+*-+/-**-*/+//*+---*+//*/+**/*--/--+/*-*+*++--*+//+*+-++--+-*-*-+--**+/+*-/+*+-/---+-*+-+-/++/+*///*/*-+-*//-+-++/++/*/-++/**--+-////-//+/*//+**/*+-+/+/+///*+*///*-/+/*/-//-*-**//-/-+--+/-*--+-++**++//*--/*++--*-/-///-+/+//--+*//-**-/*-*/+*/-*-*//--++*//-*/++//+/-++-+-*/*-+++**-/-*++++**+-+++-+-***-+//+-/**-+/*+****-*+++*/-*-/***/-/*+/*****++*+/-/-**-+-*-*-++**/*+-/*-+*++-/+/-++*-/*-****-*
18773342
/
can yield non-integers. \$\endgroup\$-/
return? \$\endgroup\$