Загрузка данных
#include <stdio.h>
#include <stdlib.h>
void free_matrix(int **matrix, int rows) {
for (int i = 0; i < rows; i++) free(matrix[i]);
free(matrix);
}
int** allocate_matrix(int rows, int cols) {
int **matrix = (int**)malloc(rows * sizeof(int*));
if (matrix == NULL) return NULL;
for (int i = 0; i < rows; i++) {
matrix[i] = (int*)malloc(cols * sizeof(int));
if (matrix[i] == NULL) {
for (int j = 0; j < i; j++) free(matrix[j]);
free(matrix);
return NULL;
}
}
return matrix;
}
void input_matrix(int **matrix, int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
}
void print_matrix(int **matrix, int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d", matrix[i][j]);
if (j < cols - 1) printf(" ");
}
if (i < rows - 1) printf("\n");
}
}
int main() {
int operation;
if (scanf("%d", &operation) != 1 || operation < 1 || operation > 3) {
printf("n/a");
return 0;
}
if (operation == 1) {
// Сложение
int rows1, cols1, rows2, cols2;
if (scanf("%d %d", &rows1, &cols1) != 2 || rows1 <= 0 || cols1 <= 0) {
printf("n/a");
return 0;
}
if (scanf("%d %d", &rows2, &cols2) != 2 || rows2 <= 0 || cols2 <= 0) {
printf("n/a");
return 0;
}
if (rows1 != rows2 || cols1 != cols2) {
printf("n/a");
return 0;
}
int **m1 = allocate_matrix(rows1, cols1);
int **m2 = allocate_matrix(rows2, cols2);
if (m1 == NULL || m2 == NULL) {
if (m1) free_matrix(m1, rows1);
if (m2) free_matrix(m2, rows2);
printf("n/a");
return 0;
}
input_matrix(m1, rows1, cols1);
input_matrix(m2, rows2, cols2);
int **result = allocate_matrix(rows1, cols1);
if (result == NULL) {
free_matrix(m1, rows1);
free_matrix(m2, rows2);
printf("n/a");
return 0;
}
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
result[i][j] = m1[i][j] + m2[i][j];
}
}
print_matrix(result, rows1, cols1);
free_matrix(m1, rows1);
free_matrix(m2, rows2);
free_matrix(result, rows1);
} else if (operation == 2) {
// Умножение
int rows1, cols1, rows2, cols2;
if (scanf("%d %d", &rows1, &cols1) != 2 || rows1 <= 0 || cols1 <= 0) {
printf("n/a");
return 0;
}
if (scanf("%d %d", &rows2, &cols2) != 2 || rows2 <= 0 || cols2 <= 0) {
printf("n/a");
return 0;
}
if (cols1 != rows2) {
printf("n/a");
return 0;
}
int **m1 = allocate_matrix(rows1, cols1);
int **m2 = allocate_matrix(rows2, cols2);
if (m1 == NULL || m2 == NULL) {
if (m1) free_matrix(m1, rows1);
if (m2) free_matrix(m2, rows2);
printf("n/a");
return 0;
}
input_matrix(m1, rows1, cols1);
input_matrix(m2, rows2, cols2);
int **result = allocate_matrix(rows1, cols2);
if (result == NULL) {
free_matrix(m1, rows1);
free_matrix(m2, rows2);
printf("n/a");
return 0;
}
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
result[i][j] = 0;
for (int k = 0; k < cols1; k++) {
result[i][j] += m1[i][k] * m2[k][j];
}
}
}
print_matrix(result, rows1, cols2);
free_matrix(m1, rows1);
free_matrix(m2, rows2);
free_matrix(result, rows1);
} else if (operation == 3) {
// Транспонирование
int rows, cols;
if (scanf("%d %d", &rows, &cols) != 2 || rows <= 0 || cols <= 0) {
printf("n/a");
return 0;
}
int **matrix = allocate_matrix(rows, cols);
if (matrix == NULL) {
printf("n/a");
return 0;
}
input_matrix(matrix, rows, cols);
int **result = allocate_matrix(cols, rows);
if (result == NULL) {
free_matrix(matrix, rows);
printf("n/a");
return 0;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[j][i] = matrix[i][j];
}
}
print_matrix(result, cols, rows);
free_matrix(matrix, rows);
free_matrix(result, cols);
}
return 0;
}