c_cpp 537.复数乘法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 537.复数乘法相关的知识,希望对你有一定的参考价值。

struct ComplexInt {
    int real;
    int imag;
    
    ComplexInt(string c) {
        int ppos = 0;
        while(c[ppos] != '+') ppos += 1;
        real = stoi(c.substr(0, ppos));
        imag = stoi(c.substr(ppos + 1, c.size() - ppos - 2)); // start after '+' and end before 'i'
    }
    
    ComplexInt(int r, int i) : real(r), imag(i) {}
    
    string ToString() {
        return to_string(real) + "+" + to_string(imag) + "i";
    }
    
    ComplexInt operator *(const ComplexInt& rhs) const {
        return ComplexInt((real * rhs.real) - (imag * rhs.imag), (real * rhs.imag) + (imag * rhs.real));
    }
};

class Solution {
public:
    string complexNumberMultiply(string a, string b) {
        return (ComplexInt(a) * ComplexInt(b)).ToString();
    }
};

以上是关于c_cpp 537.复数乘法的主要内容,如果未能解决你的问题,请参考以下文章

java 537.复数乘法(1st).java

java 537.复数乘法(1st).java

java 537.复数乘法(1st).java

java 537.复数乘法(1st).java

java 537.复数乘法(1st).java

字符串537. 复数乘法