7个问题分下来4个类
随机漫步类
类声明
const double Rad_to_deg = 45.0 / atan(1.0);
class TD
{
public:
enum Choice{ one, two };
private:
double xval;
double yval;
double tarval;
double angval;
void set_tarval();
void set_angval();
void set_yval();
void set_xval();
Choice choice;
public:
TD();
TD(double x, double y, Choice mode=one);
~TD();
void reset(double x, double y, Choice mode = one);
//前进函数
double Tarval();
TD operator+(const TD & )const;
void set_xy()
{
choice = one;
};
void set_tan()
{
choice = two;
};
friend std::ostream& operator<<(std::ostream& os, const TD&);
};
函数实现
void TD::set_tarval()
{
tarval = std::sqrt(xval * xval + yval * yval);
}
void TD::set_angval()
{
if (xval == 0 && yval == 0)
angval = 0.0;
else
{
angval = std::atan2(yval , xval);
}
}
void TD::set_xval()
{
xval = tarval*std::cos(angval);
}
void TD::set_yval()
{
yval = tarval*std::sin(angval);
}
void TD::reset(double x, double y,Choice mode)
{
choice = mode;
if (choice == one)
{
xval = x;
yval = y;
set_angval();
set_tarval();
}
else if (choice == two)
{
tarval = x;
angval = y/Rad_to_deg;
set_xval();
set_yval();
}
else
{
TD();
exit(EXIT_FAILURE);
}
}
TD::TD()
{
xval = yval = 0.0;
tarval = angval = 0.0;
choice = one;
}
TD::TD(double x, double y, Choice mode)
{
choice = mode;
if (choice == one)
{
xval = x;
yval = y;
set_angval();
set_tarval();
}
else if (choice == two)
{
tarval = x;
angval = y/Rad_to_deg;
set_xval();
set_yval();
}
else
{
TD();
exit(EXIT_FAILURE);
}
}
double TD::Tarval()
{
return sqrt(xval * xval + yval * yval);
}
TD::~TD() {};
TD TD::operator+(const TD& front)const
{
/*TD result;
result.xval = xval + front.xval;
result.yval = yval + front.yval;
result.set_tarval();
result.set_angval();
return result;*/
return TD(xval + front.xval, yval + front.yval);
}
std::ostream& operator<<(std::ostream& os, const TD& result)
{
if (result.choice == TD::one)
os << "( x, y ): (" << result.xval << " , " << result.yval << " )\n";
else
os << "( tar,ang ): ( " << result.tarval << " , " << result.angval << " )\n";
return os;
}
主函数
int main_TD()
{
double direction;
TD result(0.0,0.0);
TD step;
int steps=1;
const int MAXSIZE = 1000;
int size;
cout << "loop number:\n";
cin >> size;
double Max_step[MAXSIZE];
//创造随机数
srand(time(0));
cout << "Enter direction target:\n";
double tar_direct;
cin >> tar_direct;
cout << "Enter the step direction:\n";
double dstep;
cin >> dstep;
for (int i = 0; i < size; i++)
{
steps = 1;
while (result.Tarval() < tar_direct)
{
direction = rand() % 360;
step.reset(dstep, direction, TD::two);
result = result + step;
result.set_xy();
cout << steps << " : " << result << endl;
steps++;
}
cout << --steps << " steps get to target.\n";
Max_step[i] = tar_direct / steps;
cout << "per steps' direction is " << Max_step[i]<< endl;
cout << result << endl
<< " or\n";
result.set_tan();
cout << result << endl;
result.reset(0.0, 0.0);
}
//计算数字
double Max = Max_step[0];
double Min = Max_step[0];
double Sum = Max;
for (int i = 0; i < size-1; i++)
{
Max = Max > Max_step[i + 1] ? Max : Max_step[i+1];
Min = Min < Max_step[i + 1] ? Min : Max_step[i+1];
Sum += Max_step[i+1];
}
cout << "Max: " << Max << endl << "Min: " << Min << endl;
cout << "per steps' direction is " << Sum / size << endl;
return 0;
}
时间相加类
类声明
class TM
{
private:
int hours;
int mins;
public:
TM();
TM(int h, int m = 0);
void AddMin(int m);
void AddHr(int n);
void Reset(int h = 0, int m = 0);
friend TM operator+(const TM& m, const TM& n);
friend TM operator-(const TM& m, const TM& n);
friend TM operator*(const double m, const TM& n);
friend TM operator*(const TM& m, const double n);
friend std::ostream& operator<<(std::ostream& os, const TM& t);
};
函数实现
TM::TM()
{
hours = mins = 0;
}
TM::TM(int h, int m)
{
hours = h;
mins = m;
}
void TM::AddMin(int m)
{
mins += m;
hours += mins / 60;
mins %= 60;
}
void TM::AddHr(int n)
{
hours += n;
}
void TM::Reset(int h , int m )
{
hours = h;
mins = m;
}
TM operator+(const TM& m, const TM& n)
{
TM result;
result.mins = m.mins + n.mins;
result.hours = m.hours + n.hours + result.mins / 60;
result.mins %= 60;
return result;
}
TM operator-(const TM& m, const TM& n)
{
TM diff;
diff.mins = m.hours * 60 + m.mins - (n.hours * 60 + n.mins);
diff.hours = diff.mins / 60;
diff.mins %= 60;
return diff;
}
TM operator*(const double m, const TM& n)
{
TM result;
result.mins = m * n.hours * 60 + m * n.mins;
result.hours = result.mins / 60;
result.mins &= 60;
return result;
}
TM operator*(const TM& m, const double n)
{
return n * m;
}
std::ostream& operator<<(std::ostream& os, const TM& t)
{
os << "hours: " << t.hours << ", minutes: " << t.mins << endl;
return os;
}
主函数
int main_TM()
{
TM star(0, 0);
TM trip(10, 50);
star = star + trip;
cout << star << endl;
TM a1(1, 30);
star = star - a1;
cout << star << endl;
TM a2(11, 20);
star = a1 + a2 + trip;
cout << star << endl;
double n=2.5;
star = n * star * n;
cout << star << endl;
star.Reset();
cout << star << endl;
return 0;
}
英石磅换算类
类声明
class ST
{
public:
enum Mode{ STO, POU, POUD };
private:
enum { Lbs_per_Stn = 14 };
int Stone;
double pds_left;
double pounds;
Mode mode;
public:
ST(double pound);
ST(int stn, double lbs);
ST();
~ST();
void set_STO();
void set_POU();
void set_POUD();
friend ostream& operator<<(ostream& os,const ST& t);
//比较函数
friend bool operator<(const ST& m, const ST& n);
friend bool operator<=(const ST& m, const ST& n);
friend bool operator==(const ST& m, const ST& n);
friend bool operator>(const ST& m, const ST& n);
friend bool operator>=(const ST& m, const ST& n);
friend bool operator!=(const ST& m, const ST& n);
};
函数实现
ST::ST(double pound)
{
pounds = pound;
Stone = pound / Lbs_per_Stn;
pds_left = (int)pound % Lbs_per_Stn + pound - (int)pound;
mode = STO;
}
ST::ST(int stn, double lbs)
{
Stone = stn;
pds_left = lbs;
pounds = stn * Lbs_per_Stn + lbs;
mode = STO;
}
ST::ST()
{
Stone = 0;
pds_left = pounds = 0.0;
mode = STO;
}
ST::~ST() {};
void ST::set_STO()
{
mode = STO;
}
void ST::set_POU()
{
mode = POU;
}
void ST::set_POUD()
{
mode = POUD;
}
ostream& operator<<(ostream& os, const ST& t)
{
if (t.mode == ST::STO)
{
os << "int Stone: " << (int)t.Stone <<"lbs: " << (int)t.pds_left << endl;
}
else if (t.mode == ST::POU)
{
os << "int pounds: " << (int)t.pounds << endl;
}
else if (t.mode == ST::POUD)
{
os << "double pounds: " << t.pounds << endl;
}
return os;
}
bool operator<(const ST& m, const ST& n)
{
return m.pounds < n.pounds;
}
bool operator<=(const ST& m, const ST& n)
{
return m.pounds <= n.pounds;
}
bool operator==(const ST& m, const ST& n)
{
return m.pounds == n.pounds;
}
bool operator>(const ST& m, const ST& n)
{
return m.pounds > n.pounds;
}
bool operator>=(const ST& m, const ST& n)
{
return m.pounds >= n.pounds;
}
bool operator!=(const ST& m, const ST& n)
{
return m.pounds != n.pounds;
}
主函数
int main_ST()
{
ST a = 120;
ST b(140, 50);
ST c;
ST d(159.32);
d.set_STO();
cout << d << endl;
d.set_POUD();
cout << d << endl;
d.set_POU();
cout << d << endl;
cout<<(a < b)<<endl<<(a==d)<<endl<<(c>b)<<endl<<(d==d);
return 0;
}
复数计算类
类声明
class CO
{
private:
double realval;
double imagval;
public:
CO();
~CO();
CO(double x, double y);
CO operator+(const CO& a) const;
CO operator-(const CO& a)const;
CO operator*(const CO& a)const;
CO operator*(double n) const;
friend CO operator*(double n,const CO& a);
friend CO operator~(const CO& a);
friend ostream& operator<<(ostream& os, const CO& a);
friend istream& operator>>(istream& is, CO& a);
};
函数实现
CO::CO()
{
realval = 0.0;
imagval = 0.0;
}
CO::~CO() {};
CO::CO(double x, double y)
{
realval = x;
imagval = y;
}
CO CO::operator+(const CO& a)const
{
CO result;
result.realval = realval + a.realval;
result.imagval = imagval + a.imagval;
return result;
}
CO CO::operator-(const CO& a)const
{
return CO(realval - a.realval, imagval - a.imagval);
}
CO CO::operator*(const CO& a)const
{
CO result;
result.realval = realval * a.realval - imagval * a.imagval;
result.imagval = realval * a.imagval + a.realval * imagval;
return result;
}
CO CO::operator*(double n)const
{
return CO(realval * n, imagval * n);
}
CO operator*(double n, const CO& a)
{
return a * n;
}
CO operator~(const CO& a)
{
return CO(a.realval, -a.imagval);
}
ostream& operator<<(ostream& os, const CO& a)
{
os << "(" << a.realval << ", " << a.imagval << "i )\n";
return os;
}
istream& operator>>(istream& is, CO& a)
{
cout << "real: ";
is >> a.realval;
cout << "\nimaginary: ";
is >> a.imagval;
cout << endl;
return is;
}
主函数
int main_CO()
{
CO ma(3.0, 4.0);
CO mc;
cout << "Enter a complex number (q to quit):\n";
while (cin >> mc)
{
cout << "mc:" << mc << "\n";
cout << "共轭:" << ~mc << endl;
cout << "ma+ mc = " << ma + mc << endl;
cout << " ma - mc = " << ma - mc << endl;
cout << "ma * mc = " << ma * mc << endl;
cout << "mc * ma = " << mc * ma << endl;
cout << "2* mc = " << 2 * mc << endl;
cout << "Enter a complex number (q to quit):\n";
}
cout << "Done.\n";
return 0;
}