#include <iostream>
#include <cmath>
using namespace std;
class fs{
public:
int x = 0, y = 0;
fs(){}
fs(string s){
int m = 0, flag = 1, k = s.size();
for(int i = 0; i < k; i++)
{
if(s[i] == '-') x = m * flag, flag = -1, m = 0;
else if(s[i] == '+') x = m * flag, flag = 1, m = 0;
else if(s[i] >= '0' && s[i] <= '9') m = m * 10 + s[i] - '0';
else if(s[i] == 'i'){
if(m == 0) y = flag;
else y = m * flag;
}
}
if(y == 0) x = m * flag;
}
void display(){
if(x != 0){
cout << x;
if(y != 0){
if(y > 0) cout << '+';
if(abs(y) != 1) cout << y;
else if(y == -1) cout << '-';
cout << 'i';
}
}
else{
if(y != 0){
if(y == -1) cout << '-';
else if(y == 1) ;
else cout << y;
cout << 'i';
}
else cout << 0;
}
}
fs operator +(fs &a){
a.x += x, a.y += y;
return a;
}
};
int main()
{
string s;
fs a;
while(1){
cin >> s;
if(s == "0") break;
fs b(s);
a = a + b;
}
a.display();
return 0;
}
c++实现复数相加
最新推荐文章于 2023-04-05 11:12:13 发布
748

被折叠的 条评论
为什么被折叠?



