Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Bi ma trn

Matric.h
#include<iostream>
using namespace std;
template <typename T>
class Matrix
{
int m,n;//m la so hang,n la so cot
T **data;
public:
Matrix(int = 3,int = 3);
~Matrix();
Matrix(const Matrix<T>&);
Matrix<T> operator =(const Matrix<T>&);
Matrix<T> operator +(const Matrix<T>&);
Matrix<T> operator -(const Matrix<T>&);
Matrix<T> operator *(const Matrix<T>&);
T &operator ()(int,int)const;
template <typename K>
friend ostream &operator <<(ostream& ,const Matrix<K>& );
template <typename K>
friend istream &operator >>(istream& ,Matrix<K>& );
};
//----------------------------------------------------------------template <typename T>
Matrix<T>::Matrix(int m,int n):m(m),n(n)
{
assert(m>0&&n>0);

data = new T*[m];


//assert(data!=0);
for(int i=0;i<m;i++)
{
data[i] = new T[n];
//assert(data[i]!=0);
}
}
//----------------------------------------------------------------template <typename T>
Matrix<T>::~Matrix()
{
for(int i=0;i<m;i++) delete []data[i];
delete []data;
}
//----------------------------------------------------------------template <typename T>
Matrix<T>::Matrix(const Matrix<T>& obj)
{
m=obj.m;
n=obj.n;
data=new T*[m];
//assert(data!=0);
for(int i=0;i<m;i++)
{
data[i]=new T[n];
//assert(data[i]!=0);
}
for(int i=0;i<m;i++)

for(int j=0;j<n;j++) data[i][j]=obj(i,j);


}
//-----------------------------------------------------------------template <typename T>
Matrix<T> Matrix<T>::operator =(const Matrix<T>& obj)
{
if(this!=&obj)
{
for(int i=0;i<m;i++) delete []data[i];
delete []data;
m=obj.m;
n=obj.n;
data = new T*[m];
//assert(data!=0);
for(int i=0;i<m;i++)
{
data[i]=new T[n];
//assert(data[i]!=0);
}
for(int i=0;i<m;i++)
for(int j=0;j<n;j++) data[i][j]=obj(i,j);
}
return *this;
}
//------------------------------------------------------------------template <typename T>
T &Matrix<T>::operator ()(int i,int j)const
{
if(i>=0 && i<m && j>=0 && j<n);

return data[i][j];
}
//-----------------------------------------------------------------template <typename T>
Matrix<T> Matrix<T>::operator +(const Matrix<T>& obj)
{
if(m==obj.m && n==obj.n){
Matrix<T> Mtemp(*this);
for(int i=0;i<m;i++)
for(int j=0;j<n;j++) Mtemp(i,j)+=obj(i,j);
return Mtemp;}
else cout<<"LOI";
}
//-----------------------------------------------------------------template <typename T>
Matrix<T> Matrix<T>::operator -(const Matrix<T>& obj)
{
if((m==obj.m )&& (n==obj.n)){
Matrix<T> Mtemp(*this);
for(int i=0;i<m;i++)
for(int j=0;j<n;j++) Mtemp(i,j)-=obj(i,j);
return Mtemp;
}
else printf("Loi");

}
//-----------------------------------------------------------------template <typename T>
Matrix<T> Matrix<T>::operator *(const Matrix<T>& obj)

