Latest update Jan 6, 2023.
In this assignment you will create a calculator with a GUI. It must have the following features:
0-9
and '.
' to input operands. Note that it
should be possible to input decimal numbers.+,-,*,/
to input operators.3*2+55/0.2+
'.=
' button that evaluates the input expression, and
updates the display with the resulting value.In addition, the following is required:
Also give some thought to how to handle situations with erroneous inputs, for instance if two operators are input right after each other. In any case the program should not crash upon an error: instead it should display an appropriate error message in the display. Like in Lab 3 termination and error handling is to be value-based rather than interrupt-based.
The semantics for expressions should be the usual "left-to-right"
semantics used by calculators, where an operator that is input takes the
currently input expression as left-hand side argument, and the next input
value as the right-hand side argument. Thus, the
input 3*2+55/0.2+4
should be interpreted
as (((3*2)+55)/0.2)+4
. Note that this is different from the
interpretation that results if the usual precedence rules for arithmetic
operators are applied.
The focus of the assignment is really on how to structure a reactive program, using the features of a language like F#, rather than creating a beautiful GUI. A reactive program reacts to things happening in its environment, like some data becoming available or some user input taking place. A GUI is an example of a reactive program, but by no means the only one: for instance, many embedded systems are reactive.
In F#, reactive programs are usually based on events and event handlers. To make your task of writing the reactive part of the program easier we have provided you with a code template for a fairly generic event handling loop, which interprets event occurrences as they appear and provides the proper handling. You should actually not have to write a lot of code on your own to obtain a solution.
Unfortunately there are some issues with the builtin IEvent<'a>
data type in F# that in some situations can give rise to so-called memory
leaks. More recent versions of F# and Visual Studio can instead use
the Fsharpx
library. The easiest way of installing Fsharpx
is to run the command 'Install-package Fsharpx.Extras
' in
the NuGet package manager console. In Visual Studio this can be found under
Tools->NuGet Package Manager->Packet Manager Console. Alternatively you can
download the source
from https://github.com/fsprojects/FSharpx.Extras
and build it, if you use a platform where the NuGet packet manager is not
supported.
(Note: in Visual Studio 2015 there are some measures that have to be taken to install the package properly with NuGet: see our FAQ.)
A code template for the event handling, which is based on
the Fsharpx
library, is found
here. You can either add the
.zip archive to "\My Documents\Visual Studio
Version\Templates\ProjectTemplates\" (F# doesn't have a language specific
folder, so just place it in this directory), or create a new project and add
the .fs files. Note: if you create a new project you need to install
Fsharpx.Extras
(as described above) and add a reference to
WinForms (Project -> Add Reference -> System.Windows.Forms).
For those of you that have an older version of F# or Visual Studio, we have
made an alternative code template that is based on IEvent<'a>
.
It is found here. If you for
some reason have trouble to install Fsharpx
according to above,
then you can always fall back on this alternative.
You are free to skip the code templates if you wish, and develop your own solution. However, then be prepared to explain in some more detail how your solution works. We will demand that your solution is "declarative style", without excessive use of imperative features.