Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- //Zase nejaka elementarni zacyklena maticka a fileIO/stdO
- struct Matrix //so nice :>
- {
- int cols;
- int rows;
- int **data;
- };
- void init( struct Matrix *m )
- {
- //DONE: implementace tak, ze matice m bude mit vse vynulovane
- //vse vcetne poctu sloupcu a radku?
- m->cols = 0;
- m->rows = 0;
- m->data = 0; //nullptr to nebere :(
- //nebo jenom nastavit nuly do data?
- }
- void create( struct Matrix *m, int rows, int cols )
- {
- m->cols = cols;
- m->rows = rows;
- m->data = (int**)malloc( rows * sizeof(int*) ); //sakra, tohle chtělo okomentovat, matne si vzpominam na nejaky ty... pole prvnich prvku?
- //sizeof(int*) je velikost sloupce nebo ne? ... fuck this shit
- for ( int i = 0; i < rows; ++i )
- {
- m->data[ i ] = (int*)malloc( cols * sizeof( int ) ); //fakt bych to potřeboval ještě jednou vysvětlit... (velikost buňky v řádku?)
- for ( int j = 0; j < cols; ++j )
- m->data[ i ][ j ] = 0;
- }
- }
- void destroy( struct Matrix *m )
- {
- for ( int i = 0; i < m->rows; ++i )
- free( m->data[ i ] );
- free( m->data );
- init( m );
- }
- void load( struct Matrix *m, FILE *f )
- {
- // DONE: implementace nacteni matice ze souboru v predepsanem formatu
- // Hint: pouzit funkci create
- if(fscanf(f, "%d %d ", &m->rows, &m->cols) != 2) //mezera za %d...mela/musi tam bejt!
- {
- //chyba vstupu, bylo by vhodne to osetrit
- }
- create(m, m->rows, m->cols); //je to(konstruktor?) void :( jinak m=create(...)?
- for(int i = 0; i < m->rows; ++i)
- {
- for(int j = 0; j < m->cols; ++j)
- {
- fscanf(f, "%d ", &m->data[i][j]);
- // printf("num. %d \n", m->data[i][j]); //mozno smazat, cekovani vstupu :)
- }
- }
- }
- void print( struct Matrix *m, FILE *f )
- {
- // DONE: implemntace zapsani matice do souboru v predepsanem formatu
- fprintf(f, "%d %d\n", m->rows, m->cols);
- for(int i = 0; i < m->rows; ++i)
- {
- for(int j = 0; j < m->cols; ++j)
- {
- fprintf(f, "%d ", m->data[i][j]);
- }
- fprintf(f, "\n");
- }
- }
- void transpose( struct Matrix *m )
- {
- // DONE: provede transpozici matice, ale pouze v pripade, ze matice je ctvercova
- //chtel jsem udelat novou sturct Matrix, ale vubec mi to tady neslo deklarovat :( ...
- //struct Matrix tm;
- //create(&tm, m->cols, m->rows); //COLS are the new ROWS! YAY! :D //kdybych nevedel co delam, byla by to prasarna...
- //takze jsem skoncil s timhle no... skoda, rad bych na obou stranach "=" videl ->
- int tm[m->cols][m->rows];
- for(int i = 0; i < m->rows; ++i)
- {
- for(int j = 0; j < m->cols; ++j)
- {
- tm[j][i] = m->data[i][j];
- }
- }
- for(int i = 0; i < m->cols; ++i)
- {
- for(int j = 0; j < m->rows; ++j)
- {
- m->data[i][j] = tm[i][j];
- }
- }
- }
- int main( )
- {
- //vzorovy main, tyto prikazy nemente
- struct Matrix m;
- FILE *file = fopen( "input.txt", "r" );
- load( &m, file );
- fclose( file );// zavreni souboru input.txt
- print( &m, stdout );// vypis na obrazovku, mel by byt ten samy, jako obsah souboru input.txt
- transpose( &m );
- print( &m, stdout );// vypis na obrazovku, transponovana matice
- transpose( &m );// 2. transpozice, tedy matice by mela byt shodna s puvodni
- file = fopen( "output.txt", "w" );// otevreni jineho souboru
- print( &m, file );// zapis matice do souboru output.txt, mel by byt shodny se souborem input.txt (az na mezery na konci radku)
- fclose( file );// zavreni souboru output.txt
- destroy( &m );// zahodit matici
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement