Minh hoạt hàm friend
#include
#include
#include
using namespace std;
class VT;
class MT;
class VT
{
private:
int n;
double x[20]; // Toa do cua diem
public:
void nhapsl();
friend void in(const VT &x);
friend VT tich(const MT &a,const VT &x);
} ;
class MT
{
private:
int n;
double a[20][20];
public:
friend VT tich(const MT &a, const VT &x);
friend void in(const MT &a);
void nhapsl();
} ;
void VT::nhapsl()
{
cout << "\n Cap vec to = ";
cin >> n ;
for (int i=1; i<=n ; ++i)
{
cout << "\nPhan tu thu " << i << " = " ;
cin >> x[i];
}
}
void MT::nhapsl()
{
cout << "\n Cap ma tran = ";
cin >> n ;
for (int i=1; i<=n ; ++i)
for (int j=1; j<=n; ++j)
{
cout << "\nPhan tu thu hang "<< i << " cot " << j << "=" ;
cin >> a[i][j];
}
}
VT tich(const MT &a,const VT &x)
{
VT y;
int n=a.n;
if (n!=x.n)
return x;
y.n = n;
for (int i=1; i<=n; ++i)
{
y.x[i]=0;
for (int j=1; j<=n; ++j)
y.x[i] += a.a[i][j]*x.x[j];
}
return y;
}
void in(const VT &x)
{
cout << "\n";
for (int i=1; i<=x.n; ++i)
cout << x.x[i] << " ";
}
void in(const MT &a)
{
for (int i=1; i<=a.n; ++i)
{
cout << "\n" ;
for (int j=1; j<=a.n; ++j)
cout << a.a[i][j] << " ";
}
}
int _tmain(int argc, _TCHAR* argv[])
{
MT a; VT x,y;
a.nhapsl();
x.nhapsl();
y=tich(a,x);
cout << "\nMa tran A:";
in(a);
cout << "\n\nVec to x: " ;
in(x);
cout << "\n\nVec y = Ax: " ;
in(y);
return 0;
}
Trương Đình Huy
» Tin mới nhất:
» Các tin khác: