-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.js
187 lines (169 loc) · 4.52 KB
/
calculator.js
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import plugin from '../../lib/plugins/plugin.js'
import fetch from 'node-fetch'
export class calc extends plugin {
constructor() {
super({
/** 功能名称 */
name: '计算器',
/** 功能描述 */
dsc: '加减乘除计算器',
/** https://oicqjs.github.io/oicq/#events */
event: 'message',
/** 优先级,数字越小等级越高 */
priority: 1000,
rule: [{
/** 命令正则匹配 */
reg: '^[0-9\(\)\(\)\%]+(:?[\\+\\-\\*\\x\\/\\÷\(\)\(\)\.][0-9\(\)\(\)\%]+)+$',
/** 执行方法 */
fnc: 'calc'
}]
})
}
/**
*
* @param e oicq传递的事件参数e
*/
async calc(e) {
let emsg = this.e.msg.replace(/#|#|计算|=/g, "");
var regk = /[\(]/g,
regk2 = /[\)]/g;
emsg = emsg.replace(regk, "(").replace(regk2, ")").replace("%", "/100").replace("÷", "/").replace("x", "*").replace(/“|”/g, "\"");
let res;
try {
if (!emsg.match(/[a-zA-Z]/g) || this.e.isMaster) {
if (!emsg.match(/[\(\)\*\/]/g)) {
res = jisuan(emsg);
} else {
res = await eval(emsg);
}
} else {
this.e.reply("含有非法字符,无法计算");
return true;
}
} catch (errorall) {
this.e.reply(errorall.toString());
return true;
}
if (res == emsg) {
return true;
}
await this.e.reply(`计算结果:${JSON.stringify(res)}`);
return true; //返回true 阻挡消息不再往下
}
}
function jisuan(emsg) {
var preNum = "";
var nextNum = "";
var str = "";
for (let i = 0; i < emsg.length; i++) {
if (emsg[i] != "+" && emsg[i] != "-") {
nextNum = nextNum + "" + emsg[i];
} else if (preNum != "") {
if (str == "+") {
preNum = accAdd(preNum, nextNum);
} else {
preNum = accSub(preNum, nextNum);
}
str = emsg[i];
nextNum = "";
} else {
var preNum = nextNum;
var nextNum = "";
str = emsg[i];
}
console.log(preNum + " " + nextNum + " " + str);
}
if (preNum == "") {
console.log(preNum + " " + nextNum + " " + str);
preNum = nextNum;
} else {
if (str == "+") {
preNum = accAdd(preNum, nextNum);
} else {
preNum = accSub(preNum, nextNum);
}
}
return preNum;
}
/**
* 数字相加
* @param {*} arg1
* @param {*} arg2
* @returns
*/
function accAdd(arg1, arg2) {
return changeNum(arg1, arg2)
}
/**
* 数字相减
* @param {*} arg1
* @param {*} arg2
* @returns
*/
function accSub(arg1, arg2) {
return changeNum(arg1, arg2, false)
}
/**
* 数字相乘
* @param {*} arg1
* @param {*} arg2
* @returns
*/
function accMul(arg1, arg2) {
let m = 0;
m = accAdd(m, getDecimalLength(arg1))
m = accAdd(m, getDecimalLength(arg2))
return getNum(arg1) * getNum(arg2) / Math.pow(10, m)
}
/**
* 数字相除
* @param {*} arg1
* @param {*} arg2
* @returns
*/
function accDiv(arg1, arg2) {
let t1, t2;
t1 = getDecimalLength(arg1)
t2 = getDecimalLength(arg2)
if (t1 - t2 > 0) {
return (getNum(arg1) / getNum(arg2)) / Math.pow(10, t1 - t2)
} else {
return (getNum(arg1) / getNum(arg2)) * Math.pow(10, t2 - t1)
}
}
function changeNum(arg1 = '', arg2 = '', isAdd = true) {
function changeInteger(arg, r, maxR) {
if (r != maxR) {
let addZero = ''
for (let i = 0; i < maxR - r; i++) {
addZero += '0'
}
arg = Number(arg.toString().replace('.', '') + addZero)
} else {
arg = getNum(arg)
}
return arg
}
let r1, r2, maxR, m;
r1 = getDecimalLength(arg1)
r2 = getDecimalLength(arg2)
maxR = Math.max(r1, r2)
arg1 = changeInteger(arg1, r1, maxR)
arg2 = changeInteger(arg2, r2, maxR)
m = Math.pow(10, maxR)
if (isAdd) {
return (arg1 + arg2) / m
} else {
return (arg1 - arg2) / m
}
}
function getDecimalLength(arg = '') {
try {
return arg.toString().split(".")[1].length
} catch (e) {
return 0
}
}
function getNum(arg = '') {
return Number(arg.toString().replace(".", ""))
}