首页 > 编程知识 正文

finally的复数是什么,finally和finalize的区别

时间:2023-05-04 13:23:58 阅读:226932 作者:612

请定义一个复数类,实数和虚数是其私有数据成员。再定义一个>(大于号)的运算符重载函数,用于比较两个复数间模的大小。

 

1 #include<iostream> 2 using namespace std; 3 4 class Complex //定义Comlpex类 5 { 6 private: 7 int i,j; 8 public: 9 friend bool operator > (Complex &C1,Complex &C2); //友元函数 >10 friend istream& operator >> (istream &input, Complex &C); //友元函数 >> 输入流11 friend ostream& operator << (ostream &output, Complex &C); //友元函数 << 输出流12 Complex(void){}; //默认构造函数13 Complex(int ii,int 拼搏的蜻蜓):i(ii),j(拼搏的蜻蜓){}; //构造函数14 friend bool isEnd(Complex &C1,Complex &C2); //友元函数 判断是否停止输入15 };16 17 bool operator > (Complex &C1,Complex &C2)18 {19 if(C1.i*C1.i+C1.j*C1.j>C2.i*C2.i+C2.j*C2.j) return true; //计算是否大于20 else return false;21 }22 23 istream& operator >> (istream &input,Complex &C) //输入时调用24 {25 input>>C.i>>C.j;26 return input;27 }28 29 ostream& operator << (ostream &output,Complex &C) //输出时调用30 {31 output<<C.i;32 if(C.j>=0) output<<"+";33 output<<C.j<<"i";34 return output;35 }36 37 bool isEnd(Complex &C1,Complex &C2) //判断是否停止输入38 {39 if(!C1.i&&!C1.j&&!C2.i&&!C2.j) return true;40 else return false;41 }42 43 int main()44 {45 Complex C1,C2;46 cin>>C1>>C2; //调用Complex的输入流函数47 while(!isEnd(C1,C2)) //判断是否停止输入48 {49 if(C1>C2) cout<<"true"<<endl;50 else cout<<"false"<<endl;51 cin>>C1>>C2;52 }53 return 0;54 }

 

转载于:https://www.cnblogs.com/wzzdeblog/p/10722775.html

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。