上学期有个课程叫做操作系统,期末的时候这课程还有个课程设计,其中有个题目叫做银行家算法。

什么是银行家算法我就不解释了!

看着同学们的设计,大同小异甚至前篇一律。

清一色的控制台程序,清一色的蛋疼输入,甚至提示都是式样的!

然后我的看的淡定了,于是....

银行家算法的JavaScript版出现了...

借助html和css表现,javascript实现逻辑功能

代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>银行家算法模拟系统</title>
<script src="http://lib.sinaapp.com/js/jquery/1.7/jquery.min.js"></script>
<script>
var maxn = 101;
var num_process;
var resource;
var bmax=new Array();//最大资源数
var need=new Array();//资源需求数
var work=new Array();//资源可用数
var available=new Array();//总资源数
var allocation=new Array();//已分配资源
var finish=new Array();//是否已完成
var bp=new Array();//安全序列
var l=0; function GetWork()
{
for(j=0;j<num_resource;j++)
{
work[j]=available[j];//=available[j]-allocation[i][j];
}
for(i=0;i<num_process;i++)
{
for(j=0;j<num_resource;j++)
{
work[j]-=allocation[i][j];
}
}
/*for(j=0;j<num_resource;j++)
{
alert(work[j]);//=available[j]-allocation[i][j];
}*/
}
//求安全序列
function GetSequence()
{
l=0;
//alert(l);
GetNeed();//
GetWork();
for(i = 0; i < num_process; i ++)
finish[i] = false;//初始化各进程的分配标志 while(l < num_process)
{
flag = false;
for(i = 0; i < num_process; i ++)
{
if(finish[i])continue;
//alert(need.length);
for( j = 0; j < num_resource; j ++)
{
if(need[i][j] > work[j])break;
}
if(j == num_resource)
{
flag = true;
bp[l] = i;
l++;
finish[i] = true;
for(k = 0; k < num_resource; k ++)
work[k] += allocation[i][k];
}
}
if(!flag) break;
}
} function CreateTable()
{
table="<br/>系统资源量数目依次是:";
for(i=0;i<num_resource;i++)
{
table+=" "+available[i]+"&nbsp;&nbsp;&nbsp;&nbsp;";
};
table+="<p><p/><hr/>";
table+="请输入个进程的最大资源需求数(MAX)和已分配数(Allocation):(Need会自动计算)<br/>";
table+="<table border=0 cellspacing=1 width=100% style='text-align:center'><tr><td>&nbsp;</td><td colspan="+$("#num_resource").val()+">Max</td><td colspan="+$("#num_resource").val()+">Allocation</td colspan="+$("#num_resource").val()+"><td colspan="+$("#num_resource").val()+">Need</td></tr>";
for(i=0;i<$("#num_process").val();i++)
{
table+="<tr><td>进程"+i+"</td>";
for(j=0;j<3;j++)
{
for(x=0;x<$("#num_resource").val();x++)
{
table+="<td><input type=text id=e"+i+j+x;
if(j==2){table+=" readonly=\"readonly\" "}
table+="></td>";
}
}
table+="</tr>";
}
table+="</table>";
$("#table").html(table)
}
function GetInfo()
{
//获取最大资源数
for(i=0;i<num_process;i++)
{
bmax[i]=new Array();
for(x=0;x<num_resource;x++)
{
bmax[i][x]=$("#e"+i+"0"+x).val()
}
} //获取已分配资源数
for(i=0;i<num_process;i++)
{
allocation[i]=new Array();
for(x=0;x<num_resource;x++)
{
allocation[i][x]=$("#e"+i+"1"+x).val()
}
} } function GetNeed()
{
//计算各进程对个资源的需求量
for(i = 0; i < num_process; i ++)
{
need[i]=new Array();
for(j = 0; j < num_resource; j ++)
{
need[i][j] = bmax[i][j] - allocation[i][j];
}
}
for(i=0;i<num_process;i++)
{
//need[i]=new Array();
for(x=0;x<num_resource;x++)
{
//$("#e"+i+"2"+x).val(need[i][x]);
//alert($("#e"+i+"2"+x).val());
$("#e"+i+"2"+x).val(need[i][x]);
}
}
}
function PrintSequence()
{
if(l == num_process)
{
lhtml="该资源是安全的;安全序列为:";
for(i = 0; i < l; i ++)
{
if(i) lhtml+="=>";
lhtml+=bp[i];
} }
else lhtml="对不起,该资源状态不安全!";
lhtml+="<br/>亲,如果有新的进程请求可以直接在上表中修改数据并确定哦!";
$("#l").html(lhtml);
} $(document).ready(function(e) {
$("#ok1").click(function(e) {
num_process=$("#num_process").val();
num_resource=$("#num_resource").val();
alert(num_process+"个进程,"+num_resource+"个资源");
for(i=0;i<num_resource;i++)
{
available[i]= window.prompt("请输入系统总的资源数量:","请输入资源"+i+"的数量:");
}
CreateTable();
$("#h").show();
}); $("#ok2").click(function(e) {
GetInfo();
GetSequence();
PrintSequence();
});
$("#come").click(function(e) {
$(".cover").fadeOut();
});
$("input").change(function(e) {
var reg=new RegExp("^[0-9]*$");
if(!reg.test($(this).val())==true){
alert("对不起,您必须输入数字!");
$(this).addClass("border");
$(this).focus();
}
else{
$(this).removeClass("border");
}
}); });
</script>
<style>
body{ margin:0; text-align:center;}
input{ width:20px;}
table{ background:#999999;}
td{ background:#FFFFFF;}
.cover{ background:#00FFFF; position:absolute; width:100%; height:100%; padding-top:20px; text-align:center;}
.border{ border-bottom:1px solid #F00;}
</style>
</head> <body>
<div class="cover">
<h1>欢迎使用银行家算法模拟系统!</h1>
<input type="button" id="come" value="进入系统" style="height:50px; width:200px;" />
</div>
请输入进程数:
<input type="text" id="num_process" name="num_process" /><br/>
请输入资源数:
<input type="text" id="num_resource" name="num_resource" /><br/>
<input type="button" id="ok1" value="确定" style="width:50px;"/>
<hr/>
<div style="display:none" id="h">
<div class="table" id="table">
s
</div>
<input type="button" id="ok2" value="确定" style="width:50px;" /><br/>
<div class="l" id="l">
</div>
</div> </body>
</html>

最新文章

  1. PHP的继承方法如何获取子类名?get_class() 和 get_called_class()
  2. ASCII、UNICODE、UTF-8等关系
  3. Android开发中 Eclipse常忘记又需要的快捷键
  4. 一款可以下拉搜索html下拉框控件
  5. JavaScript 对象 之继承对象 学习笔记
  6. Python模拟HTTP Post上传文件
  7. GRE词汇3-4 +
  8. URL链接中文参数乱码的若干处理方法
  9. [Linux&amp;Vim]输入输出流
  10. WPF之核心面板(容器)控件简单介绍
  11. DOM4J 读取XML配置文件进行数据库连接
  12. [0] CollectionBase与索引符DictionaryBase与迭代器
  13. 简单Elixir游戏服设计-玩法simple_poker
  14. [LeetCode] 8. 字符串转换整数 (atoi)
  15. Wireshark简单使用教程3——附视频
  16. django项目微博第三方登录
  17. 使用Groovy+Spock轻松写出更简洁的单测
  18. with open
  19. 使用 ImageEnView 给图片加水印,及建缩略图
  20. Tomcat:A cookie header was received[xxxxxx] that contained an invalid cookie. That cookie will be ignored.

热门文章

  1. xcode加载静态链接库.a文件总是失败
  2. BJOI2019 游记
  3. bzoj 4806 炮
  4. W3Schools SQL Quiz
  5. 剑指offer-第四章解决面试题思路(复杂链表的复制)
  6. LeetCode Path Sum IV
  7. 两种方式创建Maven项目【方式一】
  8. 【brew使用技巧】fix links
  9. sql中case when的简单使用
  10. (转)Oracle 临时表用法