Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <locale.h>
- #include <stdlib.h>
- float** read_matr(char *fille, int *n){
- FILE *myfile = fopen(fille, "r");
- if (myfile == NULL){
- printf("Файл не может быть открыт или создан\n");
- }
- fscanf(myfile, "%i", n);
- float **matr;
- matr = (float**)malloc((*n) * sizeof(float*));
- for (int i = 0; i < *n; i++) {
- *(matr+i) = (float*)malloc((*n) * sizeof(float));
- }
- for (int i = 0; i < *n; i++)
- {
- for (int j = 0; j < *n; j++) {
- fscanf(myfile, "%f", (*(matr + i) + j));
- }
- }
- fclose(myfile);
- return matr;
- }
- void print_matr(float **matr, int n){
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- printf("%0.2f ", *(*(matr + i) + j));
- }
- printf("\n");
- }
- }
- float **calc_B(int n){
- float** B = (float**)malloc(n * sizeof(float*));
- for (int i = 0; i < n; i++)
- {
- *(B + i) = (float*)malloc(n * sizeof(float));
- }
- for (int i = 0; i < n; i++){
- for (int j = 0; j < n; j++)
- {
- *(*(B + i) + j) = (float)abs(i + j) / (2 + i + j*j);
- }
- }
- return B;
- }
- float** calc_E(int n)
- {
- float** E = (float**)malloc(n * sizeof(float*));
- for (int i = 0; i < n; i++){
- *(E + i) = (float*)malloc(n * sizeof(float));
- for (int j = 0; j<n; j++){
- if (i == j) {
- *(*(E + i) + j) = 1;
- }
- else *(*(E + i) + j) = 0;
- }
- }
- return E;
- }
- float** calc_C ( float **A, float **B, float **E, int n)
- {
- float** C = (float**)malloc(n * sizeof(float*));
- for ( int i = 0; i < n; i++)
- {
- *(C + i) = (float*)malloc(n * sizeof(float));
- for ( int j = 0; j < n; j++)
- {
- *(*(C + i) + j)= ( *(*(A + i) + j) + 3* *(*(B + i) + j) - 3 * *(*(E + i) + j));
- }
- }
- return C;
- }
- void write_matr(char* file,float**C, int n){
- FILE *myfile;
- myfile = fopen (file, "w");
- for(int i=0; i<n; i++){
- for(int j=0; j<n; j++){
- fprintf(myfile, "%3.1f ", *(*(C+i)+j));
- }
- fprintf(myfile, "%s", "\n");
- }
- fclose(myfile);
- }
- int main(int argc, const char * argv[]) {
- // setlocale(LC_ALL, "Russian");
- int n;
- char *input_name, *output_name;
- input_name = (char*)malloc(255 * sizeof(char));
- output_name = (char*)malloc(255 * sizeof(char));
- float** A, **B, **C, **E;
- printf("Введите имя файла, в котором хранится матрица А: ");
- // gets(input_name, 10);
- scanf("%s", input_name);
- A = read_matr(input_name, &n);
- printf("Матрица A:\n");
- print_matr(A, n);
- B = calc_B(n);
- printf("Матрица B:\n");
- print_matr(B, n);
- E = calc_E(n);
- printf("Матрица E:\n");
- print_matr(E, n);
- C=calc_C(A,B,E,n);
- printf("Матрица C:\n");
- print_matr(C, n);
- printf("Введите имя файла для записи матрицы С: ");
- scanf("%s", output_name);
- write_matr(output_name, C, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement