7-4 矩阵类 (30 分)
利用二维数组(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():以行和列的形式打印出当前矩阵。
输入格式:
矩阵的行列数 矩阵的数据 设置矩阵值的行、列和值 获取矩阵值的行、列 待相加矩阵的行列数 待相加矩阵的值 待相乘矩阵的行列数 待相乘矩阵的值
输出格式:
矩阵的行、列数 设置矩阵值后的矩阵 某行某列的矩阵值 矩阵相加结果 矩阵相乘结果 矩阵转置结果
输入样例:
在这里给出一组输入。例如:
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.Scanner;
public class Main {
public static void main(String[] args) {
DecimalFormat d = new DecimalFormat("#"); // 控制输出格式,无小数
Scanner cin = new Scanner(System.in);
Matrix matrix = new Matrix(cin.nextInt(), cin.nextInt());
Matrix t; // 暂存矩阵
matrix.input(cin); // 输入信息
System.out.println("row:" + matrix.height() + " " + "column:" + matrix.width());
matrix.set(cin.nextInt(), cin.nextInt(), cin.nextDouble());
System.out.println("after set value:");
System.out.print(matrix.toString());
int r = cin.nextInt();
int c = cin.nextInt();
System.out.println("value on (" + r + "," + c + "):" + d.format(matrix.get(r,c)));
Matrix tmp = new Matrix(cin.nextInt(),cin.nextInt());
tmp.input(cin);
System.out.println("after add:");
System.out.print(matrix.add(tmp).toString());
System.out.println("after multiply:");
tmp = new Matrix(cin.nextInt(),cin.nextInt());
tmp.input(cin);
System.out.print(matrix.multiply(tmp));
System.out.println("after transpose:");
System.out.print(matrix.transpose().toString());
}
}
class Matrix{
private int row, col;
private double[][] d;
public Matrix(int row, int col) {
this.row = row;
this.col = col;
d = new double[row][col];
}
public void input(Scanner cin) {
for(int i = 1; i <= this.row; i++) {
for(int j = 1; j <= this.col; j++) {
double val = cin.nextDouble();
this.set(i,j,val);
}
}
}
// 将第row行第col列的元素赋值为value
public void set(int row, int col, double value){
d[--row][--col] = value;
}
// 取第row行第col列的元素
public double get( int row, int col ){
return d[--row][--col];
}
// 返回矩阵的列数
public int width(){
return col;
}
// 返回矩阵的行数
public int height(){
return row;
}
// 返回当前矩阵与矩阵b相加后的矩阵
public Matrix add( Matrix b){
Matrix t = new Matrix(this.row, this.col);
for(int i = 1; i <= this.row; ++ i){
for(int j = 1; j <= this.col; ++ j ){
t.set(i, j, this.get(i, j) + b.get(i, j));
}
}
return t;
}
// 返回当前矩阵与矩阵b相乘后的矩阵
public Matrix multiply( Matrix b){
Matrix t = new Matrix(this.row, b.width());
for(int i = 1; i <= this.row; ++ i){
for(int j = 1; j <= b.width(); ++ j ){
double tt = 0;
for(int k = 1; k <= this.col; k ++ ){
tt += this.get(i, k) * b.get(k, j);
}
t.set(i, j, tt);
}
}
return t;
}
// 返回当前矩阵的转置矩阵
public Matrix transpose(){
Matrix t = new Matrix(this.row, this.col);
for( int i = 1; i <= this.row; i ++ ){
for ( int j = 1; j <= this.col; j ++ ){
t.set(j, i , this.get(i, j));
}
}
return t;
}
public String toString(){
DecimalFormat d = new DecimalFormat("#"); // 控制输出格式,无小数
String ret = "";
for(int i = 1; i <= this.row; ++ i){
for( int j = 1; j <= this.col - 1; ++ j ){
ret += d.format(this.get(i, j)) + " ";
}
ret += d.format(this.get(i, col)) + "\n";
}
return ret;
}
}