Chào các bạn! Vì nhiều lý do từ nay Truyen2U chính thức đổi tên là Truyen247.Pro. Mong các bạn tiếp tục ủng hộ truy cập tên miền mới này nhé! Mãi yêu... ♥

giai de cuong c++

BAI 1..1

# include <conio.h>

# include <iostream.h>

class complex{

private: float a,b;

public:

complex(float x=0, float y=0) //ham thiet lap

{ a=x;

b= y;

}

void nhap()//ham nhap so phuc

{

cout<<"

a= ";

cin>>a;

cout<<"

b= ";

cin>> b;

}

void display()

{

cout<<"("<<a<<" + "<<b<<"*i)";

}

friend complex operator+(complex a, complex b);

friend complex operator-(complex a, complex b)

};

complex operator+(complex a, complex b)

{

complex c;

c.a= a.a+b.a;

c.b= a.b+b.b;

return c;

}

complex operator-(complex a, complex b)

{

complex c;

c.a= a.a-b.a;

c.b= a.b-b.b;

return c;

}

void main()

{

complex sp1,sp2,sp3; cout<<"

";

cout<<"

nhap so phuc thu nhat: ";sp1.nhap();cout<<"

";

cout<<"

nhap so phuc thu 2: ";

sp2.nhap();

cout<<"

so phuc thu nhat:";

sp1.display();cout<<"

"; cout<<"

";

cout<<"

so phuc thu 2: ";

sp2.display(); cout<<"

";

sp1.display(); cout<<" +"; sp2.display();cout<<"= ";

sp3=sp1+sp2;

sp3.display();cout<<"

";

sp3.display(); cout<<" - "; sp1.display(); cout<<"= ";

(sp3-sp1).display(); cout<<"

";

getch();

}

BAI 1.2

// chuong trinh mo ta mot doi tuong complex tao doi tuong so phuc

//cac so thuc a,b mo ta phan thuc phan ao

//- ham thiet lap

//- ham nhap so phuc

// - ham in so phuc dang a+ i*b

// khai bao toan tu = cua so phuc

# include <conio.h>

# include <iostream.h>

class complex{

private: float a,b;

public:

complex(float x=0, float y=0) //ham thiet lap

{ a=x;

b= y;

}

void nhap()//ham nhap so phuc

{

cout<<"

a= ";

cin>>a;

cout<<"

b= ";

cin>> b;

}

void display()

{

cout<<"("<<a<<" + "<<b<<"*i)";

}

// khai bao toan tu + cua so phuc

complex operator +(complex &sp)

{

complex c;

c.a= a+sp.a;

c.b= b+sp.b;

return c;

}

// khai bao toan tu gan cua so phuc

void operator =(complex &sp)

{

a= sp.a;

b= sp.b;

}

};

// chuong trinh nhap mang so phuc roi tinh tong cac so phuc do

void main()

{

complex s;int n;

cout<<"

nhap n= ";

cin>>n;

cout<<"

nhap n so phuc";

complex *a= new complex [n];

for(int i=0;i<n;i++)

{ cout<<" so phuc thu "<<i+1<<":

";

a[i].nhap();

}

// tinh tong cac so phuc vua nhap.

for(i=0;i<n;i++)

{

s=s+ a[i];

}

cout<<"

tong cac so phuc vua nhap:"; s.display();

getch();

}

BAI 1.3

// xay dung lop mydate: bao gom cac: ngay thang nam.

// ham nhap ngay thang nam (ko can bien luan du lieu)

//ham hien thi thong tin theo: ngay - thang - nam

//khai bao toan >= la ham ban de so sanh hai doi tuong thoi gian

#include <conio.h>

#include <iostream.h>

class mydate{

private: int ngay, thang, nam;

public:

mydate(int ngayy=0, int thangg=0,int namm =0)

{

ngay= ngayy;

thang =thangg;

nam= namm;

}

//ham gan doi tuong

void operator =(mydate &a)

{

ngay=a.ngay;

thang= a.thang;

nam = a.nam;

}

void nhap()

{

cout<<"

nhap ngay:"; cin>> ngay;

cout<<"

nhap thang: "; cin>>thang;

cout<<"

nhap nam: "; cin>>nam;

}

void display()

{

cout<<"

thoi gian: "<<ngay<<" - "<<thang<<" - "<<nam;

}

friend int operator>(mydate &a, mydate &b);

friend int operator<(mydate &a, mydate &b);

};//het lop

int operator>(mydate &a, mydate &b)

