1 #include <iostream> 2 #include <cstdlib> 3 #include <limits> 4 using namespace std; 5 6 class MyComplex{ 7 8 double a; 9 double b; 10 11 public: 12 13 MyComplex(double a = 0.0, double b = 0.0){ 14 this->a = a; 15 this->b = b; 16 } 17 18 MyComplex operator+(const MyComplex& mc); 19 MyComplex operator-(const MyComplex& mc); 20 MyComplex operator*(const MyComplex& mc); 21 MyComplex operator/(const MyComplex& mc); 22 /* 重载<< , const不能少,否则会出错 */ 23 friend ostream& operator<<(ostream &stream, const MyComplex& mc); 24 friend istream& operator>>(istream &stream, MyComplex& mc); 25 26 }; 27 28 ostream& operator <<(ostream &stream, const MyComplex& mc){ 29 stream << "(" << mc.a; 30 stream.setf(std::ios::showpos); 31 stream << mc.b << "i)"; 32 stream.unsetf(std::ios::showpos); 33 return stream; 34 } 35 36 istream& operator>>(istream &stream, MyComplex& mc){ 37 stream >> mc.a >> mc.b; 38 return stream; 39 } 40 41 MyComplex MyComplex::operator+(const MyComplex& mc){ 42 MyComplex temp; 43 temp.a = a + mc.a; 44 temp.b = b + mc.b; 45 return temp; 46 } 47 48 MyComplex MyComplex::operator-(const MyComplex& mc){ 49 MyComplex temp; 50 temp.a = a - mc.a; 51 temp.b = b - mc.b; 52 return temp; 53 } 54 55 MyComplex MyComplex::operator*(const MyComplex& mc){ 56 MyComplex temp; 57 temp.a = a * mc.a - b*mc.b; 58 temp.b = b * mc.a + a*mc.b; 59 return temp; 60 } 61 62 MyComplex MyComplex::operator/(const MyComplex& mc){ 63 if ((mc.a == 0) && (mc.b == 0)) { 64 cout << "Divisor can not be 0"; 65 exit(0); 66 } 67 MyComplex temp; 68 temp.a = (a*mc.a + b*mc.b) / (mc.a*mc.a + mc.b*mc.b); 69 temp.b = (b*mc.a - a*mc.b) / (mc.a*mc.a + mc.b*mc.b); 70 return temp; 71 } 72 73 int main() { 74 MyComplex z1, z2; 75 cin >> z1; 76 cin >> z2; 77 cout << "z1 + z2 = " << z1 + z2 << endl; 78 cout << "z1 - z2 + z1 = " << z1 - z2 + z1 << endl; 79 cout << "z1 * z2 - z1 = " << z1 * z2 - z1 << endl; 80 cout << "z1 / z2 + z1 = " << z1 / z2 + z1 << endl; 81 cout << "z2 - z1 / z1 = " << z2 - z1 / z1; 82 // GCC及VC编译器在调试模式下会暂停,便于查看运行结果 83 #if ( defined(__DEBUG__) || defined(_DEBUG) ) 84 cin.ignore(numeric_limits<streamsize>::max(), ' '); 85 cin.get(); 86 #endif 87 return 0; 88 }
1 #include <iostream> 2 #include <exception> 3 #include <limits> 4 #include <stdexcept> /*'runtime_error' was not declared in this scope*/ 5 using namespace std; 6 7 class MyComplex{ 8 9 double a; 10 double b; 11 12 public: 13 14 MyComplex(double a = 0.0, double b = 0.0){ 15 this->a = a; 16 this->b = b; 17 } 18 19 MyComplex operator+(const MyComplex& mc); 20 MyComplex operator-(const MyComplex& mc); 21 MyComplex operator*(const MyComplex& mc); 22 MyComplex operator/(const MyComplex& mc); 23 /* 重载<< , const不能少,否则会出错 */ 24 friend ostream& operator<<(ostream &stream, const MyComplex& mc); 25 friend istream& operator>>(istream &stream, MyComplex& mc); 26 27 }; 28 29 ostream& operator <<(ostream &stream, const MyComplex& mc){ 30 stream << "(" << mc.a; 31 stream.setf(std::ios::showpos); 32 stream << mc.b << "i)"; 33 stream.unsetf(std::ios::showpos); 34 return stream; 35 } 36 37 istream& operator>>(istream &stream, MyComplex& mc){ 38 stream >> mc.a >> mc.b; 39 return stream; 40 } 41 42 MyComplex MyComplex::operator+(const MyComplex& mc){ 43 MyComplex temp; 44 temp.a = a + mc.a; 45 temp.b = b + mc.b; 46 return temp; 47 } 48 49 MyComplex MyComplex::operator-(const MyComplex& mc){ 50 MyComplex temp; 51 temp.a = a - mc.a; 52 temp.b = b - mc.b; 53 return temp; 54 } 55 56 MyComplex MyComplex::operator*(const MyComplex& mc){ 57 MyComplex temp; 58 temp.a = a * mc.a - b*mc.b; 59 temp.b = b * mc.a + a*mc.b; 60 return temp; 61 } 62 63 MyComplex MyComplex::operator/(const MyComplex& mc){ 64 if ((mc.a == 0) && (mc.b == 0)) { 65 throw runtime_error("Divisor is 0"); 66 } 67 MyComplex temp; 68 temp.a = (a*mc.a + b*mc.b) / (mc.a*mc.a + mc.b*mc.b); 69 temp.b = (b*mc.a - a*mc.b) / (mc.a*mc.a + mc.b*mc.b); 70 return temp; 71 } 72 73 int main() { // 不可修改main函数中的代码,否则OJ将给你的程序打0分 74 MyComplex z1, z2; 75 cin >> z1; 76 cin >> z2; 77 try { 78 cout << "z1 + z2 = " << z1 + z2 << endl; 79 cout << "z1 - z2 + z1 = " << z1 - z2 + z1 << endl; 80 cout << "z1 * z2 - z1 = " << z1 * z2 - z1 << endl; 81 cout << "z1 / z2 + z1 = " << z1 / z2 + z1 << endl; 82 cout << "z2 - z1 / z1 = " << z2 - z1 / z1 << endl; 83 cout << "Finished"; 84 } 85 catch (exception& e) { // catch父类异常类型,也可以捕获子类异常 86 cout << e.what() << endl; // waht()函数将存放在异常对象中的信息取出来 87 cout << "Unexpected Error"; 88 } 89 // GCC及VC编译器在调试模式下会暂停,便于查看运行结果 90 #if ( defined(__DEBUG__) || defined(_DEBUG) ) 91 cin.ignore(numeric_limits<streamsize>::max(), ' '); 92 cin.get(); 93 #endif 94 return 0; 95 }