C++ Complex Numbers Arithmetic
C++ Complex Numbers Arithmetic
#include<iostream> using namespace std; struct compl{ double r; double i; }; compl sum (compl a, compl b){ compl z; z.r=a.r+b.r; z.i=a.i+b.i; return z; } compl diff (compl a, compl b){ compl z; z.r=a.r-b.r; z.i=a.i-b.i; return z; } compl prod(compl a, compl b){ compl z; z.i=(a.i*b.i)-(a.r*b.r); z.r=(a.i*b.r)+(a.r*b.i); return z; } compl quot(compl a, compl b){ compl z; compl c; compl d; compl e; c=b; c.r=-1*b.r; d=prod(a,c); e=prod(b,c); z.i=d.i/e.i; z.r=d.r/e.i; return z; } int main () { compl z; compl c; cout<<"Enter the real part of the 1st complex number: "; cin>>z.i; cout<<"Enter the imaginary part of the 1st complex number: "; cin>>z.r; cout<<"\n"; cout<<"Enter the real part of the 2nd complex number: "; cin>>c.i; cout<<"Enter the imaginary part of the 2nd complex number: "; cin>>c.r; cout<<"\n"; compl a=sum(z,c); compl b=diff(z,c); compl d=prod(z,c); compl e=quot(z,c); cout<<"The Sum is:\n"<<"\tThe real part is: "<<a.i<<"\n"<<"\tThe imaginary part is: "<<a.r<<"i \n"; cout<<"The Difference is:\n"<<"\tThe real part is: "<<b.i<<"\n"<<"\tThe imaginary part is: "<<b.r<<"i \n"; cout<<"The Product is:\n"<<"\tThe real part is: "<<d.i<<"\n"<<"\tThe imaginary part is: "<<d.r<<"i \n"; cout<<"The Quotient is:\n"<<"\tThe real part is: "<<e.i<<"\n"<<"\tThe imaginary part is: "<<e.r<<"i"; return 0; }