#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
typedef struct marix_type
{
char* Name;
int Rows;
int Cols;
float* Values;
} MATRIX;
typedef struct matrix_list_type
{
int Num_matrixes;
MATRIX** Matrixes;
} MATRIX_LIST;
MATRIX* new_matrix(MATRIX_LIST* matrix_list, char* name, int rows, int cols)
{
MATRIX* matrix;
matrix = (MATRIX*)malloc(sizeof(MATRIX));
matrix->Name = (char*)malloc(strlen(name) + 1);
strcpy(matrix->Name, name);
matrix->Rows = rows;
matrix->Cols = cols;
matrix->Values = (float*)malloc(rows * cols * sizeof(float));
if (matrix_list->Num_matrixes == 0)
matrix_list->Matrixes = (MATRIX**)malloc(sizeof(MATRIX*));
else
matrix_list->Matrixes =
(MATRIX**)realloc(matrix_list->Matrixes, (matrix_list->Num_matrixes + 1) * sizeof(MATRIX*));
matrix_list->Matrixes[matrix_list->Num_matrixes] = matrix;
matrix_list->Num_matrixes++;
return matrix;
}
int is_epmty(MATRIX_LIST* matrix_list)
{
if (matrix_list->Num_matrixes == 0)
{
puts("List of matrixes is empty!");
return 1;
}
return 0;
}
void show_matrix_list(MATRIX_LIST* matrix_list)
{
int i;
for(i=0; i<matrix_list->Num_matrixes; i++)
printf("%s(%dx%d) ", matrix_list->Matrixes[i]->Name,
matrix_list->Matrixes[i]->Rows, matrix_list->Matrixes[i]->Cols);
}
MATRIX* search_matrix(MATRIX_LIST* matrix_list, char* name)
{
int i;
for(i=0; i<matrix_list->Num_matrixes; i++)
if (!strcmp(matrix_list->Matrixes[i]->Name, name))
return matrix_list->Matrixes[i];
return NULL;
}
void show_matrix(MATRIX_LIST* matrix_list)
{
char name[10];
MATRIX* matrix;
int i, j;
printf("indicate the name of variable:\n> ");
gets(name);
matrix = search_matrix(matrix_list, name);
if (!matrix)
{
puts("Variable not found!");
return;
}
printf("%s:\n", matrix->Name);
printf("%d x %d\n", matrix->Rows, matrix->Cols);
for(i=0; i<matrix->Rows; i++)
{
for(j=0; j<matrix->Cols; j++)
printf(" %.4f", matrix->Values[i * matrix->Cols + j]);
putchar('\n');
}
}
void add_new_matrix(MATRIX_LIST* matrix_list)
{
char name[10], dimensions[10];
int rows, cols;
char* ptr;
MATRIX* matrix;
int i, j;
printf("What are the dimensions of the matrix?\n(number of lines and columns separated by a space)\n> ");
gets(dimensions);
ptr = strchr(dimensions, ' ');
if (!ptr)
{
puts("Wrong dimensions!");
return;
}
ptr++;
rows = atoi(dimensions);
cols = atoi(ptr);
if ((rows < 1) || (cols < 1))
{
puts("Wrong dimensions!");
return;
}
printf("indicate the name of variable:\n> ");
gets(name);
if (search_matrix(matrix_list, name))
{
puts("This name is already in used!");
return;
}
matrix = new_matrix(matrix_list, name, rows, cols);
srand(time(NULL));
for(i=0; i<rows; i++)
for(j=0; j<cols; j++)
matrix->Values[i * cols + j] = (float)rand() / RAND_MAX;
}
void delete_matrix(MATRIX_LIST* matrix_list)
{
char name[10];
MATRIX* matrix;
MATRIX** matrix_list_temp;
int i, j;
printf("indicate the name of variable:\n> ");
gets(name);
matrix = search_matrix(matrix_list, name);
if (!matrix)
{
puts("Variable not found!");
return;
}
printf("remove var: %s(%dx%d)\n", matrix->Name, matrix->Rows, matrix->Cols);
free(matrix->Values);
free(matrix->Name);
if (matrix_list->Num_matrixes == 1)
matrix_list_temp = NULL;
else
{
matrix_list_temp = (MATRIX**)malloc((matrix_list->Num_matrixes - 1) * sizeof(MATRIX*));
j = 0;
for(i=0; i<matrix_list->Num_matrixes; i++)
{
if (matrix_list->Matrixes[i] == matrix)
continue;
matrix_list_temp[j] = matrix_list->Matrixes[i];
j++;
}
}
free(matrix);
free(matrix_list->Matrixes);
matrix_list->Matrixes = matrix_list_temp;
matrix_list->Num_matrixes--;
}
void transponse_matrix(MATRIX_LIST* matrix_list)
{
char name[10];
MATRIX *matrix, *trans_matrix;
int i, j;
printf("indicate the name of variable:\n> ");
gets(name);
matrix = search_matrix(matrix_list, name);
if (!matrix)
{
puts("Variable not found!");
return;
}
printf("indicate the name of variable for the result:\n> ");
gets(name);
if (search_matrix(matrix_list, name))
{
puts("This name is already in use!");
return;
}
trans_matrix = new_matrix(matrix_list, name, matrix->Cols, matrix->Rows);
for(i=0; i<trans_matrix->Rows; i++)
for(j=0; j<trans_matrix->Cols; j++)
trans_matrix->Values[i * trans_matrix->Cols + j] = matrix->Values[i + trans_matrix->Rows * j];
}
void sum_matrixes(MATRIX_LIST* matrix_list)
{
char name[10];
MATRIX *matrix1, *matrix2, *res_matrix;
int i, j;
printf("indicate the name of variable: (1st operated):\n> ");
gets(name);
matrix1 = search_matrix(matrix_list, name);
if (!matrix1)
{
puts("Variable not found!");
return;
}
printf("indicate the name of variable: (2nd operated):\n> ");
gets(name);
matrix2 = search_matrix(matrix_list, name);
if (!matrix2)
{
puts("Variable not found!");
return;
}
if ((matrix1->Rows != matrix2->Rows) || (matrix1->Cols != matrix2->Cols))
{
puts("Matrixes have different dimensions!");
return;
}
printf("indicate the name of variable for the result:\n> ");
gets(name);
if (search_matrix(matrix_list, name))
{
puts("This name is already in use!");
return;
}
res_matrix = new_matrix(matrix_list, name, matrix1->Rows, matrix1->Cols);
for(i=0; i<res_matrix->Rows; i++)
for(j=0; j<res_matrix->Cols; j++)
res_matrix->Values[i * res_matrix->Cols + j] =
matrix1->Values[i * res_matrix->Cols + j] + matrix2->Values[i * res_matrix->Cols + j];
}
void read_from_file(char filename[20], MATRIX_LIST* matrix_list)
{
FILE* f;
MATRIX* matrix;
char line[1024];
int i, j, rows, cols;
char *ptr, *ptr1;
f = fopen(filename, "r");
if (!f)
return;
while (!feof(f))
{
fgets(line, 1024, f);
ptr = strchr(line, ';');
if (!ptr)
continue;
*ptr = 0;
ptr1 = ptr + 1;
ptr = strchr(ptr1, ';');
if (!ptr)
continue;
rows = atoi(ptr1);
ptr1 = ptr + 1;
ptr = strchr(ptr1, ';');
if (!ptr)
continue;
cols = atoi(ptr1);
ptr1 = ptr + 1;
matrix = new_matrix(matrix_list, line, rows, cols);
for(i=0; i<rows; i++)
for(j=0; j<cols; j++)
{
ptr = strchr(ptr1, ';');
if (!ptr)
continue;
matrix->Values[i * cols + j] = atof(ptr1);
ptr1 = ptr + 1;
}
}
fclose(f);
}
void write_to_file(char filename[20], MATRIX_LIST* matrix_list)
{
FILE* f;
int k, i, j;
f = fopen(filename, "w");
for(k=0; k<matrix_list->Num_matrixes; k++)
{
fprintf(f, "%s;%d;%d;", matrix_list->Matrixes[k]->Name,
matrix_list->Matrixes[k]->Rows, matrix_list->Matrixes[k]->Cols);
for(i=0; i<matrix_list->Matrixes[k]->Rows; i++)
for(j=0; j<matrix_list->Matrixes[k]->Cols; j++)
fprintf(f, "%.4f;", matrix_list->Matrixes[k]->Values[i * matrix_list->Matrixes[k]->Cols + j]);
fprintf(f, "\n");
}
fclose(f);
}
void free_matrix_list(MATRIX_LIST* matrix_list)
{
int i;
if (matrix_list->Num_matrixes == 0)
return;
for(i=0; i<matrix_list->Num_matrixes; i++)
{
free(matrix_list->Matrixes[i]->Values);
free(matrix_list->Matrixes[i]->Name);
free(matrix_list->Matrixes[i]);
}
free(matrix_list->Matrixes);
}
int main()
{
MATRIX_LIST matrix_list;
char filename[] = "matrizes.dat";
int choice;
char str[5];
puts("$ matprog");
matrix_list.Num_matrixes = 0;
read_from_file(filename, &matrix_list);
if (matrix_list.Num_matrixes)
show_matrix_list(&matrix_list);
for (;;)
{
puts("\nShow matixes..................1");
puts("Show matrix...................2");
puts("Remove matrix.................3");
puts("New arbitrary matrix..........4");
puts("Transponse....................5");
puts("Sum...........................6");
puts("Exit..........................x");
putchar('>');
gets(str);
if ((str[0] == 'x') || (str[0] == 'X'))
break;
choice = atoi(str);
switch (choice)
{
case 1:
if (!is_epmty(&matrix_list))
show_matrix_list(&matrix_list);
break;
case 2:
if (!is_epmty(&matrix_list))
show_matrix(&matrix_list);
break;
case 3:
if (!is_epmty(&matrix_list))
delete_matrix(&matrix_list);
break;
case 4:
add_new_matrix(&matrix_list);
break;
case 5:
if (!is_epmty(&matrix_list))
transponse_matrix(&matrix_list);
break;
case 6:
if (!is_epmty(&matrix_list))
sum_matrixes(&matrix_list);
break;
default:
puts("Wrong choice!");
}
}
write_to_file(filename, &matrix_list);
free_matrix_list(&matrix_list);
return 0;
}