I'm having trouble reading in some numbers to calculate different operations using imaginary numbers. This is what I got:
#include<stdio.h>
struct complex
{
int r,i;
};
void read(struct complex *);
struct complex add(struct complex *,struct complex *);
struct complex sub(struct complex *,struct complex *);
struct complex mul(struct complex *,struct complex *);
struct complex div(struct complex*x,struct complex *y);
void write(struct complex *);
void main()
{
struct complex a,b,c,d,e,f;
char choice;
choice = read();
//clrscr();
read(&a);
read(&b);
c=add(&a,&b);
d=sub(&a,&b);
e=mul(&a,&b);
f=div(&a,&b);
write(&c);
write(&d);
write(&e);
write(&f);
getchar();
getchar();
}
char read(struct complex *x,char choice)
{
scanf("%d+%d[^i] %c %d+%d[^i]",&(x->r),&(x->i),&choice);
scanf("%d",&(x->r));
scanf("%d",&(x->i));
return choice;
}
struct complex add(struct complex *x,struct complex *y)
{
struct complex z;
z.r=x->r+y->r;
z.i=x->i+y->i;
return(z);
}
struct complex sub(struct complex *x,struct complex *y)
{
struct complex z;
z.r=x->r-y->r;
z.i=x->i-y->i;
return(z);
}
struct complex mul(struct complex *x,struct complex *y)
{
struct complex z;
z.r=(x->r)*(y->r)-(x->i)*(y->i);
z.i=(x->r)*(y->i)+(x->i)*(y->r);
return(z);
}
struct complex div(struct complex*x,struct complex *y)
{
struct complex z;
z.r=((x->r)*(y->r)+(x->i)*(y->i))/((y->r)*(y->r)+(y->i)*(y->i));
z.i=((x->r)*(y->i)-(x->i)*(y->r))/((y->i)*(y->i)+(y->i)*(y->r));
}
void write(struct complex *x)
{
printf("\n %d+%di",x->r,x->i);
}
I want is to read something like: 2+3i + 4+5i
and do all the operations