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... ♥

so phuc trong C++

#include<iostream.h>

#include"stdio.h"

#include<conio.h>

#include<math.h>

class so_phuc

{

private:

float thuc,ao;

public:

so_phuc()

{

thuc=0;

ao=0;

}

void nhap();

void hienthi();

void cong(so_phuc&,so_phuc&);

void tru(so_phuc&,so_phuc&);

void nhan(so_phuc&,so_phuc&);

void chia(so_phuc&,so_phuc&);

};

void so_phuc::nhap()

{

cout<<"

Phan thuc:";cin>>thuc;

cout<<" Phan ao:";cin>>ao;

}

void so_phuc::hienthi()

{

cout<<thuc;

if(ao<0);

else cout<<"+";

cout<<ao<<"i";

}

void so_phuc::cong(so_phuc&z1,so_phuc&z2)

{

this->thuc=z1.thuc+z2.thuc;

this->ao=z1.ao+z2.ao;

}

void so_phuc::tru(so_phuc&z1,so_phuc&z2)

{

this->thuc=z1.thuc-z2.thuc;

this->ao=z1.ao-z2.ao;

}

void so_phuc::nhan(so_phuc&z1,so_phuc&z2)

{

this->thuc=z1.thuc*z2.thuc-z1.ao*z2.ao;

this->ao=z1.thuc*z2.ao+z1.ao*z2.thuc;

}

void so_phuc::chia(so_phuc&z1,so_phuc&z2)

{

so_phuc kq;

if((z2.thuc==0)&&(z2.ao==0)) return;

kq.thuc=z2.thuc/(pow(z2.thuc,2)+pow(z2.ao,2));

kq.ao=-z2.ao/(pow(z2.thuc,2)+pow(z2.ao,2));

(*this).nhan(z1,kq);

}

void main()

{

so_phuc z1,z2;

so_phuc kq;

clrscr();

cout<<" Moi ban nhap vao hai so phuc"<<endl;

cout<<" So thu nhat:";

z1.nhap();

cout<<" So thu hai:";

z2.nhap();

cout<<" So thu nhat:";z1.hienthi();

cout<<"

So thu hai:";z2.hienthi();

cout<<"

Ket qua cac phep toan:

";

cout<<" Phep cong so hai so:";

kq.cong(z1,z2);

kq.hienthi();

cout<<"

Phep tru so thu nhat cho so thu hai:";

kq.tru(z1,z2);

kq.hienthi();

cout<<"

Phep nhan hai so:";

kq.nhan(z1,z2);

kq.hienthi();

cout<<"

Phep chia so thu nhat cho so thu hai:";

kq.chia(z1,z2);

kq.hienthi();

cout<<"

CHUC BAN THANH CONG";

fflush(stdin);getch();

}

#include<conio.h>

#include<iostream.h>

#include<math.h>

class sophuc

{

private: float pt,pa;

public:

void nhap();

void hienthi();

sophuc cong(sophuc sp1,sophuc sp2);

sophuc tru(sophuc sp1,sophuc sp2);

sophuc nhan(sophuc sp1,sophuc sp2);

sophuc chia(sophuc sp1,sophuc sp2);};

void sophuc::nhap()

{

cout<<"

Nhap vao phan thuc: ";

cin>>pt;

cout<<"Nhap vao phan ao: ";

cin>>pa;}

void sophuc::hienthi()

{

cout<<"

So phuc vua nhap la: "<<pt;

if (pa<0);

else cout<<"+";

cout<<pa<<"i";}

sophuc sophuc::cong(sophuc sp1,sophuc sp2)

{

sophuc t;

t.pt=sp1.pt+sp2.pt;

t.pa=sp1.pa+sp2.pa;

return t;}

sophuc sophuc::tru(sophuc sp1,sophuc sp2)

{

sophuc t;

t.pt=sp1.pt-sp2.pt;

t.pa=sp1.pa-sp2.pa;

return t;}

sophuc sophuc::nhan(sophuc sp1,sophuc sp2)

{

sophuc t;

t.pt=sp1.pt*sp2.pt-sp1.pa*sp2.pa;

t.pa=sp1.pt*sp2.pa+sp1.pa*sp2.pt;

return t;}

sophuc sophuc::chia(sophuc sp1,sophuc sp2)

{

sophuc t;

t.pt=(sp1.pt*sp2.pt+sp1.pa*sp2.pa)/(pow(sp2.pt,2)+pow(sp2.pa,2));

t.pa=(-sp2.pt*sp1.pa+sp1.pt*sp2.pa)/(pow(sp2.pt,2)+pow(sp2.pa,2));

return t;}

void main()

{

clrscr();

sophuc sp1,sp2,sp3,sp4,sp5,sp6;

sp1.nhap();

sp1.hienthi();

sp2.nhap();

sp2.hienthi();

sp3=sp3.cong(sp1,sp2);

cout<<"

Tong 2 so phuc la";

sp3.hienthi();

sp4=sp4.tru(sp1,sp2);

cout<<"

Hieu 2 so phuc la";

sp4.hienthi();

sp5=sp5.nhan(sp1,sp2);

cout<<"

Tich 2 so phuc la";

sp5.hienthi();

sp6=sp6.chia(sp1,sp2);

cout<<"

Thuong 2 so phuc la";

sp6.hienthi();

getch();}

/*

cho cau truc nhu sau:

struct sp{ float thuc, ao;};

- Viet ham tao so phuc

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

- Viet ham tinh hieu hai so phuc, ket qua tra ve hai so phuc

Viet chuong trinh nhap vao hai so phuc, in ra tong cua chung

*/

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

struct sp

{

float thuc,ao;

};

// ham tao so phuc

sp tao(float a,float b)

{

p.thuc=a;

p.ao=b;

return p;

}

// ham tinh hieu:

sp hieu(sp a,sp b)

{

p.thuc=a.thuc-b.thuc;

p.ao=a.ao-b.ao;

return p;

}

// ham in:

void in(sp p)

{

if(p.ao<0)

printf("%f%fi",p.thuc,p.ao);

else

printf("%f+%fi",p.thuc,p.ao);

}

// ham chinh:

void main()

{

sp a,b,c;

float n,m;

printf("

Nhap vao so phuc thu nhat:");

printf("

Phan thuc=");

scanf("%f",&n);

printf("

Phan ao=");

scanf("%f",&m);

a=tao(n,m);

printf("

Nhap vao so phuc thu hai:");

printf("

Phan thuc=");

scanf("%f",&n);

printf("

Phan ao=");

scanf("%f",&m);

b=tao(n,m);

c=hieu(a,b);

clrscr();

printf("

So phuc thu nhat:");in(a);

printf("

So phuc thu hai:");in(b);

printf("

Hieu hai so phuc:");in(c);

getch();

}

/*

cho cau truc nhu sau:

struct sp{ float thuc, ao;};

- Viet ham tao so phuc

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

- Viet ham tinh tong hai so phuc, ket qua tra ve hai so phuc

Viet chuong trinh nhap vao hai so phuc, in ra tong cua chung

*/

#include<stdio.h>

#include<conio.h>

struct sp

{

float thuc,ao;

};

// ham tao so phuc:

sp tao(float a,float b)

{

p.thuc=a;

p.ao=b;

return p;

}

// ham tong:

sp tong(sp a,sp b)

{

p.thuc=a.thuc+b.thuc;

p.ao=a.ao+b.ao;

return p;

}

//ham in:

void in(sp p)

{

if(p.ao<0)

printf("%0.3f%0.2fi",p.thuc,p.ao);

else

printf("%0.3f+%0.2fi",p.thuc,p.ao);

}

// ham chinh:

void main()

{

sp a,b,c;

float n,m;

clrscr();

printf("

Nhap vao so phuc thu nhat:");

printf("

Phan thuc=");

scanf("%f",&n);

printf("

Phan ao=");

scanf("%f",&m);

a=tao(n,m);

printf("

Nhap vao so phuc thu hai:");

printf("

Phan thuc=");

scanf("%f",&n);

printf("

Phan ao=");

scanf("%f",&m);

b=tao(n,m);

c=tong(a,b);

clrscr();

printf("

so phuc thu nhat:");in(a);

printf("

so phuc thu hai:");in(b);

printf("

tong cua hai so phuc:");in(c);

getch();

}

//CHUONG TRINH THUC TAP C,C++ CUA THAY DAU TRONG HIEN

//DINH NGHIA PHEP TOAN CONG, TRU, NHAN TREN LOP SO PHUC

//MADE BY: KIEN_KN

#include<conio.h>

#include<stdio.h>

#include<iostream.h>

#include<string.h>

class sophuc

{

float thuc,ao;

public:

void nhap()

{

cout<<"

Phan thuc = ";

cin>>thuc;

cout<<" Phan ao = ";

cin>>ao;

}

void ht()

{

cout<<thuc<<"+"<<ao<<"i"<<endl;

}

//DINH NGHIA PHEP CONG

friend sophuc operator +(sophuc c1,sophuc c2)

{

sophuc c;

c.thuc=c1.thuc + c2.thuc;

c.ao= c1.ao+ c2.ao;

return(c);

}

//DINH NGHIA PHEP TRU

friend sophuc operator -(sophuc c1,sophuc c2)

{

sophuc c;

c.thuc=c1.thuc - c2.thuc;

c.ao= c1.ao- c2.ao;

return(c);

}

//DINH NGHIA PHEP NHAN

friend sophuc operator *(sophuc c1,sophuc c2)

{

sophuc c;

c.thuc=c1.thuc*c2.thuc- c1.ao*c2.ao;

c.ao= c2.ao*c1.thuc+ c2.ao*c2.thuc;

return(c);

}

};

//CHUONG TRINH CHINH

void main(void)

{

sophuc d1,d2,d3,d4,d5;

cout<<"

\t \t\t CHUONG TRINH DINH NGHIA PHEP '+','-','*' ";

cout<<"

\t \t \t\t --oOowWw.SPKT.NEToOo--

";

cout<<"

Nhap so phuc thu nhat: ";

d1.nhap();

cout<<"

So phuc ban nhap la: d1= "; d1.ht();

cout<<"

Nhap so phuc thu hai: ";

d2.nhap();

cout<<"

So phuc ban nhap la: d2= "; d2.ht();

d3=d1+d2;

d4=d1-d2;

d5=d1*d2;

cout<<"

KET QUA

";

cout<<"d1 + d2 = " ;

d3.ht();

cout<<" d1 - d2 = " ;

d4.ht();

cout<<" d1 * d2 = " ;

d5.ht();

cout<<"

\t\t\t\t CHUC CAC BAN THI TOT

";

getch();

}

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

Tags: