运行效果:

可实现科学计算器的功能,如:PI,sin,cos,tan等

源代码:

  1 <!DOCTYPE html>
2 <html lang="zh">
3
4 <head>
5 <meta charset="UTF-8">
6 <title>计算器练习</title>
7 <style type="text/css">
8 table {
9 margin: 15px auto;
10 font-size: 22px;
11 border: 5px outset orange;
12
13 }
14
15 #tab-1,
16 #tab-2,
17 #tab-3 {
18 border: 3px outset rgba(15, 10, 10, 0.3);
19 }
20
21 input {
22 outline: none;
23 box-shadow: 5px 5px 2px rgba(100, 100, 100, 0.8) inset;
24 }
25
26 #txtnum {
27 text-align: right;
28 height: 50px;
29 width: 100%;
30 background: #fff;
31 font-size: 22px;
32 }
33
34 td {
35 padding: 5px;
36 background: #ccc;
37
38 }
39
40 [type=button] {
41 width: 60px;
42 height: 40px;
43 border-radius: 5px;
44 background: #fff;
45 box-shadow: 5px 3px 2px rgba(100, 100, 100, 0.6) inset;
46 }
47 </style>
48 </head>
49
50 <body>
51 <!-- 主表设计 -->
52 <table id="main" cellspacing="0">
53 <!-- (tr>td*3)*2 快捷方式-->
54 <tr>
55 <td colspan="2">
56 <input type="text" id="txtnum" value="0">
57 </td>
58 <td>
59 <table id="tab-1">
60 <tr>
61 <td><input type="button" id="cc" value="清除" onclick="txtnum.value='0';result=0 "></td>
62 <td><input type="button" id="tg" value="退格" onclick="backspace()"></td>
63 </tr>
64 </table>
65 </td>
66 </tr>
67 <tr>
68 <td>
69 <table id="tab-2">
70 <!-- (tr>(td>input)*3)*4 -->
71 <tr>
72 <td><input type="button" id="sin" value="sin" onclick="math('sin')"></td>
73 <td><input type="button" id="cos" value="cos" onclick="math('cos')"></td>
74 <td><input type="button" id="tan" value="tan" onclick="math('tan')"></td>
75 </tr>
76 <tr>
77 <td><input type="button" id="asin" value="asin" onclick="math('asin')"></td>
78 <td><input type="button" id="acon" value="acon" onclick="math('acon')"></td>
79 <td><input type="button" id="atan" value="atan" onclick="math('atan')"></td>
80 </tr>
81 <tr>
82 <td><input type="button" id="PI" value="PI" onclick="math('PI')"></td>
83 <td><input type="button" value="1/x" onclick="math('1/x')"></td>
84 <td><input type="button" value="e" onclick="math('e')"></td>
85 </tr>
86 <tr>
87 <td><input type="button" value="lnx" onclick="math('lnx')"></td>
88 <td><input type="button" value="lgx" onclick="math('lgx')"></td>
89 <td><input type="button" value="sqrt" onclick="math('sqrt')"></td>
90 </tr>
91 </table>
92 </td>
93 <td>
94 <table id="tab-3">
95 <!-- (tr>(td>input)*3)*4 -->
96 <tr>
97 <td><input type="button" id="" value="7" onclick="num('7')"></td>
98 <td><input type="button" id="" value="8" onclick="num('8')"></td>
99 <td><input type="button" id="" value="9" onclick="num('9')"></td>
100 </tr>
101 <tr>
102 <td><input type="button" id="" value="4" onclick="num('4')"></td>
103 <td><input type="button" id="" value="5" onclick="num('5')"></td>
104 <td><input type="button" id="" value="6" onclick="num('6')"></td>
105 </tr>
106 <tr>
107 <td><input type="button" id="" value="1" onclick="num('1')"></td>
108 <td><input type="button" value="2" onclick="num('2')"></td>
109 <td><input type="button" value="3" onclick="num('3')"></td>
110 </tr>
111 <tr>
112 <td><input type="button" value="0" onclick="num('0')"></td>
113 <td><input type="button" value="." onclick="decimal()"></td>
114 <td><input type="button" value="=" onclick="compute('=')"></td>
115 </tr>
116 </table>
117 </td>
118 <td>
119 <table id="tab-3">
120 <tr>
121 <td><input type="button" id="" value="+" onclick="compute('+')"></td>
122 <td><input type="button" id="" value="取整" onclick="math('i')"></td>
123 </tr>
124 <tr>
125 <td><input type="button" id="" value="-" onclick="compute('-')"></td>
126 <td><input type="button" id="" value="取余" onclick="compute('%')"></td>
127 </tr>
128 <tr>
129 <td><input type="button" id="" value="*" onclick="compute('*')"></td>
130 <td><input type="button" id="" value="x^y" onclick="compute('x^y')"></td>
131 </tr>
132 <tr>
133 <td><input type="button" id="" value="/" onclick="compute('/')"></td>
134 <td><input type="button" id="" value="+/-" onclick="reverse()"></td>
135 </tr>
136 </table>
137 </td>
138 </tr>
139 </table>
140 <script type="text/javascript">
141 //operator 运算符
142 var Boo = false; //判断是否按下计算符号的布尔变量;
143 var result = 0; //存储计算数据的变量
144 var ope; //存储计算符号的变量
145
146 function $(x) {
147 return document.getElementById(x);
148 }
149
150 function decimal() {
151 var txt = $('txtnum');
152 if (Boo) {
153 txt.value = '0.'; //若接受过运算符,文本框清零
154 } else {
155 if (txt.value.indexOf('.') == -1) { //判断数值中是否已经有小数点
156 txt.value += '.'; //若没有则加上
157 }
158 }
159 Boo = false; //若接受过运算符,文本框不能清零
160 }
161 //indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。
162 //如果要检索的字符串值没有出现,则该方法返回 -1。
163
164 function num(Num) {
165 var txt = $('txtnum');
166 if (Boo) {
167 txt.value = Num;
168 Boo = false;
169 } else {
170 if (txt.value == '0') {
171 txt.value = Num;
172 } else {
173 txt.value += Num;
174 }
175 }
176 }
177
178 function compute(op) {
179 var onum = $('txtnum').value;
180 if (onum == '') { onum = 0; }
181 Boo = true;
182 switch (ope) {
183 case '+':
184 result += parseFloat(onum); break;
185 case '-':
186 result -= parseFloat(onum); break;
187 case '*':
188 result *= parseFloat(onum); break;
189 case '/':
190 result /= parseFloat(onum); break;
191 case '=':
192 result = parseFloat(onum); break;
193 case '%':
194 result %= onum; break;
195 //{result%=onum;break;}break;
196 case 'x^y':
197 result = Math.pow(result, onum); break;
198 //{result=Math.pow(result,onum);break;}break;
199 default: result = parseFloat(onum); break;
200 }
201 $('txtnum').value = result;
202 ope = op;
203
204 }
205
206 function math(op) {
207 var onum = $('txtnum').value;
208 if (onum == '') { alert('数据不能为空'); };
209 Boo = true;
210 with (Math) {
211 switch (op) {
212 case 'sin': result = sin(onum); break;
213 case 'cos': result = cos(onum); break;
214 case 'tan': result = tan(onum); break;
215 case 'asin': result = asin(onum); break;
216 case 'acos': result = acos(onum); break;
217 case 'atan': result = atan(onum); break;
218 case 'PI': result = PI; break;
219 case '1/x': result = 1 / onum; break;
220 case 'e': result = E; break;
221 case 'lnx': result = log(onum); break;
222 case 'lgx': result = log(onum) / log(10); break;
223
224 case 'i': result = floor(onum); break;
225
226 case 'sqrt': result = jc(onum); break;
227 default: result = parseFloat(onum); break;
228 }
229 }
230 $('txtnum').value = result;
231 }
232
233 function jc(a) {
234 return Math.sqrt(a);
235 }
236
237 function reverse() {
238 var Num1 = $('txtnum').value;
239 if (Num1 == '') {
240 alert('数据不能为空');
241 } else {
242 $('txtnum').value *= -1;
243 }
244
245 }
246
247 function backspace() {
248 var txt = $('txtnum');
249 txt.value = txt.value.substring(0, txt.value.length - 1);
250 if (txt.value == '') { txt.value = 0; }
251 }
252 </script>
253 </body>
254
255 </html>

JavaScript实现科学计算器的更多相关文章

  1. 科学计算器的Java实现

    简易的科学计算器的实现 ---Java版 import javax.swing.*;//新的窗口组件包 import java.awt.*; import java.awt.event.*; publ ...

  2. 想找个计算器当本命?来试试UWP应用《纸书科学计算器》

    久违了.上次在博客园发文还是4年前,正是高中参加NOIP的时候.这4年里发生了很多事,乃至再次看到过去的文章时,仿佛看到了自己也不熟悉的风景.最近很想把我的博客重新拾起来,慢慢灌溉,写一些微不足道的技 ...

  3. 纯javascript代码编写计算器程序

    今天来分享一下用纯javascript代码编写的一个计算器程序,很多行业都能用到这个程序,例如做装修预算.贷款利率等等. 首先来看一下完成后的效果: 具体代码如下:(关注我的博客,及时获取最新WEB前 ...

  4. 整理一些《纸书科学计算器》的小Tips

    本文最开始是在2016年的文章 Win10应用<纸书科学计算器>更新啦! 发表之后撰写的,当时那篇文章收到了不少人点赞,应用在国内市场的日下载量也突然上涨,让我感到受宠若惊,这里要感谢Wp ...

  5. Win10应用《纸书科学计算器》更新啦!

    <纸书科学计算器>在2016年8月拿了计算机设计大赛国家一等奖,现在仍记得我在答辩时还给评委们普及了一波UWP平台的知识.受此鼓励,这款应用也不会停下更新的脚步^_^.最近从1.9小幅升级 ...

  6. html+css+js实现科学计算器

    代码地址如下:http://www.demodashi.com/demo/13751.html 项目描述 纯html+css+js实现一个科学计算器,支持平方开方指数对数等基本函数,支持键盘输入,有简 ...

  7. 调用JavaScript实现字符串计算器

    调用JavaScript实现字符串计算器 如果表达式是字符串的形式,那么一般我们求值都会遇到很大的问题.   这里有一种直接调用JavaScript的方法来返回数值,无疑神器. 代码如下: @Fros ...

  8. 《学习R》笔记:科学计算器、检查变量和工作区、向量、矩阵和数组、列表和数据框

    一.第二章 科学计算器 要检查两个数字是否一样,要使用 all.equal() ,不要使用 == ,== 符号仅用于比较两个整型数是否存在相同 . > all.equal(sqrt(2)^2,2 ...

  9. 数据结构之栈—强大的四则复杂运算计算器(超过windows自带的科学计算器)【中缀转后缀表达式】

    比windows自带计算器还强的四则复杂运算计算器! 实测随机打出两组复杂算式:-7.5 * 6 / ( -2 + ( -6.5 -  -5.22 ) )与7.5+-3*8/(7+2) windows ...

随机推荐

  1. 矩池云上使用nohup和&让任务后台运行

    1.nohup 用途:不挂断地运行命令. 语法:nohup Command [ Arg - ] [ & ] 无论是否将 nohup 命令的输出重定向到终端,输出都将附加到当前目录的 nohup ...

  2. 编写Python代码的注意事项

    1.标识符 标识符是定义的名称,包括类名.变量名等等 标识符的大小写是敏感的,且第一个字符必须是字母表中的字母或"_" 在python3中,中文可被用作变量名 不能使用Python ...

  3. MySql 和SQLServer 申明变量以及赋值

    sql server中变量要先申明后赋值: 局部变量用一个@标识,全局变量用两个@(常用的全局变量一般都是已经定义好的): 申明局部变量语法:declare @变量名 数据类型:例如:declare ...

  4. Hive框架

    1.什么是Hive? Hive 是基于Hadoop之上的数据仓库.2.什么是数据仓库? 数据仓库其实就是一个数据库 数据仓库是一个面向主题的,集成的,不可更新的,随时间不变化的数据集合,它用于支持 企 ...

  5. 电子检索实体书「GitHub 热点速览 v.22.12」

    不知道有没有小伙伴遇到实体书快速定位指定内容的问题,凭借着记忆里很难快速翻阅到正确的页数,但 paperless-ngx 也许能帮上你的忙,它除了能将你的实体书籍电子化变成文件库里的一员之外,还能帮你 ...

  6. Mybatis入门实例解析

    写在前面:本文全程根据Mybatis官网进行入门讲解.毫无疑问,官方文档是学习这门技术最权威的资料,与此同时我们也知道官方文档对待入门小白基本上不太友好,没有入门demo.开篇就是小白们不懂的内容.有 ...

  7. SpringBoot 开局篇

    SpringBoot SpringBoot 是什么  SpringBoot 是为了简化配置文件开发的一种解决方案 SpringBoot 遵从 规范大于配置 SpringBoot 内置传统 Spring ...

  8. 请说说你对Struts2的拦截器的理解?

    Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现. 拦截器栈(Interceptor Stac ...

  9. redis 淘汰策略有哪些?

    noeviction: 不删除策略, 达到最大内存限制时, 如果需要更多内存, 直接返回错误信息. 大多数写命令都会导致占用更多的内存(有极少数会例外, 如 DEL ). allkeys-lru: 所 ...

  10. CSS 网站布局

    Flex:https://www.html.cn/archives/8629 Grid:https://www.html.cn/archives/8510/    http://www.ruanyif ...