第一题
// 1.头文件#ifndef VECTOR_H_#define VECTOR_H_#include<iostream>namespace VECTOR
{classVector{public:enum Mode
{
RECT,
POL
};private:double x;double y;double mag;double ang;
Mode mode;voidset_mag();voidset_ang();voidset_x();voidset_y();public:Vector();Vector(double n1,double n2, Mode form = RECT);voidreset(double n1,double n2, Mode form = RECT);~Vector();// inline functiondoublexval()const{return x;}doubleyval()const{return y;}doublemagval()const{return mag;}doubleangval()const{return ang;}// set modevoidpolar_mode();voidrect_mode();// operator overloading
Vector operator+(const Vector &b)const;
Vector operator-(const Vector &b)const;
Vector operator-()const;
Vector operator*(double n)const;// friendsfriend Vector operator*(double n,const Vector &a);friend std::ostream &operator<<(std::ostream &os,const Vector &v);};}// namespace VECTOR#endif//2.函数定义#include"vector.h"#include<cmath>using std::atan;using std::atan2;using std::cos;using std::cout;using std::sin;using std::sqrt;namespace VECTOR
{constdouble Rad_to_Deg =45.0/atan(1.0);// 1弧度=57.295...°void Vector::set_mag(){
mag =sqrt(x * x + y * y);}void Vector::set_ang(){if(x ==0.0&& y ==0.0){
ang =0.0;}else{
ang =atan2(y, x);}}void Vector::set_x(){
x = mag *cos(ang);}void Vector::set_y(){
y = mag *sin(ang);}
Vector::Vector(){
x = y = mag = ang =0.0;
mode = RECT;}
Vector::Vector(double n1,double n2, Mode form){
mode = form;if(mode == RECT){
x = n1;
y = n2;set_ang();set_mag();}elseif(mode == POL){
mag = n1;
ang = n2 / Rad_to_Deg;set_x();set_y();}else{
cout <<"Incorrect 3rd argument to Vector()--";
cout <<"vector set to 0\n";
x = y = mag = ang =0.0;
mode = RECT;}}void Vector::reset(double n1,double n2, Mode form){
mode = form;if(mode == RECT){
x = n1;
y = n2;set_ang();set_mag();}elseif(mode == POL){
mag = n1;
ang = n2 / Rad_to_Deg;set_x();set_y();}else{
cout <<"Incorrect 3rd argument to Vector()--";
cout <<"vector set to 0\n";
x = y = mag = ang =0.0;
mode = RECT;}}
Vector::~Vector(){}void Vector::polar_mode(){
mode = POL;}void Vector::rect_mode(){
mode = RECT;}
Vector Vector::operator+(const Vector &b)const{returnVector(x + b.x, y + b.y);}// 矢量相减
Vector Vector::operator-(const Vector &b)const{returnVector(x - b.x, y - b.y);}// 将矢量取反
Vector Vector::operator-()const{returnVector(-x,-y);}
Vector Vector::operator*(double n)const{returnVector(n * x, n * y);}// friends
Vector operator*(double n,const Vector &a){return a * n;}
std::ostream &operator<<(std::ostream &os,const Vector &v){if(v.mode == Vector::RECT){
os <<"(x,y) = ("<< v.x <<","<< v.y <<")";}elseif(v.mode == Vector::POL){
os <<"(m,a) = ("<< v.mag <<","<< v.ang <<")";}else{
os <<"Vector object mode is invalid";}return os;}}// namespace VECTOR//3.主函数#include"vector.h"#include<cstdlib>#include<ctime>#include<fstream>#include<iostream>intmain(){usingnamespace std;using VECTOR::Vector;srand(time(0));double direction;
Vector step;
Vector result(0.0,0.0);unsignedlong steps =0;double target;double dstep;
ofstream fout;
fout.open("randwalk.txt", ios::out | ios::app);
cout <<"Enter target distance (q to quit): ";while(cin >> target){
cout <<"Enter step length: ";if(!(cin >> dstep))break;
cout <<"Target Distance: "<< target <<", Step Size: "<< dstep << endl;
fout <<"Target Distance: "<< target <<", Step Size: "<< dstep << endl;int i =0;
cout << i <<": "<< result << endl;
fout << i <<": "<< result << endl;while(result.magval()< target){
direction =rand()%360;
step.reset(dstep, direction, Vector::POL);//! 因为Vector类名称已经可用,所以这里可以直接使用
result = result + step;
steps++;
i++;
cout << i <<": "<< result << endl;
fout << i <<": "<< result << endl;}
cout <<"After "<< steps <<" steps, the subject ""has the following location:\n";
cout << result << endl;
result.polar_mode();
cout <<" or\n"<< result << endl;
cout <<"Average outward distance per step = "<< result.magval()/ steps << endl;
fout <<"After "<< steps <<" steps, the subject ""has the following location:\n";
fout << result << endl;
result.polar_mode();
fout <<" or\n"<< result << endl;
fout <<"Average outward distance per step = "<< result.magval()/ steps << endl;
steps =0;
result.reset(0.0,0.0);
cout <<"Enter target distance (q to quit): ";}
cout <<"Bye !\n";
cin.clear();while(cin.get()!='\n'){continue;}return0;}
第二题
// 1.头文件#ifndef VECTOR_H_#define VECTOR_H_#include<iostream>namespace VECTOR
{classVector{public:enum Mode
{
RECT,
POL
};private:double x;double y;// double mag;// double ang;
Mode mode;doubleset_mag();doubleset_ang();doubleset_x();doubleset_y();public:Vector();Vector(double n1,double n2, Mode form = RECT);voidreset(double n1,double n2, Mode form = RECT);~Vector();// inline functiondoublexval()const{return x;}doubleyval()const{return y;}//! 注意: 这里需要去掉const,为什么呢 ?//! 答: 这两个函数没有改变类对象啊!!doublemagval(){returnset_mag();}doubleangval(){returnset_ang();}// set modevoidpolar_mode();voidrect_mode();// operator overloading
Vector operator+(const Vector &b)const;
Vector operator-(const Vector &b)const;
Vector operator-()const;
Vector operator*(double n)const;// friendsfriend Vector operator*(double n,const Vector &a);friend std::ostream &operator<<(std::ostream &os,const Vector &v);};}// namespace VECTOR#endif//2.函数定义#include"vector.h"#include<cmath>using std::atan;using std::atan2;using std::cos;using std::cout;using std::sin;using std::sqrt;namespace VECTOR
{constdouble Rad_to_Deg =45.0/atan(1.0);// 1弧度=57.295...°double Vector::set_mag(){returnsqrt(x * x + y * y);}double Vector::set_ang(){if(x ==0.0&& y ==0.0){return0.0;}else{returnatan2(y, x);}}double Vector::set_x(){return x *cos(y);}double Vector::set_y(){return x *sin(y);}
Vector::Vector(){
x = y =0.0;
mode = RECT;}
Vector::Vector(double n1,double n2, Mode form){
mode = form;if(mode == RECT){
x = n1;
y = n2;}elseif(mode == POL){
x = n1 *cos(n2);
y = n1 *sin(n2);}else{
cout <<"Incorrect 3rd argument to Vector()--";
cout <<"vector set to 0\n";
x = y =0.0;
mode = RECT;}}void Vector::reset(double n1,double n2, Mode form){
mode = form;if(mode == RECT){
x = n1;
y = n2;}elseif(mode == POL){
x = n1 *cos(n2);
y = n1 *sin(n2);}else{
cout <<"Incorrect 3rd argument to Vector()--";
cout <<"vector set to 0\n";
x = y =0.0;
mode = RECT;}}
Vector::~Vector(){}void Vector::polar_mode(){
mode = POL;
x =set_mag();//*此时因为只有x和y.所以暂时让x充当mag,y充当ang的角色
y =set_ang();}void Vector::rect_mode(){if(mode == POL){
mode = RECT;
x =set_x();
y =set_y();}}
Vector Vector::operator+(const Vector &b)const{returnVector(x + b.x, y + b.y);}// 矢量相减
Vector Vector::operator-(const Vector &b)const{returnVector(x - b.x, y - b.y);}// 将矢量取反
Vector Vector::operator-()const{returnVector(-x,-y);}
Vector Vector::operator*(double n)const{returnVector(n * x, n * y);}// friends
Vector operator*(double n,const Vector &a){return a * n;}
std::ostream &operator<<(std::ostream &os,const Vector &v){if(v.mode == Vector::RECT){
os <<"(x,y) = ("<< v.x <<","<< v.y <<")";}elseif(v.mode == Vector::POL){
os <<"(m,a) = ("<< v.x <<","<< v.y <<")";}else{
os <<"Vector object mode is invalid";}return os;}}// namespace VECTOR//3.主函数#include"vector.h"#include<cstdlib>#include<ctime>#include<iostream>intmain(){usingnamespace std;// using namespace VECTOR;//!使用VECTOR名称空间的类Vector, --->使得类名称Vector可用//!同时该类中的public方法可用//!为什么调用方法的时候不用加Vector::而使用类中的POL就需要这个Vector::?//!答:因为调用方法是用的类对象调用方法,这个类对象已经表明了是Vector::,而单单一个变量POL并没有这种信息using VECTOR::Vector;srand(time(0));double direction;
Vector step;
Vector result(0.0,0.0);unsignedlong steps =0;double target;double dstep;
cout <<"Enter target distance (q to quit): ";while(cin >> target){
cout <<"Enter step length: ";if(!(cin >> dstep))break;while(result.magval()< target){
direction =rand()%360;
step.reset(dstep, direction, Vector::POL);//! 因为Vector类名称已经可用,所以这里可以直接使用
result = result + step;
steps++;}
cout <<"After "<< steps <<" steps, the subject ""has the following location:\n";
cout << result << endl;
result.polar_mode();//!!涉及到了极坐标的信息
cout <<" or\n"<< result << endl;
cout <<"Average outward distance per step = "<< result.magval()/ steps << endl;
steps =0;
result.reset(0.0,0.0);
cout <<"Enter target distance (q to quit): ";}
cout <<"Bye !\n";
cin.clear();while(cin.get()!='\n'){continue;}return0;}
第3题
// 1.头文件#ifndef VECTOR_H_#define VECTOR_H_#include<iostream>namespace VECTOR
{classVector{public:enum Mode
{
RECT,
POL
};private:double x;double y;double mag;double ang;
Mode mode;voidset_mag();voidset_ang();voidset_x();voidset_y();public:Vector();Vector(double n1,double n2, Mode form = RECT);voidreset(double n1,double n2, Mode form = RECT);~Vector();// inline functiondoublexval()const{return x;}doubleyval()const{return y;}doublemagval()const{return mag;}doubleangval()const{return ang;}// set modevoidpolar_mode();voidrect_mode();// operator overloading
Vector operator+(const Vector &b)const;
Vector operator-(const Vector &b)const;
Vector operator-()const;
Vector operator*(double n)const;// friendsfriend Vector operator*(double n,const Vector &a);friend std::ostream &operator<<(std::ostream &os,const Vector &v);};}// namespace VECTOR#endif//2.函数定义#include"vector.h"#include<cmath>using std::atan;using std::atan2;using std::cos;using std::cout;using std::sin;using std::sqrt;namespace VECTOR
{constdouble Rad_to_Deg =45.0/atan(1.0);// 1弧度=57.295...°void Vector::set_mag(){
mag =sqrt(x * x + y * y);}void Vector::set_ang(){if(x ==0.0&& y ==0.0){
ang =0.0;}else{
ang =atan2(y, x);}}void Vector::set_x(){
x = mag *cos(ang);}void Vector::set_y(){
y = mag *sin(ang);}
Vector::Vector(){
x = y = mag = ang =0.0;
mode = RECT;}
Vector::Vector(double n1,double n2, Mode form){
mode = form;if(mode == RECT){
x = n1;
y = n2;set_ang();set_mag();}elseif(mode == POL){
mag = n1;
ang = n2 / Rad_to_Deg;set_x();set_y();}else{
cout <<"Incorrect 3rd argument to Vector()--";
cout <<"vector set to 0\n";
x = y = mag = ang =0.0;
mode = RECT;}}void Vector::reset(double n1,double n2, Mode form){
mode = form;if(mode == RECT){
x = n1;
y = n2;set_ang();set_mag();}elseif(mode == POL){
mag = n1;
ang = n2 / Rad_to_Deg;set_x();set_y();}else{
cout <<"Incorrect 3rd argument to Vector()--";
cout <<"vector set to 0\n";
x = y = mag = ang =0.0;
mode = RECT;}}
Vector::~Vector(){}void Vector::polar_mode(){
mode = POL;}void Vector::rect_mode(){
mode = RECT;}
Vector Vector::operator+(const Vector &b)const{returnVector(x + b.x, y + b.y);}// 矢量相减
Vector Vector::operator-(const Vector &b)const{returnVector(x - b.x, y - b.y);}// 将矢量取反
Vector Vector::operator-()const{returnVector(-x,-y);}
Vector Vector::operator*(double n)const{returnVector(n * x, n * y);}// friends
Vector operator*(double n,const Vector &a){return a * n;}
std::ostream &operator<<(std::ostream &os,const Vector &v){if(v.mode == Vector::RECT){
os <<"(x,y) = ("<< v.x <<","<< v.y <<")";}elseif(v.mode == Vector::POL){
os <<"(m,a) = ("<< v.mag <<","<< v.ang <<")";}else{
os <<"Vector object mode is invalid";}return os;}}// namespace VECTOR//3.主函数#include"vector.h"#include<cstdlib>#include<ctime>#include<iostream>intmain(){usingnamespace std;// using namespace VECTOR;//!使用VECTOR名称空间的类Vector, --->使得类名称Vector可用//!同时该类中的public方法可用//!为什么调用方法的时候不用加Vector::而使用类中的POL就需要这个Vector::?//!答:因为调用方法是用的类对象调用方法,这个类对象已经表明了是Vector::,而单单一个变量POL并没有这种信息using VECTOR::Vector;srand(time(0));double direction;
Vector step;
Vector result(0.0,0.0);unsignedlong steps =0;double target;double dstep;
cout <<"How many test do you want? \n";int N;
cin >> N;double arr_step[N];double min;double max;double average;int i =0;
cout <<"Enter target distance: ";
cin >> target;
cout <<"Enter step length: ";
cin >> dstep;for(int j =0; j < N; j++){while(result.magval()< target){
direction =rand()%360;
step.reset(dstep, direction, Vector::POL);//! 因为Vector类名称已经可用,所以这里可以直接使用
result = result + step;
steps++;}
cout <<"After "<< steps <<" steps, the subject ""has the following location:\n";
cout << result << endl;
result.polar_mode();
cout <<" or\n"<< result << endl;
cout <<"Average outward distance per step = "<< result.magval()/ steps << endl;
arr_step[i++]= steps;
steps =0;
result.reset(0.0,0.0);}// if (i < N)// {// steps = 0;// result.reset(0.0, 0.0);// cout << "Enter target distance (q to quit): ";// }
min = arr_step[0];
max = arr_step[0];double total = arr_step[0];for(int j =1; j < N; j++){if(arr_step[j]< min){
min = arr_step[j];}if(arr_step[j]> max){
max = arr_step[j];}
total += arr_step[j];}
average = total / N;
cout <<"Max step = "<< max << endl;
cout <<"Min step = "<< min << endl;
cout <<"Ave step = "<< average << endl;
cout <<"Bye !\n";
cin.clear();while(cin.get()!='\n'){continue;}return0;}
第四题
//1.头文件#ifndef TIME_H_#define TIME_H_#include<iostream>classTime{private:int hours;int minutes;public:Time();Time(int h,int m =0);voidAddMin(int m);voidAddHr(int h);voidReset(int h =0,int m =0);// 声明运算符重载函数,表明操作数是参数列表和调用对象friend Time operator+(const Time &t1,const Time &t2);friend Time operator-(const Time &t1,const Time &t2);friend Time operator*(const Time &t,double n);friend Time operator*(double n,const Time &t){return t * n;}friend std::ostream &operator<<(std::ostream &os,const Time &t);voidShow()const;};#endif//2.函数定义#include"mytime.h"#include<iostream>// using std::ostream;
Time::Time(){
hours = minutes =0;}
Time::Time(int h,int m){
hours = h;
minutes = m;}void Time::AddMin(int m){
minutes += m;
hours += minutes /60;
minutes %=60;}void Time::AddHr(int h){
hours += h;}void Time::Reset(int h,int m){
hours = h;
minutes = m;}// 在调用该运算符函数时,有两种方式直接调用运算符成员函数operator+;// 或者是使用运算符形式: + ,运算符左边为调用对象,右边是参数传递的对象
Time operator+(const Time &t1,const Time &t2){
Time temp;
temp.minutes =(t1.minutes + t2.minutes)%60;
temp.hours =(t1.minutes + t2.minutes)/60+ t1.hours + t2.hours;return temp;}
Time operator-(const Time &t1,const Time &t2){
Time temp;int tot1, tot2;
tot1 = t1.hours *60+ t1.minutes;
tot2 = t2.hours *60+ t2.minutes;
temp.minutes =(tot1 - tot2)%60;
temp.hours =(tot1 - tot2)/60;return temp;}
Time operator*(const Time &t,double n){
Time result;long total_minutes =(t.minutes + t.minutes *60)* n;
result.minutes = total_minutes %60;
result.hours = total_minutes /60;return result;}void Time::Show()const{
std::cout << hours <<" hours, "<< minutes <<" minutes";}
std::ostream &operator<<(std::ostream &os,const Time &t){
os << t.hours <<" hour, "<< t.minutes <<" minutes";return os;}//3.主函数#include"mytime.h"intmain(){using std::cout;using std::endl;
Time aida(3,35);
Time tosca(2,48);
Time temp;
cout <<"Aida and Tosca:\n";
cout << aida <<"; "<< tosca << endl;
temp = aida + tosca;
cout <<"Aida + Tosca: "<< temp << endl;
temp = aida *1.17;
cout <<"Aida * 1.17:"<< temp << endl;
cout <<"10.0 * Tosca: "<<10.0* tosca << endl;return0;}
第5题
//1.头文件#ifndef STONE_H_#define STONE_H_#include<iostream>classStone{private:staticconstint Lbs_per_stn =14;enum Mode
{
form_st,//英石格式
form_ip,//整数磅格式
form_dp //浮点磅格式};
Mode mode;// 英石格式int stone;double pds_left;// 浮点格式的磅double pounds;// 整数格式的磅int Ipound;voidSetOthers();public:Stone(double lbs);// 单位为磅例如 35.3磅Stone(int stn,double lbs);// 单位为英石和磅例如 4英石 3.4磅Stone();~Stone();// 重载运算符 <<friend std::ostream &operator<<(std::ostream &os, Stone &st);// 重载 + - *
Stone operator+(const Stone &st)const;
Stone operator-(const Stone &st)const;
Stone operator*(double n)const;friend Stone operator*(double n,const Stone &st){return st * n;}voidset_form_st(){ mode = form_st;}voidset_form_ip(){ mode = form_ip;}voidset_form_dp(){ mode = form_dp;}};#endif//2.函数定义#include<iostream>using std::cout;#include"stone.h"
Stone::Stone(double lbs){
mode = form_dp;
pounds = lbs;//浮点格式的磅
stone =int(lbs)/ Lbs_per_stn;//英石格式的磅
pds_left =int(lbs)% Lbs_per_stn + lbs -int(lbs);
Ipound =int(pounds +0.5);//整数格式的磅}
Stone::Stone(int stn,double lbs){
mode = form_st;
stone = stn;//英石格式的磅
pds_left = lbs;
pounds = stn * Lbs_per_stn + lbs;//浮点格式的磅
Ipound =int(pounds +0.5);//整数格式的磅}
Stone::Stone(){
mode = form_dp;
Ipound = pounds = stone = pds_left =0;}
Stone::~Stone(){}
std::ostream &operator<<(std::ostream &os, Stone &st){if(st.mode == Stone::form_st){
os <<"Form_st: "<< st.stone <<" stone, "<< st.pds_left <<" pounds.\n";}elseif(st.mode == Stone::form_dp){
os <<"Form_dp: "<< st.pounds <<" pounds.\n";}else{
os <<"Form_ip: "<< st.Ipound <<" pounds.\n";}return os;}
Stone Stone::operator+(const Stone &st)const{
Stone temp;
temp.pounds = pounds + st.pounds;
temp.SetOthers();return temp;}
Stone Stone::operator-(const Stone &st)const{
Stone temp;
temp.pounds = pounds - st.pounds;
temp.SetOthers();return temp;}
Stone Stone::operator*(double n)const{
Stone temp;
temp.pounds = n * pounds;
temp.SetOthers();return temp;}void Stone::SetOthers(){
stone =int(pounds)/ Lbs_per_stn;//英石格式的磅
pds_left =int(pounds)% Lbs_per_stn + pounds -int(pounds);
Ipound =int(pounds +0.5);}//3.主函数#include"stone.h"intmain(){using std::cout;
Stone obj1;
obj1 =Stone(278.6);
Stone obj2(10,4.5);
cout <<"obj1 :\n"<< obj1;
cout <<"obj2 :\n"<< obj2;
Stone obj3 = obj1 + obj2;
obj3.set_form_ip();
cout <<"obj1 + obj2 :\n"<< obj3;
obj3 = obj1 - obj2;
obj3.set_form_st();
cout <<"obj1 - obj2 :\n"<< obj3;
obj3 = obj1 *3;
cout <<"obj1 * 3 :\n"<< obj3;
obj3 =3* obj2;
cout <<"3 * obj2 :\n"<< obj3;return0;}
第六题
//1.头文件#ifndef STONE_H_#define STONE_H_classStone{private:// static const int Lbs_per_stn = 14;enum{
Lbs_per_stn =14};int stone;double pds_left;double pounds;public:Stone(double lbs);// 单位为磅例如 35.3磅Stone(int stn,double lbs);// 单位为英石和磅例如 4英石 3.4磅Stone();~Stone();voidshow_lbs()const;voidshow_stn()const;booloperator>(const Stone &st);booloperator<(const Stone &st);booloperator>=(const Stone &st);booloperator<=(const Stone &st);booloperator==(const Stone &st);booloperator!=(const Stone &st);};#endif//2.函数定义#include<iostream>using std::cout;#include"stone.h"
Stone::Stone(double lbs){
stone =int(lbs)/ Lbs_per_stn;
pds_left =int(lbs)% Lbs_per_stn + lbs -int(lbs);
pounds = lbs;}
Stone::Stone(int stn,double lbs){
stone = stn;
pds_left = lbs;
pounds = stn * Lbs_per_stn + lbs;}
Stone::Stone(){
stone = pounds = pds_left =0;}
Stone::~Stone(){}void Stone::show_lbs()const{
cout << pounds <<" pounds\n";}void Stone::show_stn()const{
cout << stone <<" stone,"<< pds_left <<" pounds\n";}bool Stone::operator>(const Stone &st){if(pounds > st.pounds){returntrue;}else{returnfalse;}}bool Stone::operator<(const Stone &st){if(pounds < st.pounds){returntrue;}else{returnfalse;}}bool Stone::operator>=(const Stone &st){if(pounds >= st.pounds){returntrue;}else{returnfalse;}}bool Stone::operator<=(const Stone &st){if(pounds <= st.pounds){returntrue;}else{returnfalse;}}bool Stone::operator==(const Stone &st){if(pounds == st.pounds){returntrue;}else{returnfalse;}}bool Stone::operator!=(const Stone &st){if(pounds != st.pounds){returntrue;}else{returnfalse;}}//3.主函数#include"stone.h"#include<iostream>intmain(){using std::cin;using std::cout;
Stone arr_stone[6]={Stone(78.9),Stone(7,8.9),Stone(10,0.0)};{int i =3;
cout <<"Please enter the "<< i +1<<"th object's weight:(in pounds)\n";double temp;while(i <6&& cin >> temp){
arr_stone[i]=Stone(temp);
i++;if(i <6){
cout <<"Please enter the "<< i +1<<"th object's weight:(in pounds)\n";}}}int min =0;int max =0;int num =0;
Stone comp =Stone(11,0.0);if(arr_stone[0]>= comp){
num++;}for(int i =1; i <6; i++){if(arr_stone[i]< arr_stone[min]){
min = i;}if(arr_stone[i]> arr_stone[max]){
max = i;}if(arr_stone[i]>= comp){
num++;}}
cout <<"In the array:\n"<<"The max is NO."<< max <<" (0~5).\n"<<"==>";
arr_stone[max].show_lbs();
cout <<"The min is NO."<< min <<" (0~5).\n"<<"==>";
arr_stone[min].show_lbs();
cout <<"And "<< num <<" objects are bigger than 11 stones.\n";return0;}
第七题
//1.头文件#ifndef COMPLEX0_H_#define COMPLEX0_H_#include<iostream>classComplex0{private:double real;double ima;public:Complex0();Complex0(double x,double y);~Complex0();//ÖŘÔŘÔËËăˇű
Complex0 operator+(const Complex0 &co)const;
Complex0 operator-(const Complex0 &co)const;
Complex0 operator*(const Complex0 &co)const;
Complex0 operator*(double x)const;friend Complex0 operator*(double x,const Complex0 &co);
Complex0 operator~()const;friend std::ostream &operator<<(std::ostream &os,const Complex0 &co);friendbooloperator>>(std::istream &is, Complex0 &co);};#endif//2.函数定义#include"complex0.h"#include<cstdlib>
Complex0::Complex0(){
real =0.0;
ima =0.0;}
Complex0::Complex0(double x,double y){
real = x;
ima = y;}
Complex0::~Complex0(){}//ÖŘÔŘÔËËăˇű
Complex0 Complex0::operator+(const Complex0 &co)const{
Complex0 temp;
temp.real = real + co.real;
temp.ima = ima + co.ima;return temp;}
Complex0 Complex0::operator-(const Complex0 &co)const{
Complex0 temp;
temp.real = real - co.real;
temp.ima = ima - co.ima;return temp;}
Complex0 Complex0::operator*(const Complex0 &co)const{
Complex0 temp;
temp.real = real * co.real - ima * co.ima;
temp.ima = real * co.ima - ima * co.real;return temp;}
Complex0 Complex0::operator*(double x)const{
Complex0 temp;
temp.real = x * real;
temp.ima = x * ima;return temp;}
Complex0 operator*(double x,const Complex0 &co){return co * x;}
Complex0 Complex0::operator~()const{
Complex0 temp;
temp.real = real;
temp.ima =-ima;return temp;}
std::ostream &operator<<(std::ostream &os,const Complex0 &co){
os <<"("<< co.real <<" , "<< co.ima <<"i)";return os;}booloperator>>(std::istream &is, Complex0 &co){int ret =false;
std::cout <<"real: ";char str1[20];// char *str1;
is.getline(str1,20);if(str1[0]=='q'){
ret =false;}else{
co.real =strtod(str1,NULL);
std::cout <<"imaginary: ";char str2[20];// char *str2;
is.getline(str2,20);if(str2[0]=='q'){
ret =false;}else{
co.ima =strtod(str2,NULL);
ret =true;}}return ret;}//3.主函数#include<iostream>usingnamespace std;#include"complex0.h"intmain(){
Complex0 a(3.0,4.0);
Complex0 c;
cout <<"Enter a complex number(q to quit):\n";while(cin >> c){
cout <<"c is "<< c <<'\n';
cout <<"complex conjugate 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 complex number (q to quit :)\n";}
cout <<"Done!\n";return0;}