利用二维数组(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)print():以行和列的形式打印出当前矩阵。
package PTAworks;
/**
* @ClassName: aaa
* @Description: TODO
* @Author: Sunqker
* @Date: 2021/10/13 15:37
*/
import java.util.Scanner;
public class aaa {
public static void main(String[] args) {
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.nextInt());
System.out.println("after set value:");
matrix.print();
//获取矩阵值的行、列
int r = cin.nextInt();
int c = cin.nextInt();
System.out.println("value on (" + r + "," + c + "):" + matrix.get(r,c));
//获取待相加矩阵的行列数
Matrix tmp = new Matrix(cin.nextInt(),cin.nextInt());
//获取待相加矩阵的值
tmp.input(cin);
System.out.println("after add:");
t = matrix.add(tmp); //矩阵相加
t.print(); //打印相加后的矩阵
//待相乘矩阵的行列数
tmp = new Matrix(cin.nextInt(),cin.nextInt());
获取待相加矩阵的值
tmp.input(cin);
System.out.println("after multiply:");
t = matrix.multiply(tmp);//矩阵相乘
//待相乘矩阵的值
t.print(); //打印相乘结果
System.out.println("after transpose:");
t = matrix.transpose();
t.print();
}
}
class Matrix{
private int row, col;
private int[][] d;
public Matrix(int row, int col) {
this.row = row;
this.col = col;
d = new int[row][col];
}
public void input(Scanner cin) {
for(int i = 1; i <= this.row; i++) {
for(int j = 1; j <= this.col; j++) {
int val = cin.nextInt();
this.set(i,j,val);
}
}
}
// 1 将第row行第col列的元素赋值为value
public void set(int row, int col, int value){
d[--row][--col] = value;
}
// 2 取第row行第col列的元素
public int get( int row, int col ){
return d[--row][--col];
}
// 3 返回矩阵的列数
public int width(){
return col;
}
// 4 返回矩阵的行数
public int height(){
return row;
}
// 5 返回当前矩阵与矩阵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;
}
// 6 返回当前矩阵与矩阵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 ){
int 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;
}
// 7 返回当前矩阵的转置矩阵
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;
}
// 8 行列打印矩阵
public void print(){
//DecimalFormat d = new DecimalFormat("#"); // 控制输出格式,无小数
for(int i = 1; i <= this.row; ++ i){
for( int j = 1; j <= this.col - 1; ++ j ){
System.out.print(this.get(i, j) + " ");
}
System.out.println(this.get(i, col));
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/74928.html