- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8"/>
- <meta name="viewport" content="width=718,user-scalable=no"/>
- <title>面向对象矩阵加减乘除</title>
- <link rel="stylesheet" type="text/CSS" />
- <link rel="stylesheet" type="text/css" />
- <style>
- .result{
- width: 900px;
- height: 50px;
- line-height: 50px;
- background-color: #f1f1f1;
- }
- </style>
- </head>
- <body>
- <textarea id="text1" rows="6" cols="30">[[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]]</textarea>
- <textarea id="text2" rows="6" cols="30">[[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]]</textarea>
- <button id="add">相加</button>
- <button id="sub">相减</button>
- <button id="mul">相乘</button>
- <button id="identity-matrix">单位矩阵</button>
- <pre class="result"></pre>
- <script type="text/javascript">
- (function(){
- bind(document.getElementById("add"),'click');
- bind(document.getElementById("sub"),'click');
- bind(document.getElementById("mul"),'click');
- bind(document.getElementById("identity-matrix"),'click');
- function bind(ele,type){
- ele.addEventListener(type,addevent);
- }
- })();
- function addevent(){
- a3 = this.innerHTML;
- var result = document.querySelector(".result");
- var m1 = new Matrix(JSON.parse(document.getElementById("text1").value));
- var m2 = new Matrix(JSON.parse(document.getElementById("text2").value));
- switch (a3) {
- case "相加":
- result.innerHTML = m1.add(m2);
- break;
- case "相减":
- result.innerHTML = m1.sub(m2);
- break;
- case "相乘":
- result.innerHTML = m1.mul(m2);
- break;
- case "单位矩阵":
- result.innerHTML = m1.identity(m2);
- break;
- default:
- console.log("出错了!");
- }
- }
- function Matrix(matrix1,row,col){
- this.row = matrix1.length;
- this.col = matrix1[0].length;
- this.datas = matrix1;
- };
- Matrix.prototype.add = function(matrix){
- var result = [];
- for(var i = 0; i < this.row; i++){
- result[i] = [];
- for (var j = 0; j < this.col; j++){
- result[i][j] = this.datas[i][j] + matrix.datas[i][j];
- }
- }
- return JSON.stringify(result);
- };
- Matrix.prototype.sub = function(matrix){
- var result = [];
- for(var i = 0; i < this.row; i++){
- result[i] = [];
- for (var j = 0; j < this.col; j++){
- result[i][j] = this.datas[i][j] - matrix.datas[i][j];
- }
- }
- return JSON.stringify(result);
- };
- Matrix.prototype.mul = function(matrix){
- var result = [];
- for (var i = 0; i < this.row; i++){
- result[i] = [];
- for(var j = 0; j < matrix.col; j++){
- var num = 0;
- for(var k = 0; k < matrix.row; k++){
- num = num + (this.datas[i][k] * matrix.datas[k][j]);
- }
- result[i][j] = num;
- }
- }
- return JSON.stringify(result);
- };
- Matrix.prototype.identity = function(){
- for(var i = 0; i < this.row; i++){
- for(var j = 0; j < this.col; j++){
- if(i == j){
- this.datas[i][j] = 1;
- }else{
- this.datas[i][j] = 0;
- }
- }
- }
- return JSON.stringify(this.datas);
- }
- </script>
- </body>
- </html>
- //该片段来自于http://www.codesnippet.cn/detail/0104201512052.html
来源: http://www.codesnippet.cn/detail/0104201512052.html