I write a code about the complex number's subtraction,addition,multiplication and division.
input:
1+2i 2+2i +
output:
3+4i
I use visual studio 2013.
It show the wrong message about I : expression must have arithmetic type.
In line:z1 = real+imag*I; z2 = real+imag*I;
What should I do to solve this?
This is my code :
#include<stdio.h>
#include<complex.h>
#include<stdlib.h>
#define N 100
double ToNum(char *c);
int main(){
_Dcomplex z1, z2, z3;
char c1[N], c2[N];
char *p = c1;
char op;
double imag, real,op1, op2;
scanf("%s%s%c", c1, c2, &op);
real = ToNum(c1);
while (*p != '+' && *p != '-')
p++;
op1= *p == '+' ? 1 : -1;
imag = ToNum(++p);
imag *= op1;
z1 = real+imag*I;
real = ToNum(c2);
while (*p != '+' && *p != '-')
p++;
op2 = *p == '+' ? 1 : -1;
imag = ToNum(++p);
imag *= op2;
z2 = real + imag*I;
if (op == '+')
z3 = z1 + z2;
else if (op == '-')
z3 = z1 - z2;
else if (op = '*')
z3 = z1*z2;
else if (op == '/')
z3 = z1 / z2;
printf("%lf%+lfi\n", creal(z3), cimag(z3));
return 0;
}
double ToNum(char *c)
{
double num = 0;
num = atof(c);
return num;
}