On your first day of class, you enter the halls, eager to embark on the journey of a lifetime. Little do you know that you have forgotten to bring one essential tool to class — a calculator. 😳
Your professor, a renowned expert in the community, begins the day's lesson by presenting a series of complex mathematical problems that require the use of a calculator. The students around you huddle together, pulling out their calculators to tackle the challenge. 💪
As you search your bag in vain for your calculator, you realize that you have inadvertently left it behind. Instead of panicking or admitting defeat, your resourceful nature kicks in. You decide to rely on your intelligence and aptitude in Motoko to create your own calculator. 🧑💻
Your task is to create the code for a calculator, which is implemented as a canister.
The canister should have a public interface that allows users to perform calculations. The canister has a memory variable called counter
which represents the result of the most recent calculation.
- Define a mutable variable called
counter
of typeFloat
to store the result of the most recent calculation. - Implement the
add
function, which accepts a valuex
of typeFloat
, increments the counter, and returns its new value.
add : shared(x : Float) -> async Float;
- Implement the
sub
function, which accepts a valuex
of typeFloat
, decrements the counter, and returns its new value.
sub : shared(x : Float) -> async Float;
- Implement the
mul
function, which accepts a valuex
of typeFloat
, multiplies it with the counter, and returns the new value of the counter.
mul : shared(x : Float) -> async Float;
- Implement the
div
function, which accepts a valuex
of typeFloat
, divides the counter byx
, and returns the new value of the counter. Ensure to guard against division by zero.
div : shared(x : Float) -> async ?Float;
- Implement the
reset
function, which sets the value of counter to zero, effectively resetting it.
reset: shared () -> async ();
- Implement the
see
function, which serves as a query and returns the current value of counter.
see: shared query () -> async Float;
- Implement the
power
function, which accepts a valuex
of typeFloat
, raises the counter to the power ofx
, and returns the new value of the counter.
power: shared (x : Float) -> async Float;
- Implement the
sqrt
function, which calculates the square root of the counter and returns the new value of the counter.
sqrt: shared () -> async Float;
- Implement the
floor
function, which returns the largest integer less than or equal to the counter.
floor: shared () -> async Int;
- Deploy the
Calculator
on the Internet Computer.
At the end of the project, your canister should implement the following interface.
actor {
add : shared(x : Float) -> async Float;
sub : shared(x : Float) -> async Float;
mul : shared(x : Float) -> async Float;
div : shared(x : Float) -> async ?Float;
reset: shared () -> async ();
see: shared query () -> async Float;
power: shared (x : Float) -> async Float;
sqrt: shared () -> async Float;
floor: shared () -> async Int;
};