cslt cau 2
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
class VT
{ protected:
int n,a[100];
public:
VT(){};
~ VT(){}
void nhap();
void in();
VT tong( VT x);
};
void VT ::nhap()
{ cout<<"
nhap chieu vector:"; cin>>n;
cout<<"
nhap tung phan tu:";
for( int i=1; i<=n;i++)
{ cout<< "
nhap a["<<i<<"]="; cin>>a[i];}
}
void VT::in()
{ cout<<"
vecto la:";
cout<<"(";
for (int i=1; i<=n; i++) cout<<a[i]<<",";
cout<<")";
}
VT VT:: tong(VT x )
{ VT tong;
for (int i=1; i<=n; i++) tong.a[i]=a[i]+x.a[i];
cout<<"(";
for( i=1; i<=n; i++) cout<< x.a[i]<<",";
cout<<")";
return tong;
}
class VT2:public VT
{ public:
friend istream&operator>>(istream&is,VT2&x);
friend ostream&operator<<(ostream&os, VT2&x);
VT2 operator+(VT2);
VT2 operator-(VT2);
int operator*(VT2);
} ;
istream&operator>>(istream &is, VT2& x)
{ x.nhap();
return is;
}
ostream&operator<<(ostream &os, VT2& x)
{ x.in();
return os;
}
VT2 VT2::operator+(VT2 x)
{ VT2 k; int z;
for(int i=1; i<=n;i++) k.a[i]=a[i]+x.a[i];
cout<<"(";
for( i=1; i<=n; i++) cout<< k.a[i]<<",";
cout<<")";
cout<<"
nhap phan tu can in:";cin>>z;
for ( i=1; i<=n; i++) { if(i==z) cout<<"
phan tu thu "<<i<<" cua a+ b la:"<<k.a[i];}
return k;
}
VT2 VT2::operator-(VT2 x)
{ VT2 hieu;
for(int i=1; i<=n; i++)
hieu.a[i]=a[i]-x.a[i] ;
cout<<"(";
for( i=1; i<=n; i++) cout<< hieu.a[i]<<",";
cout<<")";
return hieu;
}
int VT2::operator *(VT2 x)
{ int tich=0; VT2 k;
for (int i=1; i<=n; i++) {k.a[i]=a[i]*x.a[i];tich=tich+k.a[i];}
cout<<tich;
return tich;
}
void main()
{ clrscr();
fflush(stdin);
int c,k;
do
{cout<<"
1 la thuc hien tren lop VT:";
cout<< "
2 la thuc hien tren lop VT2:";
cout<<"
ban chon:";
cin>>k;
switch(k)
{ case 1:
VT a,b,c;
cout<<"
nhap vecto thu nhat:"; a.nhap();
cout<<"
nhap vecto thu 2:";b.nhap();
cout<<"
tong cua hai vecto:";c=a.tong(b);c.in();
break;
case 2:
VT2 x,y;
cout<<"
nhap vecto thu nhat:"; cin>>x;cout<<x;
cout<<"
nhap vecto thu hai:"; cin>>y; cout<<y;
cout<<"
hieu hai vecto:";x-y;
cout<<"
tich vo huong 2 vecto:";x*y;
cout<<"
tong hai vecto:";x+y;
break;
default: cout<<"
nhap sai:";
}
cout<<"
ban co thuc hien tiep ko?"<<"
1 la co:
2 la ko:";
cin>>c;
}
while(c==1);
getch();
}
#include<iostream.h>
#include<conio.h>
#include<math.h>
class PS
{ protected:
float tso,mso;
public :
PS tgian();
int ssanh(PS a);
void nhap();
void in();
PS tong(PS a);
};
void PS :: nhap()
{ cout<<"
nhap tu so:"; cin>>tso;
cout<<"
nhap mau so:";
do
{ cin>>mso;
if(mso==0) cout<<"
phan so ko ton tai"<<"
nhap lai:";
}
while (mso==0);
tgian();
}
void PS::in()
{ if(tso==0) cout<<0;
else if(mso==-1) cout<<-tso;
else if(mso==1) cout<<tso;
else cout<<tso<<"/"<<mso;
}
PS PS::tong(PS a)
{ PS kq;
kq.tso=tso*a.mso+ mso*a.tso;
kq.mso=mso*a.mso;
kq.tgian();
return kq;
}
PS PS::tgian()
{ int a=abs(tso),b=abs(mso);
if(a!=0)
{ while (a!=b){(a>b)?(a-=b):(b-=a); }
tso/=a;
mso/=a;
}
}
class PS2:public PS
{ private:
char dau;
public:
friend istream & operator >>(istream & is, PS2 & a);
friend ostream & operator <<(ostream & os, PS2 & a);
PS2 operator+(PS2 a);
PS2 operator-(PS2 a);
PS2 operator*(PS2 a);
PS2 operator/(PS2 a);
PS2 operator++();
PS2 operator--();
int operator==(PS2 a)
{
if(tso*a.mso-mso*a.tso!=0) return 0;
else return 1;
}
int operator!=(PS2 a)
{
if(tso*a.mso-mso*a.tso==0) return 0;
else return 1;
}
int operator>(PS2 a)
{
if(tso/mso<=a.tso/a.mso) return 0;
else return 1;
}
int operator>=(PS2 a)
{
if(tso*a.mso-mso*a.tso<0) return 0;
else return 1;
}
int operator<(PS2 a)
{
if(tso*a.mso-mso*a.tso>=0) return 0;
else return 1;
}
int operator<=(PS2 a)
{
if(tso*a.mso-mso*a.tso>0) return 0;
else return 1;
}
};
istream & operator >>( istream &is, PS2&a)
{ //cout<<"
nhap tu so:";is>>a.tso;
//cout<<"
nhap mau so:";is>>a.mso;
a.nhap(); return is;
}
ostream & operator<<(ostream & os,PS2 &a)
{ // os<<a.tso<<"/"<<a.mso;return os;
a.in(); return os;
}
PS2 PS2::operator+(PS2 a)
{ PS2 kq;
kq.tso=tso*a.mso+ mso*a.tso;
kq.mso=mso*a.mso;
kq.tgian();
return kq;
}
PS2 PS2::operator-(PS2 a)
{ PS2 kq;
kq.tso=tso*a.mso- mso*a.tso;
kq.mso=mso*a.mso;
kq.tgian();
return kq;
}
PS2 PS2::operator*(PS2 a)
{ PS2 kq;
kq.tso=tso*a.tso;
kq.mso=mso*a.mso;
kq.tgian();
return kq;
}
PS2 PS2::operator/(PS2 a)
{ PS2 kq;
kq.tso=tso*a.mso;
kq.mso=mso*a.tso;
kq.tgian();
return kq;
}
PS2 PS2::operator++()
{ PS2 kq;
kq.tso=tso+mso;;
kq.mso=mso;
kq.tgian();
return kq;
}
PS2 PS2::operator--()
{ PS2 kq;
kq.tso=tso-mso;;
kq.mso=mso;
kq.tgian();
return kq;
}
void main()
{
clrscr();
int k,ch;
do
{
cout<<"
1 la thao tac tren lop PS: ";
cout<<"
2 la thao tac tren lop PS2: ";
cout<<"
ban lua chon: ";
cin>>k;
switch(k)
{
case 1:
PS a,b,c;
cout<<"
nhap phan so thu nhat: "; a.nhap();
cout<<"
nhap phan so thu hai: "; b.nhap();
c=a.tong(b);
cout<<"
tong hai phan so = "; c.in();
break;
case 2:
PS2 x,y;
cout<<"
nhap phan so thu nhat: "; cin>>x;cout<<x;
cout<<"
nhap phan so thu hai: "; cin>>y; cout<<y;
cout<<"
tong cua 2 phan so la:"<<x+y;
if(x>y) {cout<<"
phan so "<<x<<">"<<y; }
else if(x<y) {cout<<"
phan so "<<x<<"<"<<y; }
else {cout<<"
phan so "<<x<<"="<<y; }
break;
default:
cout<<"
nhap sai";
}
cout<<"
ban co muon thuc hien tiep ko? "<<"
1 la co:
2 la khong:";
cin>>ch;
}
while(ch==1);
getch();
}
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<iomanip.h>
class SP
{ protected:
float a,b;
public:
void nhap();
void in();
};
void SP::nhap()
{ cout<<"nhap phan thuc:";cin>>a;
cout<<"nhap phan ao:"; cin>>b;
}
void SP::in()
{cout<<a<<"+"<<b<<"i;";}
class SP2:public SP
{ public:
friend istream & operator >>(istream & is, SP2 & x);
friend ostream & operator <<(ostream & os, SP2 & x);
SP2 operator/(SP2 x);
SP2 operator*(SP2 x);
SP2 operator+(SP2 x);
SP2 operator-(SP2 x);
int operator==(SP2 x)
{
if (a==x.a&&b==x.b) return 1;
else return 0;
}
int operator!=(SP2 x)
{ if(a!=x.a||b!=x.b) return 1;
else return 0;
}
int operator>(SP2 x);
int operator>=(SP2 x);
int operator<(SP2 x);
int operator<=(SP2 x);
SP2 operator++()
{ SP2 x;
x.a=a+1;
x.b=b;
return x;
}
SP2 operator--()
{ SP2 x;
x.a=a-1;
x.b=b;
return x;
}
};
istream & operator >>(istream &is, SP2&x)
{
//{cout<<"
nhap phan thuc:"; is>>x.a;
// cout<<"
nhap phan ao:"; is>>x.b;
x.nhap();
return is;
}
ostream & operator <<(ostream &os, SP2&x)
{//os<<x.a<<"+"<<x.b<<"i;";
x.in();
return os;}
SP2 SP2::operator*(SP2 x)
{ SP2 k;
k.a=a*x.a-b*x.b;
k.b=a*x.b+b*x.a;
return k;
}
SP2 SP2::operator/(SP2 x)
{ SP2 k;
k.a=(a*x.a+b*x.b)/(x.a*x.a+x.b*x.b);
k.b=(x.a*b-x.b*a)/(x.a*x.a+x.b*x.b);
return k;
}
SP2 SP2::operator+(SP2 x)
{ SP2 tong;
tong.a=a+x.a;
tong.b=b+x.b;
return tong;
}
SP2 SP2::operator-(SP2 x)
{ SP2 hieu;
hieu.a=a-x.a;
hieu.b=b-x.b;
return hieu;
}
void main()
{
clrscr();
int k,ch;
do
{
cout<<"
1 la thao tac tren lop SP : ";
cout<<"
2 la thao tac tren lop SP2: ";
cout<<"
ban lua chon: ";
cin>>k;
switch(k)
{
case 1:
SP a,b;
cout<<"
nhap so phuc thu nhat: "; a.nhap(); a.in();
cout<<"
nhap so phuc thu hai: "; b.nhap(); b.in();
break;
case 2:
SP2 x,y;
cout<<"
nhap so phuc thu nhat: "; cin>>x; cout<<x;
cout<<"
nhap so phuc thu hai: "; cin>>y; cout<<y;
cout<<"
tong cua hai so = "<<x+y;
cout<<"
hieu cua hai so = "<<x-y;
cout<<"
tich cua hai so= "<<x*y;
cout<<"
thuong cua hai so= "<<x/y;
break;
default:
cout<<"
nhap sai";
}
cout<<"
ban co muon thuc hien tiep ko? "<<"
chon 1 la co:
chon 2 la ko:";
cin>>ch;
}
while(ch==1);
getch();
}
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class MT
{ protected:
int a[100][100];
public:
void nhap();
void in();
};
void MT::nhap()
{ cout<<"
nhap so dong cua ma tran:"; cin >>n;
cout<<"
nhap so cot cua ma tran:"; cin>>m;
cout<<"
nhap tung thanh phan cua ma tran:";
for (int i=1;i<=n;i++)
{ for (int j=1; j<=m;j++)
{cout<<"
a"<<i<<j<<"=";cin>>a[i][j];}
}
}
void MT::in()
{ cout<<"
ma tran la:";
for (int i=1;i<=n;i++)
{ cout<<"
";
for (int j=1;j<=m;j++) cout<<setw(6)<<a[i][j];
}
}
class MT2:public MT
{ public:
friend istream&operator>>(istream&is, MT2&x);
friend ostream&operator<<(ostream&os, MT2&x);
int kt();
MT2 max();
};
istream&operator>>(istream&is, MT2 &x)
{ x.nhap();
return is;
}
ostream&operator<<(ostream&os, MT2 &x)
{ x.in();
return os;
}
int MT2::kt()
{ if( n==m )
{int tong=0;
int tich=1 ;
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
{
tong+=a[i][j];
tich*=a[i][i];
}
if((tong==m)&&(tich==1)) return 1;
}
else return 0;
}
MT2 MT2::max()
{ int max=a[1][1];
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
if( max<a[i][j]) max=a[i][j];
cout<<"
phan tu lon nhat trong ma tran:"<<max;
}
void main()
{ clrscr();
int c,k;
do
{cout<<"
1 la thao tac tren lop MT:";
cout<<"
2 la thao tac tren lop MT2:";
cout<<"
ban chon:";
cin>>(k);
switch(k)
{
case 1:
MT a;
cout<<"
nhap ma tran :";a.nhap();
a.in();
break;
case 2:
MT2 b;
cout<<"nhap ma tran A:";cin >>b;cout<<b;
b.max();
if (b.kt()==1) cout <<"
ma tran la ma tran don vi" ;
else cout<<"
ma tran ko la ma tran don vi";
break;
default:
cout<<"
nhap lai:" ;
}
cout<<"
ban co muon thuc hien tiep ko?"<<"
1 la co
2 la ko:";
cin>>c;
}
while(c==1);
getch();
}
Bạn đang đọc truyện trên: Truyen2U.Net