第一题

vect.h:

#ifndef VECTOR_H_
#define VECTOR_H_
#include <iostream>
namespace VECTOR
{
class Vector
{
public:
enum Mode {RECT, POL};
// RECT for rectangular, POL for Polar modes
private:
double x; // horizontal value
double y; // vertical value
double mag; // length of vector
double ang; // direction of vector in degrees
Mode mode; // RECT or POL
// 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;} // report x value
double yval() const {return y;} // report y value
double magval() const {return mag;} // report magnitude
double angval() const {return ang;} // report angle
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-() const;
Vector operator*(double n) const;
// friends
friend Vector operator*(double n, const Vector & a);
friend std::ostream &
operator<<(std::ostream & os, const Vector & v);
}; } // end namespace VECTOR
#endif

vect.cpp:

#include <cmath>
#include "vect.h" // includes <iostream>
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout; namespace VECTOR
{
// compute degrees in one radian
const double Rad_to_deg = 45.0 / atan(1.0);
// should be about 57.2957795130823 // private methods
// calculates magnitude from x and y
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);//mag为斜边
} // set y from polar coordinate
void Vector::set_y()
{ y = mag * sin(ang);
}
// public methods
Vector::Vector() // default constructor
{
x = y = mag = ang = 0.0;
mode = RECT;
} // construct vector from rectangular coordinates if form is r
// (the default) or else from polar coordinates if form is p
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;
}
} // reset vector from rectangular coordinates if form is
// RECT (the default) or else from polar coordinates if
// form 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);
} // subtract Vector b from a
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

#include <iostream>
#include <cstdlib> // rand(), srand() prototypes
#include <ctime> // time() prototype
#include "vect.h"
#include <fstream>
int main()
{
using namespace std;
using VECTOR::Vector;
srand(time(0)); // seed random-number generator
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 distance (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++;
}
fout << "After " << steps << " steps, the subject "
"has the following location:\n";
fout << result << endl;
result.polar_mode();
fout << " or\n" << result << endl;
fout << "Average outward distance per step = "
<< result.magval()/steps << endl;
fout<<endl;
steps = 0;
result.reset(0.0, 0.0);
cout << "Enter target distance (q to quit): ";
}
cout << "Bye!\n";
cin.clear();
while (cin.get() != '\n')
continue;
fout.close();
return 0;
}

第二题

vect.h

#ifndef VECTOR_H_
#define VECTOR_H_
#include <iostream>
namespace VECTOR
{
class Vector
{
public:
enum Mode {RECT, POL};
// RECT for rectangular, POL for Polar modes
private:
double x; // horizontal value
double y; // vertical value
double mag; // length of vector
double ang; // direction of vector in degrees
Mode mode; // RECT or POL
// private methods for setting values
double set_mag()const;
double set_ang()const;
void set_x(double mag,double ang);
void set_y(double mag,double ang);
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;} // report x value
double yval() const {return y;} // report y value
double magval() const {return set_mag();} // report magnitude
double angval() const {return set_ang();} // report angle
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-() const;
Vector operator*(double n) const;
// friends
friend Vector operator*(double n, const Vector & a);
friend std::ostream &
operator<<(std::ostream & os, const Vector & v);
}; } // end namespace VECTOR
#endif
#include <cmath>
#include "vect.h" // includes <iostream>
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout; namespace VECTOR
{
// compute degrees in one radian
const double Rad_to_deg = 45.0 / atan(1.0);
// should be about 57.2957795130823 // private methods
// calculates magnitude from x and y
double Vector::set_mag()const
{
return sqrt(x * x + y * y);
} double Vector::set_ang()const
{
if (x == 0.0 && y == 0.0)
return 0.0;
else
return atan2(y, x);
} // set x from polar coordinate
void Vector::set_x(double mag,double ang)
{
x = mag * cos(ang);//mag为斜边
} // set y from polar coordinate
void Vector::set_y(double mag,double ang)
{ y = mag * sin(ang);
}
// public methods
Vector::Vector() // default constructor
{
x = y = mag = ang = 0.0;
mode = RECT;
} // construct vector from rectangular coordinates if form is r
// (the default) or else from polar coordinates if form is p
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(mag,ang);
set_y(mag,ang);
}
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
// form 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(mag,ang);
set_y(mag,ang);
}
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);
} // subtract Vector b from a
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

#include <iostream>
#include <cstdlib> // rand(), srand() prototypes
#include <ctime> // time() prototype
#include "vect.h"
#include <fstream>
int main()
{
using namespace std;
using VECTOR::Vector;
srand(time(0)); // seed random-number generator
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 distance (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++;
}
fout << "After " << steps << " steps, the subject "
"has the following location:\n";
fout << result << endl;
result.polar_mode();
fout << " or\n" << result << endl;
fout << "Average outward distance per step = "
<< result.magval()/steps << endl;
fout<<endl;
steps = 0;
result.reset(0.0, 0.0);
cout << "Enter target distance (q to quit): ";
}
cout << "Bye!\n";
cin.clear();
while (cin.get() != '\n')
continue;
fout.close();
return 0;
}

第三题

randwalks.cpp

#include <iostream>
#include <cstdlib> // rand(), srand() prototypes
#include <ctime> // time() prototype
#include "vect.h"
int main()
{
using namespace std;
using VECTOR::Vector;
int couts=0;
srand(time(0)); // seed random-number generator
double direction;
Vector step;
Vector result(0.0, 0.0);
unsigned long steps = 0;
unsigned long max_steps=0.0;
unsigned long min_steps=0.0;
double target;
double dstep;
double avg_steps=0.0;
double stepss=65536;
cout << "Enter target distance (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++;
}
if(max_steps<steps)
{
max_steps=steps;
}
if(min_steps>steps)
{
min_steps=steps;
}
stepss+=steps;
couts+=1;
steps=0;
result.reset(0.0,0.0);
cout << "Enter target distance (q to quit): ";
}
avg_steps=stepss/couts;
cout<<"Max_steps:"<<max_steps<<endl;
cout<<"Min_steps:"<<min_steps<<endl;
cout<<"avg_steps:"<<avg_steps<<endl;
cout << "Bye!\n";
cin.clear();
while (cin.get() != '\n')
continue;
return 0;
}

第4题

mytimeh

#ifndef MYTIME3_H_
#define MYTIME3_H_
#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 & a,const Time & b);
friend Time operator-(const Time & a,const Time & b);
Time operator*(double n) const;
friend Time operator*(double m, const Time & t)
{ return t * m; } // inline definition
friend std::ostream & operator<<(std::ostream & os, const Time & t);
}; #endif

mytime3.cpp

#include "mytime3.h"

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;
minutes %= 60;
} void Time::AddHr(int h)
{
hours += h;
} void Time::Reset(int h, int m)
{
hours = h;
minutes = m;
} Time operator+(const Time & a,const Time & b)
{
Time sum;
sum.minutes = a.minutes + b.minutes;
sum.hours = a.hours + b.hours + sum.minutes / 60;
sum.minutes %= 60;
return sum;
} Time operator-(const Time & a,const Time &b)
{
Time diff;
int tot1, tot2;
tot1 = a.minutes + 60 * a.hours;
tot2 = b.minutes + 60 * b.hours;
diff.minutes = (tot2 - tot1) % 60;
diff.hours = (tot2 - tot1) / 60;
return diff;
} Time Time::operator*(double mult) const
{
Time result;
long totalminutes = hours * mult * 60 + minutes * mult;
result.hours = totalminutes / 60;
result.minutes = totalminutes % 60;
return result;
} std::ostream & operator<<(std::ostream & os, const Time & t)
{
os << t.hours << " hours, " << t.minutes << " minutes";
return os;
}

 第五题

stonewt.h

#ifndef STONEWT_H_
#define STONEWT_H_
#include <iostream>
class Stonewt
{
public:
enum format{A,B,C};//三个依次对应的模式
private:
enum {Lbs_per_stn=14};
format ft;
double stone;
int ipounds;
double dpounds;
double total;
void update();
public:
Stonewt();
Stonewt(double n,format f=A);
Stonewt operator+(double n)const;
Stonewt operator-(double n)const;
Stonewt operator*(double n)const;
friend Stonewt operator+(double n,const Stonewt & st);
friend Stonewt operator-(double n,const Stonewt & st);
friend Stonewt operator*(double n,const Stonewt & st);
friend std::ostream & operator<<(std::ostream & os,const Stonewt & st);
};
#endif

stonewt.cpp

#include <iostream>
#include "stonewt.h" using namespace std; void Stonewt::update()
{
if(ft==A)
{
stone=total;
ipounds=(int)stone*Lbs_per_stn;
dpounds=stone*Lbs_per_stn;
}
else if(ft==B)
{
ipounds=total;
stone=ipounds/Lbs_per_stn;
dpounds=ipounds;
}
else if(ft==C)
{
dpounds=total;
stone=dpounds/Lbs_per_stn;
ipounds=(int)dpounds;
}
} Stonewt::Stonewt()
{
ft=A;
total=0;
update();
} Stonewt::Stonewt(double n,format f)
{
total=n;
ft=f;
update();
} Stonewt Stonewt::operator+(double n)const
{
Stonewt st(total+n,ft);
return st;
} Stonewt Stonewt::operator-(double n)const
{
Stonewt st(total-n,ft);
return st;
} Stonewt Stonewt::operator*(double n)const
{
Stonewt st(total*n,ft);
return st;
}
Stonewt operator+(double n,const Stonewt &st)
{
return (st+n);
} Stonewt operator-(double n,const Stonewt &st)
{
return (st-n);
} Stonewt operator*(double n,const Stonewt & st)
{
return (st*n);
} std::ostream & operator<<(std::ostream & os, const Stonewt & st)
{
if (st.ft == st.A)
{
cout << "Stone: " << st.stone ;
}
else if (st.ft == st.B)
{
cout << "pounds in int: " << st.ipounds ;
}
else if (st.ft == st.C)
{
cout << "pounds in double: " << st.dpounds;
} return os;
}

uerstoneet.cpp

#include <iostream>
#include "stonewt.h" using namespace std; int main()
{
Stonewt st1(100);
Stonewt st2(100.1,Stonewt::A);
Stonewt st3(100.3,Stonewt::C); Stonewt st4=13+st1;
Stonewt st5=13+st2;
Stonewt st6=13+st3; Stonewt st7 = st1 + 10;
Stonewt st8 = st1 - 10;
Stonewt st9 = st1 * 10; cout << st1 << endl;
cout << st2 << endl;
cout << st3 << endl;
cout << st4 << endl;
cout << st5 << endl;
cout << st6 << endl;
cout << st7 << endl;
cout << st8 << endl;
cout << st9 << endl;
}

第6题

complx.h

#ifndef COMPLX0_H_
#define COMPLX0_H_
#include <iostream>
class complx
{
private:
double R;
double V;
public:
complx();
complx(double R,double V);
complx operator+(const complx & st);
complx operator-(const complx & st);
complx operator*(const complx & st);
friend complx operator~(const complx & st);
friend complx operator*(int x,const complx & st);
friend std::istream & operator>>(std:: istream & is,complx & st);
friend std::ostream & operator<<(std::ostream & os,const complx & st);
}; #endif

complx.cpp

#include <iostream>
#include "complx0.h" using namespace std; complx::complx()
{
R=V=0;
} complx::complx(double R,double V)
{
this->R=R;
this->V=V;
} complx complx::operator+(const complx & st)
{
return complx(R+st.R,V+st.V);
} complx complx::operator-(const complx & st)
{
return complx(R-st.R,V-st.V);
} complx complx::operator*(const complx & st)
{
return complx(R*st.R,V*st.V);
} complx operator~(const complx & st)
{
return complx(st.R,-st.V);
} complx operator*(int x,const complx & st)
{
return complx(x*st.R,x*st.V);
} std::istream & operator>>(std::istream & is,complx & st)
{
cout<<"real:"<<endl;
is>>st.R;
if(!is)
{
return is;
}
cout<<"V:"<<endl;
is>>st.V;
cin.get();
return is;
} std::ostream & operator<<(std::ostream & os,const complx &st)
{
os<<"R:"<<st.R<<"V:"<<st.V<<endl;
return os;
}

usercomplx.cpp

#include <iostream>
using namespace std;
#include "complx0.h" // to avoid confusion with complex.h
int main()
{
complx a(3.0, 4.0); // initialize to (3,4i)
complx c;
cout << "Enter a complex number (q to quit):\n";
while (cin >> c)
{
cout << "c is " << c << '\n';
cout << "complex conjugate is " << ~c << '\n';
cout << "a is " << a <<'\n';
cout << "a + c is " << a + c << '\n';
cout << "a - c is " << a - c << '\n';
cout << "a * c is " << a * c << '\n';
cout << "2 * c is " << 2 * c << '\n';
cout << "Enter a complex number (q to quit):\n";
}
cout << "Done!\n";
return 0;
}

