64,639
社区成员
发帖
与我相关
我的任务
分享
#ifndef _AAA_
#define _AAA_
#include <iostream>
class plural
{
private:
int m_nReal;
int m_nImaginary;
public:
plural();
plural(int nReal, int nImaginary);
plural operator+(const plural &b);
plural operator-(const plural &b);
plural operator*(const plural &b);
friend plural operator*(int n, const plural &b);
friend std::ostream & operator<<(std::ostream &os, plural &b);
friend std::istream & operator>>(std::istream &is, const plural &b);
};
#endif
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
#include "mytime.h"
plural::plural()
{
m_nImaginary = 0;
m_nReal = 0;
}
plural::plural(int nReal, int nImaginary)
{
m_nImaginary = nImaginary;
m_nReal = nReal;
}
plural plural::operator*(const plural &b)
{
plural temp;
temp.m_nImaginary = this->m_nImaginary * b.m_nImaginary;
temp.m_nReal = this->m_nReal * b.m_nImaginary;
return temp;
}
std::istream & operator>>(std::istream &is, const plural &b)
{
//std::cout<<"Imaginary:";
if (is >> b.m_nImaginary)
{
cout <<"real:";
is >> b.m_nReal;
}
return is;
}
std::ostream & operator<<(std::ostream &os, plural &b)
{
os<<'('<<b.m_nReal<<','<<b.m_nImaginary<<"i)"<<endl;
return os;
}
plural operator*(int n, const plural &b)
{
plural temp;
temp.m_nImaginary = b.m_nImaginary * n;
temp.m_nReal = b.m_nReal * n;
return temp;
}
int main()
{
plural a(3, 4);
plural temp;
cout<<a;
}
friend std::ostream & operator<<(std::ostream &os, const plural &b);
friend std::istream & operator>>(std::istream &is, plural &b);