-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.ts
118 lines (113 loc) 路 2.55 KB
/
calculator.ts
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* Calculator class for basic arithmetic operations.
* @class Calculator
*/
export class Calculator {
/**
* The current value of the calculator.
* @property value
* @type number
* @public
* @default 0
* @example
* ```typescript
* const calc = new Calculator();
* console.log(calc.value); // 0
* ```
*/
public value: number;
/**
* Creates a new calculator.
* @param value The initial value of the calculator.
* @constructor
* @example
* ```typescript
* const calc = new Calculator();
* console.log(calc.value); // 0
* ```
* @example
* ```typescript
* const calc = new Calculator(1);
* console.log(calc.value); // 1
* ```
*/
constructor(value: number = 0) {
this.value = value;
}
/**
* Adds a number to the calculator. \
* {@link @libs/arithmetic!Addition~add} is used to add the number.
* @param x The number to add.
* @method add
* @public
* @example
* ```typescript
* const calc = new Calculator();
* calc.add(1);
* console.log(calc.value); // 1
* ```
*/
public add(x: number): void {
this.value += x;
}
/**
* Subtracts a number from the calculator. \
* {@link @libs/arithmetic!Subtraction~sub} is used to subtract the number.
* @param x The number to subtract.
* @method sub
* @public
* @example
* ```typescript
* const calc = new Calculator();
* calc.sub(1);
* console.log(calc.value); // -1
* ```
*/
public sub(x: number): void {
this.value -= x;
}
/**
* Multiplies the calculator by a number. \
* {@link @libs/arithmetic!Multiplication~mul} is used to multiply the calculator.
* @param x The number to multiply by.
* @method mul
* @public
* @example
* ```typescript
* const calc = new Calculator();
* calc.mul(2);
* console.log(calc.value); // 0
* ```
* @example
* ```typescript
* const calc = new Calculator(1);
* calc.mul(2);
* console.log(calc.value); // 2
* ```
*/
public mul(x: number): void {
this.value *= x;
}
/**
* Divides the calculator by a number. \
* {@link @libs/arithmetic!Division~div} is used to divide the calculator.
* @param x The number to divide by.
* @method div
* @public
* @example
* ```typescript
* const calc = new Calculator();
* calc.div(2);
* console.log(calc.value); // 0
* ```
* @example
* ```typescript
* const calc = new Calculator(1);
* calc.div(2);
* console.log(calc.value); // 0.5
* ```
*/
public div(x: number): void {
this.value /= x;
}
}