4
\$\begingroup\$

I recently had to add a small basic calculator to a project. I wrote a quick dirty one, but it seemed small enough to check with you people out here ;)

Just out of curiosity & an appetite to get surprised for some real hacky solutions, try and beat this one :

CODE (JS/JQUERY):

function scalcy() {
    var b="", p=parseFloat,t,i;
    return $("<div>").append("789/456*123-.0=+".split("").map(function(i){
        return $("<button/>").text(i).width(32).height(32).click(function(){
        b = (!p(i=$(this).text())? b.replace(/\D+$/,""): b) + i;        
        t.val(((!(i.match(/[.\d]/))? (b=eval(b.slice(0,-1))+i+"").slice(0,-1):
                p(b.match(/[.\d]+/g).pop()) || 0) + "").slice(0,8));
    })})).prepend(t=$("<input/>").width(123).click(function(){
        this.value=b="0";})).width(130);
}

Screenshot (in chrome): (DEMO)

enter image description here

Considerations: Better the UI & lesser the bugs, the better.

Minimum Functionalities:

  • Supports floating point arithmetic (/*-+),
  • Reset feature,
  • Continuous Operation (like working on with the last result)

Note:

  • Use any language & library, but please not a library call like new calculator().
  • Forking the above code itself is most welcome.

Additional Notes:

  • A graphical UI is obviously considered better than not having any at all.
  • I'd suggest to have a look at this challenge (for inspiration): Create a GUI Piano.
\$\endgroup\$
13
  • 4
    \$\begingroup\$ What is the objective winning criteria? How do you measure how great a UI is? \$\endgroup\$ – Johannes Kuhn Sep 5 '13 at 8:15
  • \$\begingroup\$ Basically the calculator should look like the calculator we all know, some buttons & a textbox (like the image above). No sacrifices to be made on that.. \$\endgroup\$ – loxxy Sep 5 '13 at 11:27
  • \$\begingroup\$ I don't like to duplicate UI things (and support only the new one). I have thoose buttons on my keyboard, which don't work on your solution. \$\endgroup\$ – Johannes Kuhn Sep 5 '13 at 11:31
  • 2
    \$\begingroup\$ Congratulations, you just made a chamelion question that invalidated a previously valid answer. \$\endgroup\$ – Johannes Kuhn Sep 5 '13 at 12:14
  • 2
    \$\begingroup\$ @loxxy Maybe no one else attempts a question which is not well-specified and has no objective winning criterion. Reading and following the comments usually generates better questions and also more interest in solving them. \$\endgroup\$ – Howard Sep 5 '13 at 13:15
9
\$\begingroup\$

Tcl/Tk, 100

pack [entry .e -textv e] -f both -e 1
bind . <Return> {catch {expr $e} e}
bind . <Escape> {set e {}}

Buttons are not required, so leave them alone.
The GUI is great. So simple that you can finally focus on the task that you try to do.

\$\endgroup\$
11
  • 1
    \$\begingroup\$ I'm not familiar with the language, so I need to ask: This isn't using a text-based interface, as opposed to graphical one? \$\endgroup\$ – shiona Sep 4 '13 at 16:02
  • 2
    \$\begingroup\$ Tk is a graphical toolkit. Yes, it has a window and so on. Just no buttons :P \$\endgroup\$ – Johannes Kuhn Sep 4 '13 at 16:26
  • 5
    \$\begingroup\$ Upvoted for strict interpretation of rules... one of my favorite games on this site. \$\endgroup\$ – boothby Sep 5 '13 at 1:58
  • 1
    \$\begingroup\$ You only save 12 characters (bind <>) for in exchange for the lmap and list formating. Yes, this trade of isn't worth it most of the time. \$\endgroup\$ – Johannes Kuhn May 2 '18 at 0:21
  • 1
    \$\begingroup\$ Outgolfed: 106 \$\endgroup\$ – sergiol May 2 '18 at 0:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.