//11.1
//vector.h -- Vector class with << mode state
#ifndef VECTOR__H__
#define VECTOR__H__
#include <iostream>
namespace VECTOR{
class Vector
{
public:
enum Mode{RECT,POL};
//RECT to rectangular,POL for Polar modes
private:
double x;//horizontal value
double y;// vertical value
double mag;//length of vector
double ang;//direction if vector in degrees
Mode mode;
//private methods for setting values
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();//set mode to POL
void rect_mode();//set mode to RECT
//operator overloading
Vector operator+(const Vector & b) const;
Vector operator-(const Vector & b) const;
Vector operator*(double n) const;
Vector operator-() const;
//friends
friend Vector operator*(double n,const Vector & a);
friend std::ostream & operator<<(std::ostream & os,const Vector & v);//返回类型为cout、cerr等标准输出对象
};
}//end namespace VECTOR
#endif
//vector.cpp --methods for the Vector class
#include <cmath>
#include "vector.h"
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
using std::cin;
namespace VECTOR
{
//computer degrees in one radian
const double Rad_to_deg = 45.0/atan(1.0);
//should be about 57.2957795130823
//private methods
//calcualtes magnitude from x and y
void Vector::set_mag()
{
mag = sqrt(x*x+y*y);
}
void Vector::set_ang()
{
if(x == 0 && y == 0)
ang =0.0;
else
ang = atan2(y,x);
}
void Vector::set_x()
{
x = mag * cos(ang);
}
void Vector::set_y()
{
y = mag * sin(ang);
}
//public methods
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_ang();
set_mag();
}
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;
}
}
//reset vector from rectangular coordinates if form is
//RECT (the default) or else from polar coordinates if from is POL
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() //destructor
{
}
void Vector::polar_mode() //set to polar mode
{
mode = POL;
}
void Vector::rect_mode()//set to rectangular mode
{
mode = RECT;
}
//operator overloading
//add two Vectors
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);
}
//reverse sign of Vector
Vector Vector::operator-() const
{
return Vector(-x,-y);
}
//multiply vector by n
Vector Vector::operator*(double n) const
{
return Vector(n*x,n*y);
}
//friend methods
//multiply n by Vector a
Vector operator*(double n,const Vector & a)
{
return a*n;
}
//display rectangular coordinates if mode is RECT,
//else display polar coordinates if mode is POL
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;
}
}//end namespace VECTOR
//randwalk.cpp -- using the Vector class
#include <iostream>
#include "vector.h"
#include <cstdlib>
#include <ctime>
#include <fstream>//
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;
ofstream fout;//
fout.open("test.txt");//
cout<<"Enter target distanee(q to quit):";
while(cin>>target)
{
cout<<"Enter step length:";
if(!(cin>>dstep))
break;
fout<<"Target Distance: "<<target<<" , Step Size: "<<dstep<<endl;//
while(result.magval()<target)
{
fout<<steps<<":"<<result<<endl;//
direction = rand() % 360;
step.reset(dstep,direction,Vector::POL);
result = result + step;
steps++;
}
fout<<steps<<":"<<result<<endl;//
cout<<"After "<<steps<<" steps,the subject "
"has the folling location:\n";
fout<<"After "<<steps<<" steps,the subject "
"has the folling location:\n";//
cout<<result<<endl;
fout<<result<<endl;
result.polar_mode();
cout<<" or \n"<<result<<endl;
fout<<" or \n"<<result<<endl;
cout<<"Average outward distance per step = "
<<result.magval()/steps<<endl;
fout<<"Average outward distance per step = "
<<result.magval()/steps<<endl;
}
cout<<"Bye!\n";
cin.clear();
cin.sync();
return 0 ;
}
//11.2
//vector.h -- Vector class with << mode state
#ifndef VECTOR__H__
#define VECTOR__H__
#include <iostream>
namespace VECTOR{
class Vector
{
public:
enum Mode{RECT,POL};
//RECT to rectangular,POL for Polar modes
private:
double x;//horizontal value
double y;// vertical value
double mag;//length of vector
double ang;//direction if vector in degrees
Mode mode;
//private methods for setting values
//void set_mag();
//void set_ang();
double set_mag();
double 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;}
double magval();
double angval();
void polar_mode();//set mode to POL
void rect_mode();//set mode to RECT
//operator overloading
Vector operator+(const Vector & b) const;
Vector operator-(const Vector & b) const;
Vector operator*(double n) const;
Vector operator-() const;
//friends
friend Vector operator*(double n,const Vector & a);
friend std::ostream & operator<<(std::ostream & os,const Vector & v);//返回类型为cout、cerr等标准输出对象
};
}//end namespace VECTOR
#endif
//vector.cpp --methods for the Vector class
#include <cmath>
#include "vector.h"
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
using std::cin;
namespace VECTOR
{
//computer degrees in one radian
const double Rad_to_deg = 45.0/atan(1.0);
//should be about 57.2957795130823
//private methods
//calcualtes magnitude from x and y
/*
void Vector::set_mag()
{
mag = sqrt(x*x+y*y);
}
void Vector::set_ang()
{
if(x == 0 && y == 0)
ang =0.0;
else
ang = atan2(y,x);
}
void Vector::set_x()
{
x = mag * cos(ang);
}
*/
double Vector::set_mag()
{
mag = sqrt(x*x+y*y);
return mag;
}
double Vector::set_ang()
{
if(x == 0 && y == 0)
ang =0.0;
else
ang = atan2(y,x);
return ang;
}
double Vector::magval()
{
return set_mag();
}
double Vector::angval()
{
return set_ang();
}
void Vector::set_x()
{
x = mag * cos(ang);
}
void Vector::set_y()
{
y = mag * sin(ang);
}
//public methods
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;
magval();
angval();
}
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;
}
}
//reset vector from rectangular coordinates if form is
//RECT (the default) or else from polar coordinates if from is POL
void Vector::reset(double n1,double n2,Mode form)
{
mode = form;
if(form == RECT)
{
x = n1;
y = n2;
magval();
angval();
}
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() //destructor
{
}
void Vector::polar_mode() //set to polar mode
{
mode = POL;
}
void Vector::rect_mode()//set to rectangular mode
{
mode = RECT;
}
//operator overloading
//add two Vectors
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);
}
//reverse sign of Vector
Vector Vector::operator-() const
{
return Vector(-x,-y);
}
//multiply vector by n
Vector Vector::operator*(double n) const
{
return Vector(n*x,n*y);
}
//friend methods
//multiply n by Vector a
Vector operator*(double n,const Vector & a)
{
return a*n;
}
//display rectangular coordinates if mode is RECT,
//else display polar coordinates if mode is POL
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;
}
}//end namespace VECTOR
//randwalk.cpp -- using the Vector class
#include <iostream>
#include "vector.h"
#include <cstdlib>
#include <ctime>
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 distanee(q to quit):";
while(cin>>target)
{
cout<<"Enter step length:";
if(!(cin>>dstep))
break;
while(result.magval()<target)
{
direction = rand() % 360;
step.reset(dstep,direction,Vector::POL);
result = result + step;
steps++;
}
cout<<"After "<<steps<<" steps,the subject "
"has the folling location:\n";
cout<<result<<endl;
result.polar_mode();
cout<<" or \n"<<result<<endl;
cout<<"Average outward distance per step = "
<<result.magval()/steps<<endl;
}
cout<<"Bye!\n";
cin.clear();
cin.sync();
return 0 ;
}
//11.3
//vector.h -- Vector class with << mode state
#ifndef VECTOR__H__
#define VECTOR__H__
#include <iostream>
namespace VECTOR{
class Vector
{
public:
enum Mode{RECT,POL};
//RECT to rectangular,POL for Polar modes
private:
double x;//horizontal value
double y;// vertical value
double mag;//length of vector
double ang;//direction if vector in degrees
Mode mode;
//private methods for setting values
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();//set mode to POL
void rect_mode();//set mode to RECT
//operator overloading
Vector operator+(const Vector & b) const;
Vector operator-(const Vector & b) const;
Vector operator*(double n) const;
Vector operator-() const;
//friends
friend Vector operator*(double n,const Vector & a);
friend std::ostream & operator<<(std::ostream & os,const Vector & v);//返回类型为cout、cerr等标准输出对象
};
}//end namespace VECTOR
#endif
//vector.cpp --methods for the Vector class
#include <cmath>
#include "vector.h"
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
using std::cin;
namespace VECTOR
{
//computer degrees in one radian
const double Rad_to_deg = 45.0/atan(1.0);
//should be about 57.2957795130823
//private methods
//calcualtes magnitude from x and y
void Vector::set_mag()
{
mag = sqrt(x*x+y*y);
}
void Vector::set_ang()
{
if(x == 0 && y == 0)
ang =0.0;
else
ang = atan2(y,x);
}
void Vector::set_x()
{
x = mag * cos(ang);
}
void Vector::set_y()
{
y = mag * sin(ang);
}
//public methods
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_ang();
set_mag();
}
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;
}
}
//reset vector from rectangular coordinates if form is
//RECT (the default) or else from polar coordinates if from is POL
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() //destructor
{
}
void Vector::polar_mode() //set to polar mode
{
mode = POL;
}
void Vector::rect_mode()//set to rectangular mode
{
mode = RECT;
}
//operator overloading
//add two Vectors
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);
}
//reverse sign of Vector
Vector Vector::operator-() const
{
return Vector(-x,-y);
}
//multiply vector by n
Vector Vector::operator*(double n) const
{
return Vector(n*x,n*y);
}
//friend methods
//multiply n by Vector a
Vector operator*(double n,const Vector & a)
{
return a*n;
}
//display rectangular coordinates if mode is RECT,
//else display polar coordinates if mode is POL
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;
}
}//end namespace VECTOR
//randwalk.cpp -- using the Vector class
#include <iostream>
#include "vector.h"
#include <cstdlib>
#include <ctime>
#include <fstream>//
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;
int testsize;
double maxtep = 0;
double mintep = 0;
static double avertep = 0;
ofstream fout;//
fout.open("test.txt");//
cout<<"Enter testsize(q to quit):";
while(!(cin>>testsize))
{
cout<<"Enter the true genre(q to quit):";
cin.clear();
cin.sync();
}
int n = testsize;
while(n--)
{
cout<<"Enter target distanee(q to quit):";
while(!(cin>>target))
{
cout<<"Enter the true genre(q to quit):";
cin.clear();
cin.sync();
}
cout<<"Enter step length:";
while(!(cin>>dstep))
{
cout<<"Enter the true genre(q to quit):";
cin.clear();
cin.sync();
}
while(result.magval()<target)
{
direction = rand() % 360;
step.reset(dstep,direction,Vector::POL);
result = result + step;
steps++;
}
cout<<"After "<<steps<<" steps,the subject "
"has the folling location:\n";
if(avertep == 0)
mintep = steps;
if(steps>maxtep)
maxtep = steps;
if(steps<mintep)
mintep = steps;
avertep += steps;
}
cout<<"In total "<<testsize<<" mesurement,the maxtep is "<<maxtep<<" ,the mintep is "<<mintep<<" and the avertep is "<<(avertep/testsize)<<endl;
return 0 ;
}
//11.4
//sale.h
#include <iostream>
#ifndef __SALE__H__
#define __SALE__H__
namespace SALES
{
class Sales
{
private:
static const int QUARTERS = 4;
double sales[QUARTERS];
double average;
double max;
double min;
public:
Sales(const double ar[],int n);
Sales();
void showSales() const;
};
}
#endif
//sale.cpp
#include <iostream>
#include "sale.h"
using std::cin;
using std::cout;
using std::endl;
using std::string;
namespace SALES
{
static double calcAverage(double arr[], int arrsize)
{
double sum = 0;
for(int i = 0;i<arrsize;i++)
sum+=arr[i];
return (sum/arrsize);
}
static double calcMax(double arr[], int arrsize)
{
int idMax = 0;
for(int i =1;i<arrsize;i++)
{
if(arr[i]>arr[idMax])
idMax = i;
}
return arr[idMax];
}
static double calcMin(double arr[], int arrsize)
{
int idMin = 0;
for(int i =1;i<arrsize;i++)
{
if(arr[i]<arr[idMin])
idMin = i;
}
return arr[idMin];
}
Sales::Sales(const double ar[],int n)
{
int times = n<QUARTERS?n:QUARTERS;
for(int i = 0;i<times;i++)
(*this).sales[i]=ar[i];
for(int i =times;i<QUARTERS;i++)
this->sales[i] = 0;
this->average = calcAverage(this->sales, times);
this->max = calcMax(this->sales, times);
this->min = calcMin(this->sales, times);
}
Sales::Sales()
{
cout << "Please enter " << QUARTERS << " sale record:";
int times = QUARTERS;
double arr[QUARTERS] = {0};
for (int i = 0;i < QUARTERS; i++) {
cin >> arr[i];
if (!cin) {
times = i; //表示输入异常
break;
}
}
*this = Sales::Sales(arr, QUARTERS); //先得到初始数据在进行操作
this->average = calcAverage(this->sales, times);
this->max = calcMax(this->sales, times);
this->min = calcMin(this->sales, times);
}
void Sales::showSales() const
{
cout << "sales: ";
for(int i = 0;i<QUARTERS;i++)
cout<<this->sales[i]<<" ";
cout << endl;
cout << "average: " <<this->average << endl;
cout << "max: " << this->max << endl;
cout << "min: " << this->min << endl;
}
}
//main.cpp
#include <iostream>
#include "sale.h"
using namespace std;
int main ()
{
using namespace SALES;
const double salesList[] = {2.24,234,53,23.6,6.4};
Sales salesCar(salesList, (sizeof(salesList)/sizeof(salesList[0]))); ;
salesCar.showSales();
Sales salesBus;
salesBus.showSales();
}
//11.5
//stonewt.h -- definition for the Stonewt class
#ifndef STONEWT__H__
#define STONEWT__H__
class Stonewt
{
public:
enum Sort{POUNDS,FRAC_POUND};
private:
static const int Lbs_per_stn = 14;
int stone; //whole stones
double pds_left;//fractional pounds
double pounds;//entire weight in pounds
Sort sort;
public:
Stonewt(int stn,double lbs,Sort form);
Stonewt();
~Stonewt();
friend Stonewt operator+(Stonewt & s1,Stonewt & s2);
friend Stonewt operator-(Stonewt & s1,Stonewt & s2);
friend Stonewt operator*(Stonewt & s1,Stonewt & s2);
friend std::ostream & operator<<(std::ostream & os, const Stonewt & s1);
};
#endif
//stonewt.cpp
#include <iostream>
using std::cout;
#include "stonewt.h"
Stonewt::Stonewt(int stn,double lbs,Sort form)
{
if(form == POUNDS)
{
stone = int(lbs)/Lbs_per_stn + stn;
pds_left = int(lbs) % Lbs_per_stn + lbs -int(lbs);
pounds = lbs+ stn * Lbs_per_stn;
sort = form;
}
else if(form == FRAC_POUND)
{
stone = int(lbs)/Lbs_per_stn + stn;
pds_left = int(lbs) % Lbs_per_stn + lbs -int(lbs);
pounds = lbs+ stn * Lbs_per_stn;
sort = form;
}
else
{
cout<<"data form don't matching.";
stone = pds_left = pounds = 0;
form = FRAC_POUND;
}
}
Stonewt::Stonewt()
{
stone = pounds = pds_left = 0;
sort = FRAC_POUND;
}
Stonewt::~Stonewt()
{
}
Stonewt operator+(Stonewt & s1,Stonewt & s2)
{
Stonewt s;
s.pounds = s1.pounds + s2.pounds;
s.stone = s.pounds/Stonewt::Lbs_per_stn;
s.pds_left = int(s.pounds) % Stonewt::Lbs_per_stn + s.pounds - int(s.pounds);
return s;
}
Stonewt operator-(Stonewt & s1,Stonewt & s2)
{
Stonewt s;
s.pounds = s1.pounds - s2.pounds;
s.stone = s.pounds/Stonewt::Lbs_per_stn;
s.pds_left = int(s.pounds) % Stonewt::Lbs_per_stn + s.pounds - int(s.pounds);
return s;
}
Stonewt operator*(Stonewt & s1,Stonewt & s2)
{
Stonewt s;
s.pounds = s1.pounds * s2.pounds;
s.stone = s.pounds/Stonewt::Lbs_per_stn;
s.pds_left = int(s.pounds) % Stonewt::Lbs_per_stn + s.pounds - int(s.pounds);
return s;
}
std::ostream & operator<<(std::ostream & os, const Stonewt & s1)
{
if(s1.sort == Stonewt::FRAC_POUND)
os<<"The stone is "<<s1.stone<<" and the pounds is "<<s1.pds_left<<std::endl;
else
os<<"The pounds is "<<s1.pounds<<std::endl;
return os;
}
//main.cpp -- user-defined conversion
#include <iostream>
#include "stonewt.h"
using std::cout;
using std::cin;
using std::endl;
int main()
{
Stonewt stone_one(120,102.2,Stonewt::FRAC_POUND);
Stonewt stone_two(22,187.8,Stonewt::POUNDS);
cout<<stone_one;
cout<<stone_two;
Stonewt stone_three = stone_one + stone_two;
cout<<stone_three;
stone_three = stone_one - stone_two;
cout<<stone_three;
stone_three = stone_one * stone_two;
cout<<stone_three;
}
//11.6
//stonewt.h -- definition for the Stonewt class
#ifndef STONEWT__H__
#define STONEWT__H__
class Stonewt
{
private:
static const int Lbs_per_stn = 14;
int stone; //whole stones
double pds_left;//fractional pounds
double pounds;//entire weight in pounds
public:
Stonewt(int stn,double lbs);
Stonewt(double _pounds);
Stonewt();
~Stonewt();
friend bool operator<(Stonewt & s1,Stonewt & s2);
friend bool operator<=(Stonewt & s1,Stonewt & s2);
friend bool operator>(Stonewt & s1,Stonewt & s2);
friend bool operator>=(Stonewt & s1,Stonewt & s2);
friend bool operator==(Stonewt & s1,Stonewt & s2);
friend bool operator!=(Stonewt & s1,Stonewt & s2);
friend std::ostream & operator<<(std::ostream & os, const Stonewt & s1);
};
#endif
//stonewt.cpp
#include <iostream>
using std::cout;
#include "stonewt.h"
Stonewt::Stonewt(int stn,double lbs)
{
stone = int(lbs)/Lbs_per_stn + stn;
pds_left = int(lbs) % Lbs_per_stn + lbs -int(lbs);
pounds = lbs+ stn * Lbs_per_stn;
}
Stonewt::Stonewt()
{
stone = pounds = pds_left = 0;
}
Stonewt::Stonewt(double _pounds)
{
stone = pds_left = 0;
pounds = _pounds;
}
Stonewt::~Stonewt()
{
}
bool operator>(Stonewt & s1,Stonewt & s2)
{
return s1.pounds > s2.pounds;
}
bool operator>=(Stonewt & s1,Stonewt & s2)
{
return s1.pounds >= s2.pounds;
}
bool operator<(Stonewt & s1,Stonewt & s2)
{
return s1.pounds < s2.pounds;
}
std::ostream & operator<<(std::ostream & os, const Stonewt & s1)
{
os<<s1.pounds;
return os;
}
bool operator<=(Stonewt & s1,Stonewt & s2)
{
return s1.pounds <= s2.pounds;
}
bool operator==(Stonewt & s1,Stonewt & s2)
{
return s1.pounds == s2.pounds;
}
bool operator!=(Stonewt & s1,Stonewt & s2)
{
return s1.pounds != s2.pounds;
}
//main.cpp -- user-defined conversion
#include <iostream>
#include "stonewt.h"
using std::cout;
using std::cin;
using std::endl;
int main()
{
const int N = 6;
Stonewt stonewt[N]= {
Stonewt(12),Stonewt(4),Stonewt(7),
};
for(int i = 3;i<6;i++)
{
cout<<"Please enter the Initialization data:";
double s_stone;
cin>>s_stone;
cin.get();
stonewt[i] = Stonewt(s_stone);
}
Stonewt s_example(11);
static int max = 0;
static int min = 0;
static int count = 0;
for(int i=0;i<N-1;i++)
{
max = stonewt[max]>stonewt[i+1]?max:i+1;
min = stonewt[min]<stonewt[i+1]?min:i+1;
}
for(int i=0;i<N;i++)
{
if(stonewt[i]>=s_example)
count++;
}
cout<<"The max element is "<<stonewt[max]<<", the min element is "<<stonewt[min]<<" and the number of elements greater than 11 is:"<<count<<endl;
}
//11.7
//comlpex0.h
#ifndef COMPLEX0__H__
#define COMPLEX0__H__
#include <iostream>
class Complex
{
private:
double real;
double imaginary;
public:
Complex();
Complex(double _real,double _imaginary);
Complex operator+(const Complex & c1);
Complex operator-(const Complex & c1);
Complex operator*(const Complex & c1);
Complex operator*(double x);
friend Complex operator*(double x,Complex & c1){return c1 * x;}
Complex operator~();
friend std::istream & operator>>(std::istream & is,Complex & c1);
friend std::ostream & operator<<(std::ostream & os,const Complex & c1);
};
#endif
//complex0.cpp
#include "complex0.h"
Complex::Complex()
{
real = imaginary = 0;
}
Complex::Complex(double _real,double _imaginary)
{
real = _real;
imaginary = _imaginary;
}
Complex Complex::operator+(const Complex & c1)
{
Complex c;
c.real = real + c1.real;
c.imaginary = imaginary + c1.imaginary;
return c;
}
Complex Complex::operator-(const Complex & c1)
{
Complex c;
c.real = real - c1.real;
c.imaginary = imaginary - c1.imaginary;
return c;
}
Complex Complex::operator*(const Complex & c1)
{
Complex c;
c.real = real * c1.real - imaginary * c1.imaginary;
c.imaginary = real * c1.imaginary + imaginary * c1.real;
return c;
}
Complex Complex::operator*(double x)
{
Complex c;
c.real = real * x;
c.imaginary = imaginary * x;
return c;
}
Complex Complex::operator~()
{
Complex c;
c.imaginary = - (*this).imaginary;
c.real = (*this).real;
return c;
}
std::istream & operator>>(std::istream & is,Complex & c1)
{
std::cout<<"real: ";
if(is >> c1.real)
{
std::cout<<"imaginary: ";
is >> c1.imaginary;
}
return is;
}
std::ostream & operator<<(std::ostream & os,const Complex & c1)
{
os<<"("<<c1.real<<","<<c1.imaginary<<"i)"<<std::endl;
return os;
}
//main.cpp
#include <iostream>
#include "complex0.h"
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<<endl;
cout<<"complex conjugate is "<<~c<<'\n';
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<<"c * 2 is "<<c * 2<<endl;
cout<<"Enter a complex number (q to quit):\n";
}
cout<<"Done!\n";
return 0;
}