{

if(a.nam>b.nam) return 1;

if((a.nam==b.nam) && (a.thang>b.thang) ) return 1;

if((a.nam== b.nam) && (a.thang == b.thang) && (a.ngay>b.ngay)) return 1;

else return 0;

}

int operator<(mydate &a, mydate &b)

{

if(a.nam<b.nam) return 1;

if((a.nam==b.nam) && (a.thang<b.thang) ) return 1;

if((a.nam== b.nam) && (a.thang == b.thang) && (a.ngay<b.ngay)) return 1;

else return 0;

}

void main()

{

clrscr();

mydate date[5];

for(int i=0;i<5;i++)

{

cout<<"

nhap doi tuong thu "<<i+1<<": ";

date[i].nhap();

}

for( i=0;i<5;i++)

date[i].display();

mydate max;

max = date[0];

for(i=1;i<5;i++)

if(date[i]>max)

{max = date[i];

}

cout<<"

thoi gian co gia tri lon nhat la:";

max.display();

getch();

}

BAI 1.3b

#include<iostream.h>

#include<conio.h>

class mydate{

private:

int d,m,y;

public:

mydate(int dd=1,int mm=1,int yy=1){

d=dd;m=mm;y=yy;

}

void set(int dd,int mm,int yy){

d=dd;m=mm;y=yy;

}

void display(){

cout<<d<<"-"<<m<<"-"<<y;

}

friend int operator>(mydate &d1,mydate &d2);

friend int operator>=(mydate &d1,mydate &d2);

friend int operator<(mydate &d1,mydate &d2);

friend int operator<=(mydate &d1,mydate &d2);

};//het lop

int operator>(mydate &d1,mydate &d2){

if(d1.y>d2.y) return 1;

if((d1.y==d2.y)&&(d1.m>d2.m)) return 1;

if((d1.y==d2.y)&&(d1.m==d2.m)&&(d1.d>d2.d))return 1;

return 0;

}

int operator>=(mydate &d1,mydate &d2){

if(d1.y>=d2.y) return 1;

if((d1.y==d2.y)&&(d1.m>=d2.m)) return 1;

if((d1.y==d2.y)&&(d1.m==d2.m)&&(d1.d>=d2.d))return 1;

return 0;

}

int operator<(mydate &d1,mydate &d2){

if(d1.y<d2.y) return 1;

if((d1.y==d2.y)&&(d1.m<d2.m)) return 1;

if((d1.y==d2.y)&&(d1.m==d2.m)&&(d1.d<d2.d))return 1;

return 0;

}

int operator<=(mydate &d1,mydate &d2){

if(d1.y<=d2.y) return 1;

if((d1.y==d2.y)&&(d1.m<=d2.m)) return 1;

if((d1.y==d2.y)&&(d1.m==d2.m)&&(d1.d<=d2.d))return 1;

return 0;

}

void main(){

mydate date[2];

int d,m,y;

for(int i=0;i<2;i++){

cout<<"

Nhap doi tuong thu "<<i<<"

";

cout<<"Nhap ngay,thang,nam: ";cin>>d>>m>>y;

date[i].set(d,m,y);

}

if(date[0]>date[1]) cout<<"

Doi tuong thu nhat lon hon";

else cout<<"doi tuong thu hai lon hon";

getch();

}

BAI 1.4

#include <conio.h>

#include <iostream.h>

class vecto{

private:

float *v;

public:

vecto(int nn);

vecto(vecto &a);

~vecto();

void nhap();

void display();

void operator=(vecto &a);

friend vecto operator+(vecto &a,vecto &b);

friend int kiemtra(vecto &a, vecto &b);

};//het lop

vecto::vecto(int nn)

{

n=nn;

v= new float [n];

}

// ham thiet lap sao chep

vecto::vecto(vecto &a)

{

n=a.n;

v= new float [n];

for(int i=0;i<n;i++)

v[i]= a.v[i];

}

vecto::~vecto()

{for (int i=0;i<n;i++)

{delete v;v++;

}

}

void vecto::nhap()

{

cout<<"

nhap so chieu cho vecto: "; cin>>n;

v=new float [n];

for( int i=0;i<n;i++)

{

cout<<"

v["<<i+1<<"]: ";

cin >>v[i];

}

}

void vecto::display()

{

cout <<"

vecto:";

for(int i=0;i<n;i++)

{

cout<<v[i]<<" ";

}

}

void vecto::operator=(vecto &a)

{

n= a.n;

v= new float [n];

for(int i=0;i<n;i++)

v[i]= a.v[i];

}

vecto operator +(vecto &a,vecto &b)

