【11.7】c++ primer plus 課後編程答案

C++ PRIMER PLUS 課後答案 
使用IDE爲window7系統下的VS2010

/*user.h*/
#ifndef USERSH_H_
#define USERSH_H_
#include <string>
#include <cmath>
using std::ostream;
using std::istream;
class complex0
{
private:
         doublereal_val;
         doubleimag_val;
public:
         complex0();
         complex0(doubler,double i=0);
         ~complex0(){};
 
         complex0 operator + (const complex0 & p1);
         complex0 operator - (const complex0 & p2);
         complex0 operator * (double n);
 
         friend complex0  operator ~ (const complex0&  p);
         friend complex0 operator * (const complex0 & p1, const complex0 & p2);
         friend ostream & operator << (ostream & os, const complex0 & p);
         friend istream & operator >> (istream & is,  complex0 & p);
};
 
 
#endif


 

/*userfucntion.cpp*/
#include "usersh.h"
#include <iostream>
 
using std::cout;
using std::cin;
using std::ostream;
 
complex0::complex0()
{
         real_val=0.0;
         imag_val=0.0;
}
 
complex0::complex0(double r,double i/* =0*/)
{
         real_val=r;
         imag_val=i;
}
 
complex0 complex0::operator + (const complex0 & p1)
{
         complex0 temp;
         temp.real_val=real_val+p1.real_val;
         temp.imag_val=imag_val+p1.imag_val;
         return temp;
}
 
complex0 complex0::operator - (const complex0 & p2)
{
         complex0 temp;
         temp.real_val=real_val-p2.real_val;
         temp.imag_val=imag_val-p2.imag_val;
         return temp;
}
 
complex0 complex0::operator * (double n)
{
         complex0 temp;
         temp.real_val=n*real_val;
         temp.imag_val=n*imag_val;
         return temp;
}
 
complex0 operator ~(const complex0&  p)
{
         complex0 temp;
         temp.real_val=p.real_val;
         temp.imag_val=-p.imag_val;
         return temp;
}
 
complex0 operator * (const complex0 &p1, const complex0 & p2)
{
         complex0 temp;
         temp.real_val=p1.real_val*p2.real_val-p1.imag_val*p2.imag_val;
         temp.imag_val=p1.real_val*p2.imag_val+p1.imag_val*p2.real_val;
         return temp;
}
 
ostream & operator << (ostream& os, const complex0 & p)
{
         cout<<'('<<p.real_val<<','<<p.imag_val<<"i)\n";
         return os;
}
 
istream & operator >> (istream& is,  complex0 & p)
{
         cout<<"real:";
         cin>>p.real_val;
 
         cout<<"imag:";
         cin>>p.imag_val;
 
         return is;
}

/*main*/
#include <iostream>
#include <Windows.h>
#include "usersh.h"
#include <string>

using namespace std;
 
int main()
{  
         complex0 a (3.0,4.0);
         complex0 c;
         cout<<"enter a compex num (q to quit):\n";
         while(cin>>c)
         {
                  cout<<"c is"<<c<<'\n';
                  cout<<"~c is"<<~c<<'\n';
                  cout<<"a is"<<a<<'\n';
                  cout<<"a + c is"<<a+c<<'\n';
                  cout<<"a - c is"<<a-c<<'\n';
                  cout<<"a * c is"<<a*c<<'\n';
                  cout<<"2 * c is"<<2*c<<'\n';
                  cout<<"enter a compex num (q to quit):\n";
         }
         cout<<"Done!\n";
 
         system("pause");
         return 0;
}


 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章