-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathct3.html
334 lines (328 loc) · 10.1 KB
/
ct3.html
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>随机小学数学题</title>
<style type="text/css">
*{
font-family: "微软雅黑";
font-size: 20px;
border-collapse: collapse;
}
#title,.p_title{
text-align: center;
font-size: 30px;
font-weight: 900;
}
#title{
height: 120px;
line-height: 120px;
}
.p_title{
clear: both;
height: 140px;
line-height: 140px;
display: none;
}
#choose{
text-align: center;
}
#out{
width: 880px;
margin: 20px auto;
padding: 20px;
}
#content{
/*height: 1000px;*/
float: left;
border: 1px black solid;
}
.question{
width: 420px;
height: 47px;
float: left;
}
.question .subject{
display: inline-block;
}
.question .result{
text-align: center;
border: 0px;
border-bottom: 1px black solid;
}
.a,.b,.c,.answer{
display: inline-block;
width: 60px;
text-align: center;
}
.op,.op2{
display: inline-block;
width: 22px;
text-align: center;
}
.answer{
color: green;
/*font-style: italic;*/
font-weight: 900;
}
.hidden{
display: none;
}
.show{
display: inline-block;
}
.seq{
display: none;
font-style: italic;
width: 30px;
}
</style>
</head>
<body>
<div id="">
<div id="title">
小学数学练习题
</div>
<div id="top">
<div id="choose">
<form action="" method="post" onsubmit="return false;" name="ckop">
选择符号:
<input type="checkbox" name="op" id="jia" value="+" checked/><label for="jia">加</label> 
<input type="checkbox" name="op" id="jian" value="-"/><label for="jian">减</label> 
<input type="checkbox" name="op" id="cheng" value="*"/><label for="cheng">乘</label> 
<input type="checkbox" name="op" id="chu" value="/"/><label for="chu">除</label> 
<br /><br />
输入范围:
<input type="text" value="0" size="8" id="minNum"/> 到 <input type="text" value="100" size="8" id="maxNum"/>
<br /><br />
数量:<input type="text" id="q_num" value="50" />
是否显示编号:<input type="checkbox" id="s_seq" onchange="show_seq(this);" />
<br /><br />
<input type="button" value="出题" onclick="work();"/>
<input type="button" value="显示答案" id="show_an" onclick="show_answer(this);" />
<input type="button" value="判题" id="check_an" onclick="check_answer(this);" />
<input type="button" value="打印" onclick="print_q();" />
<input type="button" value="隐藏横线打印" onclick="show_line(this);" />
<br /><br />
<div>注:可以先点击 "显示答案" 再后再选择 "隐藏横线打印" 打印答案</div>
<div id="show_msg"></div>
</form>
</div>
</div>
<div id="out">
<div id="content" >
<!--<div class="question">
<span class="seq">1、 </span>
<span class="subject">
<span class="a">1</span>
<span>+</span>
<span class="b">1</span>
<span>+</span>
<span class="c">1</span>
=<input type="text" size="6" class="result" />
</span>
<span class="answer">2</span>
<span class="tip">√</span>
</div>-->
</div>
</div>
</div>
</body>
<script type="text/javascript">
var op=[];// 运算符数组
var minNum,maxNum,a,b,total=0;// 获取最小值,最大值,随机数a,数b;总题数
var title = "";
// 获取参数
function getOp () {
// 获取选中的运算符
op=[];
ops = document.getElementsByName("op");
for (var i = 0; i < ops.length; i++) {
if(ops[i].checked){
op.push(ops[i].value);
}
}
// 获取数
minNum = parseInt(document.getElementById("minNum").value);
maxNum = parseInt(document.getElementById("maxNum").value);
total = document.getElementById("q_num").value;
if(isNaN(minNum) || isNaN(maxNum)){
alert("请输入正确的数字!");
return false;
}
title = "小学数学【";
for (var i = 0; i < op.length; i++) {
switch (op[i]){
case '+':title += "加、";break;
case '-':title += "减、";break;
case '*':title += "乘、";break;
case '/':title += "除、";break;
default:
break;
}
}
title = title.substr(0,title.length-1);
title+="】练习题";
document.getElementById("title").innerHTML = title;
return true;
}
// 出题按钮
function work () {
document.getElementById("content").innerHTML = "";
document.getElementById("check_an").value ="判题";
document.getElementById("show_an").value = "显示答案";
document.getElementById("s_seq").checked = false;
if(!getOp()){ return; }
for (var i = 1; i <= total; i++) {
getQuerstin(i);
}
}
// 出题并显示
function getQuerstin (seq) {
a = Math.floor(Math.random()*(maxNum-minNum+1)+minNum);
b = Math.floor(Math.random()*(maxNum-minNum+1)+minNum);
c = Math.floor(Math.random()*(maxNum-minNum+1)+minNum);
o = op[Math.floor(Math.random()*op.length)];
o2 = op[Math.floor(Math.random()*op.length)];
// 除数不能为0,并且a/b能整除,b为a的约数
if(o=='/'){ //
if(b==0){b= Math.floor(Math.random()*(maxNum)+1);}
if(a!=0){
// a = Math.floor(Math.random()*(maxNum)+1);// 让被除数不为0
b = getChuShu(a);// 除法运算,除数为被除数的约数
}
}
if(o2 == '/'){
// 除数C不能为0
if(c==0){
c = Math.floor(Math.random()*(maxNum)+1);// 让除数不为0
}
// b/c能整除
if(o == "+" || o=="-"){
if(b!=0){
//b = Math.floor(Math.random()*(maxNum)+1);// 让被除数不为0
c = getChuShu(b);// 除法运算,除数为被除数的约数
}
}
else if(o=="*" || o== "/"){
var temp = eval(a+o+b);
if(temp != "0"){
c = getChuShu(temp);// 除法运算,除数为被除数的约数
}
}
}
// 答案:
var answer = eval(a+o+b+o2+c);
// 乘号: × × 除法: ÷ ÷ − -
var str = o.replace("*","×").replace("/","÷");
var op2 = o2.replace("*","×").replace("/","÷");
var txt = "<div class='question'><span class='seq'>"+seq+"、 </span><span class='subject'><span class='a'>"+a+"</span><span class='op'>"+str+"</span><span class='b'>"+b+"</span><span class='op2'>"+op2+"</span><span class='c'>"+c+"</span>= <input type='text' size='6' class='result' /></span><span class='answer hidden'>"+answer+"</span><span class='tip'></span></div>"
var content = document.getElementById("content");
content.innerHTML = content.innerHTML + txt;
if(seq%50==0 && seq <total){
// txt += "<div style='clear: both;height: 70px;'></div>";
content.innerHTML = content.innerHTML + "<div class='p_title'>"+title+"</div>";
}
}
// 根据被除数,随机除数
function getChuShu (n) {
var yueshu = [];// 约数
for(var i=1;i<=n;i++){
if(n%i==0){yueshu.push(i)}
}
if(yueshu.length>2){ // 如果约数除了1和本身,就排除1和本身
return yueshu[Math.floor(Math.random()*(yueshu.length-2)+1)];
}
return yueshu[Math.floor(Math.random()*yueshu.length)];
}
// 显示隐藏答案
function show_answer (ts) {
var flag = false;
if(ts.value == "显示答案"){
flag= true;
ts.value = "隐藏答案";
}else{
flag = false;
ts.value = "显示答案"
}
var answers = document.getElementsByClassName("answer");
for (var i = 0; i < answers.length; i++) {
if(flag) answers[i].className = "answer";
else answers[i].className = "answer hidden";
}
}
// 判题
function check_answer (ts) {
var flag = false;
var count_true = 0,count_false = 0;
if(ts.value == "判题"){
flag= true;
ts.value = "取消判题";
}else{
flag = false;
ts.value = "判题";
}
var answers = document.getElementsByClassName("answer");
var results = document.getElementsByClassName("result");
var tips = document.getElementsByClassName("tip");
if(!flag){// 取消判题
for (var i = 0; i < tips.length; i++) {
tips[i].innerHTML ="";
}
document.getElementById("show_msg").innerHTML ="";
return;
}
for (var i = 0; i < results.length; i++) {
if(results[i].value == answers[i].innerText){
tips[i].innerHTML = "<span style='color: green;'> √ </span>";count_true++;
}else{
tips[i].innerHTML = "<span style='color: red;'> x </span>";count_false++;
}
}
document.getElementById("show_msg").innerHTML = "总题数:" + total + "  对:"+count_true +"  错:"+count_false+"  正确率:"+isFloat(count_true/total)*100 +"%";
}
// 打印
function print_q () {
var top = document.getElementById("top");
top.style.display="none";
var p_titles = document.getElementsByClassName("p_title");
for (var i = 0; i < p_titles.length; i++) {
p_titles[i].style.display = "block";
}
document.getElementById("content").style.border = "0";
window.print();
top.style.display="block";
for (var i = 0; i < p_titles.length; i++) {
p_titles[i].style.display = "none";
}
document.getElementById("content").style.border = "1px black solid";
}
function show_line (ts) {
var results = document.getElementsByClassName("result");
for (var i = 0; i < results.length; i++) {
results[i].style.display = "none";
}
print_q ();
for (var i = 0; i < results.length; i++) {
results[i].style.display = "inline-block";
}
}
function show_seq (ts) {
var seqs = document.getElementsByClassName("seq");
for (var i = 0; i < seqs.length; i++) {
if(ts.checked){
seqs[i].style.display = "inline-block";
}else{
seqs[i].style.display = "none";
}
}
}
// 小数取两位小数
function isFloat (n) {
if(parseInt(n) < parseFloat(n)) {
n = n.toFixed(2);
}
return n;
}
</script>
</html>