To Jura_: nemas nejake skusenosti z valgrindom?? asi ma nema rad vzdy pri chybovej hlaske mi hlasi ze mi nevycisti vsetku pamat dam ti tu cely zdrojak :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
enum Function { addon,multi,monotonous,submatrix,spiralX,wormX,};
typedef struct Matrix
{
int line;
int column;
int **matrix;
}TMatrix;
typedef struct params
{
int function;
char *source1;
char *source2;
}PParams;
PParams GParams(int argc, char *argv[]);
void memoryfree(TMatrix *matrix);
int read_matrix(TMatrix *matrix, char *a);
int **create_matrix(int line, int column);
void helper(void);
int add(TMatrix *matrix1, TMatrix *matrix2);
int mult(TMatrix *matrix1, TMatrix *matrix2);
int mono(TMatrix *matrix1);
int sub(TMatrix *matrix1,TMatrix *matrix2);
int spiral(TMatrix *matrix1);
int worm(TMatrix *matrix1);
//--------------------------------------------------------------------------------------------
enum errors
{
error1,
};
void error(int code)
{
if (code == error1)
{
fprintf(stderr, "Program have been used with error!n"
"Program launch with parameter -h.n");
exit(0);
}
}
//--------------------------------------------------------------------------------------------
int main(int argc, char *argv[])
{
int first,second;
PParams params = GParams(argc, argv);
TMatrix matrix1,matrix2;
first=read_matrix(&matrix1, params.source1);
if (first == 0)
{
memoryfree(&matrix1);
exit(0);
}
if (argc == 4)
{
second=read_matrix(&matrix2, params.source2);
if (second == 0)
{
memoryfree(&matrix2);
memoryfree(&matrix1);
exit (0);
}
}
switch (params.function)
{
case addon :
add(&matrix1, &matrix2);
memoryfree(&matrix1);
memoryfree(&matrix2);
break;
case multi :
mult(&matrix1, &matrix2);
memoryfree(&matrix1);
memoryfree(&matrix2);
break;
case monotonous :
mono(&matrix1);
memoryfree(&matrix1);
break;
case submatrix :
sub(&matrix1, &matrix2);
memoryfree(&matrix1);
memoryfree(&matrix2);
break;
case spiralX :
spiral(&matrix1);
memoryfree(&matrix1);
break;
case wormX :
worm(&matrix1);
memoryfree(&matrix1);
break;
}
return 0;
}
//-----------------------------------------------------------------------------------------
PParams GParams(int argc, char *argv[])
{
PParams result=
{
.function = -1,
.source1 = 0,
.source2 = 0
};
if (argc == 2 && strcmp("-h" , argv[1])==0)
{
helper();
}
else if (argc == 2) { error(error1); }
else if (argc == 3)
{
if (strcmp("-mono", argv[1]) == 0) result.function = monotonous;
else if (strcmp("-spiral", argv[1]) == 0) result.function = spiralX;
else if (strcmp("-worm", argv[1]) == 0) result.function = wormX;
else
{
error(error1);
}
result.source1=argv[2];
}
else if (argc == 4)
{
if (strcmp("-add", argv[1])==0) result.function = addon;
else if (strcmp("-mult", argv[1])==0) result.function = multi;
else if (strcmp("-sub", argv[1]) == 0) result.function = submatrix;
else
{
error(error1);
}
result.source1=argv[2];
result.source2=argv[3];
}
return result;
}
//-----------------------------------------------------------------------------------------
int read_matrix(TMatrix *matrix, char *a)
{
FILE *fr;
if ((fr = fopen(a,"r"))==NULL)
{
fprintf(stderr, "Document %s haven't been openedn" ,a);
fclose(fr);
return 0;
}
fscanf(fr,"%d%d", &matrix->line, &matrix->column);
if ( matrix->line<=0 || matrix->column<=0)
{
fprintf(stderr,"Matrix haven't right sizen");
fclose(fr);
return 0;
}
matrix->matrix=create_matrix(matrix->line, matrix->column);
if(matrix->matrix==0)
{
fclose(fr);
exit (EXIT_FAILURE);
}
for (int lines=0; lines<matrix->line; lines++)
{
for (int columns=0; columns<matrix->column; columns++)
{
if (fscanf(fr,"%d", &matrix->matrix [lines][columns]) !=1)
{
fprintf(stderr,"Entry size file is bad!n");
fclose(fr);
exit (0);
}
}
}
if (fclose(fr)==EOF)
{
fprintf(stderr,"Document %s haven't been closedn",a);
return 0;
}
return 1;
}
//-------------------------------------------------------------------------------------------
void helper(void)
{
printf("****************************************************************n"
"Program Operations with matrixes.n"
"Autor: Martin Bodockyn"
"Program work with matrixes according to parameters,n"
"whitch are order.n"
"Use: proj3 -hn"
" proj3 par1 par2n"
" proj3 par1 par2 par3n"
"Description parameters:n"
" -h Print Help (This screen).n"
" par1 Frst parameter appointed function:n"
" par2,par3 Second and third parameter using thann"
"title of text document.n"
"n "
"Example used:n"
"SUCET: proj3 -add matrix1.txt matrix2.txtn"
"SUCIN: proj3 -mult matrix1.txt matrix2.txtn"
"SPIRALA: proj3 -spiral matrix.txtn"
"SUB: proj3 -sub matrix.txt matrix2.txtn"
"CERV: proj3 -worm matrix.txtn"
"MONOTONNOST: proj3 -mono matrix.txtn"
"****************************************************************n"
);
exit (0);
}
//-----------------------------------------------------------------------------------------
int **create_matrix(int line, int column)
{
int **matrix;
matrix = (int **) malloc(line * sizeof(int *));
if (matrix==NULL)
{
fprintf(stderr,"Need memoryn");
free (matrix);
return 0;
}
for (int i=0; i< line; i++)
{
matrix[i]=(int *) malloc(column * sizeof(int));
if (matrix[i]==NULL)
{
fprintf(stderr,"Need memoryn");
free(matrix);
return 0;
}
}
return matrix;
}
//----------------------------------------------------------------------------------------
void memoryfree(TMatrix *matrix)
{
assert(matrix != NULL);
for (int i=0; i<matrix->line; i++)
{
free(matrix->matrix[i]);
}
free(matrix->matrix);
}
//----------------------------------------------------------------------------------------
int add(TMatrix *matrix1, TMatrix *matrix2)
{
if (matrix1->line != matrix2->line || matrix2->column != matrix1->column )
{
printf("#n");
return 0;
}
printf("%d %d n", matrix1->line, matrix1->column);
for (int i=0; i<matrix1->line; i++)
{
for (int j=0; j<matrix1->column; j++)
{
printf("%d ", matrix1->matrix[i][j] + matrix2->matrix[i][j]);
}
putchar('n');
}
return 0;
}
//----------------------------------------------------------------------------------------
int mult(TMatrix *matrix1, TMatrix *matrix2)
{
if (matrix1->column != matrix2->line)
{
printf("#n");
return 0;
}
printf("%d %d n", matrix1->line, matrix2->column);
for (int i=0; i<matrix1->line; i++)
{
for (int j=0; j<matrix2->column; j++)
{
int new_element = 0;
for (int v=0; v!=matrix1->column; v++)
{
new_element += matrix1->matrix[i][v] * matrix2->matrix[v][j];
}
printf("%d ",new_element);
}
putchar('n');
}
return 0;
}
//-----------------------------------------------------------------------------------------
int mono(TMatrix *matrix1)
{
int result=0;
if (matrix1->matrix[0][0] <= matrix1->matrix[0][1])
{
for (int l=0; l<matrix1->line; l++)
{
for (int c=0; c+1<matrix1->column; c++)
{
if (matrix1->matrix[l][c] <= matrix1->matrix[l][c+1])
{
result=1;
}
else
{
printf("#n");
return 0;
}
}
}
for (int c=0; c<matrix1->column; c++)
{
for (int l=0; l+1<matrix1->line; l++)
{
if (matrix1->matrix[l][c] <= matrix1->matrix[l+1][c])
{
result=1;
}
else
{
printf("#n");
return 0;
}
}
}
}
else if (matrix1->matrix[0][0] >= matrix1->matrix[0][1])
{
for (int l=0; l<matrix1->line; l++)
{
for (int c=0; c<matrix1->column; c++)
{
if (matrix1->matrix[l][c] >= matrix1->matrix[l][c+1])
{
result=1;
}
else
{
printf("#n");
return 0;
}
}
}
for (int c=0; c<matrix1->column; c++)
{
for (int l=0; l+1<matrix1->line; l++)
{
if (matrix1->matrix[l][c] >= matrix1->matrix[l+1][c])
{
result=1;
}
else
{
printf("#n");
return 0;
}
}
}
}
else
{
printf("#n");
return 0;
}
if (result==1) printf("1n");
return 0;
}
//----------------------------------------------------------------------------------------
int sub(TMatrix *matrix1,TMatrix *matrix2)
{
if (matrix1->matrix[0][0]==matrix2->matrix[0][0])
return 0;
}
//----------------------------------------------------------------------------------------
int spiral(TMatrix *matrix1)
{
int result=0,i;
while(1)
{
if(result>(matrix1->column-1-result)) break;
for (i=result;i<=(matrix1->column-1-result);i++)
{
printf("%d ",matrix1->matrix[matrix1->line-1-result][i]);
}
if((matrix1->line-2-result)<result) break;
for (i=matrix1->line-2-result;i>=result;i--)
{
printf("%d ",matrix1->matrix[i][matrix1->column-1-result]);
}
if((matrix1->column-2-result)<result) break;
for (i=(matrix1->column-2-result);i>=result;i--)
{
printf("%d ",matrix1->matrix[result][i]);
}
if((result+1)>(matrix1->line-2-result)) break;
for (i=(result+1);i<=(matrix1->line-2-result);i++)
{
printf("%d ",matrix1->matrix[i][result]);
}
result+=1;
}
return 0;
}
//----------------------------------------------------------------------------------------
int worm(TMatrix *matrix1)
{
int j,times,middle,i,lines,columns;
times=matrix1->matrix[0][0];
if (times<0) (times+=matrix1->line*matrix1->column);
for(j=0;j<times;j++)
{
lines=matrix1->line;
columns=matrix1->column;
if ((matrix1->line)%2==0)
{
middle=matrix1->matrix[lines-1][0];
} else middle=matrix1->matrix[lines-1][columns-1];
while (lines-1>=0)
{
if ((matrix1->line)%2==0||lines!=matrix1->line)
{
for (i=0;i<=(columns-2);i++)
{
matrix1->matrix[lines-1][i]=
matrix1->matrix[lines-1][i+1];
}
matrix1->matrix[lines-1][columns-1]=
matrix1->matrix[lines-2][columns-1];
lines--;
}
for (i=columns-1;i>=1;i--)
{
matrix1->matrix[lines-1][i]=
matrix1->matrix[lines-1][i-1];
}
if (lines-2>0)
{
matrix1->matrix[lines-1][0]=
matrix1->matrix[lines-2][0];
}
lines--;
}
matrix1->matrix[0][0]=middle;
}
printf("%d %d n", matrix1->line,matrix1->column);
for(int i=0; i<matrix1->line; i++)
{
for(int j=0; j<matrix1->column;j++)
{
printf("%d ", matrix1->matrix[i][j]);
}
putchar('n');
}
return 0;
}
nie je cely dorobeny ale ak mu zadas zle zadanie tak zle dealokuje pamat, ak budes mat nejasnosti cele ti to okomentujem toto je holy kod