1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#include <stdio.h> #include <math.h> struct data { double num1; double num2; }; char item[4]="+-*/"; void myprintf(double num1,double num2,int flag) { if(flag){ if(num1==0&&num2==0) printf("0.0"); else{ if(num1!=0&&num2==0) printf("%.1f",num1); else if(num1==0&&num2!=0) printf("%.1fi",num2); else{ printf("%.1f",num1); if(num2>0) printf("+%.1fi",num2); else printf("%.1fi",num2); } } } else{ if(num2<0){ printf("%.1f%.1fi",num1,num2); }else{ printf("%.1f+%.1fi",num1,num2); } }
} double simpl(double num){ if(num>0) num=(double)floor((num+0.05)*10)/10; else if(num<0){ num=fabs(num); num=-(double)floor((num+0.05)*10)/10; } return num; } void getresult(struct data cal[],int time) { double temp1,temp2,temp; switch(time) { case 0:cal[2].num1=cal[0].num1+cal[1].num1; cal[2].num2=cal[0].num2+cal[1].num2; break; case 1:cal[2].num1=cal[0].num1-cal[1].num1; cal[2].num2=cal[0].num2-cal[1].num2; break; case 2:cal[2].num1=cal[0].num1*cal[1].num1-cal[0].num2*cal[1].num2; cal[2].num2=cal[0].num1*cal[1].num2+cal[0].num2*cal[1].num1; break; case 3:temp1=cal[0].num1*cal[1].num1+cal[0].num2*cal[1].num2; temp2=cal[0].num2*cal[1].num1-cal[0].num1*cal[1].num2; temp=cal[1].num2*cal[1].num2+cal[1].num1*cal[1].num1; cal[2].num1=temp1/temp; cal[2].num2=temp2/temp; break; } cal[2].num1=simpl(cal[2].num1); cal[2].num2=simpl(cal[2].num2); myprintf(cal[2].num1,cal[2].num2,1); printf("\n");
} int main() { struct data cal[3]; for(int i=0;i<=1;i++){ scanf("%lf%lf",&cal[i].num1,&cal[i].num2); } for(int i=0;i<4;i++){ printf("("); myprintf(cal[0].num1,cal[0].num2,0); printf(")"); printf(" %c ",item[i]); printf("("); myprintf(cal[1].num1,cal[1].num2,0); printf(") = "); getresult(cal,i); } return 0; }
|