|
| 1 | +/* Title : To perform the following operations on Complex numbers: |
| 2 | + Add, subtract, multiply, divide, complex conjugate,Modulus. */ |
| 3 | + |
| 4 | +/* Program Code : */ |
| 5 | +#include <iostream> |
| 6 | +#include<cmath> |
| 7 | +using namespace std; |
| 8 | +class complex |
| 9 | +{ |
| 10 | +float real, img; |
| 11 | +public: |
| 12 | +void display(); |
| 13 | +void read(); |
| 14 | +void add(complex,complex); |
| 15 | +void sub(complex,complex); |
| 16 | +void multiply(complex,complex); |
| 17 | +void divide(complex,complex); |
| 18 | +void conjugate(complex); |
| 19 | +float modulus(complex); |
| 20 | +}; |
| 21 | +int main() |
| 22 | +{ |
| 23 | + complex sum,su,mult,div,conj,c1,c2,c,mod ; |
| 24 | +int opt; |
| 25 | +do{ |
| 26 | +cout<<"ENTER THE OPTION" ; |
| 27 | +cout<<"\n1.ADD \n2.SUB \n3.Multiply \n4.Divide \n5.Conjugate \n6.Modulus"<<'\n'<<endl; |
| 28 | +cin>>opt; |
| 29 | +switch(opt) |
| 30 | +{ |
| 31 | + case 1 : c1.read(); |
| 32 | + c2.read(); |
| 33 | + cout<<"Sum is :"<<endl; |
| 34 | + sum.add(c1,c2); |
| 35 | + break; |
| 36 | + case 2 : c1.read(); |
| 37 | + c2.read(); |
| 38 | + cout<<"Difference is :"; |
| 39 | + su.sub(c1,c2); |
| 40 | + break; |
| 41 | + case 3 : c1.read(); |
| 42 | + c2.read(); |
| 43 | + cout<<"Muliplication is : "<<endl; |
| 44 | + mult.multiply(c1,c2); |
| 45 | + break; |
| 46 | + case 4 : cout<<"Enter dividant and then divisior"<<endl; |
| 47 | + c1.read(); |
| 48 | + c2.read(); |
| 49 | + cout<<"Division is :"<<endl; |
| 50 | + div.divide(c1,c2); |
| 51 | + break; |
| 52 | + case 5 : c.read(); |
| 53 | + cout<<"Conjuage is :"<<endl; |
| 54 | + conj.conjugate(c); |
| 55 | + break; |
| 56 | + case 6 : c.read(); |
| 57 | + cout<<"Modulus of the given complex number is : "<<endl; |
| 58 | + cout<<mod.modulus(c)<<endl; |
| 59 | + break; |
| 60 | +} |
| 61 | + cout<<"\n Wish to continue ? : Press 1 or 0 \n1.Yes 0.No\n"; |
| 62 | + cin>>opt; } |
| 63 | + while(opt!=0); |
| 64 | +return 0; |
| 65 | +} |
| 66 | + |
| 67 | +void complex :: read() |
| 68 | +{ |
| 69 | +cout<<"Enter real and imaginary part : "; |
| 70 | +cin>>real>>img; |
| 71 | +} |
| 72 | +void complex :: display() |
| 73 | +{ |
| 74 | + if(img>=0) |
| 75 | +{ cout<<real<<"+"<<img<<"i"<<endl; } |
| 76 | + else |
| 77 | + cout<<real<<img<<"i"<<endl; |
| 78 | +} |
| 79 | + |
| 80 | +void complex :: add(complex c1,complex c2) |
| 81 | +{ |
| 82 | +real = c1.real+c2.real; |
| 83 | +img = c1.img+c2.img; |
| 84 | +display(); |
| 85 | + |
| 86 | +} |
| 87 | +void complex :: sub(complex c1 , complex c2) |
| 88 | +{ |
| 89 | + |
| 90 | + real=c1.real-c2.real ; |
| 91 | + img = c1.img-c2.img ; |
| 92 | + |
| 93 | + display(); |
| 94 | +} |
| 95 | +void complex :: multiply(complex c1 , complex c2) |
| 96 | +{ |
| 97 | + real=(c1.real*c2.real)-(c1.img*c2.img); |
| 98 | + img=(c1.real*c2.img)+(c1.img*c2.real); |
| 99 | + |
| 100 | + display(); |
| 101 | +} |
| 102 | +void complex :: divide(complex c1 , complex c2) |
| 103 | +{ int i; |
| 104 | + float r ,im; |
| 105 | + i=(c2.real)*(c2.real) + (c2.img)*(c2.img); |
| 106 | + real=(c1.real*c2.real)-(c1.img*c2.img*(-1)); |
| 107 | + img=(c1.real*c2.img*(-1))+(c1.img*c2.real); |
| 108 | + real=real/i; |
| 109 | + img=img/i; |
| 110 | + display(); |
| 111 | +} |
| 112 | +void complex :: conjugate(complex c) |
| 113 | +{ int t; |
| 114 | + real=c.real; |
| 115 | + img=-c.img; |
| 116 | + display(); |
| 117 | +} |
| 118 | +float complex ::modulus(complex c) |
| 119 | +{ |
| 120 | + float k; |
| 121 | + k=sqrt(c.img*c.img+ c.real*c.real) ; |
| 122 | + return k; |
| 123 | +} |
0 commit comments