Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include"stdafx.h"
- #include<fstream>
- #include<iostream>
- #include<math.h>
- using namespace std;
- double e, v, r, s;
- int i, j, n;
- char c;
- bool ProverkaTochnosti(double *x, double *p, double e);
- bool ProverkaDiagonalnogoPreobladania(int n, double**a);
- void Result(int n, double*x);
- void VyvodOtveta(int n, double*y);
- void Finish(int n, double**a, double*b, double*x, double*p);
- void InputKeyboard(int n, double**a, double*b);
- int InputFile(int n, double**a, double*b);
- void MetodZeidelya(int n, double**a, double*b, double*x, double*p, double e);
- int main() {
- setlocale(LC_ALL, "russian");
- cout << "-----------Решение СЛАУ методом Зейделя------------" << endl;
- cout << "---------------------------------------------------" << endl;
- cout << "Введите порядок матрицы: ";
- cin >> n;
- cout << "---------------------------------------------------" << endl;
- double**a = new double*[n];
- for (i = 0; i < n; i++)
- a[i] = new double[n];
- double*b = new double[n];
- double*x = new double[n];
- double*p = new double[n];
- cout << "(1)Ввод данных с файла" << endl;
- cout << "(2)Ввод данных с клавиатуры" << endl;
- cout << "---------------------------------------------------" << endl;
- cout << "Выберите способ ввода данных: ";
- cin >> c;
- cout << "---------------------------------------------------" << endl;
- switch (c) {
- case '1': InputFile(n, a, b); break;
- case '2': InputKeyboard(n, a, b); break;
- default: cout << "Способ ввода не выбран!!!" << endl;
- return -1;
- }
- ProverkaDiagonalnogoPreobladania(n, a);
- if (ProverkaDiagonalnogoPreobladania(n, a)){
- cout << "Система имеет диагональное преобладание" << endl;
- cout << "---------------------------------------------------" << endl;
- cout << "Введите точность:";
- cin >> e;
- cout << "---------------------------------------------------" << endl;
- MetodZeidelya(n, a, b, x, p, e);
- VyvodOtveta(n, p);
- Result(n, x);
- Finish(n, a, b, x, p);
- }
- else {
- cout << "---------------------------------------------------" << endl;
- cout << "Нет диагонального преобладания!!!" << endl;
- cout << "---------------------------------------------------" << endl;
- }
- system("pause");
- return 0;
- }
- void InputKeyboard(int n, double**a, double*b){
- cout << "Введите коэффициенты и свободные члены:" << endl;
- for (i = 0; i <n; i++){
- for (j = 0; j <n; j++){
- cout << "a[" << i + 1 << "," << j + 1 << "]= ";
- cin >> a[i][j];
- }
- cout << "b[" << i + 1 << "]= ";
- cin >> b[i];
- }
- }
- int InputFile(int n, double**a, double*b) {
- ifstream fin;
- fin.open("Input Data.txt");
- if (!fin.is_open()) {
- cout << "Ошибка при открытии файла!!!" << endl;
- cout << "---------------------------------------------------" << endl;
- return -1;
- }
- for (i = 0; i < n; i++){
- for (j = 0; j < n; j++){
- fin >> a[i][j];
- }
- fin >> b[i];
- }
- fin.close();
- cout << "---------------------------------------------------" << endl;
- cout << "Данные успешно считаны" << endl;
- cout << "---------------------------------------------------" << endl;
- return 0;
- }
- void Finish(int n, double**a, double*b, double*x, double*p){
- for (i = 0; i < n; i++) {
- delete[]a[i];
- }
- delete[]a;
- delete[]b;
- delete[]x;
- delete[]p;
- }
- void Result(int n, double*x){
- ofstream fout;
- fout.open("Recorded Results.txt");
- fout << "Корни системы: " << endl;
- for (i = 0; i < n; i++)
- fout << "x[" << i << "]=" << x[i] << " " << endl;
- fout.close();
- cout << "---------------------------------------------------" << endl;
- cout << "Данные успешно записаны" << endl;
- cout << "---------------------------------------------------" << endl;
- }
- void VyvodOtveta(int n, double*x){
- for (int i = 0; i < n; i++)
- cout << "x[" << i + 1 << "]=" << x[i] << endl;
- }
- bool ProverkaTochnosti(double *x, double *p, double e) {
- r = 0;
- for (int i = 0; i < n; i++){
- r += pow((x[i] - p[i]), 2);
- }
- if (sqrt(r) >= e)
- return false;
- return true;
- }
- void MetodZeidelya(int n, double**a, double*b, double*x, double*p, double e) {
- for (int i = 0; i < n; i++)
- x[i] = 0;
- do {
- for (int i = 0; i < n; i++) {
- p[i] = x[i];
- }
- for (int i = 0; i < n; i++) {
- v = 0;
- for (int j = 0; j < i; j++)
- v += (a[i][j] * x[j]);
- for (int j = i + 1; j < n; j++)
- v += (a[i][j] * p[j]);
- x[i] = (b[i] - v) / a[i][i];
- }
- } while (!ProverkaTochnosti(x, p, e));
- }
- bool ProverkaDiagonalnogoPreobladania(int n, double**a) {
- for (i = 0; i< n; i++) {
- double s = 0; {
- for (j = 0; j < n; j++)
- if (j != i)
- s += fabs(a[i][j]);
- }
- if (fabs(a[i][i]) < s)
- return false;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement