Skip to content

Instantly share code, notes, and snippets.

@stevenarellano
Last active September 26, 2022 21:30
Show Gist options
  • Save stevenarellano/0b1ab765c91d56afaa6045eebacecec3 to your computer and use it in GitHub Desktop.
Save stevenarellano/0b1ab765c91d56afaa6045eebacecec3 to your computer and use it in GitHub Desktop.
basic solidity calculator
// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;
contract Calculator {
int c;
// 1. create a constructor function that initializes our base calculator value
constructor() {}
// 2. create interface functions for adding, subtracting, multiplying, and dividing c by a number a
// add keywords and parameters if needed.
function add() {}
function sub() {}
function mul() {}
function div() {}
// 3. create a function for setting c back to 0
function reset() {}
// 4. create a getter function to return the current state of the calculator value c
function getResult() {}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;
contract Calculator {
int c;
constructor() {
c = 0;
}
function add(int a) public {
c = c + a;
}
function sub(int a) public {
c = c - a;
}
function mul(int a) public {
c = c * a;
}
function div(int a) public {
require(a > 0, "The second parameter should be larger than 0");
c = c / a;
}
function reset() public {
c = 0;
}
function getResult() public view returns (int x) {
return c;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment