+ -
当前位置:首页 → 问答吧 → 求帮忙修改

求帮忙修改

时间:2011-12-11

来源:互联网

#include <iostream>
using namespace std;
class Complex
{
public:
Complex()
{real = 0; imag = 0;}
Complex(double r, double i)
{ real = r; imag = i;}
Complex operator + (Complex &c2);
  Complex operator - (Complex &c2);

void display();
  private:
double real;
double imag;
};
  Complex Complex::operator +(Complex &c2)
  { Complex c;
  c.real = real + c2.real;
  c.imag = imag + c2.imag;
  return c;}
  Complex Complex::operator - (Complex &c2)
  {
  Complex c;
c.real = real - c2.real;
c.imag = imag - c2.imag;
return c;
}

void Complex::display()
{ cout<< "(" << real << "," << imag << "i)" <<endl;
  }
  int main()
{ Complex c1(3, 4), c2(5, -10), c3;
c3 = c1 + c2;
cout << "c1 + c2 = ";
c3.display();
c3 = c1 - c2;
cout << "c1 - c2 = ";
c3.display();

return 0;
}

我想改成数据是自己输入而不是固定的。
如何使用cin啊。

作者: youzhibc   发布时间: 2011-12-11

C++的STL本身就有complex类的
C/C++ code

#include <iostream>
#include <complex> 
using namespace std;
 int main()
{
complex<double> c1, c2, c3;
cin >> c1 >> c2;
c3 = c1 + c2;
cout << "c1 + c2 = ";
cout << c3 << endl;
c3 = c1 - c2;
cout << "c1 - c2 = ";
cout << c3 << endl;

return 0;
}



输入:
(3,4) (5,-10)
输出
c1 + c2 = (8,-6)
c1 - c2 = (-2,14)

作者: keiy   发布时间: 2011-12-11

实数部分和序数部分分开输入就好了,如
C/C++ code
int x,y;
cin>>x>>y;

作者: mscf   发布时间: 2011-12-11

谢谢,不过你这改变了我原本的结构了哇。 我必须用到构造函数的。
我想要的是针对这个代码,加一步能cin r i的。
引用 1 楼 keiy 的回复:

C++的STL本身就有complex类的
C/C++ code

#include <iostream>
#include <complex>
using namespace std;
int main()
{
complex<double> c1, c2, c3;
cin >> c1 >> c2;
c3 = c1 + c2;
cout << "c1 + c2 = ";
cout << ……

作者: youzhibc   发布时间: 2011-12-11

能具体点么,
引用 2 楼 mscf 的回复:

实数部分和序数部分分开输入就好了,如
C/C++ code
int x,y;
cin>>x>>y;

作者: youzhibc   发布时间: 2011-12-11