方便我们计算学分绩点的JavaScript

基于目前我们学校教务处的管理系统, 依靠Javascript的帮忙, 我们可以很方便地计算成绩.

测试用HTML:

<table class="datagridstyle" cellspacing="0" cellpadding="3" border="0" id="DataGrid1"
        style="width: 100%; border-collapse: collapse;">
        <tr class="datagridhead">
            <td>
                课程代码</td>
            <td>
                课程名称</td>
            <td>
                课程性质</td>
            <td>
                成绩</td>
            <td>
                补考成绩</td>
            <td>
                重修成绩</td>
            <td>
                学分</td>
            <td>
                绩点</td>
            <td>
                辅修标记</td>
        </tr>
        <tr class="datagrid1212">
            <td>
                24109505</td>
            <td>
                数据库系统实验</td>
            <td>
                专业基础课</td>
            <td>
                优秀</td>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
            <td>
                0.5</td>
            <td>
                4.50</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                24100320</td>
            <td>
                单片机原理及应用</td>
            <td>
                专业基础选修课</td>
            <td>
                78</td>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
            <td>
                2.0</td>
            <td>
                2.80</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr class="datagrid1212">
            <td>
                24100915</td>
            <td>
                UML</td>
            <td>
                校选修课</td>
            <td>
                63</td>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
            <td>
                1.5</td>
            <td>
                1.30</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                24200510</td>
            <td>
                数据库系统课程设计</td>
            <td>
                设计(论文)</td>
            <td>
                优秀</td>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
            <td>
                1.0</td>
            <td>
                4.50</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr class="datagrid1212">
            <td>
                24203910</td>
            <td>
                可编程序控制器与工业控制网络课程设计</td>
            <td>
                设计(论文)</td>
            <td>
                良好</td>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
            <td>
                1.0</td>
            <td>
                3</td>
            <td>
                &nbsp;</td>
        </tr>
    </table> 

JavaScript:

<script type="text/javascript" language="javascript">
    //一学年的平均学分绩点=该学年修全部课程的学分绩点之和÷所修课程的总学分(校选修不计算在内)
    //学业成绩平均分=一学年平均学分绩点折合为百分制的得数
    //考通过的课程,按实际成绩记分,并给予学分,但其绩点均为“0”。重修的课程在该年度中按不及格科目计算,绩点为0。
    function Calculate() {   
        var table = document.getElementById("DataGrid1");   
        var totalMark = 0;   
        var failCount = 0;   
        var subjectCount = 0;
        var totalRate = 0;   
        var totalRawRate = 0;   
        for(var i = 1; i < table.rows.length; i++) {       
            if(table.rows[i].cells[2].innerHTML == "校选修课") continue;   
            subjectCount++;   
            var mark = getMark(table.rows[i].cells[3].innerHTML);       
            if(mark < 60) {           
                failCount++;       
            }       
            totalMark += mark;       
            totalRate += getMark(table.rows[i].cells[7].innerHTML)
                * getMark(table.rows[i].cells[6].innerHTML);       
            totalRawRate += getMark(table.rows[i].cells[6].innerHTML);   
        }   
        var resultRow = document.getElementById("resultRow");   
        if(!resultRow) {       
            resultRow = document.createElement("tr");       
            resultRow.id = "resultRow";   
        }   
        resultRow.innerHTML = "";   
        var results = ["总分:", totalMark, "平均分:", (totalMark/subjectCount).toFixed(3),
            "平均学分绩点:", (totalRate/totalRawRate).toFixed(3), "不及格数:",failCount];   
        for(var i = 0; i < results.length; i++) {       
            var td = document.createElement("td");       
            td.innerHTML = results[i];       
            resultRow.appendChild(td);   
        }   
        table.lastChild.appendChild(resultRow);
    }
    function getMark(markStr) {   
        switch(markStr) {       
            case "优秀": return 95;       
            case "良好": return 85;       
            case "中等": return 75;       
            case "及格": return 65;       
            case "不及格":       
            case "" :        
            case "&nbsp;": return 0;       
            default: return parseFloat(markStr);   
        }
    }
    Calculate();
   </script>

posted @ 2007-09-05 22:33  MK2  阅读(1052)  评论(2编辑  收藏  举报