使用类的习题(c++ prime plus)
第一题
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; }