矩阵类
利用二维数组(int[])实现一个矩阵类:Matrix。要求提供以下方法:(1)set(int row, int col, int 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():以行和列的形式打印出当前矩阵。
输入格式: 矩阵的行列数 矩阵的数据 设置矩阵值的行、列和值 获取矩阵值的行、列 待相加矩阵的行列数 待相加矩阵的值 待相乘矩阵的行列数待相乘矩阵的值
输出格式: 矩阵的行、列数 设置矩阵值后的矩阵 某行某列的矩阵值 矩阵相加结果 矩阵相乘结果 矩阵转置结果
输入样例:
在这里给出一组输入。例如:
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
结尾无空行
输出样例:
在这里给出相应的输出。例如:
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 duanmingyurow;
private int duanmingyucol;
double [][]Data;
public Matrix(int duanmingyurow, int duanmingyucol,double [][]Data) {
this.duanmingyurow = duanmingyurow;
this.duanmingyucol = duanmingyucol;
this.Data = Data;
}
public void setMatrix(int duanmingyurow , int duanmingyucol, double value) {
this.Data[duanmingyurow - 1][duanmingyucol - 1] = value;
}
public double getMatrix(int duanmingyurow, int duanmingyucol) {
return Data[duanmingyurow - 1][duanmingyucol - 1] ;
}
public int width() {
return duanmingyurow;
}
public int height() {
return duanmingyucol;
}
public Matrix add(Matrix b) {
if(this.width() != b.width() && this.height() != b.height()) {
return null;
}
double add[][] = new double[this.duanmingyurow][this.duanmingyucol];
for(int i = 0;i<duanmingyucol;i++) {
for(int j = 0;j<duanmingyurow;j++) {
add[i][j] = this.Data[i][j] + b.Data[i][j];
}
}
Matrix another = new Matrix(this.duanmingyucol,this.duanmingyurow,add);
System.out.println("after add:");
return another;
}
public Matrix multiply(Matrix b) {
if(this.duanmingyucol != b.duanmingyurow) {
return null;
}
double mul[][] = new double[this.duanmingyurow][b.duanmingyucol];
double temp = 0;
for(int i = 0;i<this.duanmingyurow;i++) {
for(int k = 0;k<b.duanmingyucol;k++) {
for(int j = 0;j<this.duanmingyucol;j++)
{
temp += this.Data[i][j] * b.Data[j][k];
}
mul[i][k] = temp;
temp = 0;
}
}
Matrix another = new Matrix(this.duanmingyurow, b.duanmingyucol, mul);
System.out.println("after multiply:");
return another;
}
public Matrix transpose() {
double tran[][] = new double[this.duanmingyurow][this.duanmingyucol];
for(int i = 0;i<this.duanmingyurow;i++) {
for(int j = 0;j<this.duanmingyucol;j++) {
tran[j][i] = this.Data[i][j];
}
}
Matrix another = new Matrix(this.duanmingyucol,this.duanmingyurow,tran);
System.out.println("after transpose:");
return another;
}
public String toString() {
DecimalFormat df = new DecimalFormat("0");
String result = "";
for(int i = 0;i<this.duanmingyurow;i++) {
result += df.format(Data[i][0]);
for(int j = 1;j<this.duanmingyucol;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 duanmingyurow = scan.nextInt();
int duanmingyucol = scan.nextInt();
System.out.println("row:" + duanmingyurow +" column:" + duanmingyucol);
double data[][] = new double[duanmingyurow][duanmingyucol];
for(int i = 0;i<duanmingyurow;i++) {
for(int j = 0;j<duanmingyucol;j++) {
double d = scan.nextDouble();
data[i][j] = d;
}
}
Matrix matrix = new Matrix(duanmingyurow,duanmingyucol,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);
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)));
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));
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.multiply(mul));
System.out.print(matrix.transpose());
}
}