You are viewing a single comment's thread. Return to all comments →
using namespace std; class Complex{ public: int real,imag; void input(){ string input; cin >> input;
size_t i_pos = input.find('i'); if (i_pos != string::npos) { size_t sign_pos = input.find("+i"); if (sign_pos == string::npos) sign_pos = input.find("-i"); if (sign_pos != string::npos) { real = stoi(input.substr(0, sign_pos)); imag = stoi(input.substr(sign_pos + 2)); if (input[sign_pos] == '-') imag = -imag; } } } Complex operator+(const Complex& other ) { Complex result; result.real=this->real+other.real; result.imag=this->imag+other.imag; return result; } friend ostream& operator<<(ostream& os, const Complex& c) { os << c.real << "+i" << c.imag; return os; }
};
int main() { Complex x,y; Complex result; x.input(); y.input(); result=x+y; cout<
Seems like cookies are disabled on this browser, please enable them to open this website
Overload Operators
You are viewing a single comment's thread. Return to all comments →
include
include
include
include
include
using namespace std; class Complex{ public: int real,imag; void input(){ string input; cin >> input;
};
int main() { Complex x,y; Complex result; x.input(); y.input(); result=x+y; cout<