├── .gitignore ├── static ├── icon.ico ├── style │ ├── about.css │ └── index.css └── script │ ├── about.js │ └── index.js ├── screenshot ├── calc1.jpg └── calc2.jpg ├── src └── about.html ├── package.json ├── main.js ├── README.md ├── config └── menu.js └── index.html /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules/ 3 | 计算器-win32-x64/ 4 | dist/ 5 | -------------------------------------------------------------------------------- /static/icon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lin-xin/calculator/HEAD/static/icon.ico -------------------------------------------------------------------------------- /screenshot/calc1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lin-xin/calculator/HEAD/screenshot/calc1.jpg -------------------------------------------------------------------------------- /screenshot/calc2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lin-xin/calculator/HEAD/screenshot/calc2.jpg -------------------------------------------------------------------------------- /static/style/about.css: -------------------------------------------------------------------------------- 1 | html,body{ 2 | padding:0; 3 | margin: 0; 4 | height: 100%; 5 | } 6 | body{ 7 | font-family: '微软雅黑'; 8 | } 9 | .wrapper{ 10 | width: 100%; 11 | height: 100%; 12 | background: #E5F3F7; 13 | padding: 50px; 14 | box-sizing: border-box; 15 | } 16 | .wrapper p{ 17 | margin: 0; 18 | line-height: 30px; 19 | } 20 | .author, .code{ 21 | cursor: pointer; 22 | } -------------------------------------------------------------------------------- /src/about.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 关于计算器 5 | 6 | 7 | 8 | 9 | 10 |
11 |

名称:计算器

12 |

版本:

13 |

作者:林鑫(2981207131@qq.com)

14 |

源码:lin-xin/calculator

15 |
16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /static/script/about.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: linxin 3 | * @Date: 2017-08-29 11:02:40 4 | * @Last Modified time: 2017-08-29 11:02:40 5 | */ 6 | const { shell, remote } = require('electron'); 7 | 8 | const main = { 9 | init(){ 10 | main.getAppVersion(); 11 | main.eventHandle(); 12 | }, 13 | eventHandle(){ 14 | document.querySelector('.author').onclick = (e) => { 15 | shell.openExternal('http://blog.gdfengshuo.com'); 16 | }; 17 | document.querySelector('.code').onclick = (e) => { 18 | shell.openExternal('https://github.com/lin-xin/calculator'); 19 | } 20 | }, 21 | getAppVersion(){ 22 | document.querySelector('.version').innerHTML = remote.getGlobal('version'); 23 | } 24 | } 25 | main.init(); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "calculator", 3 | "version": "1.0.0", 4 | "description": "基于 Electron + javascript 实现的桌面计算器应用", 5 | "main": "main.js", 6 | "scripts": { 7 | "start": "electron .", 8 | "build:win": "electron-packager ./ 计算器 --platform=win32 --overwrite --icon=./icon.ico", 9 | "dist": "electron-builder --win" 10 | }, 11 | "build": { 12 | "appId": "com.linxin.calculator", 13 | "productName": "计算器", 14 | "electronVersion": "1.7.5", 15 | "win": { 16 | "icon": "./static/icon.ico" 17 | } 18 | }, 19 | "author": "linxin", 20 | "license": "ISC", 21 | "dependencies": { 22 | "mathjs": "^3.16.2" 23 | }, 24 | "devDependencies": { 25 | "electron": "^1.7.5", 26 | "electron-builder": "^19.26.0", 27 | "electron-packager": "^8.7.2" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /main.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: linxin 3 | * @Date: 2017-08-23 11:03:44 4 | * @Last Modified time: 2017-08-29 11:03:44 5 | */ 6 | const {app, BrowserWindow, Menu} = require('electron'); 7 | const path = require('path'); 8 | const url = require('url'); 9 | 10 | require('./config/menu.js'); 11 | 12 | let win; 13 | global['version'] = app.getVersion(); 14 | 15 | function createWindow() { 16 | win = new BrowserWindow({ 17 | width: 390, 18 | height: 672, 19 | fullscreen: false, 20 | resizable: false 21 | }); 22 | // win.webContents.openDevTools(); 23 | win.loadURL(url.format({ 24 | pathname: path.join(__dirname, 'index.html'), 25 | protocol: 'file', 26 | slashes: true 27 | })) 28 | 29 | win.on('closed', () => { 30 | win = null; 31 | }) 32 | } 33 | 34 | app.on('ready', createWindow); 35 | 36 | app.on('window-all-closed', () => { 37 | if(process.platform !== 'darwin'){ 38 | app.quit(); 39 | } 40 | }) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # calculator 2 | 基于 Electron + javascript 实现的桌面计算器应用。 3 | 4 | 相关文章:[Electron 实战桌面计算器应用](https://github.com/lin-xin/blog/issues/22) 5 | 6 | ## 介绍 7 | 我这里通过 Electron 实现了仿 iPhone 的计算器,通过菜单可以切换横屏和竖屏,横屏有更多的运算。 8 | 9 | 而对于 JavaScript 进行浮点数计算来说,精度丢失是个很大问题,所以我这里使用了第三方库 math.js 来解决这个精度的问题。 10 | 11 | 尽可能的实现了跟 iPhone 一样的运算: 12 | 13 | - 1 + 2 × 3 = 7 14 | - 3 += 6 (再按 = 等于 9) 15 | - 0.1 + 0.2 = 0.3 (浮点数精度处理) 16 | 17 | ## 效果图 18 | ![Image text](https://github.com/lin-xin/calculator/raw/master/screenshot/calc1.jpg) 19 | ![Image text](https://github.com/lin-xin/calculator/raw/master/screenshot/calc2.jpg) 20 | 21 | ## 环境 22 | 23 | - windows 7 24 | - Electron v1.7.5 25 | 26 | ## 运行 27 | ``` 28 | git clone https://github.com/lin-xin/calculator.git 29 | npm install 30 | npm start 31 | ``` 32 | 就会运行起来了。 33 | 34 | ## 构建 35 | ``` 36 | npm run build:win 37 | ``` 38 | 则会在项目中生成个 /计算器-win32-x64 文件夹,打开里面的 计算器.exe 即可打开计算器。 39 | 40 | 或者 41 | ``` 42 | npm run dist 43 | ``` 44 | 则会生成 dist/ 文件夹,里面有应用的安装包,就可以双击安装了。安装过程中可能会有360卫士等提示危险,不用管继续安装就可以。 -------------------------------------------------------------------------------- /static/style/index.css: -------------------------------------------------------------------------------- 1 | * { 2 | margin: 0; 3 | padding: 0; 4 | } 5 | 6 | body { 7 | font-family: 'Century Gothic', 'Arial Narrow', 'Helvetica' 8 | } 9 | sup{ 10 | font-size: 16px; 11 | line-height: 14px; 12 | } 13 | ::-webkit-scrollbar { 14 | width: 0; 15 | } 16 | .result-text { 17 | display: flex; 18 | align-items: flex-end; 19 | justify-content: flex-end; 20 | height: 144px; 21 | background: #202020; 22 | color: #fff; 23 | font-size: 75px; 24 | text-align: right; 25 | } 26 | 27 | .result-text.small { 28 | font-size: 67px; 29 | } 30 | 31 | .keyboard-special{ 32 | display: none; 33 | } 34 | .keyboard-special .keyboard-item{ 35 | font-family: 'Helvetica'; 36 | background: #c6c6c6; 37 | font-size: 25px; 38 | } 39 | .keyboard-item { 40 | float: left; 41 | width: 25vw; 42 | height: 25vw; 43 | text-align: center; 44 | line-height: 25vw; 45 | background: #CDCED1; 46 | border-left: 1px solid #6A6B6C; 47 | border-bottom: 1px solid #6A6B6C; 48 | color: #0B0B0B; 49 | font-size: 30px; 50 | box-sizing: border-box; 51 | cursor: pointer; 52 | user-select: none; 53 | -webkit-user-select: none; 54 | } 55 | .keyboard-item.active{ 56 | border: 2px solid #0B0B0B; 57 | } 58 | 59 | .keyboard-item::active { 60 | background: #bbb; 61 | } 62 | 63 | .keyboard-item.orange { 64 | color: #fff; 65 | background: #F98E12; 66 | } 67 | 68 | .keyboard-item.orange:active { 69 | background: #e88511; 70 | } 71 | 72 | .keyboard-item.large { 73 | width: 50vw; 74 | } 75 | .horizontal .result-text{ 76 | height: 80px; 77 | font-size: 50px; 78 | } 79 | .horizontal .keyboard-box{ 80 | display: flex; 81 | } 82 | .horizontal .keyboard-box .keyboard-special, 83 | .horizontal .keyboard-box .keyboard-normal{ 84 | display: block; 85 | flex: 1; 86 | } 87 | .horizontal .keyboard-item{ 88 | width: 12.5vw; 89 | height: 10vw; 90 | line-height: 10vw; 91 | } 92 | .horizontal .keyboard-item.large{ 93 | width: 25vw; 94 | } 95 | 96 | .radic-span{ 97 | font-size: 25px; 98 | line-height: 40px; 99 | } 100 | .radic-number{ 101 | position: relative; 102 | font-size: 23px; 103 | padding: 0 3px; 104 | border-top: 2px solid #202020; 105 | } 106 | .radic-number-2::before, 107 | .radic-number-3::before, 108 | .radic-number-y::before{ 109 | position: absolute; 110 | left: -11px; 111 | top: -31px; 112 | content: '2'; 113 | font-size: 12px; 114 | } 115 | .radic-number-3::before{ 116 | content: '3'; 117 | } 118 | .radic-number-y::before{ 119 | content: 'y'; 120 | top: -33px; 121 | } 122 | .log-span{ 123 | position: relative; 124 | padding-right: 10px; 125 | } 126 | .log-span:after{ 127 | position: absolute; 128 | right: -3px; 129 | top: -5px; 130 | content: '10'; 131 | font-size: 12px; 132 | } -------------------------------------------------------------------------------- /config/menu.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: linxin 3 | * @Date: 2017-08-27 11:02:58 4 | * @Last Modified time: 2017-08-29 11:02:58 5 | */ 6 | const { Menu, dialog, BrowserWindow, shell} = require('electron'); 7 | const path = require('path'); 8 | const url = require('url'); 9 | const template = [ 10 | { 11 | label: '查看', 12 | submenu: [ 13 | { 14 | label: '竖屏', 15 | type: 'radio', 16 | checked: true, 17 | click: () => { 18 | const win = BrowserWindow.fromId(1); 19 | win.setSize(390,672); 20 | win.webContents.send('change_event','vertical'); 21 | } 22 | }, 23 | { 24 | label: '横屏', 25 | type: 'radio', 26 | checked: false, 27 | click: () => { 28 | const win = BrowserWindow.fromId(1); 29 | win.setSize(670,460); 30 | win.webContents.send('change_event','horizontal'); 31 | } 32 | }, 33 | {type: 'separator'}, 34 | {label: '重载',role:'reload'}, 35 | {label: '退出',role:'quit'}, 36 | ] 37 | }, 38 | { 39 | label: '帮助', 40 | submenu: [ 41 | { 42 | label: '问题反馈', 43 | click: () => { 44 | shell.openExternal('https://github.com/lin-xin/calculator/issues'); 45 | } 46 | }, 47 | { 48 | label: '项目地址', 49 | click: () => { 50 | shell.openExternal('https://github.com/lin-xin/calculator'); 51 | } 52 | }, 53 | {type: 'separator'}, 54 | { 55 | label: '关于作者', 56 | click: () => { 57 | shell.openExternal('http://blog.gdfengshuo.com/about/'); 58 | } 59 | }, 60 | { 61 | label: '关于计算器', 62 | click: () => { 63 | const win = BrowserWindow.fromId(1); 64 | let about = new BrowserWindow({ 65 | parent: win, 66 | modal: true, 67 | width: 500, 68 | height: 300, 69 | minimizable: false, 70 | maximizable: false, 71 | resizable: false, 72 | title: '关于计算器' 73 | }) 74 | 75 | about.loadURL(url.format({ 76 | pathname: path.join(__dirname,'../src/about.html'), 77 | protocol: 'file', 78 | slashes: true 79 | })); 80 | // about.webContents.openDevTools(); 81 | about.setMenu(null); 82 | about.once('ready-to-show', () => { 83 | about.show(); 84 | }) 85 | } 86 | } 87 | ] 88 | } 89 | ] 90 | const menu = Menu.buildFromTemplate(template); 91 | Menu.setApplicationMenu(menu); -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 计算器 6 | 7 | 8 | 9 |
10 |
0
11 |
12 |
13 |
x2
14 |
x3
15 |
xy
16 |
10x
17 |
1/x
18 |
19 | x 20 |
21 |
22 | x 23 |
24 |
25 | x 26 |
27 |
sin
28 |
cos
29 |
tan
30 |
e
31 |
sinh
32 |
cosh
33 |
tanh
34 |
π
35 |
x!
36 |
ln
37 |
38 | log 39 |
40 |
exp
41 |
42 |
43 |
AC
44 |
+/-
45 |
%
46 |
÷
47 |
7
48 |
8
49 |
9
50 |
×
51 |
4
52 |
5
53 |
6
54 |
-
55 |
1
56 |
2
57 |
3
58 |
+
59 |
0
60 |
.
61 |
=
62 |
63 |
64 |
65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /static/script/index.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: linxin 3 | * @Date: 2017-08-23 11:04:01 4 | * @Last Modified time: 2017-08-29 11:04:01 5 | */ 6 | const { ipcRenderer } = require('electron'); 7 | const math = require('mathjs'); 8 | 9 | math.config({ 10 | number: "BigNumber", 11 | precision: 32 12 | }); 13 | 14 | let result = '0'; 15 | let max_length = 9; 16 | 17 | let main = { 18 | isEqual: false, // 上一步是否等号键 19 | isExpress: false, // 上一步是否运算符 20 | flag: true, // 标识是否重新输入数字 21 | history: {}, // 用于记录输入的数字和运算符 22 | register: {}, // 用于记录四则运算优先计算的寄存器 23 | events: null, 24 | // 点击数字键 25 | clickNumber(num) { 26 | const _this = this, 27 | res = document.querySelector('.result-text'), 28 | isPoint = num === '.'; 29 | 30 | if (!_this.flag) { 31 | // 数字转为字符串 32 | result = result.toString(); 33 | 34 | // 如果输入小数点并且已经结果中已经有小数点了 35 | if (result.indexOf('.') !== -1 && isPoint) { 36 | return; 37 | } 38 | 39 | // 限制长度 40 | if (result.length >= max_length) { 41 | return; 42 | } 43 | 44 | result = result + num; 45 | } else { 46 | _this.resize(); 47 | result = isPoint ? '0' + num : num; 48 | if (_this.isEqual) { 49 | _this.history = {}; 50 | _this.register = {}; 51 | _this.isEqual = false; 52 | } 53 | _this.flag = false; 54 | } 55 | _this.removeActive(); 56 | res.innerHTML = result; 57 | }, 58 | // 点击运算符 59 | clickOperat(ope, event) { 60 | const _this = this, 61 | res = document.querySelector('.result-text'); 62 | switch (ope) { 63 | case '+/-': 64 | res.innerHTML = result = math.eval(result + '*-1'); 65 | _this.resize(); 66 | _this.isEqual ? _this.flag = true : ''; 67 | break; 68 | case '%': 69 | res.innerHTML = result = math.format(math.eval(res.innerHTML + '/100'),16); 70 | _this.flag = true; 71 | _this.resize(); 72 | break; 73 | default: 74 | _this.flag = true; 75 | if (!event.classList.contains('active')) { 76 | _this.removeActive(); 77 | event.classList.add('active'); 78 | 79 | if (_this.isEqual) { 80 | _this.register = {}; 81 | // 如果点了等号后又点运算符,则把当前的结果再进行运算 82 | _this.history.operator = ope; 83 | _this.history.before = _this.checkIsMinus(result); 84 | _this.isEqual = false; 85 | } else { 86 | if (_this.register.number) { 87 | res.innerHTML = result = math.eval(_this.register.number + _this.register.ope + result); 88 | _this.register = {}; 89 | } 90 | // 四则运算,乘除优先 91 | if ((ope == '*' || ope == '/') && (_this.history.operator == '+' || _this.history.operator == '-')) { 92 | // 如果上一步是加减法,这时输入乘除,则优先乘除 93 | // 把当前数字和运算符存到计算乘除的寄存器 register 中 94 | _this.register.number = _this.checkIsMinus(result); 95 | _this.register.ope = ope; 96 | } else { 97 | _this.register = {}; 98 | // 顺序运算 99 | if (!!_this.history.before) { 100 | res.innerHTML = result = math.eval(_this.history.before + _this.history.operator + result); 101 | } 102 | _this.history.before = _this.checkIsMinus(result); 103 | _this.history.operator = ope; 104 | } 105 | _this.flag = true; 106 | } 107 | } 108 | break; 109 | } 110 | }, 111 | // 获取结果 112 | clickEqual() { 113 | const _this = this, 114 | res = document.querySelector('.result-text'); 115 | 116 | _this.flag = true; 117 | _this.removeActive(); 118 | 119 | if (_this.isEqual) { 120 | _this.history.before = _this.checkIsMinus(result); 121 | } else { 122 | if (_this.register.number) { 123 | 124 | if (_this.register.ope === '*' || _this.register.ope === '/') { 125 | _this.register.after = result; 126 | _this.history.after = _this.checkIsMinus(math.eval(_this.register.number + _this.register.ope + result)); 127 | } 128 | } else { 129 | _this.history.after = _this.checkIsMinus(result); 130 | } 131 | 132 | } 133 | console.log(_this.register.ope); 134 | // 如果不是加减乘除运算 135 | if (_this.register.ope && _this.register.ope !== '*' && _this.register.ope !== '/') { 136 | 137 | _this.clickSpecial(_this.register.ope, _this.events); 138 | } else { 139 | if (_this.history.before && _this.history.operator && _this.history.after) { 140 | try { 141 | 142 | result = _this.resultHandle(math.eval(_this.history.before + _this.history.operator + _this.history.after).toString()); 143 | // result = math.format(math.eval(_this.history.before + _this.history.operator + _this.history.after), 7); 144 | 145 | if (_this.register.number) { 146 | _this.history.operator = _this.register.ope; 147 | _this.history.after = _this.register.after; 148 | _this.register = {}; 149 | } 150 | } catch (error) { 151 | result = 'error'; 152 | } 153 | 154 | res.innerHTML = result; 155 | } 156 | } 157 | _this.isEqual = true; 158 | }, 159 | // 重置寄存器 160 | reset() { 161 | const _this = this, 162 | res = document.querySelector('.result-text'); 163 | _this.flag = true; 164 | _this.history = {}; 165 | _this.register = {}; 166 | _this.isEqual = false; 167 | res.innerHTML = result = '0'; 168 | }, 169 | // 自适应结果长度 170 | resize() { 171 | const _this = this, 172 | res = document.querySelector('.result-text'); 173 | const num = (!!result.toString) ? result.toString() : result; 174 | if (num.length > max_length) { 175 | res.classList.add('small'); 176 | } else { 177 | res.classList.remove('small'); 178 | } 179 | }, 180 | // 运算结果处理 181 | resultHandle(num) { 182 | if (typeof num == "number") { 183 | num = num.toString(); 184 | } 185 | const idx = num.indexOf('.'); 186 | 187 | if (num.length > max_length) { 188 | if (idx !== -1) { 189 | return new Number(num).toPrecision(max_length - idx - 1).replace('+', '').substring(0,max_length); 190 | } else { 191 | return new Number(num).toPrecision(max_length - 4).replace('+', '').substring(0,max_length); 192 | } 193 | } else { 194 | return num; 195 | } 196 | }, 197 | // 横屏时特殊运算 198 | clickSpecial(type, event) { 199 | const _this = this, 200 | res = document.querySelector('.result-text'); 201 | _this.flag = true; 202 | _this.removeActive(); 203 | switch (type) { 204 | case '1': // x 的平方 205 | res.innerHTML = result = math.format(math.pow(result, 2), { 206 | precision: 16 207 | }); 208 | _this.register.ope = type; 209 | _this.isEqual = true; 210 | 211 | break; 212 | case '2': // x 的立方 213 | res.innerHTML = result = math.format(math.pow(result, 3), { 214 | precision: 16 215 | }); 216 | _this.register.ope = type; 217 | _this.isEqual = true; 218 | break; 219 | case '3': // x 的 y 次幂 220 | _this.events = event; 221 | if (!event.classList.contains('active')) { 222 | if (_this.register.ope === '3') { 223 | if (_this.isEqual) { 224 | _this.register.number = result; 225 | } else { 226 | _this.register.after = result; 227 | _this.isEqual = true; 228 | } 229 | 230 | res.innerHTML = result = math.format(math.pow(_this.register.number, _this.register.after), { 231 | precision: 16 232 | }); 233 | 234 | } else { 235 | _this.removeActive(); 236 | event.classList.add('active'); 237 | _this.register.number = result; 238 | _this.register.ope = type; 239 | } 240 | } 241 | break; 242 | case '4': // 10 的 x 次幂 243 | res.innerHTML = result = math.format(math.pow(10, result), { 244 | precision: 16 245 | }); 246 | _this.register.ope = type; 247 | _this.isEqual = true; 248 | break; 249 | case '5': // 1/x 250 | res.innerHTML = result = math.format(math.divide(1, result), { 251 | precision: 16 252 | }); 253 | _this.register.ope = type; 254 | _this.isEqual = true; 255 | break; 256 | case '6': // x 开平方根 257 | res.innerHTML = result = math.format(math.sqrt(result), { 258 | precision: 16 259 | }); 260 | _this.register.ope = type; 261 | _this.isEqual = true; 262 | break; 263 | case '7': // x 开立方根 264 | res.innerHTML = result = math.format(math.cbrt(result), { 265 | precision: 16 266 | }); 267 | _this.register.ope = type; 268 | _this.isEqual = true; 269 | break; 270 | case '8': // x 的开 y 次方根 271 | _this.events = event; 272 | if (!event.classList.contains('active')) { 273 | if (_this.register.ope === '8') { 274 | if (_this.isEqual) { 275 | _this.register.number = result; 276 | } else { 277 | _this.register.after = result; 278 | _this.isEqual = true; 279 | } 280 | 281 | res.innerHTML = result = math.format(math.nthRoot(_this.register.number, _this.register.after), { 282 | precision: 16 283 | }); 284 | 285 | } else { 286 | _this.removeActive(); 287 | event.classList.add('active'); 288 | _this.register.number = result; 289 | _this.register.ope = type; 290 | } 291 | } 292 | break; 293 | case '9': // sin 正弦 294 | res.innerHTML = result = math.format(math.sin(result), { 295 | precision: 16 296 | }); 297 | _this.register.ope = type; 298 | _this.isEqual = true; 299 | break; 300 | case '10': // cos 余弦 301 | res.innerHTML = result = math.format(math.cos(result), { 302 | precision: 16 303 | }); 304 | _this.register.ope = type; 305 | _this.isEqual = true; 306 | break; 307 | case '11': // tan 正切 308 | res.innerHTML = result = math.format(math.tan(result), { 309 | precision: 16 310 | }); 311 | _this.register.ope = type; 312 | _this.isEqual = true; 313 | break; 314 | case '12': // 算术常量 e 315 | res.innerHTML = result = Math.E; 316 | _this.register.ope = type; 317 | _this.isEqual = true; 318 | break; 319 | case '13': // sinh 双曲正弦 320 | res.innerHTML = result = math.format(math.sinh(result), { 321 | precision: 16 322 | }); 323 | _this.register.ope = type; 324 | _this.isEqual = true; 325 | break; 326 | case '14': // cosh 双曲余弦 327 | res.innerHTML = result = math.format(math.cosh(result), { 328 | precision: 16 329 | }); 330 | _this.register.ope = type; 331 | _this.isEqual = true; 332 | break; 333 | case '15': // tanh 双曲正切 334 | res.innerHTML = result = math.format(math.tanh(result), { 335 | precision: 16 336 | }); 337 | _this.register.ope = type; 338 | _this.isEqual = true; 339 | break; 340 | case '16': // 圆周率 341 | res.innerHTML = result = Math.PI; 342 | _this.register.ope = type; 343 | _this.isEqual = true; 344 | break; 345 | case '17': // x 的阶乘 346 | if (result.indexOf('.') > -1 || result.indexOf('-') > -1) { 347 | _this.reset(); 348 | res.innerHTML = 'error'; 349 | } else { 350 | res.innerHTML = result = math.factorial(result); 351 | _this.register.ope = type; 352 | _this.isEqual = true; 353 | } 354 | break; 355 | case '18': // 底数为 e 的对数 356 | res.innerHTML = result = math.format(math.log(result), { 357 | precision: 16 358 | }); 359 | _this.register.ope = type; 360 | _this.isEqual = true; 361 | break; 362 | case '19': // 底数为 10 的对数 363 | res.innerHTML = result = math.format(math.log10(result), { 364 | precision: 16 365 | }); 366 | _this.register.ope = type; 367 | _this.isEqual = true; 368 | break; 369 | case '20': // e 的指数 370 | res.innerHTML = result = math.format(math.exp(result), { 371 | precision: 16 372 | }); 373 | _this.register.ope = type; 374 | _this.isEqual = true; 375 | break; 376 | default: 377 | break; 378 | } 379 | }, 380 | 381 | /** 382 | * 检测数字为负数则加上括号后返回去计算 383 | * @param {*} num 384 | */ 385 | checkIsMinus(num) { 386 | return num.toString().indexOf('-') > -1 ? '(' + num + ')' : num; 387 | }, 388 | /** 389 | * 移除 active 类 390 | */ 391 | removeActive() { 392 | const _act = document.querySelector('.active'); 393 | _act && _act.classList.remove('active'); 394 | } 395 | } 396 | 397 | // 监听菜单选择横屏或竖屏 398 | ipcRenderer.on('change_event', (event, arg) => { 399 | const box = document.querySelector('.wrapper'); 400 | if (arg === 'horizontal') { 401 | box.classList.add('horizontal'); 402 | max_length = 16; 403 | } else { 404 | box.classList.remove('horizontal'); 405 | max_length = 9; 406 | } 407 | main.reset(); 408 | }) --------------------------------------------------------------------------------