c++primer plus第十一章答案

这是主程序代码:main1.....main2..等表示第一题....第二题等代码主函数,每一小题通过main函数调用测试。
#include
#include"ston11_4.h"
using namespace std;
/*
void main1()
{
vector step;
vector result(0.0,0.0);
unsigned long steps=0;
double target;
double destep;
double dirction;

int n;
int tt;
int i=0;
cout<<"please enter you need test number";
cin>>n;
double *vc=new double[n];
tt=n;
cout<<"please enter target distance:";
while(tt--&&cin>>target)
{
cout<<"please enter destep:";
if(!(cin>>destep))
{
break;
}
while(result.magval()vc[i])
	temp2=vc[i];
}
cout<<"hing:"<>c)
	{
	cout<<"c is"<>px[i];
	
	}
	stone temp;
	temp=px[0];
	stone temp2;
	temp2=px[0];
	for(int i=0;i<6;i++)
	{
	if(temp>px[i])
		temp=px[i];
	}
	for(int i=0;i<6;i++)
	{
	if(temp2>ch;
system("spause");
}
下面代码是每题的头文件:
#ifndef VECTOR11_1_H_
#define VECTOR11_1_H_

class vector
{
private:
	double x;
	double y;
	double mag;
	double ang;
	char mode;
	void setmag();
	void setang();
	void setx();
	void sety();

public:
	
	vector();
	vector(double n1,double n2,char form='r');
	void set(double n1,double n2,char form='r');
	~vector();
	double xval() const {return x;}
	double yval() const {return y;}
	double magval() const {return x;}
	double sngval() const {return x;}
	void polar_mode();
	void rect_mode();
	vector operator+(const vector &b)const;
	vector operator-(const vector &b)const;
	vector operator-()const;
	vector operator*(double n)const;
	friend vector operator *(double n,const vector &a);
	 /*friend ostream & operator<<(ostream & os,const vector & v)
		  {
	 if(v.mode=='r')
	 {

		 os<<"(x,y)=("<
#ifndef TIME11_2_H_
#define TIME11_2_H_
#include
using namespace std;
class time
{
private:
	int hours;
	int miutes;
public:
	time();
	time(int h,int m=0);
	void addmin(int m);
	void addhour(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,const time &t)
	{return t*m;}
	friend ostream & operator<<(ostream &os,const time &t)
		 {
	 os<
#ifndef STONEWT11_3_H_
#define STONEWT11_3_H_
#include
using namespace std;
class stonewt
{
private:
	enum{lbs_stn=14};
	int stone;
	double pds;
	double pounds;
	char mode;
public:
	stonewt(double lbs);
	stonewt(int stn,double lbs);
	stonewt();
	void setmode(char m='a');
	~stonewt();
	
	friend ostream& operator<<(ostream &os,const stonewt &v)
	{
	
	if(v.mode=='a')
	{
	os<<"stone  :"<

#ifndef STON11_4_H_
#define STON11_4_H_
#include
using namespace std;
class stone
{

private:
	int st;
	double ponuds;
public:
	stone();
	stone(int st,double ponuds);
	void set(int st,double ponuds );
	friend bool operator>(const stone &s1,const stone &s2 );
	friend bool operator<(const stone &s1,const stone &s2 );
	~stone();
	friend ostream& operator<<(ostream &os,const stone &v)
	{
	os<>(istream &in, stone &v)
	{
	cout<<"please enter st:";
		in>>v.st;
		cout<<"please enter pounds:";
		in>>v.ponuds;
	return in;
	
	
	}

};




#endif

#ifndef COPLEX11_5_H_
#define COPLEX11_5_H_
#include
using namespace std;
class compl
{
private:
	double a;
	double b;
public:
	compl();
		compl(double x,double y);
		~compl();
		 compl operator+(const compl &c2)const;
		friend compl operator-(const compl&c1,const compl &c2);
		friend compl operator*(const compl&c1,const compl &c2);
		friend compl operator*(double x,const compl &c2);
		compl operator~( );
		friend ostream & operator<<( ostream &os, compl &c2)
		{
		os<<"("<>( istream &in, compl &c2)
		{
		in>>c2.a>>c2.b;
		return in;
		}
};



#endif

下面的代码是每小题头文件对应的函数代码:
#include "vector11_1.h"
#include 
using namespace std;


const double Rad_to_deg=57.295779513;
 
 
void vector::setmag()
{
	mag=sqrt(x*x+y*y);

}
	void vector::setang()
	{
	if(x==0.0 && y==0.0)
		ang=0.0;
	else
		ang=atan2(y,x);
	
	
	}

	void vector::setx()
	{
	x=mag*cos(ang);
	
	}
	void vector::sety()
	{
	
	y=mag*sin(ang);
	
	}


	vector::vector()
	{
	x=0.0;
	y=0.0;
	mode='r';
	
	}
	vector::vector(double n1,double n2,char form)
	{
	mode=form;
	if(form=='r')
	{
	x=n1;
	y=n2;
	setmag();
	setang();
	
	}
	else if(form=='p')
	{
	mag=n1;
	ang=n2/Rad_to_deg;
	setx();
	sety();

	
	}
	else {
	
	std::cout<<"incorrect 3rd argument to vector";
	std::cout<<"vector set to 0\n";
	x=y=0;
	mag=ang=0;
	mode='r';

	}
	
	}
	void vector::set(double n1,double n2,char form)
	{
	mode=form;
	if(form=='r')
	{
	x=n1;
	y=n2;
	setmag();
	setang();
	
	}
	else if(form=='p')
	{
	mag=n1;
	ang=n2/Rad_to_deg;
	setx();
	sety();

	
	}
	else {
	
	std::cout<<"incorrect 3rd argument to vector";
	std::cout<<"vector set to 0\n";
	x=y=0;
	mag=ang=0;
	mode='r';
	}
	
	}
	vector::~vector()
	{}
	
	
	void vector::polar_mode()
	{
	mode='p';
	
	}
	void vector::rect_mode()
	{
	
	mode='r';
	
	}
	vector vector::operator+(const vector &b)const
	{
	return vector(x+b.x,y+b.y);
	
	}
	vector vector::operator-(const vector &b)const
	{
	return vector(x-b.x,y-b.y);
	
	}
	vector vector::operator-()const
	{
	return vector(-x,-y);
	}
	vector vector::operator*(double n)const
	{
	return vector(x*n,y*n);
	}
	vector operator*(double n,const vector &a)
	{
	return a*n;
	
	}
	
		

#include"time11_2.h"
using namespace std;
time::time()
{
	hours=miutes=0;
}
	time::time(int h,int m)
	{
	hours=h;
	miutes=m;

	}
	void time::addmin(int m)
	{
	miutes=miutes+m;
	hours=hours+miutes/60;
	miutes=miutes%60;
	}
	void time::addhour(int h)
	{
	hours+=h;
	
	}
	void time::reset(int h,int m)
	{
	hours=h;
	miutes=m;
	
	}
	 time operator+(const time &t1,const time &t2)
	 {
	 time sum;
	 long totl=t1.miutes+t2.miutes+t1.hours*60+t2.hours*60;
	 sum.miutes=totl%60;
	 sum.hours=totl/60;
	
	 return sum;
	 }
	 time operator-(const time &t1,const time &t2)
	 {
	  time diff;
	  int totl1, totl2;
	  totl1=t1.hours*60+t1.miutes;
	   totl2=t2.hours*60+t2.miutes;
	   diff.hours=(totl1-totl2)/60;

	 diff.miutes=(totl1-totl2)%60;
	
	 return diff;
	 
	 }
	 time operator*(const time &t1,double n)
	 {
		 time result;
	long totl=t1.hours*n*60+t1.miutes*n;
	result.hours=totl/60;
	result.miutes=totl%60;
	return	result; 
	 }
	
	 
#include"stonewt11_3.h"
#include
using namespace std;
stonewt::stonewt(double lbs)
{
stone=int(lbs)/lbs_stn;
pds=int(lbs)%lbs_stn+lbs-int(lbs);
pounds=lbs;
mode='a';
}
	stonewt::stonewt(int stn,double lbs)
	{
	stone=stn;
	pds=lbs;
	pounds=stn*lbs_stn+lbs;
	mode='a';
	}
	stonewt::stonewt()
	{
	stone=pds=pounds=0;
	mode='a';
	}
	stonewt::~stonewt()
	{}
	void stonewt::setmode(char m)
	{
	mode=m;
	}

#include"ston11_4.h"
#include
using namespace std;
stone::stone()
{
st=9;
ponuds=99.8;

}
	stone::stone(int st1,double ponuds1)
	{
	st=st1;
	ponuds=ponuds1;
	
	}
	void stone::set(int st1,double ponuds1 )
	{
		st=st1;
		ponuds=ponuds1;
	
	}
	 bool operator>(const stone &s1,const stone &s2 )
	{
	if(s1.ponuds>s2.ponuds)
		return true;
	else
		return false;
	
	}
	bool operator<(const stone &s1,const stone &s2 )
	{
	if(s1.ponuds

#include
#include"coplex11_5.h"
using namespace std;
compl::compl()
{

	a=b=0.0;
}
		compl::compl(double x,double y)
		{
		a=x;
		b=y;
		}
		compl::~compl()
		{}
		 compl compl::operator+(const compl &c2)const
		 {
			return compl(a+c2.a,b+c2.b);
		 
		 }
		 compl operator-(const compl&c1,const compl &c2)
		 {
		 compl result;
		 result.a=c1.a-c2.a;
		  result.b=c1.b-c2.b;
		  return result;
		 
		 }
		compl operator*(const compl&c1,const compl &c2)
		{
		 compl result;
		 result.a=c1.a*c2.a;
		  result.b=c1.b*c2.b;
		  return result;
		
		}
		 compl operator*(double x,const compl &c2)
		 {
		 
		 compl result;
		 result.a=x*c2.a;
		  result.b=x*c2.b;
		  return result;
		 }
		compl  compl::operator~( )
		 {
		 double t;
		 t=a;
		 a=b;
		 b=t;
		 return compl(a,b);
		 }




你可能感兴趣的:(c++primer plus第十一章答案)