Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package matrix;
- import java.util.Scanner;
- import static java.lang.System.out;
- import static java.lang.System.err;
- import java.math.BigInteger;
- import java.util.ArrayList;
- import java.util.InputMismatchException;
- public class mahterfucker extends output {
- static Scanner sc = new Scanner(System.in);
- public static void main(String[] args) {
- ArrayList<Matrix> arrlist = new ArrayList<Matrix>();
- arrlist.add(new Matrix(0,0));
- arrlist.add(new Matrix(0,0));
- p("1.add Matrix\n"+
- "2.subtract Matrix\n");
- p("Choose action:");
- try{
- Byte i=sc.nextByte();
- if(i==1)
- {
- p(""+run(arrlist.get(0),i));
- }
- else if(i==2)
- {
- p(""+run(arrlist.get(1),i));
- }
- else
- {
- p("pls enter positive integer 1 or 2");
- }
- }catch(InputMismatchException e)
- {
- p("ERROR"+e);
- System.exit(0);
- }
- }
- static public int inputrow()
- {
- int row;
- try{
- p("׺°”˜`”°º× ׺°”˜`”°º×\n");
- p("ENTER ROW:");
- row = sc.nextInt();
- return row;
- }catch(InputMismatchException e)
- {
- p("ERROR:"+e);
- System.exit(1);
- }
- return 0;
- }
- static public int inputcol()
- {
- int col;
- try{
- p("ENTER Colum:");
- col = sc.nextInt();
- p("׺°”˜`”°º× ׺°”˜`”°º×\n");
- return col;
- }catch(InputMismatchException e)
- {
- p("ERROR:"+e);
- System.exit(1);
- }
- return 0;
- }
- static public Matrix run(Matrix g,byte r) {
- int number;
- int row=inputrow();
- int col=inputcol();
- p("\n.•*´¨`*•.¸¸.•*´¨`*Matrix A.¸¸.•.¸¸.•*´¨`*•.¸¸.\n");
- Matrix m = new Matrix(row, col);
- MyInteger intOne = null;
- MyInteger inttwo = null;
- for (int i = 0; i < row; i++) {
- for (int j = 0; j < col; j++) {
- p("Position" + "[" + i + "]" + "[" + j + "]: ");
- try{
- number = sc.nextInt();
- intOne = new MyInteger(number);
- m.set(i, j, intOne);
- }catch(InputMismatchException e)
- {
- p("ERROR:"+e);
- System.exit(1);
- }
- }
- }
- row=inputrow();
- col=inputcol();
- p("\n.•*´¨`*•.¸¸.•*´¨`*Matrix B.¸¸.•.¸¸.•*´¨`*•.¸¸.\n");
- Matrix n = new Matrix(row, col);
- for (int i = 0; i < row; i++) {
- for (int j = 0; j < col; j++) {
- p("Position" + "[" + i + "]" + "[" + j + "]: ");
- number = sc.nextInt();
- inttwo = new MyInteger(number);
- n.set(i, j, inttwo);
- }
- }
- if(r==1)
- return m.add(n);
- else if(r==2)
- return m.subtract(n);
- return g;
- }
- }
- class Matrix extends output {
- private int row;
- private int col;
- private Object[][] m;
- Matrix(int row, int col) {
- if (row < 0 || col < 0) {
- err.println("Row and col must be >=0");
- System.exit(1);
- }
- this.row = row;
- this.col = col;
- m = new Object[row][col];
- }
- private void cheakindex(int i, int j) {
- if (i < 0 || j < 0 || i > row || j > col) {
- err.println("Invalid index");
- System.exit(1);
- }
- }
- void set(int r, int c, Object value) {
- cheakindex(r, c);
- m[r][c] = value;
- }
- public Matrix add(Matrix n) {
- //MyInteger g = new MyInteger(0);
- if (row != n.row || col != n.col) {
- err.println("Size of matriced do not match");
- System.exit(1);
- }
- Matrix p = new Matrix(row, col);
- p("\n.•*´¨`*•.¸¸.•*´¨`*Matrix add.¸¸.•.¸¸.•*´¨`*•.¸¸.\n");
- for (int i = 0; i < row; i++) {
- for (int j = 0; j < col; j++) {
- p.m[i][j] = ((MyInteger) m[i][j]).add(n.m[i][j]);
- }
- }
- return p;
- }
- public Matrix subtract(Matrix n) {
- if (row != n.row || col != n.col) {
- err.println("Size of matrices do not math");
- System.exit(1);
- }
- Matrix p = new Matrix(row, col);
- p("\n.•*´¨`*•.¸¸.•*´¨`*Matrix subtract.¸¸.•.¸¸.•*´¨`*•.¸¸.\n");
- for (int i = 0; i < row; i++) {
- for (int j = 0; j < col; j++) {
- p.m[i][j] = ((Computable) m[i][j]).subtract(n.m[i][j]);
- }
- }
- return p;
- }
- @Override
- public String toString() {
- StringBuffer buf = new StringBuffer();
- for (int i = 0; i < row; i++) {
- for (int j = 0; j < col; j++) {
- buf.append(m[i][j].toString() + "\t");
- }
- buf.append("\n");
- }
- return new String(buf);
- }
- }
- class MyInteger implements Computable {
- private int value;
- public MyInteger(int value) {
- this.value = value;
- }
- @Override
- public String toString() {
- return Integer.toString(value);
- }
- @Override
- public Object add(Object m) {
- return new MyInteger(value + ((MyInteger) m).value);
- }
- @Override
- public Object subtract(Object m) {
- return new MyInteger(value - ((MyInteger) m).value);
- }
- public Object muti(Object m) {
- return new MyInteger(value * ((MyInteger) m).value);
- }
- }
- interface Computable {
- public Object add(Object thisObject);
- public Object subtract(Object thisObject);
- //public Object muti(Object thisObject);
- }
- class output {
- public static void p(String input) {
- out.print(input);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment