Uživatelské nástroje

Nástroje pro tento web


pitel:izp:proj3

Výpočty s maticemi

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
typedef struct tmatice {
  int radky, sloupce;
  int** matice;
} TMatice;
 
TMatice alokujMatici (int r, int s) {
  int i;
  TMatice m;
  m.radky=r;
  m.sloupce=s;
  int **mrizka=calloc(r, sizeof(int*));
  if (mrizka==NULL) {
    fprintf (stderr, "!\n");
  }
  for (i=0; i<r; i++) {
    mrizka[i]=calloc(s, sizeof(int));
    if (mrizka==NULL) {
      fprintf (stderr, "!\n");
    }
  }
  m.matice=mrizka;
  return m;
}
 
void uvolniMatici (TMatice *m) {
  int i;
  for (i=0; i<m->radky; i++) free (m->matice[i]);
  free (m->matice);
}
 
TMatice nactiMatici(char *soubor) {
  FILE *fp;
  TMatice m;
  if ((fp=fopen(soubor,"r"))==NULL) {
    m.radky=0;
    m.sloupce=0;
    return m;
  }
  int i, j, r, s;
  if (fscanf (fp, "%d %d", &r, &s) != 2) {
    fprintf (stderr, "Spatne rozmery!\n");
    fclose(fp);
    m.radky=0;
    m.sloupce=0;
    return m;
  }
  m = alokujMatici (r, s);
  for (i = 0; i < r; i++)
    for (j = 0; j < s; j++)
      if (fscanf (fp, "%d", &m.matice[i][j]) != 1) {
        uvolniMatici(&m);
        fclose(fp);
        m.radky=0;
        m.sloupce=0;
        fprintf (stderr, "Nekompatibilni matice!\n");
        return m;
      }
  fclose(fp);
  return m;
}
 
void pisMatici(TMatice m) {
  int i,j;
  printf("%d %d\n",m.radky,m.sloupce);
  for (i=0;i<m.radky;i++) {
    for (j=0;j<m.sloupce;j++) printf("%d ",m.matice[i][j]);
  printf("\n");
  }    
}
 
TMatice sectiMatice (TMatice m1, TMatice m2) {
  TMatice m;
  m = alokujMatici(m1.radky, m1.sloupce);
  if (m1.radky != m2.radky && m1.sloupce != m2.sloupce) {
    uvolniMatici(&m);
    m.radky=0;
    m.sloupce=0;
    return m;
  }
  int i, j;
  for (i=0 ;i<m1.radky; i++)
    for (j=0; j<m1.sloupce; j++) m.matice[i][j] = m1.matice[i][j] + m2.matice[i][j];
  return m;
}
 
TMatice nasobMatice (TMatice m1, TMatice m2) {
  TMatice m;
  m = alokujMatici(m1.radky, m2.sloupce);
  if (m1.sloupce != m2.radky) {
    uvolniMatici(&m);
    m.radky=0;
    m.sloupce=0;
    return m;
  }
 
  int i, j, x;
  for (i=0; i<m1.radky; i++)
    for (j=0; j<m2.sloupce; j++)
      for (x=0; x<m1.sloupce; x++)
        m.matice[i][j]+=m1.matice[i][x]*m2.matice[x][j];
 
  return m;
}
 
int monoMatice (TMatice vstup) {
  int i, j, last, status;
 
  //radky
  status=0; 
  for (i=0; i<vstup.radky; i++) {
    for (j=1; j<vstup.sloupce; j++) {
      last=status;
      if (vstup.matice[i][j] > vstup.matice[i][j-1]) status=1;
      if (vstup.matice[i][j] < vstup.matice[i][j-1]) status=-1;
      if (last!=status && last!=0) return EXIT_FAILURE;
    }
  }
 
  //sloupce
  status=0;
  for (j=0; j<vstup.sloupce; j++) {
    for (i=1; i<vstup.radky; i++) {
      last=status;
      if (vstup.matice[i][j] > vstup.matice[i-1][j]) status=1;
      if (vstup.matice[i][j] < vstup.matice[i-1][j]) status=-1;
      if (last!=status && last!=0) return EXIT_FAILURE;
    }
  }
 
  return EXIT_SUCCESS;
}
 
int subMatice (TMatice m1, TMatice m2) {
  if (m1.radky < m2.radky || m1.sloupce < m2.sloupce) return EXIT_FAILURE; //Vleze se?
  int i1, j1, i2, j2;
  for (j1=0; j1<=(m1.sloupce-m2.sloupce); j1++)
    for (i1=0; i1<=(m1.radky-m2.radky); i1++)
      if (m1.matice[i1][j1] == m2.matice[0][0])
        for (j2=0; j2<m2.sloupce; j2++)
          for (i2=0; i2<m2.radky; i2++) {
            if (m1.matice[i1+i2][j1+j2] != m2.matice[i2][j2]) break;
            if ((i2+1)==m2.radky && (j2+1)==m2.sloupce) return EXIT_SUCCESS;
          }
  return EXIT_FAILURE;
}
 
void napoveda(void) {
  printf("Program pro vypocty s maticemi.\n\n");
  printf("POUZITI:\n");
  printf("./proj3 -parametr matice1 (matice2)\n");
  printf("matice1 a matice2 jsou soubory\n\n");
  printf("PARAMETRY:\n");
  printf("-mono       zkontroluje zda je matice1 monotonni\n");
  printf("-add        secte matici1 s matici2\n");
  printf("-mult       vynasobi matici1 matici2\n");
  printf("-submatrix  hleda v matici1 matici2\n");
  printf("\nSepsal a v hlavni rohli sehral xkalab00.\n");
}
 
int main(int argc, char *argv[]) {
  if (argc == 1 || (argc == 2 && strcmp("-h", argv[1]) == 0)) {
    napoveda();
    return EXIT_SUCCESS;
  }
 
  if (argc >= 3 && (strcmp("-mono", argv[1]) == 0 || strcmp("-add", argv[1]) == 0 || strcmp("-submatrix", argv[1]) == 0 || strcmp("-mult", argv[1]) == 0)) {
    //Nacitani 1. matice
    TMatice m1 = nactiMatici(argv[2]);
    if (m1.radky<=0 || m1.sloupce<=0) {
      fprintf (stderr, "Chyba pri otevirani souboru 1!\n");
      return EXIT_FAILURE;
    }
//    pisMatici (m1); printf("\n");
 
    //Monotonnost
    if (strcmp("-mono", argv[1]) == 0) {
      int x = monoMatice (m1);
      uvolniMatici(&m1);
      if (x==0) printf ("1\n");
        else printf ("#\n");
      return EXIT_SUCCESS;
    }
 
    //Nacitani 2. matice
    TMatice m2 = nactiMatici(argv[3]);
    if (m2.radky<=0 || m2.sloupce<=0) {
      uvolniMatici(&m1);
      fprintf (stderr, "Chyba pri otevirani souboru 2!\n");
      return EXIT_FAILURE;
    }
//    pisMatici (m2); printf("\n");
 
    //Scitani matic
    if (strcmp("-add", argv[1]) == 0) {
      TMatice m = sectiMatice (m1, m2);
      if (m.radky <= 0 || m.sloupce <= 0)
        fprintf (stderr,"#\n");
      else {
        pisMatici (m);
        uvolniMatici(&m);
      }
      uvolniMatici(&m1);
      uvolniMatici(&m2);
      return EXIT_SUCCESS;
    }
 
    //Nasobeni matic
    if (strcmp("-mult", argv[1]) == 0) {
      TMatice m = nasobMatice (m1, m2);
      if (m.radky <= 0 || m.sloupce <= 0)
        fprintf (stderr,"#\n");
      else {
        pisMatici (m);
        uvolniMatici(&m);
      }
      uvolniMatici(&m1);
      uvolniMatici(&m2);
      return EXIT_SUCCESS;
    }
 
    //Submatice
    if (strcmp("-submatrix", argv[1]) == 0) {
      int x = subMatice (m1, m2);
      uvolniMatici(&m1);
      uvolniMatici(&m2);
      if (x==0) printf ("1\n");
        else printf ("#\n");
      return EXIT_SUCCESS;
    }
 
    uvolniMatici(&m1);
    uvolniMatici(&m2);
  } else {
    fprintf (stderr,"Spatne parametry!\n");
    return EXIT_FAILURE;
  }
  fprintf (stderr,"Toto by se nikdy nemelo napsat!\n");
  return EXIT_FAILURE;
}
//gcc -std=c99 -Wall -W -pedantic proj3.c -o proj3 -O2
/var/www/wiki/data/pages/pitel/izp/proj3.txt · Poslední úprava: 30. 12. 2022, 13.43:01 autor: 127.0.0.1