c </complex.h>中<complex.h>中的宏

时间:2014-12-21 09:19:10

标签: c complextype

我写了一个关于复数的减法,加法,乘法和除法的代码。

输入:
1 + 2i 2 + 2i +
输出:
3 + 4I
我使用visual studio 2013.
它显示关于I的错误消息:表达式必须具有算术类型 在线:z1 = real+imag*I; z2 = real+imag*I;
我该怎么做才能解决这个问题?

这是我的代码:

#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;
}

0 个答案:

没有答案