-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
54 lines (54 loc) · 2.03 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>python解释器</title>
<link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script src="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
<script src="lex.js"></script>
<script src="parser.js"></script>
<script src="eval.js"></script>
<script src="run.js"></script>
</head>
<body>
<h1 align="center">Mini Python解释器</h1>
<div class="container" style="margin-top:20px;">
<div class="btn-group ">
<button class="btn btn-danger" onclick="code(0)">基本计算</button>
<button class="btn btn-danger" onclick="code(1)">条件语句</button>
<button class="btn btn-danger" onclick="code(2)">循环语句/数组</button>
<button class="btn btn-danger" onclick="code(3)">函数(综合)</button>
</div>
<br/>
<textarea class="form-control" id="input" style="height:300px;"></textarea>
<br/>
<button class="btn btn-danger" onclick="run()">运行</button>
<br/>
<br/>
<textarea id="output" class="form-control" style="height:200px;">
</textarea>
<script type="text/javascript">
function code(index){
var text = ""
switch(index){
case 0:
text = "a = \"hello world\"\nb = 9\nc = 18 + b\nprint(a,' ',b,' ',c,' \\n')\nprint(b+(c-9)**2+ 8.78/2,'\\n')";
break;
case 1:
text = "age = 3\nif age >= 18:\n\tprint('adult')\nelif age >= 6:\n\tprint('teenager')\nelse:\n\tprint('kid')";
break;
case 2:
text = "sum = 0\nfor x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:\n\tsum = sum + x\nprint(sum)";
break;
case 3:
text = "def hcf(x, y):\n\tsmaller = 0\n\thcf = 0\n\tif x > y:\n\t\tsmaller = y\n\telse:\n\t\tsmaller = x\n\tfor i in range(1,smaller + 1):\n\t\tif((x % i == 0) and (y % i == 0)):\n\t\t\thcf = i\n\treturn hcf\n\ni = 2\nwhile(i < 10):\n\tprint (i,'和',i+2,\"的最大公约数是\",hcf(i,i+2),'\\n')\n\ti=i+1\n";
break;
default:break;
}
$('#input').val(text);
}
code(0)
</script>
</div>
</body>
</html>