📜  设计一个具有实部和虚部数据成员的类“复杂”.提供默认和参数化的构造函数.编写一个程序来执行两个复数的算术运算. - Java 代码示例

📅  最后修改于: 2022-03-11 14:52:19.653000             🧑  作者: Mango

代码示例1
#include
using namespace std;
class Complex
{
 private:
  float real,img;
 public:
  Complex()
  {
   real=0;
   img=0;
  }
  void accept()
  {
   cout<<"Enter the complex number:"<<"\n";
   cout<<"Real:";
   cin>>real;
   cout<<"Imaginary:";
   cin>>img;
  }
  void display()
  {
   cout<<"complex number is:";
   cout<>ch;
 switch(ch)
 {
  case 1:     cout<<"Addition:";
         //c3=c4+c5;
         c3=operator+(c4,c5);
         c3.display();
         cout<<"\n";
         break;
  case 2:   cout<<"Substraction:";
        // c3=c4-c5;
         c3=operator-(c4,c5);
         c3.display();
        cout<<"\n";
        break;
  case 3:   cout<<"Multication:";
         c3=c4*c5;
         //c3=c4.operator*(c5);
         c3.display();
         cout<<"\n";
        break;
  case 4:    cout<<"Division:";
        // c3=c4/c5;
         c3=c4.operator/(c5);
         c3.display();
         cout<<"\n";
        break;
  default:
         cout<<"EXIT";
 }
 }while(ch<=4);
 return 0;
}