以下代码无效:<br/>
我尝试使用Visual Studio进行调试。我undefined
附近有this.result
。
我尽力弄清楚错误,但没找到任何东西。
<body>
<br />
<br /><br />
<br />
<table align="center">
<tr>
<td>Number Of Nodes:</td>
<td> <input id="num" type="text" style="width:30px"/></td>
</tr><tr></tr>
<tr></tr>
<tr>
<td>
Adjacancy Matrix:
</td>
<td>
<input id="File1" type="file" />
</td>
</tr><tr></tr><tr></tr>
<tr>
<td>Node List Information:</td>
<td>
<input id="File2" type="file" />
</td>
</tr><tr></tr><tr></tr>
<tr>
<td>
Stopping Condition:</td>
<td>
<input id="File3" type="file" /></td>
</tr><tr></tr><tr></tr>
<tr>
<td>
Algorithm:</td>
<td>
<input id="bfs" type="radio" name="algo" value="BFS" />BFS
<input id="dfs" type="radio" name="algo" value="DFS" />DFS</td>
</tr><tr></tr><tr></tr><tr></tr>
<tr></tr><tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr>
<td></td>
<td>
<button id="complete" type="submit" value="Complete" onclick="completeClick()">Complete Traversal</button></td>
<td>
<button id="step" type="submit">Step-By-Step Traversal</button>
<script type="text/javascript">
var temp;
var line;
var vis;
var numofnodes;
function completeClick() {
numofnodes = parseInt(document.getElementById('num').value, 10);
temp = new Array(numofnodes);
vis = new Array(numofnodes);
document.getElementById('File1').onchange = function () {
var file = this.files[0];
var reade = new FileReader();
reade.onload = function (e) {
var lines = this.result.toString().split('\n');
for (var x = 0; x < lines.length; x++) {
var tmp = lines[x].toString().split(' ');
for (var m = 0; m < tmp.length; m++) {
temp[x][m] = tmp[m];
}
}
};
reade.readAsText(file);
};
document.getElementById('File2').onchange = function () {
var file = this.files[0];
var reade1 = new FileReader();
reade1.onload = function (e) {
line = this.result.toString().split('\n');
};
reade1.readAsText(file);
};
document.getElementById('print').innerHTML = "";
if (document.getElementById('bfs').checked) {
bfs(0);
}
else if (document.getElementById('dfs').checked) {
dfs(0);
}
};
function bfs(s) {
var queue = [];
for (var b = 0; b < numofnodes; b++) {
queue[b] = [];
}
var i, front, rear, root;
front = 0;
rear = 0;
vis[s] = 1;
print(s);
queue[rear+1] = s;
while(front!=rear)
{
root = queue[front];
for(i=0;i<numofnodes;i++)
{
if(temp[root,i]!=0 && vis[i]===0)
{
vis[i] = 1;
queue[rear+1] = i;
print(i);
}
}
front+=1;
}
};
function dfs(i) {
var j;
vis[i] = 1;
print(i);
for (j = 0; j < numofnodes; j++) {
if (vis[j] ===0) {
if (temp[i][j] === 1) {
dfs(j);
}
}
}
};
function print(i) {
document.getElementById('print').innerHTML += line[i].toString() + "<br/>";
};
</script>
</td>
</tr>
<tr><td id="print" class="print"></td></tr>
</table>
</body>
</html>
答案 0 :(得分:0)
var temp未定义
以下更改(另外2行 - 查找注释)应该修复它
document.getElementById('File1').onchange = function () {
var file = this.files[0];
var reade = new FileReader();
reade.onload = function (e) {
temp = []; // add this line
var lines = this.result.toString().split('\n');
for (var x = 0; x < lines.length; x++) {
var tmp = lines[x].toString().split(' ');
temp[x] = []; // add this line too
for (var m = 0; m < tmp.length; m++) {
temp[x][m] = tmp[m];
}
}
};
reade.readAsText(file);
};