新微赢技术网

标题: 复数问题~ [打印本页]

作者: 若叶花吹雪    时间: 2009-11-4 00:24
标题: 复数问题~
头文件:
#ifndef COMPLEX_H_
#define COMPLEX_H_
class Complex0
{
private:
double real;
double imaginary;
public:
//construction
Complex0();
Complex0( double rea = 0.0, double ima = 0.0 );
~Complex0();
// set real,imag from coordinate.
void setReal( double rea );
void setImag( double ima );
Complex0 operator+( const Complex0 &a );
Complex0 operator-( const Complex0 &a );
Complex0 operator*( const Complex0 &a );
Complex0 operator*( double m );
}
#endif

函数实现:
#include <iostream>
using namespace std;
#include "complex.h"
Complex0::Complex0()
{
real = 0.0;
imaginary = 0.0;
}
Complex0::Complex0( double rea , double ima )
{
real = real;
imaginary = ima;
}
Complex0::~Complex0()
{
cout << "Bye1" << endl;
}
// set real,imag from coordinate.
void Complex0::setReal( double rea )
{
real = rea;
}
void Complex0::setImag( double ima )
{
imaginary = ima;
}
Complex0 Complex0::operator+( const Complex0 &a )
{
Complex0 ccc;
ccc.real = real + a.real;
ccc.imaginary = imaginary + a.imaginary;
return ccc;
}
Complex0 Complex0::operator-( const Complex0 &a )
{
Complex0 ccc;
ccc.real = real - a.real;
ccc.imaginary = imaginary - a.imaginary;
return ccc;
}
Complex0 Complex0::operator*( const Complex0 &a )
{
Complex0 ccc;
ccc.real = real * a.real - imaginary * a.imaginary;
ccc.imaginary = real * a.imaginary + imaginary * a.real;
return ccc;
}
Complex0 Complex0::operator*( double m )
{
Complex0 ccc;
ccc.real = m * real;
ccc.imaginary = m * imaginary;
return ccc;
}

主函数:
#include <iostream>
using namespace std;
#include "complex.h"
int main (void)
{
Complex0 a( 3.0, 4.0 );
Complex0 c;
cout << "Enter a complex number (q to quit )";
while( cin >> c )
{
cout << "c is "<< c << endl;
cout << "complex conjugate is " << -c << endl;
cout << "a is "<< a << endl;
cout <<"a + c = "<<a + c << endl;
cout << "Enter a complex number (q to quit)";
}
cout << "Bye`!";
return 0;
}
作者: 王者风范    时间: 2009-11-4 00:24
修改不出来,还请楼下的帮忙,
作者: $星辰    时间: 2009-11-4 00:24
Complex0();
Complex0( double rea = 0.0, double ima = 0.0 );
都是默认构造函数。


Complex0 Complex0::operator*( const Complex0 &a )
{
Complex0 ccc; 有一个宽字符


还有,使用了<<, >>, 一元-操作符但是没有重载他们。
作者: 青松    时间: 2009-11-4 00:24
什么叫做宽字符?
作者: 最ヅ后愛上你    时间: 2009-11-4 00:24
我并不详细了解
是相对于ANSI字符来说的。
象我们中文输入的一个汉字,是一个宽字符。
作者: ⊿°屵重缺銭    时间: 2009-11-4 00:24
头文件"
#ifndef COMPLEX_H_
#define COMPLEX_H_
class Complex0
{
private:
double real;
double imaginary;
public:
//construction
Complex0();
Complex0( double rea , double ima = 0.0 );
~Complex0();
// set real,imag from coordinate.
void setReal( double rea );
void setImag( double ima );
Complex0 operator+( const Complex0 &a );
// Complex0 operator-( const Complex0 &a );//删去,主函数没有用
// Complex0 operator*( const Complex0 &a );
// Complex0 operator*( double m );
friend ostream operator<<(ostream &os ,Complex0 & com );//去掉了const;
friend ostream operator>>(ostream &os ,Complex0 & com );//去掉了const
};
#endif
函数实现:
#include <iostream>
using namespace std;
#include "complex.h"
Complex0::Complex0()
{
real = 0.0;
imaginary = 0.0;
}
Complex0::Complex0( double rea , double ima )
{
real = real;
imaginary = ima;
}
Complex0::~Complex0()
{
cout << "Bye1" << endl;
}
// set real,imag from coordinate.
void Complex0::setReal( double rea )
{
real = rea;
}
void Complex0::setImag( double ima )
{
imaginary = ima;
}
Complex0 Complex0::operator+( const Complex0 &a )
{
Complex0 ccc;
ccc.real = real + a.real;
ccc.imaginary = imaginary + a.imaginary;
return ccc;
}
/* Complex0 Complex0::operator-( const Complex0 &a ) //删去,主函数没有用
{
Complex0 c;
ccc.real = real - a.real;
ccc.imaginary = imaginary - a.imaginary;
return c;
}
Complex0 Complex0::operator*( const Complex0 &a )
{
Complex0 c;
ccc.real = real * a.real - imaginary * a.imaginary;
ccc.imaginary = real * a.imaginary + imaginary * a.real;
return ccc;
}
Complex0 Complex0::operator*( double m )
{
Complex0 c;
ccc.real = m * real;
ccc.imaginary = m * imaginary;
return c;
}
*/
ostream operator <<(ostream &os ,Complex0 & com ) // 后加的,按照楼上的提示
{
os << "the complex's real :" << com.real << endl;
os << "the complex's imaginary: "<< com.imaginary << endl;
return os;
}
ostream operator >>(ostream &os ,Complex0 & com )
{
os << "the complex's real :" ;
os >> com.real << endl;
os << "the complex's imaginary: ";
os >> com.imaginary << endl;
return os;
}
主函数:
#include <iostream>
using namespace std;
#include "complex.h"
int main(void)
{
Complex0 a( 3.0, 4.0 );
Complex0 c;
cout << "Enter a complex number (q to quit )";
while( cin >> c )
{
cout << "c is "<< c << endl;
cout << "complex conjugate is " << -c << endl;
cout << "a is "<< a << endl;
cout <<"a + c = "<<a + c << endl;
cout << "Enter a complex number (q to quit)";
}
cout << "Bye!";
return 0;
}
作者: 红苹果校园    时间: 2009-11-4 00:24
  1. #ifndef COMPLEX_H_
  2. #define COMPLEX_H_class Complex0
  3. {
  4. private:
  5. double real;
  6. double imaginary;
  7. public:
  8. //construction
  9. // Complex0(); 这个去掉。
  10. Complex0( double rea = 0.0, double ima = 0.0 );
  11. ~Complex0();
  12. // set real,imag from coordinate.
  13. void setReal( double rea );
  14. void setImag( double ima );
  15. Complex0 operator+( const Complex0 &a );
  16. Complex0 operator-( const Complex0 &a );
  17. Complex0 operator*( const Complex0 &a );
  18. Complex0 operator*( double m );

  19. friend ostream& operator<<(ostream &os, const Complex0 &a);
  20. friend istream& operator>>(istream &is, Comlex0 &a); //不要加const,因为要改变a
  21. Complex0 operator-();//一元,好象是这样。
  22. }
复制代码

作者: 听妈妈的话    时间: 2009-11-4 00:24
你把 / / Complex0(); 这个去掉。
那么在函数中友个 +重载里面的第一句不好实现!Complex0 ccc;
作者: 賤xs騷    时间: 2009-11-4 00:24
怎么没有高手帮忙呀
作者: v九天〓云龙    时间: 2009-11-4 00:24

  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;

  4. class Complex0
  5. {
  6. private:
  7. double real;
  8. double imaginary;
  9. public:
  10. //construction
  11. Complex0( double rea = 0.0, double ima = 0.0 );
  12. ~Complex0();
  13. // set real,imag from coordinate.
  14. void setReal( double rea );
  15. void setImag( double ima );
  16. Complex0 operator+( const Complex0 &a ) const;
  17. Complex0 operator-( const Complex0 &a ) const;
  18. Complex0 operator-() const;
  19. Complex0 operator*( const Complex0 &a ) const;
  20. Complex0 operator*( double m ) const;
  21. friend ostream & operator<<(ostream & os, const Complex0 & c);
  22. friend istream & operator>>(std::istream & is, Complex0 & c);
  23. };

  24. /*Complex0::Complex0()
  25. {
  26. real = 0.0;
  27. imaginary = 0.0;
  28. }*/

  29. Complex0::Complex0(double rea , double ima )
  30. {
  31. real = rea;
  32. imaginary = ima;
  33. }

  34. Complex0::~Complex0()
  35. {
  36. cout << "Bye1" << endl;
  37. }

  38. // set real,imag from coordinate.
  39. void Complex0::setReal( double rea )
  40. {
  41. real = rea;
  42. }

  43. void Complex0::setImag( double ima )
  44. {
  45. imaginary = ima;
  46. }

  47. Complex0 Complex0::operator+( const Complex0 & a) const
  48. {
  49. return Complex0(real+a.real, imaginary+a.imaginary);
  50. }

  51. Complex0 Complex0::operator-( const Complex0 &a ) const
  52. {
  53. return Complex0(real - a.real, imaginary - a.imaginary);
  54. }

  55. Complex0 Complex0::operator-() const
  56. {
  57. return Complex0(-real, -imaginary);
  58. }

  59. Complex0 Complex0::operator*( const Complex0 & a) const
  60. {
  61. return Complex0(real*a.real - imaginary*a.imaginary,
  62. real*a.imaginary + imaginary*a.real);
  63. }

  64. Complex0 Complex0::operator*( double m ) const
  65. {
  66. return Complex0(m * real, m * imaginary);
  67. }

  68. ostream & operator<<(ostream & os, const Complex0 & c)
  69. {
  70. if(c.imaginary > 0)
  71. os<<c.real<<"+i"<<c.imaginary;
  72. else if(c.imaginary == 0)
  73. os<<c.real;
  74. else
  75. os<<c.real<<"-i"<<-c.imaginary;
  76. return os;
  77. }

  78. istream & operator>>(std::istream & is, Complex0 & c)
  79. {
  80. if(is>>c.real)
  81. {
  82. if(is>>c.imaginary)
  83. return is;
  84. else
  85. {
  86. exit(1);
  87. }
  88. }
  89. else
  90. {
  91. exit(1);
  92. }
  93. }

  94. int main (void)
  95. {
  96. Complex0 a(3.0, 4.0);
  97. Complex0 c;
  98. cout << "Enter a complex number (q to quit )\n";
  99. while( cin >> c )
  100. {
  101. cout << "c is "<< c << endl;
  102. cout << "complex conjugate is " << -c << endl;
  103. cout << "a is "<< a << endl;
  104. cout <<"a + c = "<<a + c << endl;
  105. cout << "Enter a complex number (q to quit)";
  106. }
  107. cout << "Bye`!";
  108. system("pause");
  109. return 0;
  110. }
复制代码





欢迎光临 新微赢技术网 (http://bbs.weiying.cn/) Powered by Discuz! X3.2