5. Решение контрольной задачи



Download 6,99 Mb.
bet7/7
Sana11.07.2022
Hajmi6,99 Mb.
#774602
TuriРешение
1   2   3   4   5   6   7
Bog'liq
Численные методы

Приложение А

Листинг
Main.cpp


#include
#include "matrix.h"
using namespace std;
matrix gauss(const matrix& A, const matrix& b)
{
matrix _A = A;
matrix _b = b;
matrix _x(_A.getSizeM(),1);
vector indexes(_A.getSizeM());
intindex{0};
complex tmp{0,0};
for(inti{0}; i< (int)indexes.size(); i++) indexes[i] = i;
for(int k{0}; k < _A.getSizeN(); k++)
{
index = _A.mainColumn(k);
_A.swapColumn(index,k);
swap(indexes[k], indexes[index]);
tmp = _A[k][k];
for(int j{0}; j < _A.getSizeM(); j++) _A[k][j] = _A[k][j]/tmp;
_b[k][0] = _b[k][0]/tmp;
for(inti{k+1}; i< _A.getSizeN(); i++)
{
tmp = _A[i][k];
for(int j{k}; j < _A.getSizeM(); j++)
{
_A[i][j] = _A[i][j] – _A[k][j] * tmp;
}
_b[i][0] = _b[i][0] – _b[k][0] * tmp;
}
}
cout""Result:""endl;
cout""A:""endl"_A"endl;
cout""b:""endl"_b"endl;
_x = _b;
for(inti{_A.getSizeN()-1}; i>= 0; i--)
{
for(int j{_A.getSizeM()-1}; j >i; j--)
{
_x[i][0] = _x[i][0] – _x[j][0] * _A[i][j];
}
}
matrix x(_x.getSizeN(),1);
for(inti{0}; i< _x.getSizeN(); i++)
{
x[indexes[i]] = _x[i];
}
return x;
}
intmain() {
matrix A, b;
while(true)
{
cout""A = ";
cin"A;
if(cin.fail() || cin.bad())
{
cin.clear();
cin.sync();
}
else break;
}
while(true)
{
cout""b = ";
cin"b;
if(cin.fail() || cin.bad())
{
cin.clear();
cin.sync();
}
else break;
}
cout""A:""endl"A"endl;
cout""b:""endl"b"endl;
matrix x = gauss(A,b);
cout""x:""endl"x"endl;
system("pause");
return 0;
}
Complex.h
#pragma once
#include
#include
#include
class complex
{
private:
double re;
double im;
public:
complex();
complex(double, double);
complex(const complex&);
double Re() const;
double Re();
double Im() const;
double Im();
double Abs() const;
complex& operator = (const complex&);
friend complex& operator +(const complex&, const complex&);
friend complex& operator -(const complex&, const complex&);
friend complex& operator /(const complex&, const complex&);
friend complex& operator *(const complex&, const complex&);
friend std::ostream& operator "(std::ostream&, const complex&);
friend std::istream& operator "(std::istream&, complex&);
};
std::istream&errorprint(std::istream&, const char*);
Complex.cpp
#include "complex.h"
complex::complex():re{0.0},im{0.0}
{}
complex::complex(double r, double i):re{r},im{i}
{}
complex::complex(const complex& value):re{value.Re()},im{value.Im()}
{}
double complex::Re(){return re;}
double complex::Im(){return im;}
double complex::Re()const{return re;}
double complex::Im()const{return im;}
double complex::Abs() const
{
return double{std::sqrt(re*re+im*im)};
}
complex&complex::operator =(const complex& right)
{
if(this != &right)
{
re = right.Re();
im = right.Im();
}
return *this;
}
complex& operator + (const complex& left, const complex& right)
{
return (*new complex{left.Re()+right.Re(), left.Im()+right.Im()});
}
complex& operator – (const complex& left, const complex& right)
{
return (*new complex{left.Re()-right.Re(), left.Im()-right.Im()});
}
complex& operator * (const complex& left, const complex& right)
{
double _re = left.Re() * right.Re() – left.Im() * right.Im();
double _im = left.Re() * right.Im() + left.Im() * right.Re();
return *new complex(_re,_im);
}
complex& operator / (const complex& left, const complex& right)
{
double tmp = right.Re() * right.Re() + right.Im() * right.Im();
double _re = (left.Re() * right.Re() + left.Im() * right.Im())/tmp;
double _im = (left.Im() * right.Re() – left.Re() * right.Im())/tmp;
return *new complex(_re,_im);
}
std::ostream& operator "(std::ostream& out, const complex& value)
{
char op;
double _im = value.im;
if(value.im<0)
{
op = '-';
_im *= (-1);
}
else op = '+';
out"value.Re()"std::setw(2)"op"std::setw(10)"_im"'i';
return out;
}
std::istream& operator "(std::istream& in, complex& value)
{
char op = ' ', i = ' ';
double _re, _im;
in"_re"op"_im"i;
if(in.fail())
{
in.clear(std::ios_base::failbit);
}
else if(op != '-' && op!='+')
{
return errorprint(in,"!!!ERROR: Invalid input(Missing '-' or '+')!!!");
}
else if(i != 'i') return errorprint(in,"!!!ERROR: Invalid input(Missing 'i')!!!");
else if(in.bad()) std::cout""!!!ERROR: Stream is corrupted""std::endl;
else
{
if(op == '-') _im *= (-1);
value = complex(_re,_im);
}
return in;
}
std::istream&errorprint(std::istream&in, const char *error)
{
in.clear(std::ios_base::badbit);
std::cout"error"std::endl;
return in;
}
Matrix.h
#pragma once
#include "complex.h"
#include
#include
#include
#include
class matrix
{
private:
std::vectorintsizeN;
intsizeM;
public:
matrix();
matrix(int);
matrix(int, int);
matrix(const matrix&);
~matrix();
intmainColumn(constint&) const;
void swapColumn(constint&, constint&);
intgetSizeN() const;
intgetSizeM() const;
std::vector& operator [](int);
matrix& operator = (const matrix&);
friend std::istream& operator "(std::istream&, matrix&);
friend std::ostream& operator "(std::ostream&, matrix&);
};
Matrix.cpp
#include "matrix.h"
matrix::matrix():sizeN{0}, sizeM{0}, array{std::vector(0))}
{
}
matrix::matrix(int _size):sizeN{_size}, sizeM{_size}, array{std::vector(_size))}
{
}
matrix::matrix(int _sizeN, int _sizeM):sizeN{_sizeN}, sizeM{_sizeM}, array{std::vector(_sizeM))}
{
}
matrix::matrix(const matrix& value):matrix(value.sizeN, value.sizeM)
{
std::copy(value.array.begin(),value.array.end(),array.begin());
}
matrix::~matrix()
{
array.clear();
}
intmatrix::getSizeN() const
{
return sizeN;
}
intmatrix::getSizeM() const
{
return sizeM;
}
intmatrix::mainColumn(constint& index) const
{
int _index{0};
complex max{0,0};
for(inti = index; i{
if(max.Abs(){
max = array[index][i];
_index = i;
}
}
return _index;
}
void matrix::swapColumn(constint&i, constint& j)
{
for(int z{0}; z {
std::swap(array[z][i],array[z][j]);
}
}
std::vector& matrix::operator [] (inti)
{
if(i=0) return array[i];
}
matrix&matrix::operator =(const matrix& right)
{
if(this!=&right)
{
sizeN = right.sizeN;
sizeM = right.sizeM;
array = std::vector(sizeM));
std::copy(right.array.begin(),right.array.end(),array.begin());
}
return *this;
}
std::istream& operator "(std::istream& in, std::vector&dest)
{
if(dest.size()!=0) dest.clear();
complex _data;
char c = ' ';
inti{0};
for(_data; in"_data;)
{
dest.push_back(_data);
i++;
}
if(in.bad())
{
std::string err = "!!!ERROR: Invalid element input in column "+std::to_string(i)+" !!!";
return errorprint(in,err.c_str());
}
if(in.fail())
{
in.clear();
in"c;
if(c != ';')
{
in.unget();
in.clear(std::ios_base::failbit);
if(dest.size() != 0)
{
dest.clear();
return errorprint(in,"!!!ERROR: Invalid input(Missing ';')");
}
}
}
return in;
}
std::istream& operator "(std::istream& in, matrix& value)
{
std::vector _strdata;
matrix _matdata;
inti{0};
char c = ' ';
in"c;
if(c!= '[')
{
return errorprint(in,"!!!ERROR: Invalid input(Missing '[')!!!");
}
for(_strdata; in"_strdata;)
{
_matdata.array.push_back(_strdata);
if(i == 0) _matdata.sizeM = _matdata.array[i].size();
i++;
}
if(in.bad())
{
std::string err = "!!!ERROR: Invalid element input in string "+std::to_string(i)+" !!!";
return errorprint(in,err.c_str());
}
if(in.fail())
{
in.unget();
in.clear();
in"c;
if(c != ']') return errorprint(in,"!!!ERROR: Invalid input(Missing ']')!!!");
else
{
_matdata.sizeN = i;
value = _matdata;
}
}
return in;
}
std::ostream& operator "(std::ostream& out, matrix& value)
{
out.precision(4);
out"std::fixed;
for(inti{0};i{
out""[";
for(int j{0}; j{
out"std::setw(12)"value.array[i][j];
}
out""]""std::endl"std::endl;
}
return out;
}

Download 6,99 Mb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish