Kttc Tien Luong
#include <stdio.h>#include <conio.h>#include <iostream.h>#include <math.h> class sophuc { public: float pt,pa; public: // void nhap(); // void in(); friend istream & operator>>(istream & is,sophuc &); friend ostream & operator<<(ostream & os,sophuc &); float sophuc::tinhcan(); sophuc operator +(sophuc x); sophuc operator -(sophuc x); sophuc operator *(sophuc x); sophuc operator /(sophuc x); }; istream & operator>>(istream & is,sophuc & a) { cout<<" nhap phan thuc: "; is>>a.pt; cout<<" nhap phan ao: "; is>>a.pa; return is; } ostream & operator<<(ostream & os,sophuc & a) { os<<a.pt; if(a.pa<0); else os<<"+"; os<<a.pa<<"i"; return os; } sophuc sophuc::operator+(sophuc a) { sophuc c; c.pt=pt+a.pt; c.pa=pa+a.pa; return c; } sophuc sophuc::operator -(sophuc a) { sophuc c; c.pt=pt-a.pt; c.pa=pa-a.pa; return c; } float sophuc::tinhcan() { float k3=pt*pt+pa*pa; return sqrt(k3); } sophuc sophuc::operator *(sophuc x) { sophuc c; c.pt=pt*x.pt-pa*x.pa; c.pa=pt*x.pa+pa*x.pt; cout<<c.pt; cout<<"+"; cout<<c.pa<<"i"; return c; } sophuc sophuc::operator /(sophuc x) { sophuc c; c.pt=(pt*x.pt+pa*x.pa)/pow(x.tinhcan(),2); c.pa=(pa*x.pt-pt*x.pa)/pow(x.tinhcan(),2); cout<<x.pt; if(c.pa<0); else cout<<"+"; cout<<c.pa<<"i"; return c; } void main() { sophuc a,b,c; clrscr(); cout<<" nhap so phuc thu nhat: "; cin>>a; cout<<" nhap so phuc thu hai: "; cin>>b; int k; cout<<" lua chon phep toan can thuc hien: "; cout<<" 1 la tinh tong"; cout<<" 2 la tinh hieu"; cout<<" 3 la tinh tich"; cout<<" 4 la tinh thuong"; cin>>k; switch(k) { case 1: cout<<" tong 2 so phuc la: "; cout<<a+b; break; case 2: cout<<" so phuc 1 tru so phuc 2 la: "; cout<<a-b; break; case 3: cout<<" tich 2 so phuc la: "; cout<<a*b; break; case 4: cout<<" thuong so phuc 1 chia so phuc 2 la: "; a/b; break; default: cout<<" ko lua chon j!!!"; } getch(); }
Bạn đang đọc truyện trên: ZingTruyen.Store