Advertisement
myamikova9

C5Lab

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