#include <stdio.h> #include <stdlib.h>
typedef struct { float a; float b; } COMPLEX;
COMPLEX *comp_add(COMPLEX *a, COMPLEX *b) { a->a += b->a; a->b += b->b; return a; }
COMPLEX *comp_substract(COMPLEX *a, COMPLEX *b) { a->a -= b->a; a->b -= b->b; return a; }
COMPLEX *comp_multiply(COMPLEX *a, COMPLEX *b) { COMPLEX *c = (COMPLEX *)malloc(sizeof(COMPLEX)); c->a = a->a * b->a - a->b * b->b; c->b = a->b * b->a + a->a * b->b; *a = *c; free(c); return a; }
COMPLEX *comp_divide(COMPLEX *a, COMPLEX *b) { COMPLEX c = *b; c.b = -c.b; comp_multiply(a, &c); comp_multiply(b, &c); a->a /= b->a; a->b /= b->a; return a; }
int main() { COMPLEX a, b; char opr; scanf("%c%f%f%f%f", &opr, &a.a, &a.b, &b.a, &b.b); switch (opr) { case '-': comp_substract(&a, &b); break; case '*': comp_multiply(&a, &b); break; case '/': comp_divide(&a, &b); break; default: case '+': comp_add(&a, &b); break; } printf("%.2f+%.2fi\n", a.a, a.b); return 0; }
|