{

vecto c(a.n);

for(int i=0;i<a.n;i++)

{

c.v[i]= a.v[i] + b.v[i];

}

return c;

}

int kiemtra(vecto &a, vecto &b)

{

if(a.n==b.n) return 1;

else return 0;

}

void main()

{int n;

vecto a(n),b(n);

a.nhap();

a.display();

b.nhap();

b.display();

if(kiemtra(a,b))

{

cout<< "

tong hai vecto :";

(a+b).display();

}

else cout<<"

hai vec to ko cung chieu";

getch();

}

BAI 1.5

#include <conio.h>

#include <iostream.h>

#include <math.h>

class ps{

private: int tu,mau;

public:

{

tu= 0;

mau =1;

}

void nhap();

void display()

{if(tu==0 || mau==1) cout<<tu;

else cout<<tu<<"/"<<mau;

}

ps operator +(ps &b)

{

ps c;

c.tu=tu*b.mau +mau*b.tu;

c.mau= mau*b.mau;

c.toigian();

return c;

}

void toigian();

};//het lop

void ps::nhap()

{

cout<<"

nhap tu so:";cin>> tu;

tt:

cout<<"

nhap mau so:"; cin>> mau;

if(mau==0) goto tt;

}

void ps::toigian()

{int a,b;

a = abs(tu);

b= abs(mau);

while(a!=b)

{

if(a>b) a=a-b;

else b=b-a;

}

tu=tu/a;

mau=mau/a;

}

void main()

{

ps ps1,ps2;

ps1.nhap();ps1.display();cout<<"

";

ps2.nhap();ps2.display();

cout<<"

tong hai phan so: ";

(ps1+ps2).display();

getch();

}

BAI 1.6

#include <conio.h>

#include <iostream.h>

class ps{

private: int tu,mau;

public:

{

tu= 0;

mau =1;

}

void nhap();

void display()

{ if(tu==0 || mau==1) cout<<tu;

else cout<<tu<<"/"<<mau;

}

int operator >(ps &b)

{

if(tu*b.

mau - b.tu*mau >0) return 1;

else return 0;

}

};//het lop

void ps::nhap()

{

cout<<"

nhap tu so:";cin>> tu;

tt:

cout<<"

nhap mau so:"; cin>> mau;

if(mau==0) goto tt;

}

void main()

{

cout<<"

ban muon nhap bao nhieu ps: ";

cin>>n;

ps *a= new ps[n];

for(int i=0;i<n;i++)

{

cout<<"

nhap ps thu "<<i+1;

a[i].nhap();

}

for(i=0;i<n;i++)

{cout<<"

";

a[i].display();

}

for( i=0;i<n;i++)

for(int j=i+1;j<n;j++)

if(a[i]>a[j])

{

tg= a[i];

a[i]= a[j];

a[j] = tg;

}

cout<<"

sau khi sap xep:";

for(i=0;i<n;i++)

{cout<<" ";

a[i].display();

}

getch();

}

BAI 1.7

#include <conio.h>

#include <iostream.h>

class ps{

private: int tu,mau;

public:

{

tu= 0;

mau =1;

}

void nhap();

void display()

{if(tu==0 ||mau==1) cout<<tu;

else cout<<tu<<"/"<<mau;

}

int operator >=(ps &b)

{

if(tu*b.mau - b.tu*mau >=0) return 1;

else return 0;

}

};//het lop

void ps::nhap()

{

cout<<"

nhap tu so:";cin>> tu;

tt:

cout<<"

nhap mau so:"; cin>> mau;

if(mau==0) goto tt;

}

void main()

{

ps a[5],max,min;

for(int i=0;i<5;i++)

{

cout<<"

nhap ps thu "<<i+1;

a[i].nhap();

}

cout<<"

cac phan so vua nhap: ";

for(i=0;i<5;i++)

{

a[i].display();

cout<<" ";

}

max =min =a[0];

for(i=1;i<5;i++)

{

if(a[i]>=max) max=a[i];

if(min>=a[i]) min=a[i];

}

cout<<"

phan so lon nhat:"; max.display();

cout<<"

phan so be nhat: "; min.display();

getch();

}

bai1.8

#include <conio.h>

#include <iostream.h>

class vector{

private: int n;

float *v;

public:

vector()

{}

vector(int size)

{

n=size;

v=new float [n];

}

void nhap();

vector (vector &a);

~vector()

{

delete v;

}

void display();

void operator =(vector &b);

friend vector operator *(float k,vector &a);

}; //het lop

vector operator *(float k,vector &a)

{

vector b(a.n);

for(int i=0;i<a.n;i++)

b.v[i] = a.v[i] *k;

return b;

}

void vector::operator =(vector &b)

{

n=b.n;

for(int i=0;i<n;i++)

{

v[i]= b.v[i];

}

}

void vector::display()

{

cout<<"

vector: ";

for (int i=0;i<n;i++)

{

cout<<v[i]<<" ";

}

}

vector::vector(vector &a)

{

n=a.n;

v=new float [n];

for(int i=0;i<n;i++)

{

v[i]=a.v[i];

}

}

void vector::nhap()

{

cout<<"

nhap n="; cin>>n;

v= new float[n];

for(int i=0;i<n;i++)

{

cout<<"

v["<<i+1<<"]:"; cin>> v[i];

}

}

void main()

{

float k;

vector V,Ve;V.nhap();

V.display();

cout <<"

nhap so thuc: "; cin>>k;

cout<<"

tich cua k voi vecto la: ";

Ve=k*V;

Ve.display();

getch();

}

bai1.9

#include <conio.h>

#include <iostream.h>

class Mydate{

private: int ngay, thang, nam;

public:

Mydate()

{}

Mydate(int ngayy, int thangg,int namm)

{

ngay = ngayy;

thang = thangg;

nam = namm;

}

void nhap()

{

cout<<"

nhap ngay:"; cin >>ngay;

cout<<"

nhap thang: "; cin>> thang;

cout<<"

nhap nam: "; cin>> nam;

}

void display()

{

cout<<ngay<<" - "<<thang<<" -"<<nam;

}

int operator>(Mydate &date1)

{

if(nam>date1.nam) return 1;

if(nam==date1.nam && thang> date1.thang) return 1;

if(nam==date1.nam && thang== date1.thang && ngay> date1.ngay) return 1;

else return 0;

}

friend void hoan_doi( Mydate &a, Mydate &b);

};//het lop

void hoan_doi( Mydate &a, Mydate &b)

{Mydate tg;

tg=a;

a=b;

b=tg;

}

// chuong trinh chinh: nhap n doi tuong Mydate...

void main()

{

cout<<"

ban can nhap bao nhieu doi tuong Mydate: "; cin>> n;

Mydate *date,tg;

for(int i=0;i<n;i++)

{

cout<<"

nhap doi tuong thu "<< i+1;

date[i].nhap();

}

for( i=0;i<n;i++)

{

cout<<"

doi tuong: ";

date[i].display();

}

for(i=0;i<n;i++)

for(int j=i+1;j<n;j++)

if(date[i]>date[j])

hoan_doi(date[i],date[j]);

cout<<"

sau khi sap xep:

";

for( i=0;i<n;i++)

{

cout<<"

doi tuong: ";

date[i].display();

}

getch();

}

bai1.10

#include <conio.h>

#include <iostream.h>

class vector{

private: int n;

float *v;

public:

vector(int size);

vector()

{}

void nhap();

vector (vector &a);

~vector()

{

delete v;

}

void display();

void operator =(vector &b);

vector operator +(vector &a);

friend int kiem_tra(vector &a, vector &b);

}; //het lop

vector vector::operator +(vector &a)

{

vector b;b.n = n;

for(int i=0;i<n;i++)

{

b.v[i]= v[i]+a.v[i];

}

return b;

}

void vector::operator =(vector &b)

{

n=b.n;

for(int i=0;i<n;i++)

{

v[i]= b.v[i];

}

}

void vector::display()

{

cout<<"

vector: ";

for (int i=0;i<n;i++)

{

cout<<v[i]<<" ";

}

}

vector::vector(vector &a)

{

n=a.n;

v=new float [n];

for(int i=0;i<n;i++)

{

v[i]=a.v[i];

}

}

void vector::nhap()

{

cout<<"

nhap n="; cin>>n;

v= new float[n];

for(int i=0;i<n;i++)

{

cout<<"

v["<<i+1<<"]:"; cin>> v[i];

}

}

vector::vector(int size)

{

n= size;

v= new float[n];

for(int i=0;i<n;i++)

{

cout<<"

v["<<i+1<<"]:"; cin>> v[i];

}

}

int kiem_tra(vector &a, vector &b)

{

if(a.n==b.n) return 1;

else return 0;

}

void main()

{

vector a,b;

a.nhap();a.display(); b.nhap();b.display();

if(kiem_tra(a,b))

{

cout<<"

tong 2 vecto la:"; (a+b).display();

}

else cout<<"

hai vec to khong cung chieu";

getch();

}

Bạn đang đọc truyện trên: Truyen247.Pro

Tags: #lamletri