以下是一个基于JavaScript的简单代码运行原理可视化程序的示例:
```html
<!DOCTYPE html>
<html>
<head>
<title>代码运行原理可视化</title>
<style>
#code {
width: 500px;
height: 400px;
border: 1px solid black;
padding: 10px;
margin-bottom: 10px;
font-family: 'Courier New', Courier, monospace;
white-space: pre;
overflow: auto;
}
</style>
</head>
<body>
<h1>代码运行原理可视化</h1>
<textarea id="code">
function factorial(n) {
if (n === 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
console.log(factorial(5));
</textarea>
<button οnclick="runCode()">运行代码</button>
<h3>执行结果:</h3>
<pre id="output"></pre>
<script>
function runCode() {
var code = document.getElementById('code').value;
var output = document.getElementById('output');
output.innerHTML = '';
try {
var result = eval(code);
output.innerHTML = result;
} catch (error) {
output.innerHTML = '发生错误:' + error;
}
}
</script>
</body>
</html>