c++primer plus第十一章答案

这是主程序代码:main1.....main2..等表示第一题....第二题等代码主函数,每一小题通过main函数调用测试。
#include<iostream>
#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()<target)
{
	dirction=rand()%360;
	step.set(destep,dirction,'p');
	result=result+step;
	steps++;
}

vc[i]=steps;
steps=0;
i++;
result.set(0.0,0.0);

cout<<"plese enter terget:";

}
double temp1=vc[0];
double temp2=vc[0];
for(int i=0;i<n;i++)
{
cout<<vc[i]<<endl;
}
for(int i=0;i<n;i++)
{
if(temp1<vc[i])
	temp1=vc[i];
}
for(int i=0;i<n;i++)
{
if(temp2>vc[i])
	temp2=vc[i];
}
cout<<"hing:"<<temp1<<endl;
cout<<"low:"<<temp2<<endl;
}



void main2()
{
	time a;
	time b(5,40);
	time c(2,55);
	cout<<"A,Band c:\n";
	cout<<a<<";"<<b<<";"<<c<<endl;
	a=b+c;
	cout<<"b+c  :"<<a<<endl;
	a=b*2.74;
	cout<<"b*2,74   :"<<a<<endl;
	cout<<"10*b  :"<<10*b<<endl;
	return;

}



void main3()
{
	stonewt tt1;
	stonewt tt2(556.9);
	stonewt tt3(23,33.2);
	cout<<tt1<<"   "<<tt2<<"   "<<tt3<<endl;
	tt1.setmode('b');
	tt2.setmode('b');
	tt3.setmode('b');
	cout<<tt1<<"   "<<tt2<<"   "<<tt3<<endl;
	tt1.setmode('c');
	tt2.setmode('c');
	tt3.setmode('c');
	cout<<tt1<<"   "<<tt2<<"   "<<tt3<<endl;
}

void main5()
{
	compl a(3.0,4.0);
	compl c;
	cout<<"enter a complex number or qto quit:\n";
	while(cin>>c)
	{
	cout<<"c is"<<c<<endl;
	cout<<"complex conjugate is"<<~c<<endl;
	cout<<"a is"<<a<<endl;
	cout<<"a+c is"<<a+c<<endl;
	cout<<"a-c is"<<a-c<<endl;
	cout<<"a*c is"<<a*c<<endl;
	cout<<"2*c is"<<2*c<<endl;
	cout<<"enter a complex number or qto quit:\n";
	
	}
}*/

void main4()
{
	stone px[6]={stone(23,34.4),stone(65,22.3),stone(4,5.6)};
	for(int i=3;i<6;i++)
	{
	cin>>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<px[i])
		temp2=px[i];
	}
	cout<<temp<<"   "<<temp2<<endl;
}
void main()
{
	main4();
	
	char ch;
	cin>>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)=("<<v.x<<","<<v.y<<")";

	 }
	 else if(v.mode=='p')
	 {
		  os<<"(m,a)=("<<v.mag<<","<<v.ang<<")";
	 }
	 else
		 os<<"vector object mode is invalid";
	 return os;
	
	 }*/
		
};



#endif
#ifndef TIME11_2_H_
#define TIME11_2_H_
#include<iostream>
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<<t.hours<<"hours,"<<t.miutes<<"miutes";
	 return os;
	 
	 }

};



#endif
#ifndef STONEWT11_3_H_
#define STONEWT11_3_H_
#include<iostream>
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  :"<<v.stone<<endl;
	
	}
	else if(v.mode=='b')
		os<<"pds  :"<<v.pds<<endl;
	else if(v.mode=='c')
		os<<"pounds  :"<<v.pounds<<endl;
	else
		cout<<"please enter right ";
	return os;
	}


};

#endif

#ifndef STON11_4_H_
#define STON11_4_H_
#include<iostream>
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<<v.st<<"   "<<v.ponuds;
	return os;
	
	
	}

	friend istream& operator>>(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<iostream>
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<<"("<<c2.a<<","<<c2.b<<'i'<<")";
		return os;
		}
		friend istream & operator>>( istream &in, compl &c2)
		{
		in>>c2.a>>c2.b;
		return in;
		}
};



#endif

下面的代码是每小题头文件对应的函数代码:
#include "vector11_1.h"
#include <iostream>
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<iostream>
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<iostream>
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<s2.ponuds)
		return true;
	else
		return false;
	}
	stone::~stone()
	{
	
	}

#include<iostream>
#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);
		 }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值