-
Notifications
You must be signed in to change notification settings - Fork 2
/
038-library-assignment.sol
74 lines (63 loc) · 1.68 KB
/
038-library-assignment.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* @title Library Assignment
*/
library Calculator {
function performArithmeticOperation(
uint256 value1,
uint256 value2,
string memory operator
) public pure returns (uint256) {
// incase of subtraction
if (
keccak256(abi.encodePacked(operator)) ==
keccak256(abi.encodePacked("-"))
) {
return value1 - value2;
}
// incase of multiplication
if (
keccak256(abi.encodePacked(operator)) ==
keccak256(abi.encodePacked("*"))
) {
return value1 * value2;
}
// incase of division
if (
(keccak256(abi.encodePacked(operator)) ==
keccak256(abi.encodePacked("/")))
) {
if (value2 == 0) return 0;
return value1 / value2;
}
// default case: incase if addition
return value1 + value2;
}
}
contract CalculatorTest {
// manipulate these values to affect the result
uint256 value1 = 3;
uint256 value2 = 4;
// set an operator --available options (+, -, *, /)
string operator = "*";
// contract owner
address owner;
// set the value of owner to the owner of the contract
constructor() {
owner = msg.sender;
}
// test execution of our calculator
function CalculateResultOfExpression()
public
view
returns (uint256, address)
{
uint256 result = Calculator.performArithmeticOperation(
value1,
value2,
operator
);
return (result, owner);
}
}