2023-11-06 10:31:51 +08:00
|
|
|
import AbstractCalculator from './calculator';
|
|
|
|
|
|
|
|
export default class NumCalculator extends AbstractCalculator {
|
|
|
|
result: number = 0;
|
|
|
|
|
|
|
|
constructor(num: number | string | AbstractCalculator) {
|
|
|
|
super();
|
|
|
|
if (num instanceof NumCalculator) {
|
|
|
|
this.result = num.result;
|
|
|
|
} else if (typeof num === 'number') {
|
|
|
|
this.result = num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-10 14:54:16 +08:00
|
|
|
add(num: number | string | AbstractCalculator): this {
|
2023-11-06 10:31:51 +08:00
|
|
|
if (num instanceof NumCalculator) {
|
|
|
|
this.result += num.result;
|
|
|
|
} else if (typeof num === 'number') {
|
|
|
|
this.result += num;
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2023-11-10 14:54:16 +08:00
|
|
|
sub(num: number | string | AbstractCalculator): this {
|
2023-11-06 10:31:51 +08:00
|
|
|
if (num instanceof NumCalculator) {
|
|
|
|
this.result -= num.result;
|
|
|
|
} else if (typeof num === 'number') {
|
|
|
|
this.result -= num;
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2023-11-10 14:54:16 +08:00
|
|
|
mul(num: number | string | AbstractCalculator): this {
|
2023-11-06 10:31:51 +08:00
|
|
|
if (num instanceof NumCalculator) {
|
|
|
|
this.result *= num.result;
|
|
|
|
} else if (typeof num === 'number') {
|
|
|
|
this.result *= num;
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2023-11-10 14:54:16 +08:00
|
|
|
div(num: number | string | AbstractCalculator): this {
|
2023-11-06 10:31:51 +08:00
|
|
|
if (num instanceof NumCalculator) {
|
|
|
|
this.result /= num.result;
|
|
|
|
} else if (typeof num === 'number') {
|
|
|
|
this.result /= num;
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
equal(): number {
|
|
|
|
return this.result;
|
|
|
|
}
|
|
|
|
}
|