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:

The input strings will not have extra blank.
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.
##思路
这道题目本质上是求解实数相乘,输入格式是a+bi形式的字符串格式。我们花了不到一分钟的时间就构造出了解决问题的总体架构:

  • 首先将每个字符串的整数部分分隔开
  • 然后按照实数相乘的法则,求对应部分的乘积和
  • 最后将结果以字符串形式表示

那么我们很明显,略显麻烦的是字符串的分割,我们可以使用sring类的成员函数split,但是感觉这还是不够友好,幸好,C++STL提供了stringstream类,这种类方法可以实现各种输入输出流的转换及分割。嗯,我们先来了解下stringstream。
###stringstream
C++引入了ostringstream、istringstream、stringstream这三个类,要使用他们创建对象就必须包含sstream.h头文件。

istringstream类用于执行C++风格的串流的输入操作。
ostringstream类用于执行C风格的串流的输出操作。
strstream类同时可以支持C风格的串流的输入输出操作。

istringstream类是从istream和stringstreambase派生而来,ostringstream是从ostream和 stringstreambase派生而来, stringstream则是从iostream类和stringstreambase派生而来。
他们的继承关系如下图所示:
stringstream
所以stringstream,能够根据给定类型的变量,在stringstream内部做好分割。我们结合该题的代码,做进一步了解。
##代码

class Solution {
public:
    string complexNumberMultiply(string a, string b) {
        // define the var
        int an,ai,bn,bi;
        stringstream aa(a);
        stringstream bb(b);
        char plus;
        stringstream res;
        // split
        aa>>an>>plus>>ai;
        bb>>bn>>plus>>bi;
        res<<an*bn-ai*bi<<"+"<<an*bi+bn*ai<<"i";
        return res.str(); 
    }
};

代码:

aa>>an>>plus>>ai;
bb>>bn>>plus>>bi;

这里stringstream对象将遇到的第一个整数输入给an,字符‘+'输入给plus,第二个整数输入给ai。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Roaring Kitty

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值