#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//kadai22
//calculation of complex
//1.define struct
typedef struct complex {
double re_part;
double im_part;
} COMPLEX;
//2.other functions
//2.1.*c_new generates new complex
COMPLEX *c_new(double re, double im)
{
COMPLEX *z = (COMPLEX *)malloc(sizeof(COMPLEX));
z->re_part = re;
z->im_part = im;
return z;
}
//2.2.*c_read
COMPLEX *c_read(){
double re,im;
printf("RE_PART:");
scanf("%lf",&re);
printf("IM_PART:");
scanf("%lf",&im);
return c_new(re,im);
}
//2.3.*c_display
void c_display(COMPLEX *z){
printf("(%f + %f*i)", z->re_part, z->im_part);
}
//2.4.addition
COMPLEX *c_plus(COMPLEX *x, COMPLEX *y){
return c_new(x->re_part + y->re_part, x->im_part + y->im_part);
}
//2.5.substraction
COMPLEX *c_minus(COMPLEX *x, COMPLEX *y){
return c_new(x->re_part - y->re_part, x->im_part - y->im_part);
}
//2.6.multiplication
//(A+Bi)*(C+Di)=(AC-BD)+(AD-BC)i
COMPLEX *c_cross(COMPLEX *x, COMPLEX *y){
return c_new((x->re_part * y->re_part) - (x->im_part * y->im_part),
(x->re_part * y->im_part) + (x->im_part * y->re_part));
}
//2.7.division
COMPLEX *c_slash(COMPLEX *x, COMPLEX *y){
return c_new(((x->re_part * y->re_part) + (x->im_part * y->im_part))/(pow(y->re_part,2) + pow(y->im_part,2)),
((x->im_part * y->re_part) - (x->re_part * y->im_part))/(pow(y->re_part,2) + pow(y->im_part,2)));
}
//3.main
int main(){
//3.0.preparation
COMPLEX *zf = c_new(0,0);//object1
COMPLEX *z1;//object2
COMPLEX *za;//put answer
int op;//operator sign
char opp;//printed operator
INI://3.1.enter the first complex
printf("Enter the first complex.\n");
zf = c_read();
c_display(zf);
printf("\n");
while(1){//basic calculation line starts
//3.2.enter the operator sign and check it
op=6;//When you back by "goto", without this initialization, you couldn't enter the next operator sign.
while((op != 1)&&(op != 2)&&(op != 3)&&(op !=4)&&(op != 7)&&(op != 0)){
printf("Enter the operator sign number.\n1 : +\n2 : -\n3 : *\n4 : /\n7 : clear\n0 : exit\n");
scanf("%d",&op);
}
if(op == 0){//0:exit
printf("Bye\n");
return 0;
}
if(op == 7){//7:clear
za = c_new(0,0);
goto INI;
}
//3.3.enter the next complex
printf("Enter the next complex.\n");
z1 = c_read();
c_display(z1);
printf("\n");
//3.4.calculation
if(op == 1){
za = c_plus(zf,z1);
opp = '+';
}
if(op == 2){
za = c_minus(zf, z1);
opp = '-';
}
if(op == 3){
za = c_cross(zf, z1);
opp = '*';
}
if(op == 4){
za = c_slash(zf, z1);
opp = '/';
}
//3.5.print result
printf("\n");
c_display(zf);
printf(" ");
printf("%c",opp);
printf(" ");
c_display(z1);
printf("\n");
printf(" = ");
c_display(za);
printf("\n\n");
//3.6.prepare for the next loop
*zf=*za;
}//basic calculation line ends here
return 0;
}
Tuesday, January 26, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment