吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 6308|回复: 0
收起左侧

[Java 转载] JavaScript写的网页IEEE754规格化数计算器

[复制链接]
LittleStars 发表于 2014-7-7 13:25
最近在做课程设计,源代码分享.用到以前学过的计算机组成原理,还有最新自学的Javascript
部分代码有参考学长的课程设计.
MainPage.HTML

[HTML] 纯文本查看 复制代码
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>IEEE754规格化数计算器</title>
    <script type="text/javascript" src="./js/func.js"></script>
</head>
<body>
	计算短浮点数(总位数32)的IEEE754数据<br>
    本系统计算32位的IEEE754数据<br />
    <form action="stooges.php">
        <fieldset>
            <p>
                输入输出:<br/>
                <input type="text" name="number" id="number"size="40" value="0">
            </p>
            <p>
                <input type="button" id="Converter1" name="Converter1"
                    value="IEEE754二进制转十进制">
                <input type="button" id="Converter2" name="Converter2"
                   value="十进制转IEEE754二进制">
            </p>
        </fieldset>
        例如:IEEE单精度数代码11000010111001001000000000000000转换成十进制数值表示<br />
        为-114.25<br />
    </form>
</body>
</html>




JavaScript部分:
func.js
[JavaScript] 纯文本查看 复制代码
function ChangeToDecimalism() {
    var num = IEEEToDec();
    document.getElementById("number").value = num;
}

function ChangeToIEEE754() {
    var num = DecToIEEE();
    document.getElementById("number").value = num;
}

function DecToIEEE() {
    var flag = 1;
    var number = document.getElementById("number").value
    var len = number.length
    for (var i = 0; i < len; i++){
        if (number[i] < '0' || number[i] > '9'){
		if(number[i]=='.' || number[i]=='-')
			continue;
		else{
            		flag = 0;
            		break;
		}
        }
    }
    if (flag == 0){
        alert("您输入的数据有误!");
    }
    else{
        number = parseFloat(number);
        var S = "";
        var M = "";
        var E = "";
        if (number >= 0) {
            S = 0;
        }
        else {
            S = 1;
            number = 0 - number;
        }
        M = number.toString(2);
        E = M.indexOf(".");
        if (E != -1) {
            E = (E + 126).toString(2);
            M = M.substring(1);
            var mm = new Array(2);
            mm = M.split(".");
            M = mm[0] + mm[1];
            var ml = M.length
            for (var i = 0; i < 23 - ml; i++) {
                M += "0";
            }
        }
        else {
            E = (M.length - 1 + 127).toString(2);
            M = M.substring(1);
            var temp = M.length
            for (var i = 0; i < 23 - temp; i++) {
                M += "0";
            }
        }
        var newNumber = S + E + M;
        return newNumber;
    }
}
function IEEEToDec() {
    var flag = 1;
    var number = document.getElementById("number").value
    var len = number.length;
    if (len == 32) {
        for (var i = 0; i < len; i++) {
            if (number[i] < '0' || number[i] > '1') {
                flag = 0;
                break;
            }
        }
        if (flag == 0) {
            alert("您输入的数据不争确!");
        }
        else {
            var S = number.substring(0, 1);
            var E = number.substring(1, 9);
            var M = number.substring(9, number.length);
            E = parseInt(E, 2) - 127;
            M = "1" + M;
            temp1 = M.substring(0, E + 1);
            temp2 = M.substring(E + 1, M.length);
            m_int = parseInt(temp1, 2);
            m_float = parseFloat("0." + temp2, 10).toString().substring(2, M.length);
            var m_float2 = 0;
            for (var i = 0; i < m_float.length; i++) {
                var x = m_float.charAt(i);
                m_float2 += parseFloat(x) * Math.pow(2, -(i + 1));
            }
            M = m_int + m_float2;
            if (parseInt(S) == 1) {
                M = 0 - M;
            }
            return M;
        }
    }
    else
        alert("请输入一个长度为32的二进制数。");
}



发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-15 21:50

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表