#include<conio.h>
#include<iostream.h>
#include<iomanip.h>
#include<stdio.h>
#include<stdlib.h>
class Array
{
protected:
int n;
float *val;
public:
Array()
{
n=0;
val=NULL;
cout<<"\nKhoi tao thanh cong!"; }
Array(int m);
Array(const Array& b);
~Array()
{
delete val;
}
friend istream& operator>>(istream& is,Array &a);
friend ostream& operator<<(ostream& os,Array &a);
Array& operator=(const Array &a);
Array operator+(Array& b);
Array operator-(Array& b);
};
//======Khoi tao mang co n phan tu==============
Array::Array(int m)
{
n=m;
val=new float[n];
}
//======Khoi tao 1 mang tu 1 mang da co=========
Array::Array(const Array& b)
{
this->n=b.n;
this->val=new float[n];
for(int i=0;i<this->n;i++)
{
this->val[i]=b.val[i];
}
}
//=======Ham da nang toan tu nhap============
istream& operator>>(istream& is,Array& a)
{
cout<<"\n\nArray "<<a.
n<<" phan tu!"; for(int i=0;i<a.n;i++)
{
cout<<"\nNhap phan tu thu "<<i
+1<<":"; is>>a.val[i];
}
return is;
}
//=======Ham da nang toan tu xuat============
ostream& operator<<(ostream& os,Array& a)
{
for(int i=0;i<a.n;i++)
{
os<<setw(6)<<setprecision(1)<<a.val[i];
}
return os;
}
//========da nang hoa toan tu gan=========
Array& Array::operator=(const Array &a)
{
if(this!=&a)
{
delete val;
val=new float[n=a.n];
for(int i=0;i<n;i++)
val[i]=a.val[i];
}
return *this;
}
//======Ham cong mang===================
Array Array::operator+(Array& b)
{
if(n!=b.n)
{
cout<<"\nHai Array co size khac nhau"; return *this;
}
else
{
Array kq;
kq.n=n;
kq.val=new float[kq.n];
for(int i=0;i<kq.n;i++)
{
kq.val[i]=val[i]+b.val[i];
}
return kq;
}
}
//=========Hieu hai Array=====================
Array Array::operator-(Array& b)
{
if(n!=b.n)
{
cout<<"\nHai Array co size khac nhau"; return *this;
}
else
{
Array kq;
kq.n=n;
kq.val=new float[kq.n];
for(int i=0;i<kq.n;i++)
{
kq.val[i]=val[i]-b.val[i];
}
return kq;
}
}
//===========MENU=====================
void Menu();
void menu_Array();
void menu_Array()
{
Array A1(3),A2(3);
int chon;
while(1)
{
clrscr();
cout<<"\n++++++++++++++++++++++MENU++++++++++++++++++++++"; cout<<"\n++++ 1. Khoi tao Array rong +++++"; cout<<"\n++++ 2. Khoi tao Array n phan tu +++++"; cout<<"\n++++ 3. Khoi tao Array tu mot Array khac ++++++"; cout<<"\n++++ 4. Cong hai Array ++++++"; cout<<"\n++++ 5. Hieu hai Array ++++++"; cout<<"\n++++ 6. Nhap Array ++++++"; cout<<"\n++++ 7. Hien thi Array ++++++"; cout<<"\n++++ 0. Exit ++++++"; cout<<"\n++++++++++++++++++++++++++++++++++++++++++++++++"; cout<<"\n\tMoi ban chon:";cin>>chon
; switch(chon)
{
case 1: Array(); getch(); break;
case 2: int m;
cout<<"\nBao nhieu phan tu:"; Array
(m
); cout<<"\nKhoi tao thanh cong!"; getch();
break;
case 3:
Array a1(A1);
cout<<"\nArray thu nhat la:"<<A1
; cout<<"\nArray duoc khoi tao la:"; getch();
break;
case 4: Array kq(3);
kq=A1+A2;
clrscr();
cout<<"\nArray thu nhat:\n"<<A1
; cout<<"\nArray thu hai :\n"<<A2
; cout<<"\nArray tong :\n"<<kq
; getch();
break;
case 5:
Array kq1(3);
kq1=A1-A2;
clrscr();
cout<<"\nArray thu nhat:\n"<<A1
; cout<<"\nArray thu hai :\n"<<A2
; cout<<"\nArray tong :\n"<<kq1
; getch();
break;
case 6: cout<<"\nNhap Array thu nhat:"; cin>> A1
; cout<<"\nNhap Array thu hai:"; cin>> A2
; getch();
break;
case 7:
cout<<"\nArray thu nhat:\n"<< A1
; cout<<"\nArray thu hai:\n"<< A2
; getch();
break;
case 0:Menu();
}
}
}
/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/
class Vector:public Array
{
public:
Vector()
{
}
Vector(int m);
Vector(Vector& v);
~Vector()
{
delete val;
}
friend istream& operator>>(istream& is,Vector &vt);
friend ostream& operator<<(ostream& os,Vector &vt);
Vector& operator=(const Vector &a);
Vector operator+(const Vector& b);
Vector operator+=(const Vector& b);
Vector operator-(const Vector& b);
void init_vt(int pt);
};
void Vector::init_vt(int pt)
{
val=new float [n=pt];
}
//&&&&&&_Ham khoi tao Vector n chieu_&&&&&&&&&&&
Vector::Vector(int schieu)
{
n=schieu;
val=new float[n];
}
//$$$$$$_Ham khoi tao Vector tu mot Vector da co_&&&&&
Vector::Vector(Vector& v)
{
this->val=new float[this->n=v.n];
for(int i=0;i<this->n;i++)
this->val[i]=v.val[i];
}
//#$$$$_Ham da nang toan tu nhap_$$$$$$$$$$$
istream& operator>>(istream& is,Vector& vt)
{
cout<<"\nSo chieu cua Vector la:"<<vt.
n; for(int i=0;i<vt.n;i++)
{
cout<<"\nChieu thu "<<i
+1<<": "; is>>vt.val[i];
}
return is;
}
//=============Da nang toan tu gan=============
Vector& Vector::operator=(const Vector &a)
{
if(this!= &a)
{
delete val;
this->n=a.n;
val=new float[this->n];
for(int i=0;i<n;i++)
val[i]=a.val[i];
}
return *this;
}
//$$$$$_Ham da nang toan tu xuat_$$$$$$$$$$$
ostream& operator<<(ostream& os,Vector& vt)
{
os<<"\n(";
os<<vt.val[0];
for(int i=1;i<vt.n-1;i++)
{
os<<setprecision(1)<<", "<<vt.val[i];
}
os<<", "<<vt.val[i]<<" )";
return os;
}
//$$$$$$$$_Ham cong hai Vector_$$$$$$$$$$$
Vector Vector::operator+(const Vector &b)
{
if(n!=b.n)
{
cout<<"\nHai Array co size khac nhau"; return *this;
}
else
{
Vector kq;
kq.n=n;
kq.val=new float[kq.n];
for(int i=0;i<kq.n;i++)
{
kq.val[i]=val[i]+b.val[i];
}
return kq;
}
}
//=============================================
Vector Vector::operator+=(const Vector &b)
{
//Vector kq;
//kq.n=n;
// kq.val=new float[kq.n];
for(int i=0;i<n;i++)
{
val[i]+=b.val[i];
}
return *this;
}
//--------------Ham tru hai Vector-----------
Vector Vector::operator-(const Vector &b)
{
if(n!=b.n)
{
cout<<"\nHai Array co size khac nhau"; return *this;
}
else
{
Vector kq;
kq.n=n;
kq.val=new float[kq.n];
for(int i=0;i<kq.n;i++)
{
kq.val[i]=val[i]-b.val[i];
}
return kq;
}
}
//==========================================================
void menu_vector();
void menu_vector()
{
Vector A1(4),A2(4);//int so_vector;
int chon;
while(1)
{
clrscr();
cout<<"\n++++++++++++++++++++++MENU+++++++++++++++++++++++"; cout<<"\n++++ 1. Khoi tao Vector n phan tu +++++"; cout<<"\n++++ 2. Khoi tao Vector tu mot Vector khac +++++"; cout<<"\n++++ 3. Nhap Vector +++++"; cout<<"\n++++ 4. Hien thi Vector +++++"; cout<<"\n++++ 5. Cong Vector ++++++"; cout<<"\n++++ 6. Hieu Vector ++++++"; cout<<"\n++++ 0. Exit ++++++"; cout<<"\n+++++++++++++++++++++++++++++++++++++++++++++++++"; cout<<"\n\tMoi ban chon:";cin>>chon
; switch(chon)
{
case 1: int m;
cout<<"\nKhoi tao bao nhieu phan tu:"; Vector(m);
getch();
break;
case 2:
Vector a1(A1);
clrscr();
cout<<"\nVevtor dau tien da nhap la:"<<A1
; cout<<"\nVector thu hai duoc khoi tao tu Vector tren:"; getch();
break;
case 5: Vector kq(3);
kq=A1+A2;
clrscr();
cout<<"\nVector thu nhat:\n"<<A1
; cout<<"\nVector thu hai :\n"<<A2
; cout<<"\nVector tong :\n"<<kq
; getch();
break;
case 6: Vector kq1(3);
kq1=A1-A2;
clrscr();
cout<<"\nVector thu nhat:\n"<<A1
; cout<<"\nVector thu hai :\n"<<A2
; cout<<"\nVector Hieu :\n"<<kq1
; getch();
break;
case 3: cout<<"\nNhap Vector thu nhat:"; cin>> A1
; cout<<"\nNhap Vector thu hai:"; cin>> A2
; getch();
break;
case 4:
cout<<"\nVector thu nhat:\n"<< A1
; cout<<"\nVector thu hai:\n"<< A2
; getch();
break;
case 0:Menu();
}
}
}
void Menu()
{
Array a;
Vector v;
int mainchon;
while(1)
{
clrscr();
cout<<"\n+++++++++MENU+++++++++"; cout<<"\n++++ 1. Array ! ++++++"; cout<<"\n++++ 2. Vector! ++++++"; cout<<"\n++++ 0. Exit ! ++++++"; cout<<"\n++++++++++++++++++++++"; cout<<"\n\tMoi ban chon:";cin>>mainchon
; switch(mainchon)
{
case 1: menu_Array();
break;
case 2: menu_vector();
break;
case 0: exit(1);
}
}
}
void main()
{
Menu();
}