解题思路:
注意事项:
参考代码:
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> typedef struct complex { double re; double im; }complex; enum Option { ADD = 43, SUB=45, MUL=42, DIV=47 }; complex* Add(complex* ret,complex c1, complex c2) { ret->re = c1.re + c2.re; ret->im = c1.im + c2.im; return ret; } complex* Sub(complex* ret, complex c1, complex c2) { ret->re = c1.re - c2.re; ret->im = c1.im - c2.im; return ret; } complex* Mul(complex* ret, complex c1, complex c2) { ret->re = c1.re*c2.re -c1.im*c2.im ; ret->im = c1.re*c2.im+ c1.im*c2.re; return ret; } complex* Div(complex* ret, complex c1, complex c2) { ret->re = (c1.re * c2.re +c1.im * c2.im)/(c2.re*c2.re+c2.im*c2.im); ret->im = (c1.im * c2.re- c1.re * c2.im)/(c2.re * c2.re + c2.im * c2.im); return ret; } int main() { complex* ret = (complex*)malloc(1 * sizeof(complex)); if (ret == NULL) { return; } char x = 0; complex c1; complex c2; scanf("%c %lf %lf %lf %lf", &x, &c1.re, &c1.im, &c2.re, &c2.im); switch (x) { case ADD: ret = Add(ret,c1, c2); break; case SUB: ret = Sub(ret,c1, c2); break; case MUL: ret = Mul(ret,c1, c2); break; case DIV: ret = Div(ret,c1, c2); break; } printf("%0.2lf+%0.2lfi", ret->re, ret->im); free(ret); ret = NULL; return 0; }
0.0分
2 人评分