找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1532|回复: 9

复数问题~

[复制链接]
发表于 2009-11-4 00:24:15 | 显示全部楼层 |阅读模式 IP:江苏扬州
头文件:
#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:16 | 显示全部楼层 IP:江苏扬州
修改不出来,还请楼下的帮忙,
回复

使用道具 举报

发表于 2009-11-4 00:24:17 | 显示全部楼层 IP:江苏扬州
Complex0();
Complex0( double rea = 0.0, double ima = 0.0 );
都是默认构造函数。


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


还有,使用了<<, >>, 一元-操作符但是没有重载他们。
回复

使用道具 举报

发表于 2009-11-4 00:24:21 | 显示全部楼层 IP:江苏扬州
什么叫做宽字符?
回复

使用道具 举报

发表于 2009-11-4 00:24:22 | 显示全部楼层 IP:江苏扬州
我并不详细了解
是相对于ANSI字符来说的。
象我们中文输入的一个汉字,是一个宽字符。
回复

使用道具 举报

发表于 2009-11-4 00:24:24 | 显示全部楼层 IP:江苏扬州
头文件"
#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:25 | 显示全部楼层 IP:江苏扬州
  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:27 | 显示全部楼层 IP:江苏扬州
你把 / / Complex0(); 这个去掉。
那么在函数中友个 +重载里面的第一句不好实现!Complex0 ccc;
回复

使用道具 举报

发表于 2009-11-4 00:24:28 | 显示全部楼层 IP:江苏扬州
怎么没有高手帮忙呀
回复

使用道具 举报

发表于 2009-11-4 00:24:30 | 显示全部楼层 IP:江苏扬州

  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. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 21:23 , Processed in 0.298567 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表