728x90
반응형
코드 :
using System;
using System.Collections;
using System.Linq;
namespace Matrix_Calculator {
class Matrix {
// 행과 열 변수
private int row;
private int column;
// 행렬 변수 (ArrayList 사용)
private ArrayList matrix;
// Matrix 클래스 생성자
public Matrix() {
Console.WriteLine("Matrix Creates...");
}
// Matrix 클래스 생성자 (매개 변수로 행과 열 받는 경우)
public Matrix(int _row, int _column) : this() {
this.row = _row;
this.column = _column;
matrix = new ArrayList();
}
// 행렬 입자 생성 메소드
public void inputMatrix() {
matrix = new ArrayList();
// 한 행씩 입력한다.
for (int i = 0; i < row; i++) {
Console.Write("input the One Row : ");
string input = Console.ReadLine();
string[] columns = input.Split(' ');
ArrayList columns_number = new ArrayList();
for(int j = 0; j < column; j++) {
columns_number.Add(Convert.ToInt32(columns[j]));
}
matrix.Add(columns_number);
}
}
// 행과 열 입력 메소드
public void inputRowColumn() {
Console.Write("input the Row, Column (ex. 3 3) : ");
string input = Console.ReadLine();
string[] row_column = input.Split(' ');
row = Convert.ToInt32(row_column[0]);
column = Convert.ToInt32(row_column[1]);
}
// 행렬 출력 메소드
public void printMatrix() {
foreach(ArrayList row in matrix) {
foreach(int column in row) {
Console.Write($"{column} ");
}
Console.Write("\n");
}
}
//더하기 메소드
static public Matrix Plus(Matrix m1, Matrix m2) {
Matrix result;
/*
행렬의 크기들을 비교하여 더 작은 행과 열에 맞추어 계산한다.
Ex) 1 2 3 3 4 4 6 3
4 5 6 + 1 2 = 5 7 6
7 8 9 7 8 9
*/
if(m1.row > m2.row && m1.column > m2.column) {
result = PlusMatrix(m1, m2, m2.row, m2.column);
}
else if(m1.row <= m2.row) {
result = PlusMatrix(m1, m2, m1.row, m2.column);
}
else if(m1.column <= m2.column) {
result = PlusMatrix(m1, m2, m2.row, m1.column);
}
else {
result = PlusMatrix(m1, m2, m1.row, m1.column);
}
return result;
}
//빼기 메소드
/*
행렬의 크기들을 비교하여 더 작은 행과 열에 맞추어 계산한다.
Ex) 1 2 3 3 4 -2 -2 3
4 5 6 - 1 2 = 3 3 6
7 8 9 7 8 9
*/
static public Matrix Minus(Matrix m1, Matrix m2) {
Matrix result;
if(m1.row > m2.row && m1.column > m2.column) {
result = MinusMatrix(m1, m2, m2.row, m2.column);
}
else if(m1.row <= m2.row) {
result = MinusMatrix(m1, m2, m1.row, m2.column);
}
else if(m1.column <= m2.column) {
result = MinusMatrix(m1, m2, m2.row, m1.column);
}
else {
result = MinusMatrix(m1, m2, m1.row, m1.column);
}
return result;
}
static private Matrix PlusMatrix(Matrix m1, Matrix m2, int row, int column) {
/*
반복되는 코드들을 간추려 메소드화 시켰다.
*/
Matrix res = new Matrix(row, column);
for(int i = 0; i < row; i++) {
ArrayList m1_row = (ArrayList) m1.matrix[i];
ArrayList m2_row = (ArrayList) m2.matrix[i];
ArrayList columns = new ArrayList();
for(int j = 0; j < column; j++) {
columns.Add((int)m1_row[j] + (int)m2_row[j]);
}
res.matrix.Add(columns);
}
return res;
}
static private Matrix MinusMatrix(Matrix m1, Matrix m2, int row, int column) {
/*
반복되는 코드들을 간추려 메소드화 시켰다.
*/
Matrix res = new Matrix(row, column);
for(int i = 0; i < row; i++) {
ArrayList m1_row = (ArrayList) m1.matrix[i];
ArrayList m2_row = (ArrayList) m2.matrix[i];
ArrayList columns = new ArrayList();
for(int j = 0; j < column; j++) {
columns.Add((int)m1_row[j] - (int)m2_row[j]);
}
res.matrix.Add(columns);
}
return res;
}
}
class main {
static void Main() {
Matrix m1 = new Matrix();
m1.inputRowColumn();
m1.inputMatrix();
m1.printMatrix();
Matrix m2 = new Matrix();
m2.inputRowColumn();
m2.inputMatrix();
m2.printMatrix();
Matrix m3 = Matrix.Plus(m1, m2);
m3.printMatrix();
Matrix m4 = Matrix.Minus(m1, m2);
m4.printMatrix();
}
}
}
결과값 :
728x90
반응형
'제작 > 기타 프로그램' 카테고리의 다른 글
[C#] 행렬 계산기 (6) - 전체 코드 (0) | 2020.10.21 |
---|---|
[C#] 행렬 계산기 (5) - 곱셈 계산 (0) | 2020.10.21 |
[C#] 행렬 계산기 (4) - 이차원 배열로 더 간단하게 표현 (0) | 2020.10.20 |
[C#] 행렬 계산기 (3) - 대리 연산자를 활용하여 함축화 (0) | 2020.10.15 |
[C#] 행렬 계산기 (1) - 행렬 생성 및 입력 출력 (0) | 2020.10.09 |