使用类的习题(c++ prime plus)的更多相关文章

  1. Java类的继承与多态特性-入门笔记

    相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...

  2. Java 9 揭秘(5. 实现服务)

    Tips做一个终身学习的人. Implementing Services 在这章中,主要介绍如下内容: 什么服务,服务接口,服务提供者: 在 JDK 9之前和在JDK 9中如何实现服务 如何使用Jav ...

  3. 树形动态规划(树状DP)小结

    树状动态规划定义 之所以这样命名树规,是因为树形DP的这一特殊性:没有环,dfs是不会重复,而且具有明显而又严格的层数关系.利用这一特性,我们可以很清晰地根据题目写出一个在树(型结构)上的记忆化搜索的 ...

  4. YTU 2443: C++习题 复数类--重载运算符3+

    2443: C++习题 复数类--重载运算符3+ 时间限制: 1 Sec  内存限制: 128 MB 提交: 1368  解决: 733 题目描述 请编写程序,处理一个复数与一个double数相加的运 ...

  5. YTU 2441: C++习题 复数类--重载运算符2+

    2441: C++习题 复数类--重载运算符2+ 时间限制: 1 Sec  内存限制: 128 MB 提交: 847  解决: 618 题目描述 定义一个复数类Complex,重载运算符"+ ...

  6. YTU 2440: C++习题 复数类--重载运算符+,-,*,/

    2440: C++习题 复数类--重载运算符+,-,*,/ 时间限制: 1 Sec  内存限制: 128 MB 提交: 1189  解决: 774 题目描述 定义一个复数类Complex,重载运算符& ...

  7. YTU 2439: C++习题 复数类--重载运算符+

    2439: C++习题 复数类--重载运算符+ 时间限制: 1 Sec  内存限制: 128 MB 提交: 1022  解决: 669 题目描述 定义一个复数类Complex,重载运算符"+ ...

  8. YTU 2437: C++ 习题 比较大小-类模板

    2437: C++ 习题 比较大小-类模板 时间限制: 1 Sec  内存限制: 128 MB 提交: 1144  解决: 805 题目描述 声明一个类模板,利用它分别实现两个整数.浮点数和字符的比较 ...

  9. YTU 2436: C++ 习题 输出日期时间--友元类

    2436: C++ 习题 输出日期时间--友元类 时间限制: 1 Sec  内存限制: 128 MB 提交: 1243  解决: 690 题目描述 设计一个日期类和时间类,编写display函数用于显 ...

  10. 《C++primer》v5 第7章 类 读书笔记 习题答案

    7.1.7.2.7.3 #include<iostream> #include<cstdio> #include<vector> #include<strin ...

随机推荐

  1. (0403)位运算符+interface

    1)interface 2)位运算符

  2. eval()

    s='12*2'd=eval(s)#字符串运算函数print(d) 结果: 24

  3. MySQL 学习(四)并集查询

    联合查询,它是用 union 关键字把多条 select 语句的查询结果合并为一个结果集.纵向合并的前提是被合并的结果集的字段数量.顺序和数据类型必须完全一致.字段名不一样的情况下,会将第一个结果集的 ...

  4. 监听异常:The listener supports no services

    数据库版本:单机环境19c 实例是正常的 [oracle@sit19c admin]$ sqlplus / as sysdba SQL*Plus: Release 19.0.0.0.0 - Produ ...

  5. go语言的结构体、指针、方法详解

    资源来自:https://blog.csdn.net/DXB2021/article/details/122652779 结体体定义如下: type author struct{ field1 typ ...

  6. Idea报错:Command line is too long.

    https://blog.csdn.net/qq_40682764/article/details/109215368 run–>edit configurations–>你的项目–> ...

  7. 安装MogHA

    # 一.关于MogHAMogHA 是云和恩墨基于 MogDB 同步异步流复制技术自研的一款保障数据库主备集群高可用的企业级软件系统 (适用于 MogDB 和 openGauss 数据库) MogHA ...

  8. while跟if循环

    While(表达式)//循环,达成括号里的条件,一直循环执行里面的程序直到得出的值不成立后退出循环 { 循环体} 几个练习题: using System; namespace while练习2 { c ...

  9. pip download 参数与使用

    --no-clean Don't clean up build directories. 不要清理构建目录. -c, --constraint <file> Constrain versi ...

  10. 暑假学习四 8.26 Hadoop的简单使用

    先说今日情况,本来打算返校结果回不去,然后身体差发烧了,就随便学了一些东西,阑尾炎还是好疼. 有一说一,我觉得我的电脑真的有点拉啊,三台虚拟机开启之后,运存直接90%,呜呜响,和我本人情况一样,麻了 ...