【11.7】c++ primer plus 课后编程答案

来源:互联网 发布:网络工作 编辑:程序博客网 时间:2024/06/01 07:34

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;}


 


 

阅读全文
0 0
原创粉丝点击