Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. // gaus.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <conio.h>
  7. #include "atlstr.h"
  8. #include <fstream>
  9. #include <windows.h>
  10.  
  11. #define eps 0.0001 //точность
  12. #define N 10000 //размер матрицы
  13. using namespace std;
  14.  
  15.    // Условие сходимости
  16. bool converge(double *xk, double* xkp)
  17. {
  18.     for (int i = 0; i < 4; i++)
  19.     {
  20.         if (fabs(xk[i]-xkp[i]) >=eps)
  21.             return false;
  22.     }
  23.     return true;
  24. }  
  25.  
  26. void main(void)
  27. {
  28.  
  29. /*
  30.     Ход метода, где:
  31.     a[n][n] - Матрица коэффициентов
  32.     x[n] - Вектор текущего решения
  33. */
  34.   cout << " Size matrix  " << N;
  35.  
  36.   char  a[N][N];
  37.   double  x[N];
  38.   double  p[N];
  39.   char  b[N];
  40.  
  41.  
  42.  // cout << "    Enter matrix ";
  43.  
  44.   ifstream input_file("D:/A.txt"); //открываем файл
  45.   if (!input_file) cout  << "  Error. \n";
  46.   int i=0;
  47.   int j=0;
  48.   while (!input_file.eof())
  49.   {
  50.      input_file >> a[i][j];
  51.      cout << a[i][j];
  52.      if (input_file != " ") {j++;}
  53.      if (j==N) {i++;j=0;}
  54.  }
  55.   input_file.close();
  56.  
  57.    ifstream input_file1("D:/b.txt");
  58.    if (!input_file1) cout  << "  Error. \n";
  59.    int k =0;
  60.    while (!input_file.eof())
  61.    {
  62.        input_file >> b[k];
  63.        k++;
  64.    }
  65.    input_file1.close();
  66.  
  67. do
  68. {
  69.     for(int i = 0; i < N; i++)
  70.     {
  71.         double var = 0;
  72.         for(int j = 0; j < N; j++)
  73.             if(j != i) var += (a[i][j]*x[j]);
  74.         p[i] = x[i];
  75.         x[i]=(b[i] - var)/a[i][i];
  76.     }
  77. }
  78. while(!converge(x,p));
  79. cout << "Answer   ";
  80. for(int t=0;t<N;t++)
  81. {  
  82.     cout << x[t] << "; ";
  83. }
  84. getch();
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement