Загрузка данных
#include <stdio.h>
#include <stdlib.h>
int input_matrix(int ***matrix, int *rows, int *cols);
void output_matrix(int **matrix, int rows, int cols);
int **allocate_matrix(int rows, int cols);
void free_matrix(int **matrix, int rows);
int **sum_matrix(int **a, int **b, int rows, int cols);
int **multiply_matrix(int **a, int rows_a, int cols_a, int **b, int rows_b, int cols_b);
int **transpose_matrix(int **matrix, int rows, int cols);
int main(void) {
int operation;
int rows_a, cols_a, rows_b, cols_b;
int **a = NULL;
int **b = NULL;
int **result = NULL;
int result_rows = 0;
int result_cols = 0;
if (scanf("%d", &operation) != 1 || operation < 1 || operation > 3 ||
!input_matrix(&a, &rows_a, &cols_a)) {
printf("n/a");
return 1;
}
if (operation == 1 || operation == 2) {
if (!input_matrix(&b, &rows_b, &cols_b)) {
printf("n/a");
free_matrix(a, rows_a);
return 1;
}
}
if (operation == 1 && rows_a == rows_b && cols_a == cols_b) {
result = sum_matrix(a, b, rows_a, cols_a);
result_rows = rows_a;
result_cols = cols_a;
} else if (operation == 2 && cols_a == rows_b) {
result = multiply_matrix(a, rows_a, cols_a, b, rows_b, cols_b);
result_rows = rows_a;
result_cols = cols_b;
} else if (operation == 3) {
result = transpose_matrix(a, rows_a, cols_a);
result_rows = cols_a;
result_cols = rows_a;
}
if (result == NULL) {
printf("n/a");
} else {
output_matrix(result, result_rows, result_cols);
}
free_matrix(a, rows_a);
free_matrix(b, operation == 1 || operation == 2 ? rows_b : 0);
free_matrix(result, result_rows);
return 0;
}
int input_matrix(int ***matrix, int *rows, int *cols) {
int result = 1;
if (scanf("%d %d", rows, cols) != 2 || *rows <= 0 || *cols <= 0) {
result = 0;
}
if (result) {
*matrix = allocate_matrix(*rows, *cols);
if (*matrix == NULL) {
result = 0;
}
}
for (int i = 0; result && i < *rows; i++) {
for (int j = 0; result && j < *cols; j++) {
if (scanf("%d", *(*matrix + i) + j) != 1) {
result = 0;
}
}
}
if (!result && *matrix != NULL) {
free_matrix(*matrix, *rows);
*matrix = NULL;
}
return result;
}
int **allocate_matrix(int rows, int cols) {
int **matrix = malloc(rows * sizeof(int *));
int *data = NULL;
if (matrix != NULL) {
data = malloc(rows * cols * sizeof(int));
if (data == NULL) {
free(matrix);
matrix = NULL;
} else {
for (int i = 0; i < rows; i++) {
*(matrix + i) = data + i * cols;
}
}
}
return matrix;
}
void free_matrix(int **matrix, int rows) {
if (matrix != NULL) {
if (rows > 0) {
free(*matrix);
}
free(matrix);
}
}
void output_matrix(int **matrix, int rows, int cols) {
for (int i = 0; i < rows; i++) {
if (i > 0) {
printf("\n");
}
for (int j = 0; j < cols; j++) {
if (j > 0) {
printf(" ");
}
printf("%d", *(*(matrix + i) + j));
}
}
}
int **sum_matrix(int **a, int **b, int rows, int cols) {
int **result = allocate_matrix(rows, cols);
if (result != NULL) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
*(*(result + i) + j) = *(*(a + i) + j) + *(*(b + i) + j);
}
}
}
return result;
}
int **multiply_matrix(int **a, int rows_a, int cols_a, int **b, int rows_b, int cols_b) {
int **result = allocate_matrix(rows_a, cols_b);
if (result != NULL) {
for (int i = 0; i < rows_a; i++) {
for (int j = 0; j < cols_b; j++) {
*(*(result + i) + j) = 0;
for (int k = 0; k < cols_a; k++) {
*(*(result + i) + j) += *(*(a + i) + k) * *(*(b + k) + j);
}
}
}
}
(void)rows_b;
return result;
}
int **transpose_matrix(int **matrix, int rows, int cols) {
int **result = allocate_matrix(cols, rows);
if (result != NULL) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
*(*(result + j) + i) = *(*(matrix + i) + j);
}
}
}
return result;
}