-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.html
144 lines (124 loc) · 4.25 KB
/
stack.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
<!DOCTYPE html>
<html>
<head>
<title>Stack Visualization and Data Display</title>
<style>
body {
display: flex;
justify-content: space-between;
}
#stackContainer {
display: flex;
flex-direction: column-reverse;
width: 400px;
border: 1px solid black;
padding: 10px;
height: 500px;
overflow: auto;
}
#dataContainer, #worktapeContainer, #outputtapeContainer {
display: flex;
/* flex-direction: column-reverse; */
flex-wrap: wrap;
gap: 10px;
width: 1200px;
border: 1px solid black;
padding: 10px;
height: 100px;
overflow: auto;
}
.stackElement, .dataElement {
margin-bottom: 0;
border-bottom: 1px solid #ccc;
}
.vertical-layout {
display: flex;
flex-direction: column;
}
#pushButton, #popButton, #binarySearchButton, #resetButton {
display: inline-block;
margin-left: 20px;
}
#stackSize, #binarySearchList, #binarySearchTarget {
margin-top: 20px;
width: 200px;
}
#stateContainer {
margin-top: 20px;
}
#currentState {
font-weight: bold;
}
#times {
margin-top: 20px;
}
#customImage {
width: 500px; /* Set the width of the image */
height: 500px; /* Set the height of the image */
}
#space1 {
width: 2px;
height: 2px;
}
#space2 {
width: 80px;
height: 80px;
}
</style>
</head>
<body>
<div>
<div id="stackContainer"></div>
<button id="pushButton">push</button>
<button id="popButton">pop</button>
<button id="resetButton">reset</button>
<input type="text" id="inputBox" />
<div id="stackSize">Stack Size: 0</div>
</div>
<script>
window.onload = function() {
var socket = new WebSocket("ws://localhost:8888/stacksocket");
socket.onopen = function(event) {
socket.send("Hello Server");
};
var data;
document.getElementById('pushButton').addEventListener('click', function(){
var inputBoxValue = document.getElementById('inputBox').value;
if (inputBoxValue == '') {return;}
socket.send(inputBoxValue); // 发送输入框中的值给后端
});
document.getElementById('popButton').addEventListener('click', function() {
//检测栈是否为空
socket.send("pop");
document.getElementById('stackSize').innerText = 'Stack Size: 0';
});
document.getElementById('resetButton').addEventListener('click', function() {
socket.send("reset");
document.getElementById('stackSize').innerText = 'Stack Size: 0';
});
socket.onmessage = function(event) {
console.log("Received data from server: ", event.data);
alldata = JSON.parse(event.data);
var stackContainer = document.getElementById('stackContainer');
data = alldata.stack; //函数栈中元素
while(stackContainer.firstChild) {
stackContainer.firstChild.remove();
}
for(var i = 0; i < data.length; i++) {
var stackElement = document.createElement('div');
stackElement.textContent = data[i];
stackElement.className = 'stackElement';
stackContainer.appendChild(stackElement);
}
document.getElementById('stackSize').innerText = 'Stack Size: ' + data.length;
};
socket.onclose = function(event) {
console.log("Client notified socket has closed",event);
};
socket.onerror = function(error) {
console.log("Error: " + error);
};
}
</script>
</body>
</html>