008:超简单的复数类

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdlib>
 4 using namespace std;
 5 class Complex {
 6 private:
 7     double r,i;
 8 public:
 9     void Print() {
10         cout << r << "+" << i << "i" << endl;
11     }
12 Complex(){}
13     Complex(const char* str){
14         r = str[0] - '0';
15         i = str[2] - '0';
16     } 
17 };
18 int main() {
19     Complex a;
20     a = "3+4i"; a.Print();
21     a = "5+6i"; a.Print();
22     return 0;
23 }

 

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdlib>
 4 using namespace std;
 5 class Complex {
 6 private:
 7     double r,i;
 8 public:
 9     void Print() {
10         cout << r << "+" << i << "i" << endl;
11     }
12  Complex(){}
13     Complex& operator=(const char* str){
14         r = str[0] - '0';
15         i = str[2] - '0';
16         return *this;
17     } 
18 };
19 int main() {
20     Complex a;
21     a = "3+4i"; a.Print();
22     a = "5+6i"; a.Print();
23     return 0;
24 }

posted @ 2022-09-13 22:30  balabalahhh  阅读(35)  评论(0编辑  收藏  举报