diff --git "a/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter10/10.5/main" "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter10/10.5/main" new file mode 100755 index 0000000000000000000000000000000000000000..453aea2dca68c8f74e126c7488a71a96c8042e57 Binary files /dev/null and "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter10/10.5/main" differ diff --git "a/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter10/10.5/main.cpp" "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter10/10.5/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..4b2db05a179ad88c68cb2b23fb52f0dedbec34dd --- /dev/null +++ "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter10/10.5/main.cpp" @@ -0,0 +1,43 @@ +#include +#include +#include "stack.hpp" + +typedef customer Item; + +int main() { + Stack stack; + Item cust; + double totalPayment = 0.0; + char input[35]; + + while (true) { + std::cout << "Enter fullname (or 'q' to quit): "; + std::cin.getline(input, 35); + + if (strcmp(input, "q") == 0) { + break; + } + + strcpy(cust.fullname, input); + + std::cout << "Enter payment: "; + std::cin >> cust.payment; + std::cin.ignore(); // 忽略cin保留的换行符 + + if (stack.push(cust)) { + std::cout << "Customer added to stack.\n"; + } else { + std::cout << "Failed to add customer.\n"; + } + } + + while (!stack.isempty()) { + stack.pop(cust); + totalPayment += cust.payment; + std::cout << "Removed customer: " << cust.fullname << ", Payment: " << cust.payment << "\n"; + } + + std::cout << "Total payment: " << totalPayment << std::endl; + + return 0; +} \ No newline at end of file diff --git "a/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter10/10.5/stack.hpp" "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter10/10.5/stack.hpp" new file mode 100644 index 0000000000000000000000000000000000000000..3b7e04b2076854a8076c335753a7831c359c2a60 --- /dev/null +++ "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter10/10.5/stack.hpp" @@ -0,0 +1,51 @@ + +struct customer { + char fullname[35]; + double payment; +}; + +typedef customer Item; + +class Stack { + +private: + enum { MAX = 10 }; + Item items[MAX]; + int top; + +public: + Stack(); + bool isempty() const; + bool isfull() const; + bool push(const Item & item); + bool pop(Item & item); +}; + +Stack::Stack() { + top = 0; +} + +bool Stack::isempty() const { + return top == 0; +} + +bool Stack::isfull() const { + return top == MAX; +} + +bool Stack::push(const Item & item) { + if (top < MAX) { + items[top++] = item; + return true; + } else + return false; +} + +bool Stack::pop(Item & item) { + if (top > 0) { + item = items[--top]; + return true; + } else + return false; +} + diff --git "a/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter10/10.6/Move.hpp" "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter10/10.6/Move.hpp" new file mode 100644 index 0000000000000000000000000000000000000000..5e2d4055988772b0eba04e8e3288762d4a0978a0 --- /dev/null +++ "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter10/10.6/Move.hpp" @@ -0,0 +1,46 @@ +#ifndef MOVE_HPP +#define MOVE_HPP + +#include + +class Move { +private: + double x; + double y; + +public: + + Move(double a = 0, double b = 0); + + + void showmove() const; + + + Move add(const Move &m) const; + + + void reset(double a = 0, double b = 0); +}; + + +Move::Move(double a, double b) : x(a), y(b) {} + +void Move::showmove() const { + + std::cout << "Current x: " << x << ", Current y: " << y << std::endl; +} + + +Move Move::add(const Move &m) const { + + return Move(x + m.x, y + m.y); +} + + +void Move::reset(double a, double b) { + + x = a; + y = b; +} + +#endif diff --git "a/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter10/10.6/main" "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter10/10.6/main" new file mode 100755 index 0000000000000000000000000000000000000000..643a3b55cdb27e00c219ff6fe775e2096750f95d Binary files /dev/null and "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter10/10.6/main" differ diff --git "a/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter10/10.6/main.cpp" "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter10/10.6/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..7ee44ca5f9a8ca6f2a5c38fdf4419508d83076da --- /dev/null +++ "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter10/10.6/main.cpp" @@ -0,0 +1,21 @@ +#include "Move.hpp" + +int main() { + + Move move1(3, 4); + move1.showmove(); + + + Move move2(2, 6); + move2.showmove(); + + + Move move3 = move1.add(move2); + move3.showmove(); + + + move1.reset(0, 0); + move1.showmove(); + + return 0; +} diff --git "a/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter11/11.3/main.cpp" "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter11/11.3/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..652279247103f85421a7253b5374cdec1a15e5c9 --- /dev/null +++ "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter11/11.3/main.cpp" @@ -0,0 +1,59 @@ +#include +#include "vector.h" +#include +#include +#include + +int main() { + using namespace std; + using VECTOR::Vector; + srand(time(0)); + + double direction; + Vector step; + Vector result(0.0, 0.0); + unsigned long steps; + double target; + double dstep; + int numTests; + + cout << "Enter the number of tests: "; + cin >> numTests; + + cout << "Enter target distance (q to quit): "; + while (cin >> target) { + cout << "Enter step length: "; + if (!(cin >> dstep)) + break; + + unsigned long maxSteps = 0; + unsigned long minSteps = ULONG_MAX; + unsigned long totalSteps = 0; + + for (int i = 0; i < numTests; ++i) { + result.reset(0.0, 0.0); + steps = 0; + while (result.magval() < target) { + direction = rand() % 360; + step.reset(dstep, direction, Vector::POL); + result = result + step; + steps++; + } + + if (steps > maxSteps) + maxSteps = steps; + if (steps < minSteps) + minSteps = steps; + totalSteps += steps; + } + + cout << "After " << numTests << " tests:\n"; + cout << "Maximum steps: " << maxSteps << endl; + cout << "Minimum steps: " << minSteps << endl; + cout << "Average steps: " << (double)totalSteps / numTests << endl; + + cout << "Enter target distance (q to quit): "; + } + cout << "Bye!\n"; + return 0; +} diff --git "a/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter11/11.3/vector.cpp" "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter11/11.3/vector.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..8b1e3dfbfacf277a15792834329c55f452c42bcc --- /dev/null +++ "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter11/11.3/vector.cpp" @@ -0,0 +1,130 @@ +// vector.cpp -- methods for the Vector class +#include +#include "vector.h" // includes + +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); + + void Vector ::set_mag() { + mag = sqrt(x * x + y * y); + + } + + void Vector::set_ang() { + if (x == 0.0 && y == 0.00) + ang = 0.0; + else + ang = atan2(y, x); + } + + // default constructor + Vector::Vector() { + x = y = mag =ang = 0.0; + mode = RECT; + } + + // construct vector from rectangular coordinates if form is RECT (the default) + // or else from polar coordinates if form is POL + 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) { + double mag = n1; + double ang = n2 / Rad_to_deg; + x = mag * cos(ang); + y = mag * sin(ang); + } else { + cout << "Incorrect 3rd argument to Vector() -- 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) { + double mag = n1; + double ang = n2 / Rad_to_deg; + x = mag * cos(ang); + y = mag * sin(ang); + } else { + cout << "Incorrect 3rd argument to Vector() -- vector set to 0\n"; + x = y = mag = ang =0.0; + mode = RECT; + } + } + + Vector::~Vector() { + } + + // set to polar mode + void Vector::polar_mode() { + mode = POL; + } + + // set to rectangular mode + void Vector::rect_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.magval() << ", " + << v.angval() * Rad_to_deg << ")"; + else + os << "Vector object mode is invalid"; + return os; + } + +} // namespace VECTOR diff --git "a/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter11/11.3/vector.h" "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter11/11.3/vector.h" new file mode 100644 index 0000000000000000000000000000000000000000..ce7513966456ac334d535eff444f1155097d89c9 --- /dev/null +++ "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter11/11.3/vector.h" @@ -0,0 +1,43 @@ +#ifndef VECTOR_H_ +#define VECTOR_H_ + +#include +#include + +namespace VECTOR { + class Vector { + public: + enum Mode { RECT, POL }; + private: + double x; // horizontal value + double y; // vertical value + double mag; + double ang; + Mode mode; // RECT or POL + // private methods for setting values + void set_mag(); + void set_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 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); + }; +} // namespace VECTOR + +#endif // VECTOR_H_ diff --git "a/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter11/11.4/main.cpp" "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter11/11.4/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..89b8abfcfa4c47e60ccd1332994e54d9aafc9845 --- /dev/null +++ "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter11/11.4/main.cpp" @@ -0,0 +1,21 @@ +#include +#include "mytime3.h" + +int main() { + Time t1(1, 30); + Time t2(5, 40); + + std::cout << "Time 1: " << t1 << std::endl; + std::cout << "Time 2: " << t2 << std::endl; + + Time t3 = t1 + t2; + std::cout << "Sum: " << t3 << std::endl; + + Time t4 = t1 - t2; + std::cout << "Difference: " << t4 << std::endl; + + Time t5 = t1 * 2.5; + std::cout << "Time 1 multiplied by 3: " << t5 << std::endl; + + return 0; +} diff --git "a/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter11/11.4/mytime3.cpp" "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter11/11.4/mytime3.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..5547210c0327b9a67cb1c51a2e2c7d0e415945e3 --- /dev/null +++ "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter11/11.4/mytime3.cpp" @@ -0,0 +1,85 @@ +#include "mytime3.h" + + +Time::Time() : hours(0), minutes(0) {} + + +Time::Time(int h, int m) : hours(h), minutes(m) { + while (minutes >= 60) { + hours++; + minutes -= 60; + } + while (minutes < 0) { + hours--; + minutes += 60; + } + if (hours < 0) hours = 0; +} + + +void Time::AddMin(int m) { + minutes += m; + while (minutes >= 60) { + hours++; + minutes -= 60; + } +} + + +void Time::AddHr(int h) { + hours += h; +} + + +void Time::Reset(int h, int m) { + hours = h; + minutes = m; + while (minutes >= 60) { + hours++; + minutes -= 60; + } + while (minutes < 0) { + hours--; + minutes += 60; + } + if (hours < 0) hours = 0; +} + + +Time operator+(const Time& t1, const Time& t2) { + Time sum; + sum.minutes = t1.minutes + t2.minutes; + sum.hours = t1.hours + t2.hours + sum.minutes / 60; + sum.minutes %= 60; + return sum; +} + + +Time operator-(const Time& t1, const Time& t2) { + Time diff; + int total1 = t1.minutes + 60 * t1.hours; + int total2 = t2.minutes + 60 * t2.hours; + diff.minutes = (total1 - total2) % 60; + diff.hours = (total1 - total2) / 60; + return diff; +} + + +Time operator*(double n, const Time& t) { + Time result; + long totalMinutes = t.hours * n * 60 + t.minutes * n; + result.hours = totalMinutes / 60; + result.minutes = totalMinutes % 60; + return result; +} + + +Time operator*(const Time& t, double n) { + return n * t; +} + + +std::ostream& operator<<(std::ostream& os, const Time& t) { + os << t.hours << " hours, " << t.minutes << " minutes"; + return os; +} diff --git "a/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter11/11.4/mytime3.h" "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter11/11.4/mytime3.h" new file mode 100644 index 0000000000000000000000000000000000000000..7d644f67a8f19ce29ab7def43662af8bcb072ff2 --- /dev/null +++ "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter11/11.4/mytime3.h" @@ -0,0 +1,26 @@ +#ifndef MYTIME3_H +#define MYTIME3_H + +#include + +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& t1, const Time& t2); + friend Time operator-(const Time& t1, const Time& t2); + friend Time operator*(double n, const Time& t); + friend Time operator*(const Time& t, double n); + friend std::ostream& operator<<(std::ostream& os, const Time& t); +}; + +#endif diff --git "a/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter9/9.3.cpp" "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter9/9.3.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..40963ad86ce2e3882596a8e05057f925b70d9877 --- /dev/null +++ "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter9/9.3.cpp" @@ -0,0 +1,31 @@ +#include +#include +#include + +using namespace std; + +struct chaff { + char dross[20]; + int slag; +}; + +int main() { + + char buffer[sizeof(chaff) * 2]; + chaff* chaffs = new (buffer) chaff[2]; + + + strcpy(chaffs[0].dross, "chaff1"); + chaffs[0].slag = 10; + strcpy(chaffs[1].dross, "chaff2"); + chaffs[1].slag = 15; + + + for (int i = 0; i < 2; i++) { + cout << "chaffs[" << i << "]:"; + cout << " dross: " << chaffs[i].dross; + cout << " slag: " << chaffs[i].slag << endl; + } + + return 0; +} diff --git "a/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter9/9.4/9.4" "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter9/9.4/9.4" new file mode 100755 index 0000000000000000000000000000000000000000..7fa2d70aaa53308856d2c00147ae7f4abf7b1493 Binary files /dev/null and "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter9/9.4/9.4" differ diff --git "a/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter9/9.4/main.cpp" "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter9/9.4/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..bc71208ba9b79805e55bc1560939b6b164e14ec7 --- /dev/null +++ "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter9/9.4/main.cpp" @@ -0,0 +1,23 @@ +// main.cpp +#include +#include "sales.h" + +using namespace SALES; + +int main(){ + Sales s1, s2; + + setSales(s1); + + const double salesData[QUARTERS] = {300.0, 500.0, 250.0, 300.0}; + setSales(s2, salesData, QUARTERS); + + std::cout << "Sales for s1:" << std::endl; + showSales(s1); + std::cout << std::endl; + + std::cout << "Sales for s2:" << std::endl; + showSales(s2); + + return 0; +} diff --git "a/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter9/9.4/sales.cpp" "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter9/9.4/sales.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..0f867240981bbb1aff90217c56f5a72b30e1398b --- /dev/null +++ "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter9/9.4/sales.cpp" @@ -0,0 +1,60 @@ +// sales.cpp +#include +#include "sales.h" + +namespace SALES{ + + using std::cin; + using std::cout; + using std::endl; + + void setSales(Sales &s, const double ar[], int n){ + + int a = n < QUARTERS ? n : QUARTERS; + double sum = 0.0; + s.max = s.min = ar[0]; + + for (int i = 0; i < a ; i++){ + s.sales[i] = ar[i]; + sum += ar[i]; + if (ar[i] > s.max) + s.max = ar[i]; + if (ar[i] < s.min) + s.min = ar[i]; + } + + for (int i = a; i < QUARTERS; i++){ + s.sales[i] = 0.0; + } + + s.average = sum / n; + } + + void setSales(Sales &s){ + + double sum = 0.0; + cout << "Please enter sales for 4 quarters:" << endl; + + for (int i = 0; i < QUARTERS; i++){ + cout << "Quarter " << (i + 1) << ": "; + cin >> s.sales[i]; + sum += s.sales[i]; + if (i == 0 || s.sales[i] > s.max) + s.max = s.sales[i]; + if (i == 0 || s.sales[i] < s.min) + s.min = s.sales[i]; + } + s.average = sum / QUARTERS; + } + + void showSales(const Sales &s){ + cout << "Sales: "; + for (int i = 0; i < QUARTERS; i++){ + cout << s.sales[i] << " "; + } + cout << endl; + cout << "Average: " << s.average << endl; + cout << "Max: " << s.max << endl; + cout << "Min: " << s.min << endl; + } +} diff --git "a/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter9/9.4/sales.h" "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter9/9.4/sales.h" new file mode 100644 index 0000000000000000000000000000000000000000..2cdf13d37787b76411817664b8b54a18c5fc4d56 --- /dev/null +++ "b/C++/codes/2024-07/\345\210\230\350\260\246/C++PrimerPlusExercises/Chapter9/9.4/sales.h" @@ -0,0 +1,22 @@ +namespace SALES +{ + const int QUARTERS = 4; + + struct Sales + { + double sales[QUARTERS]; + double average; + double max; + double min; + }; + + // 从数组 ar 中复制最多 4 项到 s 的 sales 成员中,并计算和存储输入项目的平均值、最大值和最小值; + // 如果 sales 中有剩余元素,则将其设置为 0 + void setSales(Sales &s, const double ar[], int n); + + // 交互式地收集 4 个季度的销售数据,存储在 s 的 sales 成员中,并计算和存储其平均值、最大值和最小值 + void setSales(Sales &s); + + // 显示结构 s 中的所有信息 + void showSales(const Sales &s); +}