美文网首页
Complex Number Multiplication

Complex Number Multiplication

作者: 风之羁绊 | 来源:发表于2017-10-12 15:19 被阅读0次

这道题虽然并不难,复数相乘的问题,但也可以回顾一下字符串和int相互转化的方法。
字符串一般有char数组和c++中string。
char 数组=string.c_str() (转化); string (char 数组)(强转)
对于char数组,atoi和itoa可以分别完成char字符串到数字和数字到char字符串的转化。
对于string,在c++11之前,常用的方法可以用输入,输出流stringstream来进行转化。
例如 void int2str(int &int_temp,string &string_temp)
{
std::stringstream ss;
ss<<int_temp;
ss>>string_temp;
}
void str2num(int &int_temp,string& string_temp){
stringstream ss(string_temp);
ss >> int_temp;
}
当然,也可以把int换成double等等形式,但可能会有精度损失。
在c++11中,又推出来to_string,stoi等等新的直接从string到xx和xx到string的方法。详见http://blog.csdn.net/aquester/article/details/27672521
然后这道题第二个问题是字符串的分割的问题:
这个问题处理的方法也是不少的,我们可以通过各种方法,最麻烦的就是写个循环,然后把子串分割开来。也可以使用stl或者c的自带函数。下面这个代码(后面题解中)我觉得写的还是比较漂亮的。
https://pastebin.com/UfwUvprY

相关文章

网友评论

      本文标题:Complex Number Multiplication

      本文链接:https://www.haomeiwen.com/subject/lmxoyxtx.html