{
if(n==obj.m){
Matrix<T> Mtemp(m,obj.n);
for(int i=0;i<m;i++)
for(int j=0;j<obj.n;j++)
{
Mtemp(i,j)=0;
for(int k=0;k<n;k++) Mtemp(i,j)+=data[i][k]*obj(k,j);
}
return Mtemp;}else cout<<"LOI";
}
//------------------------------------------------------------------template <typename K>
ostream &operator <<(ostream& out, const Matrix<K>& obj)
{
for(int i=0;i<obj.m;i++)
{
out<<"

";

for(int j=0;j<obj.n;j++) out<<obj(i,j)<<" ";


out<<endl;
}
return out;
}
//-----------------------------------------------------------------template <typename K>
istream &operator >>(istream& in,Matrix<K>& obj)
{
for(int i=0;i<obj.m;i++)
for(int j=0;j<obj.n;j++)

{
cout<<" .phan tu ["<<i<<","<<j<<"] = ";
in>>obj(i,j);
}
return in;
}
Main
#include<iostream>
#include"Matrix.h"
#include<conio.h>
using namespace std;
int main()
{
Matrix<int> m1(3,3),m2(3,1);
cout<<"Nhap ma tran m1"<<endl;
cin>>m1;
cout<<"Nhap ma tran m2"<<endl;
cin>>m2;
cout<<"cac ma tran vua nhap"<<endl;
cout<<"ma tran m1"<<endl;
cout<<m1;
getch();
cout<<"ma tran m2"<<endl;
cout<<m2;
getch();
m1= m1+m2;
cout<<"m1 = m1 + m2"<<endl;
cout<<m1;
getch();

Matrix<int> m3;
m3=m1;
cout<<"m3 = m1"<<endl;
cout<<m3;
getch();
m3=m3-m2;
cout<<"m3 = m3 - m2"<<endl;
cout<<m3;
getch();
cout<<"ma tran m1"<<endl;
cout<<m1;
getch();
Matrix<int> m4;
m4=m1*m2;
cout<<"m4 = m1 * m2"<<endl;
cout<<m4;
system("PAUSE");
return EXIT_SUCCESS;
}

Bi ngn xp:
Stack.h
template <class T> class stack;
#include "List.h"
#ifndef _STACK_H
#define _STACK_H
template <class T>
class Stack
{

Node<T> *pTop;
void copy(const Stack&s);
public:
Stack();
Stack(const Stack& s);
~Stack();
Stack& operator=(const Stack& s);
void MakeEmpty();
bool IsEmpty();
bool Push(const T& x);
bool Pop(T& x);
};
#include "Stack.cpp"
#endif

Stack.cpp
#include <iostream.h>
#include "Stack.h"
#include "List.h"
#ifndef _STACK_CPP
#define _STACK_CPP

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

template <class T>


Stack<T>::Stack()
{

pTop = NULL;
}

template <class T>


Stack<T>::Stack(const Stack& s)
{
pTop = NULL;
copy(s);
}

template <class T>


Stack<T>::~Stack()
{
MakeEmpty();
}

template <class T>


Stack<T>& Stack<T>::operator =(const Stack& s)
{
if(this != &s)
copy(s);
return (*this);
}

template <class T>


void Stack<T>::MakeEmpty()
{
Node<T> *p;

while(pTop != NULL)
{
p = pTop;
pTop = pTop->next;
delete p;
}
}

template <class T>


void Stack<T>::copy(const Stack& s)
{
MakeEmpty();
Node<T>* p = new Node<T>;
this->pTop = p;

Node<T>*q = s.pTop;
while(q != NULL)
{
p->InsertAfter(q->data);
q = s.pTop->next;
}
}

template <class T>


bool Stack<T>::IsEmpty()
{
return pTop == NULL;
}

template <class T>


bool Stack<T>::Push(const T& x)
{
Node<T> *pNew = new Node<T>(x,pTop);
if(pNew == NULL)
return 0;
pTop = pNew;
return 1;
}

template <class T>


bool Stack<T>::Pop(T& x)
{
if(IsEmpty())
return 0;
else
{
Node<T> *p = pTop;
x = p->data;
pTop = pTop->next;
delete p;
return 1;
}
}
#endif

Main.cpp
#include <iostream.h>

#include "Stack.h"
void main()
{
Stack<int> s;
s.Push(12);
int x;
s.Pop(x);
cout << x;
}

Bi danh sch
List.h
#ifndef _LIST_H
#define _LIST_H
template <class T> class Stack;
template <class T>
class Node
{
T data;
Node<T> *next;
public:
Node();
Node(const T& d , Node* p = NULL);
bool InsertAfter(const T& d);
friend class Stack<T>;
};
#include "List.cpp"
#endif

List.cpp
#include "List.h"
#include <iostream.h>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
#ifndef _LIST_CPP
#define _LIST_CPP

template <class T>


Node<T>::Node()
{
next = NULL;
}

template <class T>


Node<T>::Node(const T& d , Node* p)
{
data = d;
next = p;
}

template <class T>


bool Node<T>::InsertAfter(const T &d)
{
Node *pNew = new Node(d,next);

if(pNew == NULL)
return 0;
next = pNew;
return 1;

}
#endif

You might also like