Опубликован: 10.12.2015 | Доступ: свободный | Студентов: 605 / 89 | Длительность: 07:10:00
Лекция 14:

Параметризованные функции

< Лекция 13 || Лекция 14 || Лекция 15 >

В приводимом ниже примере иллюстрируется использование параметризованной функции для обращения матриц. Параметризуется тип входного массива. При этом функция inversion производит обращение как целочисленных массивов, так и вещественных массивов двойной точности.

//==========================================================
// Name        : template_inversion.cpp
// Author      : Marat
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//==========================================================

#include <iostream>
using namespace std;
#include<stdlib.h>

#include<math.h>

#define n 4  //Порядок матриц
long i,j,k;

#define col 100
#define lin 100

////////////////////////////////////////////////////
//Обращение квадратной матрицы методом Жордана-Гаусса
////////////////////////////////////////////////////
template<class type>void inversion(type *alpha)
{
	long double a[col][lin];
	long t,s;
	long double c;

for(i=0;i<n;i++)
{
	for(j=0;j<n;j++)
	{
		a[i][j]=alpha[i][j];
		a[i][n+j]=0;
	}
	a[i][n+i]=1;
}

for(s=0;s<n;s++)
{
	for(t=s;t<n;t++)
	{
		if(a[t][s]!=0) goto beta;
	}
	cout<<"\nThere is no single root!"; exit(1);
	beta:
	for(j=0;j<2*n;j++)
	{
		long double b;
		b=a[s][j];
		a[s][j]=a[t][j];
		a[t][j]=b;
	}

	c=1/a[s][s];
	for(j=0;j<2*n;j++)
	{
		a[s][j]*=c;
	}

	for(t=0;t<n;t++)
	{
		if(t==s) goto teta;
		c=-a[t][s];
	for(j=0;j<2*n;j++)
	{
		a[t][j]+=c*a[s][j];
	}

	teta:;	}
}

	for(i=0;i<n;i++)
{
		cout<<"\n";

	for(j=0;j<n;j++)
	{
		a[i][j]=alpha[i][j];
		cout.width(5);   //Задание ширины в 5 символов
		cout<<"\t"<<a[i][j];
	}

}

cout<<"\nInverted matrix";
for(i=0;i<n;i++)
{
	cout<<"\n";
	cout.precision(2);     //Два десятичных разряда
	for(j=n;j<2*n;j++)
	{
		cout<<"\t"<<a[i][j];
	}
}

cout<<"\nChecking results";

for(i=0;i<n;i++)
{
	cout<<"\n";
	for(j=0;j<n;j++)
	{
		c=0;
		for(k=0;k<n;k++)
		{
			c+=a[i][k]*a[k][j+n];
		}
		cout.precision(0);     //Нет десятичных разрядов
cout.setf(ios::fixed); //Использование фиксированного формата
		cout.width(5);    //Задание ширины в 5 символов
		cout<<"\t"<<fabs(c);
	}
}
cout<<"\n"<<endl;
}

int main() {
	int gamma[col][lin]={
							{7,5,3,1},
							{0,1,1,1},
							{4,2,1,3},
							{8,1,9,5}
							};

	double beta[col][lin]={
						{1.0, 2.0, 3.0, 4.0},
						{2.0, 3.0, 4.0, 5.0},
						{4.0, 2.0, 1.0, 3.0},
						{8.0, 1.0, 9.0, 5.0}
						};

	cout<<"\nInteger array";
	inversion(gamma);

	cout<<"\nDouble array";
	inversion(beta);
	return 0;
}

Результат:

Integer array
7 5 3 1
0 1 1 1
4 2 1 3
8 1 9 5
Inverted matrix
0.053 -0.5 0.11 0.026
0.14 0.5 -0.053 -0.096
0.035 0.25 -0.26 0.1
-0.18 0.25 0.32 -0.0044
Checking results
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
Double array
1 2 3 4
2 3 4 5
4 2 1 3
8 1 9 5
Inverted matrix
-0.16 -0.053 0.21 0.053
-2 1.8 -0.16 -0.12
-0.6 0.58 -0.32 0.088
1.7 -1.3 0.26 -0.018
Checking results
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

