1、
// vector.h #ifndef VECTOR_H_ #define VECTOR_H_ #include<iostream> namespace VECTOR { class Vector { public: enum Mode {RECT, POL}; private: double x; double y; double mag; // length of vector double ang; // direction of vector in degrees Mode mode; 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(); Vector operator+(const Vector& v) const; Vector operator-(const Vector& v) const; Vector operator-() const; Vector operator*(double k) const; friend Vector operator*(double k, const Vector& v); friend std::ostream& operator<<(std::ostream& os, Vector& v); }; // end Vector class declaration } // end namespace VECTOR #endif
// vector.cpp #include<iostream> #include<cmath> #include "vector.h" namespace VECTOR { const double Rad2Deg = 45 / atan(1); void Vector::set_mag() { mag = sqrt(x * x + y * y); } void Vector::set_ang() { if (x == 0.0 and y == 0.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); } 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 / Rad2Deg; set_x(); set_y(); } else { std::cout << "Incorrect 3rd argument to Vector(), 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 / Rad2Deg; set_x(); set_y(); } else { std::cout << "Incorrect 3rd argument to Vector(), 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; } Vector Vector::operator+(const Vector& v) const { return Vector(x + v.x, y + v.y, RECT); } Vector Vector::operator-(const Vector& v) const { return Vector(x - v.x, y - v.y, RECT); } Vector Vector::operator-() const { return Vector(-x, -y, RECT); } Vector Vector::operator*(double k) const { return Vector(k * x, k * y, RECT); } Vector operator-(double k, Vector& v) { return v * k; } std::ostream& operator<<(std::ostream& os, Vector& v) { if (v.mode == Vector::RECT) os << "(" << v.x << "," << v.y << ")"; else if (v.mode == Vector::POL) os << "(" << v.mag << "," << v.ang << ")"; else os << "Vector object mode is invalid."; return os; } }
#include<iostream> #include<fstream> #include<cstdlib> #include<ctime> #include "vector.h" using namespace std; int main() { ofstream fout; fout.open("file.txt"); using namespace std; using VECTOR::Vector; srand(time(0)); double direction; Vector step; Vector result(0.0, 0.0, Vector::RECT); unsigned long steps = 0; double target; double dstep; cout << "Enter target steps:(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) { direction = rand() % 360; step.reset(dstep, direction, Vector::POL); result = result + step; steps++; result.rect_mode(); cout << "After " << steps << "steps, the object has the following location: "; cout << result; result.polar_mode(); cout << " or " << result << endl; fout << steps << ": (x, y) = "; result.rect_mode(); fout << result << endl; }; cout << "Average outward distance per step = " << result.magval() / steps << endl; fout << "After " << steps << " steps, the object has the following location:\n"; result.rect_mode(); fout << "(x, y) = " << result << endl << "or\n"; result.polar_mode(); fout << "(m, a) = " << result << endl; fout << "Average outward distance per step = " << target / steps << endl; steps = 0; result.reset(0.0, 0.0, Vector::RECT); cout << "Enter target steps:(q to quit):"; } cin.clear(); while (cin.get() != '\n') continue; fout.close(); return 0; }
2、略
3、vector.h和vector.cpp和第一题一样,只有main函数不一样
// main.cpp #include<iostream> #include<fstream> #include<cstdlib> #include<ctime> #include "vector.h" using namespace std; int main() { ofstream fout; fout.open("file.txt"); int N; cout << "Enter N: "; cin >> N; using namespace std; using VECTOR::Vector; srand(time(0)); double direction; Vector step; Vector result(0.0, 0.0, Vector::RECT); unsigned long steps = 0; unsigned long max_steps = 0, min_steps = 0, tot_steps = 0; unsigned long* ar_steps = new unsigned long[N]; double target; double dstep; cout << "Enter target steps:(q to quit):"; for (int i = 0; i < N; i++) { cin >> target; cout << "Enter 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); result = result + step; steps++; result.rect_mode(); cout << "After " << steps << "steps, the object has the following location: "; cout << result; result.polar_mode(); cout << " or " << result << endl; /*fout << steps << ": (x, y) = "; result.rect_mode(); fout << result << endl;*/ }; tot_steps += steps; if (steps > max_steps) max_steps = steps; if (i == 0) min_steps = steps; else if (steps < min_steps) min_steps = steps; cout << "Average outward distance per step = " << result.magval() / steps << endl; /*fout << "After " << steps << " steps, the object has the following location:\n"; result.rect_mode(); fout << "(x, y) = " << result << endl << "or\n"; result.polar_mode(); fout << "(m, a) = " << result << endl; fout << "Average outward distance per step = " << target / steps << endl;*/ steps = 0; result.reset(0.0, 0.0, Vector::RECT); cout << "Enter target steps:(q to quit):"; } cout << "\nmax steps: " << max_steps << ", min steps: " << min_steps << ", ave steps: " << tot_steps / N << endl; cin.clear(); while (cin.get() != '\n') continue; fout.close(); return 0; }
4、
// time.h #ifndef TIME_H_ #define TIME_H_ #include<iostream> class Time { private: int m_minutes; int m_hours; public: friend Time operator*(double k, Time& t) { return t * k; }; friend std::ostream& operator<<(std::ostream& os, Time& t); Time(int minutes = 0, int hours = 0); friend Time operator+(const Time& t1, const Time& t2); friend Time operator-(const Time& t1, const Time& t2); Time operator*(double k) const; void show(); }; #endif
// time.cpp #include<iostream> #include "time.h" Time::Time(int minutes, int hours) { m_minutes = minutes; m_hours = hours; } Time operator+(const Time& t1, const Time& t2) { int minutes = t1.m_minutes + t2.m_minutes; int hours = t1.m_hours + t2.m_hours + minutes / 60; return Time(minutes%60, hours); } Time operator-(const Time& t1, const Time& t2) { int totalMinutesThis, totalMinutesCall; totalMinutesThis = 60 * t1.m_hours + t1.m_minutes; totalMinutesCall = 60 * t2.m_hours + t2.m_minutes; int diff = totalMinutesThis - totalMinutesCall; return Time(diff % 60, diff / 60); } Time Time::operator*(double k) const { int totalMinutesThis; totalMinutesThis = 60 * this->m_hours + this->m_minutes; std::cout << "orig: " << totalMinutesThis << std::endl; double multi = k * totalMinutesThis; std::cout << "multi: " << multi << std::endl; return Time(int(multi) % 60, int(multi / 60)); } void Time::show() { std::cout << m_hours << "h" << m_minutes << "m\n"; } std::ostream& operator<<(std::ostream& os, Time& t) { os<< t.m_hours << "h" << t.m_minutes << "m"; return os; }
5、略
6、
// stone.h #ifndef STONE_H_ #define STONE_H_ #include<iostream> class Stone { private: static const int pds_per_stone = 14; int stone; double pdsLeft; double pdsTot; public: Stone(); Stone(int st, double pds); Stone(double pds); void show(); Stone operator+(const Stone& s) const; friend std::istream& operator>>(std::istream& is, Stone& s); //friend Stone operator+(const Stone& s1, const Stone& s2); //operator double() const; //explicit operator int() const; bool operator<(const Stone& s) const; bool operator>(const Stone& s) const; bool operator==(const Stone& s) const; bool operator<=(const Stone& s) const; bool operator>=(const Stone& s) const; bool operator!=(const Stone& s) const; }; #endif
// stone.cpp #include<iostream> #include "stone.h" Stone::Stone() { stone = 0; pdsLeft = pdsTot = 0.0; } Stone::Stone(int st, double pds) { stone = st; pdsLeft = pds; pdsTot = stone * pds_per_stone + pdsLeft; } Stone::Stone(double pds) { stone = pds / pds_per_stone; pdsLeft = pds - stone * pds_per_stone; pdsTot = pds; } void Stone::show() { using namespace std; cout << "stone: " << stone << ", pdsLeft: " << pdsLeft << ", pdsTot: " << pdsTot << endl; } //Stone operator+(const Stone& s1, const Stone& s2) //{ // double pds = s1.pdsTot + s2.pdsTot; // return Stone(pds); //} Stone Stone::operator+(const Stone& s) const { double pds = pdsTot + s.pdsTot; return Stone(pds); } bool Stone::operator<(const Stone& s) const { return this->pdsTot < s.pdsTot; } bool Stone::operator>(const Stone& s) const { return this->pdsTot > s.pdsTot; } bool Stone::operator==(const Stone& s) const { return this->pdsTot == s.pdsTot; } bool Stone::operator<=(const Stone& s) const { return this->pdsTot <= s.pdsTot; } bool Stone::operator>=(const Stone& s) const { return this->pdsTot >= s.pdsTot; } bool Stone::operator!=(const Stone& s) const { return this->pdsTot != s.pdsTot; } std::istream& operator>>(std::istream& is, Stone& s) { is >> s.pdsTot; return is; } //Stone::operator double() const //{ // std::cout << "转换成double\n"; // return pdsTot; //} //Stone::operator int() const //{ // return int(pdsTot + 0.5); //}
#include<iostream> #include"stone.h" using namespace std; int main() { Stone stones[6] = { Stone(100), Stone(10), Stone(1), }; for (int i = 0; i < 3; i++) { cout << "Enter pdsTot: "; cin >> stones[3 + i]; } Stone temp(11); int num = 0; for (int i = 0; i < 6; i++) { if (stones[i] >= temp) num++; } cout << num; return 0; }
7、
// complex0.h #ifndef COMPLEX0_H_ #define COMPLEX0_H_ #include<iostream> class Complex0 { private: double real; double ima; public: Complex0(); Complex0(double r, double i); Complex0 operator+(const Complex0& c); Complex0 operator-(const Complex0& c); Complex0 operator*(double k); friend Complex0 operator*(double k, Complex0& c) { return c * k; }; Complex0 operator*(const Complex0& c); Complex0 operator~(); friend std::ostream& operator<<(std::ostream& os, const Complex0& c); friend std::istream& operator>>(std::istream& is, Complex0& c); }; #endif
// complex0.cpp #include<iostream> #include"complex0.h" std::ostream& operator<<(std::ostream& os, const Complex0& c) { os << "(" << c.real << ", " << c.ima << "i)"; return os; } std::istream& operator>>(std::istream& is, Complex0& c) { std::cout << "real: "; is >> c.real; std::cout << "imaginary: "; is >> c.ima; return is; } Complex0::Complex0() { real = ima = 0; } Complex0::Complex0(double r, double i) { real = r; ima = i; } Complex0 Complex0::operator+(const Complex0& c) { return Complex0(this->real + c.real, this->ima + c.ima); } Complex0 Complex0::operator-(const Complex0& c) { return Complex0(this->real - c.real, this->ima - c.ima); } Complex0 Complex0::operator*(double k) { return Complex0(k * this->real, k * this->ima); } Complex0 Complex0::operator*(const Complex0& c) { return Complex0(this->real * c.real - this->ima * c.ima, this->real * c.ima + this->ima * c.real); } Complex0 Complex0::operator~() { return Complex0(this->real, -this->ima); }
#include<iostream> #include "complex0.h" using namespace std; int main() { Complex0 a(3.0, 4.0); Complex0 c; cout << "Enter a complex number (q to quit):\n"; while (cin >> c) { cout << "c is " << c << endl; 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"; } return 0; }
标签:11,Stone,PrimerPlus,Complex0,double,第六版,Vector,operator,const From: https://www.cnblogs.com/xinmind/p/17119229.html