| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 665 人关注过本帖, 1 人收藏
标题:关于输入运算符重载
取消只看楼主 加入收藏
kiadragon
Rank: 2
等 级:论坛游民
帖 子:20
专家分:24
注 册:2013-4-13
结帖率:50%
收藏(1)
已结贴  问题点数:20 回复次数:4 
关于输入运算符重载
程序代码:
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
#include <string>
using namespace std;
class Complex
{

 public:
  friend ostream &operator<<( ostream &,const Complex &);
  friend istream &operator>>( istream &, Complex & );
  Complex operator+(const Complex  );
  Complex operator-( const Complex );
  Complex operator*( const Complex );
  Complex operator/( const Complex );
  bool operator==(const Complex );
  bool operator!=( const Complex );

 private:
  double realpart;
  double imaginarypart;
};
#endif

这是头文件,然后是输入输出(其他就没有贴了,因为编译通过但是输出输入出现了问题)
程序代码:
ostream &operator<<( ostream &output , const Complex &c)
{
  output << "(" << c.realpart << ", " << c.imaginarypart << ")";
  return output;
}
istream &operator>>( istream &input , Complex &c)
{
  input.ignore();
  cin >> c.realpart ;
  input.ignore(2);
  cin >> c.imaginarypart;
  input.ignore();
  return cin;
}

这是cpp。
我的希望输入样式
(0, 0)
(4.3, 8.2)
(3.2 9.4)
(2.1, 9.2)
但是输到第二个结束后就直接停止输入了,输出一堆东西来
Enter a complex number in the form: (a, b)
? (0, 0)
x: (0, 0)
y: (9.2, 8.2)
z: k:
x=y+z:
(-1.53603e-41, 9.72077e-270) = (0, 4.85575e-270) + (-1.53603e-41, 4.86502e-270)
x=y-z:
(1.53603e-41, -9.27509e-273) = (0, 4.85575e-270) - (-1.53603e-41, 4.86502e-270)
x=y*z:
(-0, 0) = (0, 4.85575e-270) * (-1.53603e-41, 4.86502e-270)
x=y/z:
(-0, 0.998094) = (0, 4.85575e-270) / (-1.53603e-41, 4.86502e-270)
(-0, 0.998094) != (-0.611574, -2.87136e-42)
(-0.611574, -2.87136e-42) == (-0.611574, -2.87136e-42)
问题如上图
求大神搭救啊
搜索更多相关主题的帖子: Complex color 
2013-04-19 14:03
kiadragon
Rank: 2
等 级:论坛游民
帖 子:20
专家分:24
注 册:2013-4-13
收藏
得分:0 
2013-04-19 14:03
kiadragon
Rank: 2
等 级:论坛游民
帖 子:20
专家分:24
注 册:2013-4-13
收藏
得分:0 
没人么
2013-04-19 14:59
kiadragon
Rank: 2
等 级:论坛游民
帖 子:20
专家分:24
注 册:2013-4-13
收藏
得分:0 
回复 4楼 zhuxiaoneng
好像还是不行耶,我分开一个一个字节回车输入就没问题
Enter a complex number in the form: (a, b)
? (0, 0)
x: (
0
,
 
0
y: (
3.4
,
 
8.2
z: (
9.1
,
  
5
k: (
0
,
 
0

x=y+z:
(12.5, 13.2) = (3.4, 8.2) + (9.1, 5)
x=y-z:
(-5.7, 3.2) = (3.4, 8.2) - (9.1, 5)
x=y*z:
(30.94, 41) = (3.4, 8.2) * (9.1, 5)
x=y/z:
(0.373626, 1.64) = (3.4, 8.2) / (9.1, 5)
(0.373626, 1.64) != (0, 0)
(0, 0) == (0, 0)

如果要改代码的话怎么改
2013-04-19 17:23
kiadragon
Rank: 2
等 级:论坛游民
帖 子:20
专家分:24
注 册:2013-4-13
收藏
得分:0 
不是输入的,我把全代码贴出来吧
程序代码:
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
#include <string>
using namespace std;
class Complex
{

 public:
  friend ostream &operator<<( ostream &,const Complex &);
  friend istream &operator>>( istream &, Complex & );
  Complex operator+(const Complex  );
  Complex operator-( const Complex );
  Complex operator*( const Complex );
  Complex operator/( const Complex );
  bool operator==(const Complex );
  bool operator!=( const Complex );

 private:
  double realpart;
  double imaginarypart;
};
#endif

Complex.h文件
程序代码:
#include <iostream>
#include <iomanip>
#include <string>
#include "Complex.h"
using namespace std;
/*Complex::Complex()
{
  realpart =0 ;
  imaginarypart = 0 ;
  }//constructor*/
ostream &operator<<( ostream &output , const Complex &c)
{
  output << "(" << c.realpart << ", " << c.imaginarypart << ")";
  return output;
}
istream &operator>>( istream &input , Complex &c)
{
  input.ignore();
  cin >> c.realpart ;
  input.ignore(2);
  cin >> c.imaginarypart;
  input.ignore();
  return cin;
}
Complex Complex::operator+( const Complex right)
{
  Complex temp;
  temp.realpart = realpart + right.realpart;
  temp.imaginarypart = imaginarypart + right.imaginarypart;
  return temp;
}
Complex Complex::operator-( const Complex right)
{
  Complex temp;
  temp.realpart = realpart - right.realpart;
  temp.imaginarypart = imaginarypart - right.imaginarypart;
  return temp;
}
Complex Complex::operator*( const Complex right)
{
  Complex temp;
  temp.realpart = realpart * right.realpart;
  temp.imaginarypart = imaginarypart * right.imaginarypart;
  return temp;
}
Complex Complex::operator/( const Complex right)
{
  Complex temp;
  temp.realpart = realpart / right.realpart;
  temp.imaginarypart = imaginarypart / right.imaginarypart;
  return temp;
}
bool Complex::operator==(const Complex right )
{
  if(realpart == right.realpart)
    if(imaginarypart == right.imaginarypart)
      return true;
  return false;
}
bool Complex::operator!=( const Complex right )
{
  if(*this ==right)
    return false;
  return true;
}

Complex.cpp文件
程序代码:
#include <iostream>
#include <iomanip>
#include "Complex.h"
using namespace std;
int main()
{
  Complex x,y;
  Complex z,k;
  cout << "Enter a complex number in the form: (a, b)" << endl << "? (0, 0)" << endl;
  cout << "x: " ;
  cin >> x ;
  cout << "y: " ;
  cin >> y ;
  cout << "z: " ;
  cin >> z ;
  cout << "k: " ;
  cin >> k ;
  cout << endl;
  x = y + z;
  cout << "x=y+z:" << endl;
  cout << x;
  cout << " = ";
  cout << y ;
  cout << " + " ;
  cout << z << endl;
  x = y - z;
  cout << "x=y-z:" << endl;
  cout << x;
  cout << " = ";
  cout << y ;
  cout << " - " ;
  cout << z << endl;
  x = y * z;
  cout << "x=y*z:" << endl;
  cout << x;
  cout << " = ";
  cout << y ;
  cout << " * " ;
  cout << z << endl;
  x = y / z;
  cout << "x=y/z:" << endl;
  cout << x;
  cout << " = ";
  cout << y ;
  cout << " / " ;
  cout << z << endl;
  if(x!=k)
    {
      cout << x ;
      cout << " != ";
      cout << k << endl;
    }
  if(k==k)
    {
      cout << k ;
      cout << " == ";
      cout << k << endl;
    }
  return 0;
}

main.cpp 那个?(0, 0)是cout出来的
2013-04-19 18:13
快速回复:关于输入运算符重载
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030553 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved