//====================11_1.hpp===========================
#ifndef VECTOR_H
#define VECTOR_H
#include<iostream>
namespace VECTOR
{
class Vector {
public:
enum Mode { RECT, POL };
//rectangular mode polar mode
private:
double x;
double y;
double mag;//magnitude of Vector
double ang;//direction of Vector in degrees
Mode mode;//RECT or POL
void set_mag();
void set_ang();
void set_x();
void set_y();
public:
Vector();
Vector(double n1, double n2, Mode form = RECT);
void reset(double n1, double n2, Mode form = RECT);
~Vector();
double xval()const { return x; }
double yval()const { return y; }
double magval()const{return mag;}
double angval()const { return ang; }
void polar_mode();
void rect_mode();
//operator overloading
Vector operator+(const Vector & b)const;
Vector operator-(const Vector & b)const;
Vector operator-()const;//reverse sign of Vector
Vector operator*(double n)const;
//friends
friend Vector operator*(double n, const Vector & a);
friend std::ostream &operator<<(std::ostream & os, const Vector & v);
};
}
#endif
//==================11_1.cpp===================================
#include<cmath>
#include"11_1.hpp"
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::cout;
using std::atan2;
namespace VECTOR {
//compute degrees
const double Rad_to_deg = 45.0 / atan(1.0);
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);
}
//set x from polar coordinate
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 (form == RECT)
{
x = n1;
y = n2;
set_mag();
set_ang();
}
else if (form == 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 (form == RECT)
{
x = n1;
y = n2;
set_mag();
set_ang();
}
else if (form == 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;
}
//operator overloading
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(n * x, n * y);
}
//multiply n by Vector a
Vector operator*(double n, const Vector & a)
{
return n * a;
}
std::ostream & operator<<(std::ostream & os, const Vector & v)
{
if (v.mode == Vector::RECT)
os << "(x,y) = (" << v.x << "," << v.y << ")";
else if (v.mode == Vector::POL)
{
os << "(m,a) = (" << v.mag << "," << v.ang * Rad_to_deg << ")";
}
else
os << "Vector object mode is invalid";
return os;
}
}
//===================11_1_main.cpp================================
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<ctime>
#include"11_1.hpp"
int main()
{
using namespace std;
using VECTOR::Vector;
srand(time(0));
double direction;
Vector step;
Vector result(0.0, 0.0);
unsigned long steps = 0;
double target;
double dstep;
cout << "Enter target distance (q to quit): ";
ofstream fout;
fout.open("1_1.txt");
while (cin >> target)
{
cout << "Enter target step length: ";
if (!(cin >> dstep))
break;
fout<<"Target distance : "<<target<<" , Step size : "<<dstep<<endl;
while (result.magval() < target)
{
direction = rand() % 360;
step.reset(dstep, direction, Vector::POL);
fout<<steps<<" : "<<result<<endl;
result = result + step;
steps++;
}
fout<<"After "<<steps<<" steps, the subject has the following location: \n"<<result<<endl;
fout<<"After outward distance per step ="<<result.magval()/steps<<endl;
fout<<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<endl;
cout<<"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<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 = ";
cout << result.magval() / steps << endl;
steps = 0;
result.reset(0.0, 0.0);*/
result.polar_mode();
steps=0;
result.reset(0.0,0.0);
cout << "Enter target distance (q to quit): "<<endl;
}
cout << "Bye!\n";
fout.close();
cin.clear();
while (cin.get() != '\n')
continue;
return 0;
}
//=================11_2.hpp================================
#ifndef VECTOR_H
#define VECTOR_H
#include<iostream>
namespace VECTOR
{
class Vector {
public:
enum Mode { RECT, POL };
//rectangular mode polar mode
private:
double x;
double y;
Mode mode;//RECT or POL
//void set_x();
//void set_y();
public:
Vector();
Vector(double n1, double n2, Mode form = RECT);
void reset(double n1, double n2, Mode form = RECT);
~Vector();
//double xval()const {return x;}
//double yval()const {return y;}
double magval();
double angval();
void polar_mode();
void rect_mode();
//operator overloading
Vector operator+(const Vector & b)const;
Vector operator-(const Vector & b)const;
Vector operator-()const;//reverse sign of Vector
Vector operator*(double n)const;
//friends
friend Vector operator*(double n, const Vector & a);
friend std::ostream &operator<<(std::ostream & os, Vector & v);
};
}
#endif
//=======================11_2.cpp================================
#include<cmath>
#include"11_2.hpp"
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::cout;
using std::atan2;
namespace VECTOR {
//compute degrees
const double Rad_to_deg = 45.0 / atan(1.0);
double Vector::magval()
{
double mag=sqrt(x*x+y*y);
return mag;
}
double Vector::angval()
{
double ang;
if(x==0&&y==0)
ang=0;
else
ang=atan2(y,x);
return ang;
}
//set x from polar coordinate
/*void Vector::set_x()
{
x = mag * cos(ang);
}
void Vector::set_y()
{
y = mag * sin(ang);
}*/
Vector::Vector() {
x = y =0.0;
mode = RECT;
}
Vector::Vector(double n1, double n2, Mode form)
{
mode = form;
if (form == RECT)
{
x = n1;
y = n2;
}
else if (form == POL)
{
x=n1*cos(n2/Rad_to_deg);
y=n1*sin(n2/Rad_to_deg);
}
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 (form == RECT)
{
x = n1;
y = n2;
}
else if (form == POL) {
x=n1*cos(n2/Rad_to_deg);
y=n1*sin(n2/Rad_to_deg);
}
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;
}
void Vector::rect_mode() {
mode = RECT;
}
//operator overloading
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(n * x, n * y);
}
//multiply n by Vector a
Vector operator*(double n, const Vector & a)
{
return n * a;
}
std::ostream & operator<<(std::ostream & os, Vector & v)
{
if (v.mode == Vector::RECT)
os << "(x,y) = (" << v.x << "," << v.y << ")";
else if (v.mode == Vector::POL)
{
double mag=v.magval();
double ang=v.angval();
os << "(m,a) = (" << mag << "," << ang * Rad_to_deg << ")";
}
else
os << "Vector object mode is invalid";
return os;
}
}
//=======================11_2_main.cpp================================
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<ctime>
#include"11_2.hpp"
int main()
{
using namespace std;
using VECTOR::Vector;
srand(time(0));
double direction;
Vector step;
Vector result(0.0, 0.0);
unsigned long steps = 0;
double target;
double dstep;
cout << "Enter target distance (q to quit): ";
ofstream fout;
fout.open("1_1.txt");
while (cin >> target)
{
cout << "Enter target step length: ";
if (!(cin >> dstep))
break;
fout<<"Target distance : "<<target<<" , Step size : "<<dstep<<endl;
while (result.magval() < target)
{
direction = rand() % 360;
step.reset(dstep, direction, Vector::POL);
fout<<steps<<" : "<<result<<endl;
result = result + step;
steps++;
}
fout<<"After "<<steps<<" steps, the subject has the following location: \n"<<result<<endl;
fout<<"After outward distance per step ="<<result.magval()/steps<<endl;
fout<<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<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 = ";
cout << result.magval() / steps << endl;
steps = 0;
result.reset(0.0, 0.0);*/
result.polar_mode();
steps=0;
result.reset(0.0,0.0);
cout << "Enter target distance (q to quit): ";
}
cout << "Bye!\n";
fout.close();
cin.clear();
while (cin.get() != '\n')
continue;
return 0;
}
//==========================11_3.hpp================================
//1_1.h
#ifndef VECTOR_H
#define VECTOR_H
#include<iostream>
namespace VECTOR
{
class Vector {
public:
enum Mode { RECT, POL };
//rectangular mode polar mode
private:
double x;
double y;
double mag;//magnitude of Vector
double ang;//direction of Vector in degrees
Mode mode;//RECT or POL
void set_mag();
void set_ang();
void set_x();
void set_y();
public:
Vector();
Vector(double n1, double n2, Mode form = RECT);
void reset(double n1, double n2, Mode form = RECT);
~Vector();
double xval()const { return x; }
double yval()const { return y; }
double magval()const{return mag;}
double angval()const { return ang; }
void polar_mode();
void rect_mode();
//operator overloading
Vector operator+(const Vector & b)const;
Vector operator-(const Vector & b)const;
Vector operator-()const;//reverse sign of Vector
Vector operator*(double n)const;
//friends
friend Vector operator*(double n, const Vector & a);
friend std::ostream &operator<<(std::ostream & os, const Vector & v);
};
}
#endif
//=====================11_3.cpp===================================
#include<cmath>
#include"11_3.hpp"
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::cout;
using std::atan2;
namespace VECTOR {
//compute degrees
const double Rad_to_deg = 45.0 / atan(1.0);
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);
}
//set x from polar coordinate
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 (form == RECT)
{
x = n1;
y = n2;
set_mag();
set_ang();
}
else if (form == 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 (form == RECT)
{
x = n1;
y = n2;
set_mag();
set_ang();
}
else if (form == 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;
}
//operator overloading
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(n * x, n * y);
}
//multiply n by Vector a
Vector operator*(double n, const Vector & a)
{
return n * a;
}
std::ostream & operator<<(std::ostream & os, const Vector & v)
{
if (v.mode == Vector::RECT)
os << "(x,y) = (" << v.x << "," << v.y << ")";
else if (v.mode == Vector::POL)
{
os << "(m,a) = (" << v.mag << "," << v.ang * Rad_to_deg << ")";
}
else
os << "Vector object mode is invalid";
return os;
}
}
//=====================11_3_main.cpp==========================
#include <iostream>
#include <cstring>
#include <string>
#include <cctype>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include"11_3.hpp"
int main()
{
using namespace std;
using VECTOR::Vector;
srand(time(0));
double direction;
Vector step;
Vector result(0.0,0.0);
unsigned long steps=0;
int numbers;
double target;
double dstep;
cout<<"Enter your cyliment numbers"<<endl;
cin>>numbers;
cin.get();
cout<<"Enter target distance (q to quit):";
ofstream fout;
fout.open("11_3.txt");
unsigned long steps_numbers[numbers];
int i;
for(i=0;i<numbers;i++)
{
cin>>target;
cout<<"enter every step length"<<endl;
if(!(cin>>dstep))
break;
fout<<"Target distance :"<<target<<" , step size:"<<dstep<<endl;
while(result.magval()<target)
{
direction=rand()%360;
step.reset(dstep,direction,Vector::POL);
fout<<steps<<":"<<result<<endl;
result=result+step;
steps++;
}
fout<<"After "<<steps<<" steps, the subject has the following location: \n"<<result<<endl;
fout<<"After outward distance per step ="<<result.magval()/steps<<endl;
fout<<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<endl;
cout<<"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<endl;
steps_numbers[i]=steps;
result.polar_mode();
steps=0;
result.reset(0.0, 0.0);
cout<<"Enter target distance (q to quit):"<<endl;
cout<<"bye\n";
cin.clear();
while(cin.get()!='\n')
continue;
}
fout.close();
unsigned long max_step=steps_numbers[0];
unsigned long min_step=steps_numbers[0];
unsigned long aver_step;
unsigned long sum_steps=0;
int j;
for(j=1;j<numbers;j++)
{
if(max_step<steps_numbers[j])
max_step=steps_numbers[j];
if(min_step>steps_numbers[j])
min_step=steps_numbers[j];
sum_steps+=steps_numbers[j];
}
cout<<"max_step is "<<max_step<<endl;
cout<<"min_step is "<<min_step<<endl;
cout<<"aver_step is "<<sum_steps/j<<endl;
cout<<"that is done!"<<endl;
}
//===================11_4.hpp===========================
#include<iostream>
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*(double n,const Time &t);
friend Time operator*(const Time &t,double n);
friend std::ostream &operator<<(std::ostream &os,const Time &t);
};
//=========================11_4.cpp==========================
#include"11_4.hpp"
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+hours;
minutes=minutes%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 temp;
temp.hours=t1.hours+t2.hours+(t1.minutes+t2.minutes)/60;
temp.minutes=(t1.minutes+t2.minutes)%60;
return temp;
}
Time operator-(const Time &t1,const Time &t2)
{
Time temp;
int m1,m2;
m1=t1.hours*60+t1.minutes;
m2=t2.hours*60+t2.minutes;
temp.minutes=(m1-m2)%60;
temp.hours=(m1-m2)/60;
return temp;
}
Time operator*(double n,const Time &t)
{
}
Time operator*(const Time &t,double n)
{
Time temp;
int sum;
sum=(t.hours*60+t.minutes)*n;
temp.minutes=sum%60;
temp.hours=sum/60;
return temp;
}
std::ostream &operator<<(std::ostream &os,const Time &t)
{
os<<t.hours<<"hours , "<<t.minutes<<"minutes ";
return os;
}
//===============11_4_main.cpp=====================
#include<iostream>
#include"11_4.hpp"
int main()
{
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;
temp=aida*2.17;
cout<<"temp * 2.17 "<<temp<<endl;
cout<<"10*temp= "<<10*temp<<endl;
return 0;
}
//=======================11_5.hpp=============================
#include<iostream>
class Stonewt
{
public:
enum mode{STONE,LBS_LEFT,POUNDS};
private:
enum{lbs_per_stn=14};
int stone;
double pds_left;
double pounds;
mode fm;
//void set_stone();
//void set_pds_left();
//void set_pounds();
public:
Stonewt(double lbs,mode form);
Stonewt(int stn,double lbs,mode form);
Stonewt();
~Stonewt();
void to_pounds();
void reset(mode form);
//void reset(int stn,double lbs,mode form);
//Stonewt &operator(double n);
friend std::ostream &operator<<(std::ostream &os,const Stonewt &st);
friend Stonewt operator+(Stonewt &st1, Stonewt &st2);
friend Stonewt operator-(Stonewt &st1,Stonewt &st2);
friend Stonewt operator*(double n,Stonewt &st);
//void show_lbs() const;
//void show_stn() const;
};
//===========================11_5.cpp============================
//#include<iostream>
#include"11_5.hpp"
using std::cout;
using std::endl;
Stonewt::Stonewt(double lbs, mode form)
{
fm=form;
if(fm==STONE)
{
//pds_left=int(lbs)%lbs_per_stn+lbs-int(lbs);
//pounds=lbs;
stone=int(lbs)/lbs_per_stn;
pds_left=int(lbs)%lbs_per_stn+lbs-int(lbs);
}
else if(fm==LBS_LEFT)
{
stone=int(lbs)/lbs_per_stn;
pds_left=int(lbs)%lbs_per_stn+lbs-int(lbs);
}
else if(fm==POUNDS)
{
pounds=lbs;
}
//return temp;
}
Stonewt::Stonewt(int stn,double lbs,mode form)
{
//Stonewt temp;
fm=form;
if(fm==STONE)
{
stone=stn;
pds_left=lbs;
}
else if(fm==POUNDS)
{
pounds=stn*lbs_per_stn+lbs;
}
//return temp;
}
Stonewt::Stonewt()
{
fm=STONE;
stone=pounds=pds_left=0;
}
Stonewt::~Stonewt()
{
cout<<"bye"<<endl;
}
void Stonewt::to_pounds()
{
if(fm==STONE||fm==LBS_LEFT)
pounds=stone*lbs_per_stn+pds_left;
fm=POUNDS;
}
void Stonewt::reset(mode form)
{
Stonewt temp;
if(form==STONE||form==LBS_LEFT)
{
//pds_left=int(lbs)%lbs_per_stn+lbs-int(lbs);
//pounds=lbs;
stone=int(pounds)/lbs_per_stn;
pds_left=int(pounds)%lbs_per_stn+(pounds)-int(pounds);
}
}
Stonewt operator+(Stonewt &st1,Stonewt &st2)
{
st1.to_pounds();
st2.to_pounds();
Stonewt temp;
temp.pounds=st1.pounds+st2.pounds;
temp.fm=st1.fm;
return temp;
}
Stonewt operator-(Stonewt &st1,Stonewt &st2)
{
st1.to_pounds();
st2.to_pounds();
Stonewt temp;
temp.pounds=st1.pounds-st2.pounds;
temp.fm=st1.fm;
return temp;
}
Stonewt operator*(double n,Stonewt &st)
{
st.to_pounds();
Stonewt temp;
temp.pounds=n*st.pounds;
temp.fm=st.fm;
return temp;
}
std::ostream & operator<<(std::ostream &os,const Stonewt &st)
{
if(st.fm==Stonewt::STONE)
{
os<<"integral stone is "<<st.stone<<endl;
}
else if(st.fm==Stonewt::LBS_LEFT)
{
os<<"lbs_left stone is "<<st.stone<<" , "<<st.pds_left<<endl;
}
else if(st.fm==Stonewt::POUNDS)
{
os<<"pounds is "<<st.pounds<<endl;
}
return os;
}
/*void Stonewt::show_stn() const
{
cout<<stone<<" stone,"<<pds_left<<"pounds\n";
}
void Stonewt::show_lbs() const
{
cout<<pounds<<" pounds\n";
}*/
//===========================11_5_main.cpp=========================
#include<iostream>
#include"11_5.hpp"
using std::cout;
using std::endl;
int main()
{
Stonewt zhuzhu=Stonewt(277,Stonewt::LBS_LEFT);
Stonewt baobao(285.7,Stonewt::POUNDS);
Stonewt guagua(21,8,Stonewt::STONE);
cout<<"zhuzhu "<<zhuzhu<<endl;
cout<<"baobao "<<baobao<<endl;
cout<<"guagua "<<guagua<<endl;
zhuzhu.to_pounds();
baobao.to_pounds();
guagua.to_pounds();
cout<<"zhuzhu "<<zhuzhu<<endl;
cout<<"baobao "<<baobao<<endl;
cout<<"guagua "<<guagua<<endl;
Stonewt add;
add=zhuzhu+baobao;
cout<<"zhuzhu + baobao "<<add<<endl;
Stonewt minus;
minus=baobao-guagua;
cout<<"baobao - guagua"<<minus<<endl;
Stonewt multiply;
multiply=10*zhuzhu;
cout<<"10 * zhuzhu "<<multiply<<endl;
}
/*
void display(const Stonewt & st,int n)
{
for(int i=0;i<n;i++)
{
cout<<"WOW! ";
st.show_stn();
}
}*/
//==================11_6.hpp====================
#include<iostream>
class Stonewt
{
private:
enum{LBS_PER_STN=14};
int stone;
double pds_left;
double pounds;
public:
Stonewt(double lbs);
Stonewt(int stn,double lbs);
Stonewt();
~Stonewt();
void show_lbs() const;
void show_stn() const;
bool operator>(const Stonewt &st);
bool operator<(const Stonewt &st);
bool operator>=(const Stonewt &st);
bool operator<=(const Stonewt &st);
bool operator==(const Stonewt &st);
bool operator!=(const Stonewt &st);
friend std::ostream & operator<<(std::ostream &os,const Stonewt &st);
};
//=====================11_6.cpp==========================
#include<iostream>
#include"11_6.hpp"
using std::cout;
using std::endl;
Stonewt::Stonewt(double lbs)
{
stone=int(lbs)/LBS_PER_STN;
pds_left=int(lbs)%LBS_PER_STN+lbs-int(lbs);
pounds=lbs;
}
Stonewt::Stonewt(int stn,double lbs)
{
stone=stn;
pds_left=lbs;
pounds=stn*LBS_PER_STN+lbs;
}
Stonewt::Stonewt()
{
stone=pounds=pds_left=0;
}
Stonewt::~Stonewt()
{
cout<<"bye"<<endl;
}
void Stonewt::show_stn() const
{
cout<<stone<<" stone, "<<pds_left<<" pounds\n";
}
void Stonewt::show_lbs() const
{
cout<<pounds<<" pounds\n";
}
bool Stonewt::operator>(const Stonewt &st)
{
if(this->pounds>st.pounds)
return true;
else
return false;
}
bool Stonewt::operator<(const Stonewt &st)
{
if(this->pounds<st.pounds)
return true;
else
return false;
}
bool Stonewt::operator>=(const Stonewt &st)
{
if(this->pounds>=st.pounds)
return true;
else
return false;
}
bool Stonewt::operator<=(const Stonewt &st)
{
if(this->pounds<=st.pounds)
return true;
else
return false;
}
bool Stonewt::operator==(const Stonewt &st)
{
if(this->pounds==st.pounds)
return true;
else
return false;
}
bool Stonewt::operator!=(const Stonewt &st)
{
if(this->pounds!=st.pounds)
return true;
else
return false;
}
std::ostream &operator<<(std::ostream &os,const Stonewt &st)
{
os<<"the subject pounds is "<<st.pounds<<endl;
os<<"the subject stone is "<<st.stone<<" ,and pds_left is "<<st.pds_left<<endl;
return os;
}
//====================11_6_main.cpp=======================
#include<iostream>
#include"11_6.hpp"
using namespace std;
const int size=6;
int main()
{
Stonewt s[size];
Stonewt a(11,0);
s[0]=Stonewt(200);
s[1]=Stonewt(201);
s[2]=Stonewt(202);
int i;
double p;
for(i=3;i<size;i++)
{
cout<<"enter your information"<<endl;
cin>>p;
cin.get();
s[i]=Stonewt(p);
}
int j;
Stonewt max_temp;
Stonewt min_temp=s[0];
int counts=0;
int max,min;
for(j=0;j<size;j++)
{
if(s[j]>max_temp)
{
max_temp=s[j];
max=j;
}
if(j<5&&s[j+1]<min_temp)
{
min_temp=s[j+1];
min=j+1;
}
if(s[j]>a)
counts++;
}
cout<<"the max element is "<<max_temp<<" at "<<max<<" location"<<endl;
cout<<"the min element is "<<min_temp<<" at "<<min<<" location"<<endl;
cout<<"elements >= 11 stone has "<<counts<<endl;
return 0;
}
//=============================11_7.hpp===========================
#include<iostream>
class complex_
{
private:
float real_;
float image_;
public:
complex_();
complex_(float x,float y);
~complex_();
friend complex_ operator+(const complex_ &x,const complex_ &y);
friend complex_ operator-(const complex_ &x,const complex_ &y);
friend complex_ operator*(const complex_ &x,const complex_ &y);
friend complex_ operator*(double n,const complex_ &x);
friend complex_ operator~(const complex_ &x);
friend std::ostream &operator<<(std::ostream &os,const complex_ &x);
friend std::istream &operator>>(std::istream &is,complex_ &x);
};
//====================11_7.cpp======================================
#include<iostream>
#include"11_7.hpp"
using std::cout;
using std::endl;
complex_::complex_()
{
real_=0.0;
image_=0.0;
}
complex_::complex_(float x,float y)
{
real_=x;
image_=y;
}
complex_::~complex_()
{
cout<<"bye"<<endl;
}
complex_ operator+(const complex_ &x,const complex_ &y)
{
complex_ temp;
temp.real_=x.real_+y.real_;
temp.image_=x.image_+y.image_;
return temp;
}
complex_ operator-(const complex_ &x,const complex_ &y)
{
complex_ temp;
temp.real_=x.real_-y.real_;
temp.image_=x.image_-y.image_;
return temp;
}
complex_ operator*(const complex_ &x,const complex_ &y)
{
complex_ temp;
temp.real_=x.real_*y.real_-x.image_*y.image_;
temp.image_=x.real_*y.image_+x.image_*y.real_;
return temp;
}
complex_ operator*(double n,const complex_ &x)
{
complex_ temp;
temp.real_=n*x.real_;
temp.image_=n*x.image_;
return temp;
}
complex_ operator~(const complex_ &x)
{
complex_ temp;
temp.real_=x.real_;
temp.image_=-x.image_;
return temp;
}
std::ostream &operator<<(std::ostream &os,const complex_ &x)
{
os<<"x is ("<<x.real_<<", "<<x.image_<<"i"<<")"<<endl;
}
std::istream &operator>>(std::istream &is,complex_ &x)
{
/*float a,b;
cout<<"enter your real"<<endl;
is>>a;
//is.get();
cout<<"enter your image"<<endl;
is>>b;
x.real_=a;
x.image_=b;
return is;*/
cout<<"enter your real"<<endl;
is>>x.real_;
cout<<"enter your image"<<endl;
is>>x.image_;
return is;
}
//=======================11_7_main.cpp=======================
#include<iostream>
#include"11_7.hpp"
using namespace std;
int main()
{
complex_ a(3.0,4.0);
complex_ c;
cout<<"enter a complex number(q to quit)\n";
while(cin>>c)
{
cout<<"c is "<<c<<'\n';
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 (q to quit):\n";
}
cout<<"Done!"<<endl;
return 0;
}