Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

ИТиУвТС / лабораторная 1

.doc
Скачиваний:
8
Добавлен:
01.04.2014
Размер:
59.39 Кб
Скачать

Файл Slau.h

#ifndef _SLAU_H_

#define _SLAU_H_

#include <iostream>

#include <cmath>

using std::cout;

using std::endl;

using std::cin;

using std::fabs;

class SLAU

{

public:

SLAU(int val);

~SLAU();

bool ForwardStroke(); // Прямой ход метода Гаусса

bool BackStroke(); // Обратный ход метода Гаусса

void PrintResult(); // Вывод полученых корней

void PrintUtochRes(); // Печать уточнений

void Nevazka(); // Поиск невязок

bool Utochnenie(double nv); // Уточнение корней

private:

const int n; // Количество уравнений

double **ms; // Двумерный масив индексов

double **nev; // Копия двумерного массива

double *tmp; // Массив значений x1, x2, xn

double *tmp2; // Массив невязок

double *tmp3; // Массив корней, которые постепенно уточняются

double nevazka; // Общая невязка системы

// Закрытые функции

void InputValue(); // Ввод значений в матрицу

void PrintCurrent(); // Печатает текущие состояние матрицы

void Replace(int a, int b); // Меняет соответствующие строки

};

#endif /* _SLAU_H_ */

Файл Slau.cpp

#include "Slau.h"

SLAU::SLAU(int val): n(val)

{

ms = new double*[n];

nev = new double*[n];

for(int i = 0; i < n; i++)

{

ms[i] = new double [n+1];

nev[i] = new double [n+1];

}

tmp = new double[n];

tmp2 = new double[n];

tmp3 = new double[n];

InputValue();

}

SLAU::~SLAU()

{

for (int i = 0; i < n; i++)

{

delete[]ms[i];

delete[]nev[i];

}

delete[]ms;

delete[]nev;

delete tmp;

delete tmp2;

delete tmp3;

}

void SLAU::PrintCurrent()

{

for(int i = 0; i < n; i++)

{

for(int j = 0; j < n+1; j++)

cout <<"\t" <<ms[i][j];

cout << endl;

}

}

void SLAU::InputValue()

{

for(int i = 0; i < n; i++)

{

cout <<"+-Ввод коофициентов в уравнении №" << i+1 << endl;

for(int j = 0; j < n+1; j++)

{

cout <<"|"<< endl;

cout <<"+---a"<<j+1<<": ";

cin >> ms[i][j];

nev[i][j] = ms[i][j];

}

cout << endl;

}

}

bool SLAU::ForwardStroke()

{

double temp;

for(int i = 0; i < n-1; i++)

{

int index = 0;

double max = fabs(ms[i][i]);

for(int b = i+1; b < n; b++)

{

if(fabs(ms[b][i]) > max)

{

max = fabs(ms[b][i]);

index = b;

}

}

if(index != 0)

Replace(index, i);

if(ms[i][i] == 0)

{

cout <<"Главный элемент равен нулю..." << endl;

return false;

}

for(int j = 0; j < n-1-i; j++)

{

temp = ms[j+i+1][i]/ms[i][i];

for(int h = 0; h < n+1; h++)

ms[j+i+1][h] -= ms[i][h]*temp;

}

}

return true;

}

bool SLAU::BackStroke()

{

if(ms[n-1][n-1] == 0)

{

cout <<"Главный элемент равен нулю..." << endl;

return false;

}

tmp[n-1] = ms[n-1][n]/ms[n-1][n-1];

for(int k = n-2; k >= 0; k--)

{

double s = 0;

for(int i = k+1; i < n; i++)

{

s+=ms[k][i]*tmp[i];

}

if(ms[n-1][n-1] == 0)

{

cout <<"Главный элемент равен нулю..." << endl;

return false;

}

tmp[k] = (ms[k][n]-s)/ms[k][k];

}

return true;

}

void SLAU::Replace(int a, int b)

{

double *tmp = new double [n+1];

for(int i = 0; i < n+1; i++)

{

tmp[i] = ms[a][i];

ms[a][i] = ms[b][i];

ms[b][i] = tmp[i];

}

delete tmp;

}

void SLAU::Nevazka()

{

for(int i = 0; i < n; i++)

{

double sum = 0.0;

double d = 0.0;

for(int j = 0; j < n; j++)

{

sum += ms[i][j] * tmp[j];

}

tmp2[i] = fabs(ms[i][n] - sum);

if(fabs(tmp2[i]) > nevazka)

nevazka = fabs(tmp2[i]);

}

}

void SLAU::PrintResult()

{

for(int i = 0; i < n; i++)

{

cout <<"x" << i+1 <<": " << tmp[i] <<" ";

}

cout << endl;

for(int i = 0; i < n; i++)

cout <<"Невязка для уравнения №"<< i+1 <<": " << tmp2[i] << endl;

cout <<"Общая невязка: " << nevazka << endl << endl;

}

bool SLAU::Utochnenie(double nv)

{

if(nevazka > nv)

{

for(int i = 0; i < n; i++)

tmp3[i] = tmp[i];

cout << endl << endl;

cout <<"######### Результаты уточнения СЛАУ #########"<< endl;

cout <<"#################################################"<< endl << endl;

for(int i = 0; i < n; i++)

{

for(int j = 0; j < n+1; j++)

ms[i][j] = nev[i][j];

ms[i][n] = tmp2[i];

}

ForwardStroke();

BackStroke();

for(int i = 0; i < n; i++)

tmp3[i] += tmp[i];

for(int i = 0; i < n; i++)

ms[i][n] = tmp3[i];

ForwardStroke();

BackStroke();

}else return false;

PrintUtochRes();

return true;

}

void SLAU::PrintUtochRes()

{

cout <<"Уточненые корни (соответственно)..." << endl;

for(int i = 0; i < n; i++)

{

cout <<"x" << i+1 <<": " << tmp3[i] <<" ";

}

cout << endl << endl;

}

Файл test.cpp

#include "Slau.h"

int main()

{

int v = 0;

double nevaz = 0.0;

setlocale(0, "rus");

cout <<"######### Решение СЛАУ методом Гауса #########"<< endl;

cout <<"##################################################"<< endl << endl;

cout <<"Введите количество уравнений: ";

cin >> v;

SLAU *slau = new SLAU(v);

if(slau->ForwardStroke())

if(slau->BackStroke())

{

slau->Nevazka();

cout <<"########## Результаты решения СЛАУ ##########"<< endl;

cout <<"#################################################"<< endl << endl;

slau->PrintResult();

while(slau->Utochnenie(nevaz));

}

delete slau;

system("pause");

return 0;

}

Министерство образования Республики Беларусь

Учреждение образования

«Белорусский государственный университет информатики и

радиоэлектроники»

кафедра «Вычислительные методы и программирование»

Лабораторная работа №1

Тема: «Решение СЛАУ методом Гаусса»

Выполнил студент группы 922403

Олешкевич А.С.

Принял Смирнов В.Л.

Минск 2010 г.