自学内容网 自学内容网

C语言 | Leetcode C语言题解之第537题复数乘法

题目:

题解:

bool parseComplexNumber(const char * num, int * real, int * image) {
    char *token = strtok(num, "+");
    *real = atoi(token);
    token = strtok(NULL, "i");
    *image = atoi(token);
    return true;
};

char * complexNumberMultiply(char * num1, char * num2){
    int real1 = 0, imag1 = 0;
    int real2 = 0, imag2 = 0;
    char * res = (char *)malloc(sizeof(char) * 20);
    parseComplexNumber(num1, &real1, &imag1);
    parseComplexNumber(num2, &real2, &imag2);
    snprintf(res, 20, "%d+%di", real1 * real2 - imag1 * imag2, real1 * imag2 + imag1 * real2);
    return res;
}

原文地址:https://blog.csdn.net/m0_59237910/article/details/143474486

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!