Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************
- ** Soubor: proj3
- ** Datum: 2011/23/11
- ** Autor: Vojtech Hrdina, xhrdin07@stud.fit.vutbr.cz
- ** Projekt: Iteracni vypocty, projekt c. 2 pro predmet IZP
- ** Popis: Maticove operace
- ******************************************************/
- // prace se vstupem vystupem
- #include <stdio.h>
- // obecne funkce jazyka C
- #include <stdlib.h>
- // matematicky funkce(pow,...)
- #include <math.h>
- #include <string.h>
- #define MYABS(x) ((x)<0 ? (-x) : (x))
- #define ERRCPARAMS -1
- #define ERRCNULL -2
- #define ERRCALLOC -3
- #define ERRCFORM -4
- #define STVHELP 0
- #define STVTEST 1
- #define STVADD 2
- #define STVMULT 3
- #define STVTRANS 4
- #define STVEXPR 5
- #define STVCAROM 7
- const char* ERRCMESSAGE[]=
- {
- "Nespravne parametry",
- "Soubor neexistuje",
- "Alokace se nezdarila",
- "Spatny format dat"
- };
- typedef struct TMatrix
- {
- int pocetradku;
- int pocetsloupcu;
- int **pole;
- int error;
- FILE* soubor;
- int *radky;
- }TMatrix;
- // funkce na parametry
- int loadparams(char **argc, int argv)
- {
- // parametr pro otevreni napovedy
- if(argv == 2 && (!strcmp(argc[1],"-h")))
- {
- return STVHELP;
- }
- // parametr TEST
- else if(argv == 3 && (!strcmp(argc[1],"--test")))
- {
- return STVTEST;
- }
- // parametr pro operaci scitani
- else if(argv == 4 && (!strcmp(argc[1],"--add")))
- {
- return STVADD;
- }
- // parametr pro operaci nasobeni
- else if(argv == 4 && (!strcmp(argc[1],"--mult")))
- {
- return STVMULT;
- }
- // parametr pro operaci transpozici
- else if(argv == 3 && (!strcmp(argc[1],"--trans")))
- {
- return STVTRANS;
- }
- // parametr pro operaci vyraz
- else if(argv == 4 && (!strcmp(argc[1],"--expr")))
- {
- return STVEXPR;
- }
- // parametr pro operaci kulecnik
- else if(argv == 7 && (!strcmp(argc[1],"--carom")))
- {
- return STVCAROM;
- }
- else
- {
- return ERRCPARAMS;
- }
- }
- // funkce na otevreni souboru
- void openfile(char* jmeno, TMatrix *matice)
- {
- int Xradku;
- int Ysloupcu;
- FILE* soubor;
- soubor = fopen(jmeno,"r");
- if (soubor == NULL)
- {
- matice->error=ERRCNULL;
- return;
- }
- // a nacteni poctu radku a sloupcu
- if(fscanf (soubor,"%d %d", &Ysloupcu, &Xradku) != 2)
- {
- matice->error=ERRCFORM;
- }
- matice->soubor=soubor;
- matice->pocetsloupcu=Ysloupcu;
- matice->pocetradku=Xradku;
- return;
- }
- // alokace pameti
- void alokace(TMatrix * matice)
- {
- int i;
- matice->pole= (int** ) malloc (matice->pocetradku*sizeof (int *));
- if(matice->error == 0)
- {
- for (i = 0; i < matice-> pocetradku; i++)
- {
- matice->pole[i] = (int* ) malloc(matice->pocetsloupcu * sizeof(int));
- if(matice->pole[i] == NULL)
- { // pri chybe dealokuje pamet
- for(i = i - 1; i >= 0; i--)
- {
- free(matice->pole[i]);
- }
- free(matice->pole);
- matice->error=ERRCALLOC;
- }
- else if(matice->error != 0)
- {
- break;
- }
- }
- }
- if(matice->pole == NULL)
- {
- matice->error= ERRCALLOC;
- }
- return;
- }
- // dealokace pameti
- void dealokace(TMatrix *matice)
- {
- int i=0;
- while(i < matice->pocetradku)
- {
- free(matice->pole[i]);
- }
- free(matice->pole);
- i++;
- }
- // naplneni matice
- void fillmatice(TMatrix *matice)
- {
- int j;
- int i;
- int k;
- for(j = 0; j < matice->pocetsloupcu; j++)
- {
- for(i = 0; i < matice->pocetradku; i++)
- {
- // kontrola
- if(fscanf(matice->soubor, "%d", &matice->pole[i][j]) != 1)
- {
- matice->error = ERRCFORM;
- return;
- }
- }
- // testovani konce souboru
- if(fscanf(matice->soubor, "%d", &k) == 1)
- {
- matice->error = ERRCFORM;
- return;
- }
- }
- return;
- }
- // zarovnani
- void align(TMatrix *matice)
- {
- int i;
- int j;
- int m;
- matice->radky = (int*) malloc(matice->pocetradku * (sizeof(int)));
- for(i = 0; i < matice->pocetradku; i++)
- {
- matice->radky[i] = 0;
- }
- for(j = 0; j < matice->pocetsloupcu; j++)
- {
- for(i = 0; i < matice->pocetradku; i++)
- {
- m = 0;
- if(matice->pole[i][j] == 0)
- {
- m = 1;
- }
- else
- {
- while(MYABS(matice->pole[i][j]) >= pow(10,m)) // z mat knihovny, umocnovani pow
- {
- m++;
- }
- }
- if(matice->pole[i][j] < 0)
- {
- m++;
- }
- else if(matice->radky[i] > m)
- {
- matice->radky[i] = m;
- }
- }
- }
- return;
- }
- // vypis pole
- void view(TMatrix *matice)
- {
- int m;
- int j;
- int i;
- int k;
- printf("%d %d\n", matice->pocetsloupcu, matice->pocetradku);
- for(j = 0; j < matice->pocetsloupcu; j++)
- {
- for(i = 0; i < matice->pocetradku; i++)
- {
- if(matice->pole[i][j] < 0)
- {
- m = matice->radky[i]-1;
- }
- else
- {
- m = matice->radky[i];
- }
- if(matice->pole[i][j] == 0)
- {
- for(k = 0; k < m; k++)
- {
- printf(" ");
- }
- }
- else
- {
- if(i == 0)
- {
- m--;
- }
- while(MYABS(matice->pole[i][j]) < pow(10,m))
- {
- m--;
- printf(" ");
- }
- }
- printf("%d", matice->pole[i][j]);
- }
- printf(" \n");
- }
- // dealokace
- free(matice->radky);
- return;
- }
- // zavreni
- void close(TMatrix *matice)
- {
- fclose(matice->soubor);
- }
- // vypis chyb
- void printerror(int error)
- {
- fprintf(stderr,"%s\n",ERRCMESSAGE[-1-error]);
- }
- // funkce na scitani
- void add(TMatrix *matA, TMatrix *matB, TMatrix *matResult)
- {
- int j;
- int i;
- {
- for(j = 0;j < matA-> pocetsloupcu; j++)
- {
- for(i = 0; i < matA->pocetradku; i++)
- {
- matResult->pole[i][j] = matA->pole[i][j] + matB->pole[i][j];
- }
- }
- }
- return;
- }
- // funkce MULT
- void mult(TMatrix *matA, TMatrix *matB, TMatrix *matResult)
- {
- int j;
- int i;
- int k;
- {
- for(j = 0; j < matA->pocetsloupcu; j++)
- {
- for(i = 0; i < matB->pocetradku; i++)
- {
- matResult->pole[i][j] = 0;
- for(k = 0; k < matA->pocetradku; k++)
- {
- matResult->pole[i][j] = matResult->pole[i][j] + matA->pole[k][j] * matB->pole[i][k];
- }
- }
- }
- }
- return;
- }
- // funkce transpozice
- void transpozice(TMatrix *matice, TMatrix *matResult)
- {
- int j;
- int i;
- for(j = 0; j < matice->pocetsloupcu; j++)
- {
- for(i = 0; i < matice->pocetradku; i++)
- {
- matResult->pole[j][i] = matice->pole[i][j];
- }
- }
- return;
- }
- void carom(TMatrix *matice, int numberx, int numbery, char* smer, int power)
- {
- int cesta1;
- int cesta2;
- int i;
- if(numberx > matice->pocetradku || numbery > matice->pocetsloupcu)
- {
- matice->error=ERRCPARAMS;
- }
- else
- {
- if(strcmp(smer, "V") == 0)
- {
- cesta1 = -1;
- cesta2 = 0;
- }
- else if(strcmp(smer, "Z") == 0)
- {
- cesta1 = 1;
- cesta2 = 0;
- }
- else if(strcmp(smer, "JV")==0)
- {
- cesta1 = -1;
- cesta2 = -1;
- }
- else if(strcmp(smer, "JZ") == 0)
- {
- cesta1 = 1;
- cesta2 = -1;
- }
- else if(strcmp(smer, "SV")==0)
- {
- cesta1 = -1;
- cesta2 = 1;
- }
- else if(strcmp(smer, "SZ") == 0)
- {
- cesta1 = 1;
- cesta2 = 1;
- }
- else
- {
- matice->error = ERRCPARAMS;
- }
- if(matice->error == 0)
- {
- int posx = cesta1;
- int posy = cesta2;
- for(i = 0; i < power; i++)
- {
- printf("%d ",matice->pole[posx][posy]);
- // odraz koule
- if(posy + cesta2 < 0 || posy + cesta2 >= matice->pocetsloupcu)
- {
- cesta2 = cesta2 * (-1);
- }
- if(posx + cesta1 < 0 || posx + cesta1 >= matice->pocetradku)
- {
- cesta1 = cesta1 * (-1);
- }
- // pri zmene sudyho / lichyho cisla se pricitava +-1 pro Xovej
- if(cesta1 == 1 && posy %2 == 0 && cesta2 !=0) {}
- else if(cesta1 == -1 && posy %2 == 1 &&cesta2 !=0) {}
- else
- {
- posx = posx + cesta1;
- }
- posy = posy + cesta2;
- }
- printf("\n");
- }
- }
- return;
- }
- void help()
- {
- printf(
- "autor Vojtech Hrdina, student FIT\n"
- "Program pracujici s maticemi.\n"
- "zadani s parametry \n"
- " -h pro napovedu\n"
- " --add pro soucet matice.txt a matice2.txt\n"
- " --mult pro soucin matice.txt a matice2.txt\n"
- " --trans pro transpozici matice.txt\n"
- " --expr pro vyraz (A*BT+A) \n"
- " --carom kulecnikova koule\n");
- }
- int main(int argv,char **argc)
- {
- int para1 = loadparams(argc,argv);
- if(para1 == ERRCPARAMS)
- {
- printerror(para1);
- }
- else
- {
- TMatrix matice, matA, matB, matResult;
- matice.error = 0;
- switch(para1)
- {
- case(STVHELP):
- break; // navrati false
- // test
- case(STVTEST):
- // soubor se otevre
- openfile(argc[2], &matice);
- if(matice.error == 0)
- {
- // naolokuje
- alokace( &matice);
- }
- if(matice.error==0)
- {
- // naplni
- fillmatice( &matice);
- }
- if(matice.error==0)
- {
- // zavre
- close( &matice);
- // zarovna
- align( &matice);
- // a vypise
- view( &matice);
- }
- if(matice.error!=ERRCALLOC)
- {
- // uvolni pamet
- dealokace( &matice);
- }
- if(matice.error!=0)
- {
- // i to by mohlo nstat
- printerror(matice.error);
- }
- break;
- // scitani
- case(STVADD):
- matA.error=0;
- matB.error=0;
- matResult.error=0;
- openfile(argc[2], &matA);
- if(matA.error==0)
- {
- alokace( &matA);
- }
- if(matA.error==0)
- {
- fillmatice( &matA);
- close( &matB);
- }
- if(matA.error!=0)
- {
- fprintf(stderr,"Matice A:");
- printerror(matA.error);
- }
- else
- {
- openfile(argc[3], &matB);
- if(matB.error==0)
- {
- alokace( &matB);
- }
- if(matB.error==0)
- {
- fillmatice( &matB);
- close( &matB);
- }
- if(matB.error!=0)
- {
- fprintf(stderr,"Matice B:");
- printerror(matB.error);
- }
- else
- {
- if(matA.pocetradku == matB.pocetradku && matA.pocetsloupcu == matB.pocetsloupcu)
- {
- matResult.pocetradku=matA.pocetradku;
- matResult.pocetsloupcu=matA.pocetsloupcu;
- alokace( &matResult);
- if(matResult.error==0)
- {
- add(&matA,&matB,&matResult);
- align(&matResult);
- view(&matResult);
- if(matResult.error!=ERRCALLOC)
- {
- dealokace( &matResult);
- }
- }
- }
- else
- {
- matResult.error=ERRCFORM;
- printerror(matResult.error);
- }
- }
- if(matB.error!=ERRCALLOC)
- {
- dealokace(&matB);
- }
- }
- if(matA.error!=ERRCALLOC)
- {
- dealokace (&matA);
- }
- break;
- // nasobeni
- case(STVMULT):
- matA.error=0;
- matB.error=0;
- matResult.error=0;
- openfile(argc[2],&matA);
- if(matA.error==0)
- {
- alokace(&matA);
- }
- if(matA.error==0)
- {
- fillmatice(&matA);
- close(&matA);
- }
- if(matA.error!=0)
- {
- fprintf(stderr,"Matice B:");
- printerror(matA.error);
- }
- else
- {
- openfile(argc[3],&matB);
- if(matB.error==0)
- {
- alokace(&matB);
- }
- if(matB.error==0)
- {
- fillmatice(&matB);
- close(&matB);
- }
- if(matB.error!=0)
- {
- fprintf(stderr,"Matice B:");
- printerror(matB.error);
- }
- else
- {
- if(matA.pocetradku==matB.pocetsloupcu)
- {
- matResult.pocetradku=matA.pocetsloupcu;
- matResult.pocetradku=matB.pocetradku;
- alokace(&matResult);
- if(matResult.error==0)
- {
- mult(&matA,&matB,&matResult);
- align(&matResult);
- view(&matResult);
- if(matResult.error!=ERRCALLOC)
- {
- dealokace(&matResult);
- }
- }
- }
- else
- {
- matResult.error=ERRCFORM;
- printerror(matResult.error);
- }
- }
- if(matB.error!=ERRCNULL)
- {
- dealokace( &matB);
- }
- }
- if(matA.error!=ERRCALLOC)
- {
- dealokace(&matA);
- }
- break;
- // transpozice pracuje se 2 poli
- case(STVTRANS):
- matice.error=0;
- matResult.error=0;
- openfile(argc[2],&matice);
- if(matice.error==0)
- {
- alokace(&matice);
- }
- if(matice.error==0)
- {
- fillmatice(&matice);
- close(&matice);
- }
- if(matice.error==0)
- {
- matResult.pocetradku=matice.pocetsloupcu;
- matResult.pocetsloupcu=matice.pocetradku;
- alokace(&matResult);
- if(matResult.error==0)
- {
- transpozice(&matice,&matResult);
- align(&matResult);
- view(&matResult);
- dealokace(&matResult);
- }
- }
- if(matice.error!=ERRCALLOC)
- {
- dealokace(&matice);
- }
- if(matResult.error!=0)
- {
- printerror(matResult.error);
- }
- break;
- // vyraz
- case(STVEXPR):
- matA.error=0;
- matB.error=0;
- matResult.error=0;
- openfile(argc[2],&matA);
- if(matA.error==0)
- alokace(&matA);
- if(matA.error==0)
- {
- fillmatice(&matA);
- close(&matA);
- }
- if(matA.error!=0)
- {
- printerror(matA.error);
- }
- else
- {
- openfile(argc[3],&matB);
- if(matB.error==0)
- alokace(&matB);
- if(matB.error==0)
- {
- fillmatice(&matB);
- close(&matB);
- }
- if(matB.error!=0)
- {
- printerror(matB.error);
- }
- else if(matA.pocetradku==matB.pocetradku&&matB.pocetradku==matB.pocetsloupcu)
- {
- matResult.pocetsloupcu=matB.pocetradku;
- matResult.pocetradku=matB.pocetsloupcu;
- alokace(&matResult);
- if(matResult.error==0)
- {
- transpozice(&matB,&matResult);
- dealokace(&matB);
- matB.pocetsloupcu=matA.pocetsloupcu;
- matB.pocetradku=matResult.pocetradku;
- alokace(&matB);
- }
- if(matB.error!=0)
- {
- printerror(matB.error);
- }
- else
- {
- mult(&matA,&matResult,&matB);
- dealokace(&matResult);
- matResult.pocetradku=matA.pocetradku;
- matResult.pocetsloupcu=matA.pocetsloupcu;
- alokace(&matResult);
- if(matResult.error==0)
- {
- add(&matA,&matB,&matResult);
- align(&matResult);
- view(&matResult);
- dealokace(&matResult);
- }
- else
- {
- printerror(matResult.error);
- }
- }
- }
- if(matB.error!=ERRCALLOC)
- {
- dealokace(&matB);
- }
- }
- if(matA.error!=ERRCALLOC)
- {
- dealokace(&matA);
- }
- break;
- case(STVCAROM):
- openfile(argc[6],&matice);
- if(matice.error==0)
- {
- alokace(&matice);
- }
- if(matice.error==0)
- {
- fillmatice(&matice);
- close(&matice);
- }
- if(matice.error==0)
- {
- carom(&matice,atoi(argc[2]),atoi(argc[3]),argc[4],atoi(argc[5]));
- }
- if(matice.error!=ERRCALLOC)
- {
- dealokace(&matice);
- }
- if(matice.error!=0)
- {
- printerror(matice.error);
- }
- break;
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment