21xrx.com
2024-04-26 12:11:51 Friday
登录
文章检索 我的文章 写文章
两个复数相加的C++程序
2021-07-08 15:12:50 深夜i     --     --
C + +

将两个复数相加的 C++ 程序。

 

#include <iostream>


using namespace std;

class complex
{
   public :
      int real, img;
};

int main()
{
   complex a, b, c;

   cout << "Enter a and b where a + ib is the first complex number.";
   cout << "\na = ";
   cin >> a.real;
   cout << "b = ";
   cin >> a.img;
   cout << "Enter c and d where c + id is the second complex number.";
   cout << "\nc = ";
   cin >> b.real;
   cout << "d = ";
   cin >> b.img;
   
   c.real = a.real + b.real;
   c.img = a.img + b.img;

   if (c.img >= 0)
      cout << "Sum of two complex numbers = " << c.real << " + " << c.img << "i";
   else
      cout << "Sum of two complex numbers = " << c.real << " " << c.img << "i";

   return 0;
}

 

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复