Description
利用二维数组(double[])实现一个矩阵类:Matrix。要求提供以下方法: (1)set(int row, int col, double value):将第row行第col列的元素赋值为value; (2)get(int row,int col):取第row行第col列的元素; (3)width():返回矩阵的列数; (4)height():返回矩阵的行数; (5)Matrix add(Matrix b):返回当前矩阵与矩阵b相加后的矩阵; (6)Matrix multiply(Matrix b):返回当前矩阵与矩阵b相乘后的矩阵。 (7)Matrix transpose():返回当前矩阵的转置矩阵; (8)toString():以行和列的形式打印出当前矩阵。
Input
矩阵的行列数 矩阵的数据 设置矩阵值的行、列和值 获取矩阵值的行、列 待相加矩阵的行列数 待相加矩阵的值 待相乘矩阵的行列数 待相乘矩阵的值
Output
矩阵的行、列数 设置矩阵值后的矩阵 某行某列的矩阵值 矩阵相加结果 矩阵相乘结果 矩阵转置结果
Sample Input
3 3 1 2 3 4 5 6 7 8 9 2 3 8 1 3 3 3 1 2 3 4 5 6 7 8 9 3 2 1 2 1 2 1 2
Sample Output
row:3 column:3 after set value: 1 2 3 4 5 8 7 8 9 value on (1,3):3 after add: 2 4 6 8 10 14 14 16 18 after multiply: 6 12 17 34 24 48 after transpose: 1 4 7 2 5 8 3 8 9
import java.text.DecimalFormat;
import java.util.*;
class Matrix{
private int row;//行
private int col;//列
//private double value;
double [][]Data;
public Matrix(int row, int col,double [][]Data) {
this.row = row;
this.col = col;
// this.value = value;
this.Data = Data;
}
public void setMatrix(int row , int col, double value) {
this.Data[row - 1][col - 1] = value;
}
public double getMatrix(int row, int col) {
return Data[row - 1][col - 1] ;
}
public int width() {
return row;
}
public int height() {
return col;
}
public Matrix add(Matrix b) {
if(this.width() != b.width() && this.height() != b.height()) {
return null;
}
double add[][] = new double[this.row][this.col];
for(int i = 0;i<col;i++) {
for(int j = 0;j<row;j++) {
add[i][j] = this.Data[i][j] + b.Data[i][j];
}
}
Matrix another = new Matrix(this.col,this.row,add);
System.out.println("after add:");
return another;
}
public Matrix multiply(Matrix b) {
if(this.col != b.row) {
return null;
}
double mul[][] = new double[this.row][b.col];
double temp = 0;
for(int i = 0;i<this.row;i++) {
for(int k = 0;k<b.col;k++) {
for(int j = 0;j<this.col;j++)
{
temp += this.Data[i][j] * b.Data[j][k];
}
mul[i][k] = temp;
temp = 0;
}
}
Matrix another = new Matrix(this.row, b.col, mul);
System.out.println("after multiply:");
return another;
}
public Matrix transpose() {
double tran[][] = new double[this.row][this.col];
for(int i = 0;i<this.row;i++) {
for(int j = 0;j<this.col;j++) {
tran[j][i] = this.Data[i][j];
}
}
Matrix another = new Matrix(this.col,this.row,tran);
System.out.println("after transpose:");
return another;
}
public String toString() {
DecimalFormat df = new DecimalFormat("0");
String result = "";
//result += df.format(Data[0][0]);
for(int i = 0;i<this.row;i++) {
result += df.format(Data[i][0]);
for(int j = 1;j<this.col;j++) {
result += " " + df.format(Data[i][j]);
}
result += "\n";
}
return result;
}
}
public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int row = scan.nextInt();
int col = scan.nextInt();
System.out.println("row:" + row +" column:" + col);
//set value
double data[][] = new double[row][col];
for(int i = 0;i<row;i++) {
for(int j = 0;j<col;j++) {
double d = scan.nextDouble();
data[i][j] = d;
}
}
Matrix matrix = new Matrix(row,col,data);
int srow = scan.nextInt();
int scol = scan.nextInt();
double sv = scan.nextDouble();
System.out.println("after set value:");
matrix.setMatrix(srow, scol, sv);
System.out.print(matrix);
//get value
DecimalFormat df = new DecimalFormat("0");
int vrow = scan.nextInt();
int vcol = scan.nextInt();
System.out.print("value on (" +vrow + "," + vcol +"):");
System.out.println(df.format(matrix.getMatrix(vrow, vcol)));
//add
int addrow = scan.nextInt();
int addcol = scan.nextInt();
double addMatrix[][] = new double[addrow][addcol];
for(int i = 0;i<addrow;i++) {
for(int j = 0;j<addcol;j++) {
double ad = scan.nextDouble();
addMatrix[i][j] = ad;
}
}
Matrix add = new Matrix(addrow,addcol,addMatrix);
System.out.print(matrix.add(add));
// //mul
int mulrow = scan.nextInt();
int mulcol = scan.nextInt();
double mulMatrix[][] = new double[mulrow][mulcol];
for(int i = 0;i<mulrow;i++) {
for(int j = 0;j<mulcol;j++) {
double mu = scan.nextDouble();
mulMatrix[i][j] = mu;
}
}
Matrix mul = new Matrix(mulrow,mulcol,mulMatrix);
//System.out.print(matrix.add(add));
System.out.print(matrix.multiply(mul));
//transpose
System.out.print(matrix.transpose());
}
}
版权声明:本文为Yolanda_Salvatore原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。