/*Louisda16th/Ashwith*/ #include<stdio.h> #include<math.h> void pol(); void cart(); void arithmetic(int oper); void powr(); void root(); struct compcart { double real; /*structure for cartesian form*/ double img; }; struct comppol { double abs; /*structure for polar form*/ double arg; }; int main() { int oper; printf("n Select an option(0 to quit): n 1 - Addn 2 - Subtractn 3 - Multiplyn 4 - Dividen 5 - Powern 6 - Roots"); printf("n 7 - Convert Polar to Cartesian form n 8 - Convert Cartesian to Polar form n"); scanf("%d",&oper); switch(oper) { case 0: /*Goes to the appropriate function depending on the option*/ return; break; case 1: case 2: case 3: case 4: arithmetic(oper); break; case 5: powr(); break; case 6: root(); break; case 7: cart(); break; case 8: pol(); break; default: main(); } return 0; } void arithmetic(int oper) { struct compcart z1, z2, z; printf("n Enter two Complex Numbers (x+iy):n x1?"); scanf("%lf",&z1.real); printf("n y1?"); scanf("%lf",&z1.img); printf("n x2?"); scanf("%lf",&z2.real); printf("n y2?"); scanf("%lf",&z2.img); switch(oper) { case 1: z.real = z1.real+z2.real; /*addition*/ z.img = z1.img+z2.img; break; case 2: z.real=z1.real-z2.real; /*subtraction*/ z.img=z1.img-z2.img; break; case 3: z.real=(z1.real*z2.real)-(z1.img*z2.img); /*multiplication*/ z.img=(z1.real*z2.img)+(z1.img*z2.real); break; case 4: /*division*/ z.real=((z1.real*z2.real)+(z1.img*z2.img))/(pow(z2.real,2)+pow(z2.img,2)); z.img=((z1.img*z2.real)-(z1.real*z2.img))/(pow(z2.real,2)+pow(z2.img,2)); break; } if (z.img>0) printf("n Answer = %lf+%lfi",z.real,z.img); /*Had to add this to print x+iy and*/ else /*x-iy dependin on the sign of y*/ printf("n Answer = %lf%lfi",z.real,z.img); main(); } void pol() { struct compcart z; struct comppol r; printf("Enter a Complex Number (x+iy):n x?"); scanf("%lf",&z.real); printf("n y?"); scanf("%lf",&z.img); r.abs=sqrt(pow(z.real,2)+pow(z.img,2)); /*find absolute value*/ r.arg=atan((z.img/z.real)); /*find argument*/ printf("n The Complex Number in Polar Form is: %lf(cos(%lf)+isin(%lf)) n",r.abs,r.arg,r.arg); } void cart() { struct compcart z; struct comppol r; printf("n Enter a Complex Number In Polar Form r,%c.n r(Absolute Value)?",233); scanf("%lf",&r.abs); printf("n %c(Argument, In Radians)?",233); scanf("%lf",&r.arg); z.real=r.abs*cos(r.arg); /*find real part*/ z.img=r.abs*sin(r.arg); /*find imaginary part*/ if (z.img>0) printf("n The cartesian form of the complex number is: %lf+%lfi n",z.real,z.img); else printf("n The cartesian form of the complex number is: %lf%lfi n",z.real,z.img); } void powr() { struct compcart z,u; /*this function basically uses De-Moivre's*/ struct comppol r, v; /* theorem for complex numbers. First the*/ int exp; /*numbers converted to polar form*/ printf("n Enter a Complex Number In Cartesian form (x+iy). n x?"); scanf("%lf",&z.real); printf("n y?"); scanf("%lf",&z.img); r.abs=sqrt(pow(z.real,2)+pow(z.img,2)); /*conversion to polar*/ r.arg=atan((z.img/z.real)); printf("n Exponent?"); scanf("%d",&exp); v.abs=pow(r.abs,exp); /*use of De-Moivre's theorem*/ v.arg=(r.arg*exp); u.real=v.abs*cos(v.arg); u.img=v.abs*sin(v.arg); if (u.img>0) printf("n Answer = %lf+%lfi",u.real,u.img); else printf("n Answer = %lf%lfi",u.real,u.img); } void root() { struct compcart z,u; struct comppol r,v; int root, i; printf("n Enter a Complex Number In Cartesian form (x+iy). n x?"); scanf("%lf",&z.real); printf("n y?"); scanf("%lf",&z.img); r.abs=sqrt(pow(z.real,2)+pow(z.img,2)); /*This function first converts the number to*/ r.arg=atan((z.img/z.real)); /*polar form and then uses the formula for finding*/ /*the nth roots of the number*/ printf("n Order of Root?"); scanf("%d",&root); printf("n Roots:"); for(i=0;i<=(root-1);i++) { v.abs=pow(r.abs,1.0/root); v.arg=((2.0*i*3.141592654)+r.arg)/root; u.real=v.abs*cos(v.arg); u.img=v.abs*sin(v.arg); if (u.img>0) printf("n %lf+%lfi",u.real,u.img); else printf("n %lf%lfi",u.real,u.img); } }
Complex Number Calculator
Page 1 of 10 Replies - 401 Views - Last Post: 09 August 2006 - 06:56 AM
#1
Complex Number Calculator
Posted 09 August 2006 - 06:56 AM
Description: just compile, build and runA simple calculator which performs simple arithmetic, n finds roots of complex numbers. Also it can convert polar to cartesian form n vice-versa.
Page 1 of 1