1
2
3
4
5
6
计算器练习7
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
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 //operator 运算符
142 var Boo = false; //判断是否按下计算符号的布尔变量;
143 var result = 0; //存储计算数据的变量
144 var ope; //存储计算符号的变量
145
146 function$(x) {147 returndocument.getElementById(x);148 }149
150 functiondecimal() {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 functionnum(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 functioncompute(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 functionmath(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 functionjc(a) {234 returnMath.sqrt(a);235 }236
237 functionreverse() {238 var Num1 = $(‘txtnum‘).value;239 if (Num1 == ‘‘) {240 alert(‘数据不能为空‘);241 } else{242 $(‘txtnum‘).value *= -1;243 }244
245 }246
247 functionbackspace() {248 var txt = $(‘txtnum‘);249 txt.value = txt.value.substring(0, txt.value.length - 1);250 if (txt.value == ‘‘) { txt.value = 0; }251 }252
253
254
255