728x90
반응형
코드 :
using System;
namespace matrixNamespace {
class Matrix {
int row;
int column;
static int[][] matrix;
public Matrix() {
Console.WriteLine("Matrix Create...");
Console.Write("input the Row, Column (ex. 3 3) : ");
string input = Console.ReadLine();
string[] row_column = input.Split(' ');
this.row = Convert.ToInt32(row_column[0]);
this.column = Convert.ToInt32(row_column[1]);
matrix = new int[row][];
makeMatrix();
printMatrix();
}
public Matrix(int row, int column) {
this.row = row;
this.column = column;
Array.Resize<int>(ref matrix[0], row);
}
static public void makeMatrix() {
for(int i = 0; i < matrix.Length; i++) {
Console.Write("input the columns (ex.1 2 3) : ");
string line = Console.ReadLine();
string[] columns = line.Split(' ');
int[] columnsInt = new int[columns.Length];
for (int j = 0; j < columns.Length; j++) {
columnsInt[j] = Convert.ToInt32(columns[j]);
}
matrix[i] = columnsInt;
}
}
static public void printMatrix() {
for(int i = 0; i < matrix.Length; i++) {
for(int j = 0; j < matrix[i].Length; j++) {
Console.Write($"{matrix[i][j]} ");
}
Console.Write("\n");
}
}
}
class matrixCode {
static void Main() {
Matrix m1 = new Matrix();
}
}
}
메소드 하나하나 봐보자
public Matrix() {
Console.WriteLine("Matrix Create...");
Console.Write("input the Row, Column (ex. 3 3) : ");
string input = Console.ReadLine();
string[] row_column = input.Split(' ');
this.row = Convert.ToInt32(row_column[0]);
this.column = Convert.ToInt32(row_column[1]);
matrix = new int[row][];
makeMatrix();
printMatrix();
}
1. 생성자
행과 열을 입력하고자 한다.
Console.Write(...)
string input = Console.ReadLine();
으로 행과 열을 입력받는다.
그러고 Split 메서드를 사용하여 분할하여 row_column 배열에 할당한다.
그러고 Matrix 클래스 내의 인스턴스인 row와 column에 할당한다.
this.row = Convert.ToInt32(row_column[0]);
this.column = Convert.ToInt32(row_column[1]);
부분이 다음과 같다.
그러고 row만큼의 matrix를 생성한다.
그러고 matrix, 행렬을 만들기 위한 메소드인 makeMatrix를 실행한다.
static public void makeMatrix() {
for(int i = 0; i < matrix.Length; i++) {
Console.Write("input the columns (ex.1 2 3) : ");
string line = Console.ReadLine();
string[] columns = line.Split(' ');
int[] columnsInt = new int[columns.Length];
for (int j = 0; j < columns.Length; j++) {
columnsInt[j] = Convert.ToInt32(columns[j]);
}
matrix[i] = columnsInt;
}
}
2. makeMatrix 메서드
위 생성자에서 만들어둔 matrix의 일차원 배열, row 값만큼 For문을 이용해 반복한다.
그 반복문 안에서는 열, column 값을 받는다.
columnsInt라는 배열 변수를 새로 만들고 이를 matrix[i]에 할당하면서 열의 값들을 matrix에 할당한다.
static public void printMatrix() {
for(int i = 0; i < matrix.Length; i++) {
for(int j = 0; j < matrix[i].Length; j++) {
Console.Write($"{matrix[i][j]} ");
}
Console.Write("\n");
}
}
3. printMatrix 메서드 (출력 함수)
이중 반복문(for문)을 사용하여 출력한다. 이는 나중에 곱셉에서도 비슷하게 활용될 것이다.
이상으로 마치겠다. 다음에는 더하기, 빼기, 곱하기까지 되는 계산기를 만들어 볼 것이다.
많이들 기대바랍니다!
728x90
반응형
'제작 > 기타 프로그램' 카테고리의 다른 글
[C#] 행렬 계산기 (6) - 전체 코드 (0) | 2020.10.21 |
---|---|
[C#] 행렬 계산기 (5) - 곱셈 계산 (0) | 2020.10.21 |
[C#] 행렬 계산기 (3) - 대리 연산자를 활용하여 함축화 (0) | 2020.10.15 |
[C#] 행렬 계산기(2) - 더하기, 빼기 메소드 (0) | 2020.10.11 |
[C#] 행렬 계산기 (1) - 행렬 생성 및 입력 출력 (0) | 2020.10.09 |