В следующей программе производится сортировка матриц по строкам по возрастанию, а также, и транспонирование.

//==========================================================
// Name        : template_matrix.cpp
// Author      : Marat
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//==========================================================

#include <iostream>
using namespace std;
#include<stdlib.h>

#define n 4  //Порядок матриц
long i,j,k;

////////////////////////////////////////////////
//Функция транспонирования квадратных матриц////
template<class type> void trans(type A[4][4]);//
////////////////////////////////////////////////

///////////////////////////////////////////////////////
//Сортировка матрицы по строкам по возрастанию
///////////////////////////////////////////////////////
template<class type> void sorting(type array[n][n]);///
///////////////////////////////////////////////////////


///////////////////////////////////////////
//Функция печати //////////////////////////
///////////////////////////////////////////
template<class type> void print(type *c);//
///////////////////////////////////////////

int main() {
	double double_a[n][n]={ {4.1, 4.2, 4.3, 4.4},
					{3.1, 3.2, 3.3, 3.4},
					{2.1, 2.2, 2.3, 2.4},
					{1.1, 1.2, 1.3, 1.4}};
	int      int_a[n][n] =		{{41,  42,  43,  44},
						{31,  32,  33,  34},
						{21,  22,  23,  24},
						{11,  12,  13,  14}};

	cout<<"\nDouble array:";
	print(double_a);
	cout<<"\nSosting by ascending:";
	sorting(double_a);
	cout<<"\nTransponing:";
	trans(double_a);
	print(double_a);

	cout<<"\nInteger array:";
	print(int_a);
	cout<<"\nSosting by ascending:";
	sorting(int_a);
	cout<<"\nTransponing:";
	trans(int_a);
	print(int_a);
	return 0;
}

template<class type> void trans(type A[4][4])
{
type *ptr[]={(type *)&A[0], (type *)&A[1], (type *)&A[2], (type *)&A[3]};
type x;

 for(int i=0; i<n-1; i++)
	for (int j=i+1;j<n;j++)
	{x = ptr[i][j];
	ptr[i][j] = ptr[j][i];
	ptr[j][i] = x;
	}
}

template<class type> void sorting(type array[n][n])
{
type *par[n];      // Вспомогательный массив указателей
for (i=0; i<n; i++)  // Цикл перебора строк
par[i] = (type *)&array[i];

type si,sk;// Упорядочение указателей на строки массива
for (i=0; i<n-1; i++)
{
	for (j=0,si=0;j<n;j++)
	si=par[i][j];
		for (int k=i+1;k<n;k++)
		{
			for (j=0, sk=0; j<n; j++)
			sk=par[k][j];

			if (si>sk)
			{
			type *pa = par[i] ;
			par[i] = par[k];
			par[k] = pa;
			type a = si;
			si = sk;
			sk = a;
			}
		}
}

print(par);

}

template<class type> void print(type *c)
{
for(i=0;i<n;i++)
{
	cout<<"\n";
	for(j=0;j<n;j++)
		{
			cout<<"\t"<<c[i][j];
		}

}
cout<<"\n"<<endl;
}

Результат:

Double array:
4.1 4.2 4.3 4.4
3.1 3.2 3.3 3.4
2.1 2.2 2.3 2.4
1.1 1.2 1.3 1.4
Sorting by ascending:
1.1 1.2 1.3 1.4
2.1 2.2 2.3 2.4
3.1 3.2 3.3 3.4
4.1 4.2 4.3 4.4
Transponing:
4.1 3.1 2.1 1.1
4.2 3.2 2.2 1.2
4.3 3.3 2.3 1.3
4.4 3.4 2.4 1.4
Integer array:
41 42 43 44
31 32 33 34
21 22 23 24
11 12 13 14
Sorting by ascending:
11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44
Transponing:
41 31 21 11
42 32 22 12
43 33 23 13
44 34 24 14
< Лекция 13 || Лекция 14 || Лекция 15 >
Зося Ковалева
Зося Ковалева

Хочу получить удостоверение. Сколько стоит оплата?