c++primer plus第11章答え
7264 ワード
これはメインプログラムコードです:main 1.....main2..等表示第一题....第2題などのコード主関数は,各小題が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<>n;
double *vc=new double[n];
tt=n;
cout<>target)
{
cout<>destep))
{
break;
}
while(result.magval()vc[i])
temp2=vc[i];
}
cout<>c)
{
cout<>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<
#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<
#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<
#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>(istream &in, stone &v)
{
cout<>v.st;
cout<>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<>( 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<
#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
#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);
}