Facebook
From Gray Pheasant, 6 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 337
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6.     FILE* file;
  7.     file = fopen("macierz.txt", "r");
  8.     int row=0,col=0;
  9.     int tab[3][3], tab1[3][3], tab2[3][3];
  10.  
  11.     printf("Elementy w pliku\n\n");
  12.  
  13.     while( fscanf( file, "%d,", &tab[row][col] ) != EOF )
  14.     {
  15.         printf("%d ", tab[row][col]);
  16.         col++;
  17.  
  18.         if(col==3 && row==2)   // Ten warunek == wype³nieniem ca³ej tabeli wartoœciami z 1 macierzy.
  19.         {
  20.             printf("\n\n");
  21.             row=0;
  22.             col=0;
  23.             while( fscanf( file, "%d,", &tab1[row][col] ) != EOF )
  24.             {
  25.                 printf("%d ", tab1[row][col]);
  26.                 col++;
  27.                 if(col==3)
  28.                 {
  29.                     printf("\n");
  30.                     col=0;
  31.                     row++;
  32.                 }
  33.             }
  34.         }
  35.         if(col==3)
  36.         {
  37.             printf("\n");
  38.             col=0;
  39.             row++;
  40.         }
  41.     }
  42.     printf("\n");
  43.     fclose(file);
  44.     int i,j;
  45.  
  46.  
  47.     printf("Wynik mnozenia pierwszej macierzy przez druga:\n\n");
  48.  
  49.     for(i=0; i<3; i++)
  50.     {
  51.  
  52.         tab2[0][i] = tab[0][0]*tab1[0][i]+tab[0][1]*tab1[1][i]+tab[0][2]*tab1[2][i];
  53.  
  54.     }
  55.     printf("\n");
  56.     for(i=0; i<3; i++)
  57.     {
  58.  
  59.         tab2[1][i] = tab[1][0]*tab1[0][i]+tab[1][1]*tab1[1][i]+tab[1][2]*tab1[2][i];
  60.  
  61.     }
  62.     printf("\n");
  63.     for(i=0; i<3; i++)
  64.     {
  65.  
  66.         tab2[2][i] = tab[2][0]*tab1[0][i]+tab[2][1]*tab1[1][i]+tab[2][2]*tab1[2][i];
  67.  
  68.     }
  69.     for(i=0;i<3;i++)
  70.     {
  71.         for(j=0;j<3;j++)
  72.         {
  73.             printf("%d ", tab2[i][j]);
  74.         }
  75.         printf("\n");
  76.     }
  77.     FILE* file2;
  78.     file2 = fopen("wynik.txt", "w");
  79.     if(file2==NULL) printf("Nie mogê otworzyæ!");            //zapisanie do pliku
  80.     else
  81.     {
  82.         fprintf(file2,"Wynik to: \n");
  83.         for(i=0; i<3; i++)
  84.         {
  85.             for(j=0; j<3; j++)
  86.             {
  87.                 fprintf(file2,"%d ", tab2[i][j]);
  88.             }
  89.             fprintf(file2,"\n");
  90.         }
  91.  
  92.         fclose(file2);
  93.  
  94.     }
  95.     return 0;
  96. }