LeetCode OJ-537.Complex Number Multiplication

来源:互联网 发布:2016年十大网络神曲 编辑:程序博客网 时间:2024/05/17 15:56

LeetCode OJ-537.Complex Number Multiplication

题目描述

Given two strings representing two complex numbers.

You need to return a string representing their multiplication. Note i2 = -1 according to the definition.

Example 1:

Input: "1+1i", "1+1i"Output: "0+2i"Explanation: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i, and you need convert it to the form of 0+2i.

Example 2:

Input: "1+-1i", "1+-1i"Output: "0+-2i"Explanation: (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i, and you need convert it to the form of 0+-2i.

Note:

  1. The input strings will not have extra blank.
  2. The input strings will be given in the form of a+bi, where the integer a and b will both belong to the range of [-100, 100]. And the output should be also in this form.

题目理解

​ 简单来说就是求复数乘法运算,本题的重点在于将字符串中复数的实部虚部获取出来。至于实际运算,找关系式就好了。对于获取实部虚部,这里建议使用状态机处理,否则太多if-else会比较头疼。获取的关键部分,在代码中给了注释,具体可以参考代码。

Code

// 获取实部虚部void get_real_vir(const string &s, int &real, int &vir){    // 解析字符串时使用的状态机    enum parse_state {        ps_start = 0,        ps_real_symbol,        ps_real,        ps_plus,        ps_vir_symbol,        ps_vir,        ps_end    } state;    state = ps_start;    int i, j, k;    char sys_real1 = 0, sys_vir1 = 0;  // 记录实部虚部的符号,0 + 1 -    int real1 = 0, vir1 = 0;    j = 0;    k = 0;    stack<char> real_stack;    stack<char> vir_stack;    for (i = 0; i < s.length(); ++i) {        switch (state) {            case ps_start:                if (s[i] == '-') {                    sys_real1 = 1;                    state = ps_real_symbol;                }                else {                    sys_real1 = 0;                    state = ps_real;                    real_stack.push(s[i]);  // 处理实部虚部一定要做压栈,                                            // 否则多位数的实部虚部无法正确处理                                            // 直接乘10的j次幂,68会变86                                            // 因为字符串遍历是从百位十位开始的                                            // 需要保存各个位置的数再依次处理                }                break;            case ps_real_symbol:                state = ps_real;                real_stack.push(s[i]);                break;            case ps_real:                if (s[i] == '+') {                    while (!real_stack.empty()) {                        real1 += (real_stack.top() - '0') * std::pow(10, j++);                        real_stack.pop();                    }                    real1 *= (sys_real1 == 0 ? 1 : -1);                    state = ps_plus;                }                else {                    real_stack.push(s[i]);                }                break;            case ps_plus:                if (s[i] == '-') {                    state = ps_vir_symbol;                    sys_vir1 = 1;                }                else {                    state = ps_vir;                    vir_stack.push(s[i]);                }                break;            case ps_vir_symbol:                vir_stack.push(s[i]);                state = ps_vir;                break;            case ps_vir:                if (s[i] == 'i') {                    state = ps_end;                    while (!vir_stack.empty()) {                        vir1 += (vir_stack.top() - '0') * std::pow(10, k++);                        vir_stack.pop();                    }                    vir1 *= (sys_vir1 == 0 ? 1 : -1);                }                else {                    vir_stack.push(s[i]);                }                break;            case ps_end:                break;            default:                break;        }    }    real = real1;    vir = vir1;}string complex_number_multiply(const string &s1, const string &s2){    string res;    int real1, vir1;    get_real_vir(s1, real1, vir1);    int real2, vir2;    get_real_vir(s2, real2, vir2);    int a = real1 * real2;    int b = (real1 * vir2 + real2 * vir1);    int c = vir1 * vir2;    int real = a + (-1) * c;    int vir = b;    string real_str;    stack<char> real_stack;    if (real < 0) {        real_str.push_back('-');        real *= -1;    }    else if (real == 0) {        real_str += "0";    }    while (real > 0) {        real_stack.push((real % 10) + '0');        real /= 10;    }    while (!real_stack.empty()) {        real_str.push_back(real_stack.top());        real_stack.pop();    }    string vir_str;    stack<char> vir_stack;    if (vir < 0) {        vir_str.push_back('-');        vir *= -1;    }    else if (vir == 0) {        vir_str += "0";    }    while (vir > 0) {        vir_stack.push((vir % 10) + '0');        vir /= 10;    }    while (!vir_stack.empty()) {        vir_str.push_back(vir_stack.top());        vir_stack.pop();    }    res = real_str + "+" + vir_str + "i";    cout << res << endl;    return res;}
0 0
原创粉丝点击