Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.89 KB | None | 0 0
  1. #include"stdafx.h"
  2. #include<fstream>
  3. #include<iostream>
  4. #include<math.h>
  5.  
  6. using namespace std;
  7.  
  8. double e, v, r, s;
  9. int i, j, n;
  10. char c;
  11.  
  12. bool ProverkaTochnosti(double *x, double *p, double e);
  13. bool ProverkaDiagonalnogoPreobladania(int n, double**a);
  14. void Result(int n, double*x);
  15. void VyvodOtveta(int n, double*y);
  16. void Finish(int n, double**a, double*b, double*x, double*p);
  17. void InputKeyboard(int n, double**a, double*b);
  18. int InputFile(int n, double**a, double*b);
  19. void MetodZeidelya(int n, double**a, double*b, double*x, double*p, double e);
  20.  
  21.  
  22. int main() {
  23.     setlocale(LC_ALL, "russian");
  24.     cout << "-----------Решение СЛАУ методом Зейделя------------" << endl;
  25.     cout << "---------------------------------------------------" << endl;
  26.     cout << "Введите порядок матрицы: ";
  27.     cin >> n;
  28.     cout << "---------------------------------------------------" << endl;
  29.     double**a = new double*[n];
  30.     for (i = 0; i < n; i++)
  31.         a[i] = new double[n];
  32.     double*b = new double[n];
  33.     double*x = new double[n];
  34.     double*p = new double[n];
  35.     cout << "(1)Ввод данных с файла" << endl;
  36.     cout << "(2)Ввод данных с клавиатуры" << endl;
  37.     cout << "---------------------------------------------------" << endl;
  38.     cout << "Выберите способ ввода данных: ";
  39.     cin >> c;
  40.     cout << "---------------------------------------------------" << endl;
  41.     switch (c) {
  42.     case '1': InputFile(n, a, b); break;
  43.     case '2': InputKeyboard(n, a, b); break;
  44.     default: cout << "Способ ввода не выбран!!!" << endl;
  45.     return -1;
  46.     }
  47.  
  48.     ProverkaDiagonalnogoPreobladania(n, a);
  49.     if (ProverkaDiagonalnogoPreobladania(n, a)){
  50.         cout << "Система имеет диагональное преобладание" << endl;
  51.         cout << "---------------------------------------------------" << endl;
  52.         cout << "Введите точность:";
  53.         cin >> e;
  54.         cout << "---------------------------------------------------" << endl;
  55.         MetodZeidelya(n, a, b, x, p, e);
  56.         VyvodOtveta(n, p);
  57.         Result(n, x);
  58.         Finish(n, a, b, x, p);
  59.     }
  60.     else {
  61.         cout << "---------------------------------------------------" << endl;
  62.         cout << "Нет диагонального преобладания!!!" << endl;
  63.         cout << "---------------------------------------------------" << endl;
  64.     }
  65.     system("pause");
  66.     return 0;
  67. }
  68.  
  69. void InputKeyboard(int n, double**a, double*b){
  70.     cout << "Введите коэффициенты и свободные члены:" << endl;
  71.     for (i = 0; i <n; i++){
  72.         for (j = 0; j <n; j++){
  73.             cout << "a[" << i + 1 << "," << j + 1 << "]= ";
  74.             cin >> a[i][j];
  75.         }
  76.         cout << "b[" << i + 1 << "]= ";
  77.         cin >> b[i];
  78.     }
  79. }
  80.  
  81. int InputFile(int n, double**a, double*b) {
  82.     ifstream fin;
  83.     fin.open("Input Data.txt");
  84.     if (!fin.is_open()) {
  85.         cout << "Ошибка при открытии файла!!!" << endl;
  86.         cout << "---------------------------------------------------" << endl;
  87.         return -1;
  88.     }
  89.     for (i = 0; i < n; i++){
  90.         for (j = 0; j < n; j++){
  91.             fin >> a[i][j];
  92.  
  93.         }
  94.         fin >> b[i];
  95.     }
  96.     fin.close();
  97.     cout << "---------------------------------------------------" << endl;
  98.     cout << "Данные успешно считаны" << endl;
  99.     cout << "---------------------------------------------------" << endl;
  100.     return 0;
  101. }
  102.  
  103.  
  104. void Finish(int n, double**a, double*b, double*x, double*p){
  105.     for (i = 0; i < n; i++) {
  106.         delete[]a[i];
  107.     }
  108.     delete[]a;
  109.     delete[]b;
  110.     delete[]x;
  111.     delete[]p;
  112. }
  113.  
  114. void Result(int n, double*x){
  115.     ofstream fout;
  116.     fout.open("Recorded Results.txt");
  117.     fout << "Корни системы: " << endl;
  118.     for (i = 0; i < n; i++)
  119.         fout << "x[" << i << "]=" << x[i] << " " << endl;
  120.     fout.close();
  121.     cout << "---------------------------------------------------" << endl;
  122.     cout << "Данные успешно записаны" << endl;
  123.     cout << "---------------------------------------------------" << endl;
  124. }
  125.  
  126.  
  127. void VyvodOtveta(int n, double*x){
  128.     for (int i = 0; i < n; i++)
  129.         cout << "x[" << i + 1 << "]=" << x[i] << endl;
  130. }
  131.  
  132.  
  133. bool ProverkaTochnosti(double *x, double *p, double e) {
  134.     r = 0;
  135.     for (int i = 0; i < n; i++){
  136.         r += pow((x[i] - p[i]), 2);
  137.     }
  138.     if (sqrt(r) >= e)
  139.         return false;
  140.     return true;
  141. }
  142.  
  143.  
  144. void MetodZeidelya(int n, double**a, double*b, double*x, double*p, double e) {
  145.  
  146.     for (int i = 0; i < n; i++)
  147.         x[i] = 0;
  148.     do {
  149.         for (int i = 0; i < n; i++) {
  150.             p[i] = x[i];
  151.         }
  152.         for (int i = 0; i < n; i++) {
  153.             v = 0;
  154.             for (int j = 0; j < i; j++)
  155.                 v += (a[i][j] * x[j]);
  156.             for (int j = i + 1; j < n; j++)
  157.                 v += (a[i][j] * p[j]);
  158.             x[i] = (b[i] - v) / a[i][i];
  159.         }
  160.     } while (!ProverkaTochnosti(x, p, e));
  161. }
  162.  
  163.  
  164. bool ProverkaDiagonalnogoPreobladania(int n, double**a) {
  165.     for (i = 0; i< n; i++) {
  166.         double s = 0; {
  167.             for (j = 0; j < n; j++)
  168.             if (j != i)
  169.                 s += fabs(a[i][j]);
  170.         }
  171.         if (fabs(a[i][i]) < s)
  172.             return false;
  173.     }
  174.     return true;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement