-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
153 lines (141 loc) · 3.92 KB
/
Main.java
File metadata and controls
153 lines (141 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import java.util.*;
import java.lang.*;
public class Main{
static String matSub(int matrix1[][], int matrix2[][], int rows, int cols) {
int matrixToBePrinted[][] = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
matrixToBePrinted[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
String toBeReturned = "";
for (int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
System.out.print(matrixToBePrinted[i][j] + " ");
toBeReturned += (matrixToBePrinted[i][j] + " ");
}
System.out.println("");
toBeReturned += ("\n");
}
return toBeReturned;
}
static String matAdd(int matrix1[][], int matrix2[][], int rows, int cols) {
int matrixToBePrinted[][] = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
matrixToBePrinted[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
String toBeReturned = "";
for (int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
System.out.print(matrixToBePrinted[i][j] + " ");
toBeReturned += (matrixToBePrinted[i][j] + " ");
}
System.out.println("");
toBeReturned += ("\n");
}
return toBeReturned;
}
static String calculator() {
double a, b, result;
System.out.println("Enter two numbers:");
Scanner inp = new Scanner(System.in);
a = inp.nextDouble();
b = inp.nextDouble();
System.out.println("Enter operator:");
inp.nextLine();
String op = inp.nextLine();
if(op.equals("+")) {
System.out.println(a + b);
result = a + b;
} else if (op.equals("-")) {
System.out.println(a - b);
result = a - b;
} else if (op.equals("*")) {
System.out.println(a * b);
result = a * b;
} else if (op.equals("/")) {
System.out.println(a / b);
result = a / b;
} else {
System.out.println("Invalid operation!");
return "";
}
return String.format ("%f %s %f = %f\n", a, op, b, result);
}
static int[][] matRead(int rows, int cols) {
int matrix[][] = new int [rows][cols];
Scanner inp = new Scanner(System.in);
System.out.println(
"Enter matrix in the form\n" +
"a b c\n" +
"d e f\n" +
"For a 2 by 3 matrix, for example"
);
String values = "";
for(int row = 0; row != rows; row++) {
values = inp.nextLine();
Scanner valueReader = new Scanner (values);
for(int col = 0; col != cols; col++) {
matrix[row][col] = valueReader.nextInt();
}
valueReader.close();
}
return matrix;
}
static String matCalc() {
System.out.println("Enter dimensions of matrices, rows first(Both matrices need same dimensions).");
Scanner inp = new Scanner(System.in);
int rows, cols;
rows = inp.nextInt();
cols = inp.nextInt();
int matrix1[][] = new int [rows][cols];
matrix1 = matRead(rows, cols);
int matrix2[][] = new int [rows][cols];
matrix2 = matRead(rows, cols);
inp.nextLine();
System.out.println("Enter operation to be performed:\n" +
"+" +
" -"
);
String operation = inp.nextLine();
if(operation.equals("+")) {
return matAdd(matrix1, matrix2, rows, cols);
} else if (operation.equals("-")) {
return matSub(matrix1, matrix2, rows, cols);
} else {
System.out.println("Invalid operation!");
}
return "";
}
public static void main(String[] args) {
String history = "";
Scanner inp = new Scanner(System.in);
while(true) {
System.out.println(
"Enter \"h\" for history," +
"\"m\" for maths operation," +
"\"mat\" for matrix operation," +
"\"x\" to exit."
);
String operation = inp.nextLine();
if (operation.equals("h")) {
if (history.equals("")) {
System.out.println("Nothing calculated!");
} else {
System.out.println(history);
}
} else if (operation.equals("m")) {
history += (calculator());
} else if (operation.equals("mat")) {
history += (matCalc());
} else if (operation.equals("x")) {
break;
} else {
System.out.println("Invalid operation!");
}
}
inp.close();
}
}