charpter11_homework.cpp
#include<iostream>
#include<fstream>
#include<ctime>
#include"vect_hw.h"
#include"time_hw.h"
#include"stonewt_hw5.h"
#include "complex_hw.h"
//函数原型
void p11_1(void);
void p11_3(void);
void p11_4(void);
void p11_5(void);
void p11_6(void);
void p11_7(void);
//名称空间
using namespace std;
using VECTOR::Vector;
//主函数
int main()
{
//p11_1();
//p11_3();
//p11_4();
//p11_5();
//p11_6();
p11_7();
return 0;
}
//---------------作业一/二--------------
void p11_1(void)
{
srand(time(0));//设置时间随机数种子
double direction;//随机方向
Vector step;//每次移动的类
Vector result;//总的移动
double dstep;//步长
double target;//目标总长度
unsigned long steps=0;//总步数
ofstream fout;//创建类对象
fout.open("homework_1.txt");//关联文件
cout<<"Enter the target(q to quit): ";
while(cin>>target)
{
cout<<"Enter the dstep: ";
if(!(cin>>dstep))
break;
//文本输出
fout<<"Target Distance: "<<target<<" , Step Size: "<<dstep<<endl;
while(result.magval()<target)
{
//文本输出
fout<<steps<<" : (x,y) = "<<"("<<result.xval()<<" , "<<result.yval()<<")"<<endl;
direction=rand()%360;
step.reset(dstep,direction,Vector::POL);//私有成员不可访问
result=result+step;
steps++;
}
//屏幕输出
cout<<"steps= "<<steps<<"location: "<<result<<endl;
result.polar_mode();
cout<<"polar: "<<result<<endl;
cout<<"average= "<<result.magval()/steps<<endl;
//文本输出
fout<<"After "<<steps<<" steps, the subject has the following location: \n";
fout<<"(x,y)= "<<"("<<result.xval()<<" , "<<result.yval()<<")"<<endl;
fout<<"or\n";
fout<<"(m,a)= "<<"("<<result.magval()<<" , "<<result.angval()<<")"<<endl;
fout<<"average= "<<result.magval()/steps<<endl;
//重置参数
steps=0;
result.reset(0.0,0.0);
cout<<"Enter the target(q to quit):";
}
cout<<"BYE!\n";
cin.clear();//清除错误标记
while(cin.get()!='\n')//读取多余缓冲输入
continue;
fout.close();//关闭文件
return;
}
//---------作业三------------------------
void p11_3(void)
{
srand(time(0));//设置时间随机数种子
double direction;//随机方向
Vector step;//每次移动的类
Vector result;//总的移动
double dstep;//步长
double target;//目标总长度
unsigned long steps;//总步数
int num;//循环次数
cout<<"Enter the target(q to quit): ";
cin>>target;
cout<<"Enter the dstep: ";
cin>>dstep;
cout<<"Enter the num: ";
cin>>num;
double* ps=new double [num];
for(int i=0;i<num;i++)
{
while(result.magval()<target)
{
direction=rand()%360;
step.reset(dstep,direction,Vector::POL);//私有成员不可访问
result=result+step;
steps++;
}
ps[i]=result.magval()/steps;
steps=0;
result.reset(0.0,0.0);
}
double v_max,v_min,v_sum;
v_max=v_min=ps[0];
v_sum=0.0;
for(int i=0;i<num;i++)
{
if(v_max<(ps[i]))
v_max=ps[i];
if(v_min>(ps[i]))
v_min=ps[i];
v_sum+=ps[i];
}
cout<<"Max= "<<v_max<<endl;
cout<<"Min= "<<v_min<<endl;
cout<<"Average= "<<v_sum/num<<endl;
cout<<"BYE!\n";
cin.clear();//清除错误标记
while(cin.get()!='\n')//读取多余缓冲输入
continue;
//释放指针
delete []ps;
return;
}
//-----------作业四---------------
void p11_4(void)
{
Time t1(3,35);
Time t2(2,48);
Time temp;
cout<<"t1: "<<t1<<" t2: "<<t2<<endl;
temp=t1+t2;
cout<<"t1+t2= "<<temp<<endl;
temp=t1-t2;
cout<<"t1-t2= "<<temp<<endl;
temp=t1*1.17;
cout<<"t1*1.17= "<<temp<<endl;
temp=10.0*t2;
cout<<"10.0*t2= "<<temp<<endl;
return;
}
//----------作业五-----------
void p11_5(void)
{
Stonewt s1=231.5;//单参数构造函数,将某种类型化为类
Stonewt s2(10,2.5);
Stonewt s3;
cout<<"s1= "<<s1<<" s2= "<<s2<<endl;
s1.set_stn();
s2.set_stn();
cout<<"s1= "<<s1<<" s2= "<<s2<<endl;
s3=s2+s1;
s3.set_stn();
cout<<"s1+s2= "<<s3<<endl;
s3=s1-s2;
s3.set_stn();
cout<<"s1-s2= "<<s3<<endl;
s3=s1*2.0;
s3.set_stn();
cout<<"s1*2.0= "<<s3<<endl;
s3=4.0*s1;
cout<<"4.0*s1= "<<s3<<endl;
}
//----------作业六-------------
void p11_6(void)
{
const int Max=6;
Stonewt arr[Max]={
Stonewt(12,10.2),
Stonewt(125.6),
Stonewt(120.1)
};
for(int i=3;i<Max;i++)
{
cout<<"Enter the pounds: ";
double temp;
while(!(cin>>temp))
{
cin.clear();
while(cin.get()!='\n')
continue;
cout<<"Bad input,enter again: ";
}
arr[i]=temp;
}
Stonewt st(11,0.0);//11英石
Stonewt max=arr[0];
Stonewt min=arr[0];
int num=0;
for(int i=0;i<Max;i++)
{
if(max<arr[i])
max=arr[i];
if(min>arr[i])
min=arr[i];
if(st<arr[i])
num++;
}
cout<<"max= "<<max<<endl;
min.set_stn();
cout<<"min= "<<min<<endl;
cout<<"num= "<<num<<endl;
}
//------------作业七-------------
void p11_7(void)
{
complex a(3.0,4.0);
complex c;
cout<<"Enter a complex number(q to quit):\n";
while(cin>>c)
{
cout<<"c is"<<c<<endl;
cout<<"~c: "<<~c<<endl;
cout<<"a is "<<a<<endl;
cout<<"a+c: "<<a+c<<endl;
cout<<"a-c: "<<a-c<<endl;
cout<<"a*c:"<<a*c<<endl;
cout<<"2*a: "<<2*a<<endl;
cout<<"Enter a complex number(q to quit):\n";
}
cout<<"Done!\n";
return;
}
vect_hw.h
#ifndef VECT_HW_H_
#define VECT_HW_H_
#include <cmath>
#include <iostream>
namespace VECTOR
{
class Vector
{
public://!!!此处为公共访问
//枚举常量
enum Mode{RECT,POL};
private:
double m_x;
double m_y;
//创建枚举变量
Mode mode;
public://公共接口,保持不变
Vector();
Vector(double n1,double n2,Mode form=RECT);
~Vector(){;};//析构函数,内联函数
void reset(double n1,double n2,Mode form=RECT);
double xval()const{return m_x;};
double yval()const{return m_y;};
double magval()const{return sqrt(m_x*m_x+m_y*m_y);};
double angval()const;
void polar_mode(){mode=POL;};
void rect_mode(){mode=RECT;};
//运算符重载
Vector operator+(const Vector& v)const;
Vector operator-(const Vector& v)const;
Vector operator-()const;
Vector operator*(double n)const;
//友元函数
friend Vector operator*(double n,const Vector& v){return v*n;};
friend std::ostream& operator<<(std::ostream& os,Vector& v);//非成员函数不可以使用const限定符
};
}
#endif
vect_hw.cpp
#include<iostream>
#include<cmath>
#include"vect_hw.h"
using namespace std;
namespace VECTOR
{
const double Rad_to_deg=45.0/atan(1.0);
double Vector::angval()const
{
if(m_x==0.0 && m_y==0.0)
return 0.0;
else
return atan2(m_y,m_x);
}
Vector::Vector()
{
m_x=m_y=0.0;
mode=RECT;
}
Vector::Vector(double n1,double n2,Mode form)
{
mode=form;
if(form==RECT)
{
m_x=n1;
m_y=n2;
}
else if(form==POL)
{
m_x=n1*cos(n2);
m_y=n1*sin(n2);
}
else
{
cout<<"Error,reset!\n";
m_x=m_y=0.0;
mode=RECT;
}
}
void Vector::reset(double n1,double n2,Mode form)
{
mode=form;
if(form==RECT)
{
m_x=n1;
m_y=n2;
}
else if(form==POL)
{
m_x=n1*cos(n2);
m_y=n1*sin(n2);
}
else
{
cout<<"Error,reset!\n";
m_x=m_y=0.0;
mode=RECT;
}
}
//运算符重载
//C++中只有被声明为const的成员函数才能被一个const类对象调用
Vector Vector::operator+(const Vector& v)const
{
return Vector(m_x+v.m_x,m_y+v.m_y);
}
Vector Vector::operator-(const Vector& v)const
{
return Vector(m_x-v.m_x,m_y-v.m_y);
}
Vector Vector::operator-()const
{
return Vector(-m_x,-m_y);
}
Vector Vector::operator*(double n)const
{
return Vector(m_x*n,m_y*n);
}
//友元函数
std::ostream& operator<<(std::ostream& os,Vector& v)//非成员函数不可以使用const限定符
{
if(v.mode==Vector::RECT)
os<<"x: "<<v.m_x<<" y: "<<v.m_y;
else if(v.mode==Vector::POL)
os<<"mag: "<<v.magval()<<" ang: "<<v.angval()*Rad_to_deg;
else
os<<"Invalid";
return os;
}
}
time_hw.h
#ifndef TIME_HW_H_
#define TIME_HW_H_
#include<iostream>
using std::ostream;
class Time
{
private:
int hours;
int minutes;
public:
Time(){;};//原型+定义
Time(int h,int m=0);
void AddMin(int m);
void AddHr(int h);
void Reset(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& t1,double n);//此函数与下面函数重载,特征标不同,参数顺序不同
//友元函数非成员函数
friend Time operator*(double m,Time& t){return t*m;};//内联函数,friend只能声明在这里
friend std::ostream& operator<<(std::ostream& os,const Time& t);//重载<<
};
#endif
time_hw.cpp
#include<iostream>
#include"time_hw.h"
Time::Time(int h,int m)//默认参数只能出现在原型中
{
hours=h;
minutes=m;
}
void Time::AddMin(int m)
{
int total_min=minutes+m;
minutes=total_min%60;
hours+=total_min/60;
}
void Time::AddHr(int h)
{
hours+=h;
}
void Time::Reset(int h,int m)
{
hours=h;
minutes=m;
}
//全部用友元函数重载运算符
Time operator+(const Time& t1,const Time& t2)
{
Time new_time;
int total_minutes=(t1.hours+t2.hours)*60+t1.minutes+t2.minutes;
new_time.hours=total_minutes/60;
new_time.minutes=total_minutes%60;
return new_time;//此处返回局部变量,不可返回临时变量的引用
}
Time operator-(const Time& t1,const Time& t2)
{
Time new_time;
int total_minutes=(t1.hours-t2.hours)*60+t1.minutes-t2.minutes;
new_time.hours=total_minutes/60;
new_time.minutes=total_minutes%60;
return new_time;//此处返回局部变量,不可返回临时变量的引用
}
Time operator*(const Time& t1,double n)//此函数与下面函数重载,特征标不同,参数顺序不同
{
Time new_time;
int total_minutes=(t1.hours*60+t1.minutes)*n;
new_time.hours=total_minutes/60;
new_time.minutes=total_minutes%60;
return new_time;//此处返回局部变量,不可返回临时变量的引用
}
//友元函数非成员函数
ostream& operator<<(ostream& os,const Time& t)//重载<<
{
os<<t.hours<<" hours, "<<t.minutes<<" minutes";
return os;//返回ostream&类
}
stonewt_hw5.h
#ifndef STONEWT_HW5_H_
#define STONEWT_HW5_H_
#include<iostream>
using std::ostream;
class Stonewt
{
public:
enum Mode{STONE,PDS};//公有数据成员
private:
static const int Lbs_per_stn=14;
int stone;
double pds_left;
double pounds;
Mode mode;
public:
Stonewt(double lbs,Mode form=PDS);//隐式转换
Stonewt(int stn,double lbs,Mode form=PDS);
Stonewt(){stone=0;pds_left=pounds=0.0;mode=PDS;};//内联函数
~Stonewt(){;};
void set_stn(){mode=STONE;};
void set_pds(){mode=PDS;};
转换函数,必须是类方法,将类转换为某种类型
//operator int();
//operator double();
//重载乘法运算符
Stonewt operator* (double n);//不能返回临时变量的引用
Stonewt operator+ (Stonewt& s);
Stonewt operator- (Stonewt& s);
//重载6个关系运算符
bool operator> (Stonewt& s){return (pounds>s.pounds);};
bool operator< (Stonewt& s){return (pounds<s.pounds);};
bool operator>= (Stonewt& s){return (pounds>=s.pounds);};
bool operator<= (Stonewt& s){return (pounds>=s.pounds);};
bool operator== (Stonewt& s){return (pounds==s.pounds);};
bool operator!= (Stonewt& s){return (pounds!=s.pounds);};
//友元函数
friend Stonewt operator*(double n,Stonewt& t){return n*t.pounds;};//此处为double*double
//重载<<,因为第一个操作数是os,所以必须是友元函数
friend ostream& operator<<(ostream& os,Stonewt& s1);
};
#endif
stonewt_hw5.h
#include<iostream>
#include"stonewt_hw5.h"
using std::cout;
using std::endl;
Stonewt::Stonewt(double lbs,Mode form)//隐式转换
{
stone=(int)lbs/Lbs_per_stn;
pds_left=(int)lbs%Lbs_per_stn+lbs-(int)lbs;
pounds=lbs;
mode=form;
}
Stonewt::Stonewt(int stn,double lbs,Mode form)
{
stone=stn;
pds_left=lbs;
pounds=stn*Lbs_per_stn+lbs;
mode=form;
}
转换函数,必须是类方法,将类转换为某种类型
没有返回类型,但也会返回相应的值
//Stonewt::operator int()
//{
// return int(pounds+0.5);
//}
//Stonewt::operator double()
//{
// return pounds;
//}
//重载乘法运算符
Stonewt Stonewt::operator* (double n)//不能返回临时变量的引用
{
/*Stonewt temp;
temp.pounds=pounds*n;
temp.stone=(int)pounds/Lbs_per_stn;
temp.pds_left=(int)pounds%Lbs_per_stn+pounds-(int)pounds;
return temp;*/
//以上代码不好,要利用构造函数
return Stonewt(pounds*n);
}
Stonewt Stonewt::operator+ (Stonewt& s)
{
return Stonewt(pounds+s.pounds);
}
Stonewt Stonewt::operator- (Stonewt& s)
{
return Stonewt(pounds-s.pounds);
}
//重载<<,因为第一个操作数是os,所以必须是友元函数
ostream& operator<<(ostream& os,Stonewt& s1)
{
if(s1.mode==Stonewt::STONE)
cout<<"stone: "<<s1.stone<<" pds_left: "<<s1.pds_left;
else if(s1.mode==Stonewt::PDS)
cout<<"pounds: "<<s1.pounds;
else
cout<<"Error!"<<endl;
return os;
}
complex_hw.h
#ifndef COMPLEX_HW_H_
#define COMPLEX_HW_H_
using std::ostream;
using std::istream;
class complex
{
private:
double real;
double imag;
public:
complex(){real=imag=0.0;};
complex(double n1,double n2){real=n1;imag=n2;};
~complex(){;};
//重载运算符
complex operator+(complex& c){return complex(real+c.real,imag+c.imag);};
complex operator-(complex& c){return complex(real-c.real,imag-c.imag);};
complex operator~(){return complex(real,-imag);};
complex operator*(complex& c);
complex operator*(double n);
//友元函数重载
friend complex operator*(double n,complex& c){return c*n;};
friend ostream& operator<<(ostream& os,complex& c);
friend istream& operator>>(istream& is,complex& c);
};
#endif
complex_hw.cpp
#include <iostream>
#include "complex_hw.h"
using namespace std;
complex complex::operator*(complex& c)
{
complex temp;
temp.real=real*c.real-imag*c.imag;
temp.imag=real*c.imag+imag*c.real;
return temp;
}
complex complex::operator*(double n)
{
return complex(n*real,n*imag);
}
//友元函数重载
ostream& operator<<(ostream& os,complex& c)
{
os<<"("<<c.real<<" , "<<c.imag<<")";
return os;
}
istream& operator>>(istream& is,complex& c)
{
cout<<"real: ";
is>>c.real;
if(!is)
return is;
cout<<"imag: ";
is>>c.imag;
is.get();//读取缓冲在输入行里的回车符·
return is;
}
如有错误,欢迎指正!