Advertisement
myamikova9

CLab5Xcode

Mar 28th, 2019
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <locale.h>
  3. #include <stdlib.h>
  4.  
  5.  
  6. float** read_matr(char *fille, int *n){
  7.    
  8.     FILE *myfile = fopen(fille, "r");
  9.     if (myfile == NULL){
  10.         printf("Файл не может быть открыт или создан\n");
  11.     }
  12.    
  13.     fscanf(myfile, "%i", n);
  14.     float **matr;
  15.     matr = (float**)malloc((*n) * sizeof(float*));
  16.     for (int i = 0; i < *n; i++) {
  17.         *(matr+i) = (float*)malloc((*n) * sizeof(float));
  18.     }
  19.     for (int i = 0; i < *n; i++)
  20.     {
  21.         for (int j = 0; j < *n; j++) {
  22.             fscanf(myfile, "%f", (*(matr + i) + j));
  23.         }
  24.     }
  25.     fclose(myfile);
  26.     return matr;
  27. }
  28. void print_matr(float **matr, int n){
  29.     for (int i = 0; i < n; i++) {
  30.         for (int j = 0; j < n; j++) {
  31.             printf("%0.2f ", *(*(matr + i) + j));
  32.         }
  33.         printf("\n");
  34.     }
  35. }
  36.  
  37. float **calc_B(int n){
  38.     float** B = (float**)malloc(n * sizeof(float*));
  39.     for (int i = 0; i < n; i++)
  40.     {
  41.         *(B + i) = (float*)malloc(n * sizeof(float));
  42.     }
  43.     for (int i = 0; i < n; i++){
  44.         for (int j = 0; j < n; j++)
  45.         {
  46.             *(*(B + i) + j) = (float)abs(i + j) / (2 + i + j*j);
  47.         }
  48.     }
  49.    
  50.    
  51.     return B;
  52. }
  53. float** calc_E(int n)
  54. {
  55.     float** E = (float**)malloc(n * sizeof(float*));
  56.     for (int i = 0; i < n; i++){
  57.         *(E + i) = (float*)malloc(n * sizeof(float));
  58.         for (int j = 0; j<n; j++){
  59.             if (i == j) {
  60.                 *(*(E + i) + j) = 1;
  61.             }
  62.             else *(*(E + i) + j) = 0;
  63.         }
  64.     }
  65.     return E;
  66. }
  67. float** calc_C ( float **A, float **B, float **E, int n)
  68.     {
  69.        float** C = (float**)malloc(n * sizeof(float*));
  70.         for ( int i = 0; i < n; i++)
  71.         {
  72.             *(C +  i) = (float*)malloc(n * sizeof(float));
  73.             for ( int j = 0; j < n; j++)
  74.             {
  75.                     *(*(C + i) + j)= ( *(*(A +  i) + j) + 3* *(*(B + i) + j) - 3 * *(*(E + i) + j));
  76.             }
  77.         }
  78.  
  79.         return C;
  80.     }
  81. void write_matr(char* file,float**C, int n){
  82.  
  83.     FILE *myfile;
  84.     myfile = fopen (file, "w");
  85.     for(int i=0; i<n; i++){
  86.         for(int j=0; j<n; j++){
  87.             fprintf(myfile, "%3.1f ", *(*(C+i)+j));
  88.         }
  89.         fprintf(myfile, "%s", "\n");
  90.     }
  91.     fclose(myfile);
  92. }
  93.  
  94.  
  95. int main(int argc, const char * argv[]) {
  96. //    setlocale(LC_ALL, "Russian");
  97.     int n;
  98.     char *input_name, *output_name;
  99.     input_name = (char*)malloc(255 * sizeof(char));
  100.     output_name = (char*)malloc(255 * sizeof(char));
  101.     float** A, **B, **C, **E;
  102.     printf("Введите имя файла, в котором хранится матрица А: ");
  103. //   gets(input_name, 10);
  104.     scanf("%s", input_name);
  105.     A = read_matr(input_name, &n);
  106.     printf("Матрица A:\n");
  107.     print_matr(A, n);
  108.     B = calc_B(n);
  109.     printf("Матрица B:\n");
  110.     print_matr(B, n);
  111.     E = calc_E(n);
  112.     printf("Матрица E:\n");
  113.     print_matr(E, n);
  114.     C=calc_C(A,B,E,n);
  115.     printf("Матрица C:\n");
  116.     print_matr(C, n);
  117.     printf("Введите имя файла для записи матрицы С: ");
  118.     scanf("%s", output_name);
  119.     write_matr(output_name, C, n);
  120.  
  121.     return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement