Browse Source

新增了部分题目

Pchen. 5 months ago
parent
commit
c2d569985a

+ 71 - 0
code/几何图形的继承和派生.cpp

@@ -0,0 +1,71 @@
+#include <iostream>
+#include <iomanip>
+#include <cmath>
+
+// 基类 Shape
+class Shape {
+public:
+    // 虚函数 GetArea,用于计算面积
+    virtual double GetArea() const = 0;
+    // 虚析构函数
+    virtual ~Shape() {}
+};
+
+// 派生类 Rectangle
+class Rectangle : public Shape {
+protected:
+    double length;
+    double width;
+
+public:
+    Rectangle(double l, double w) : length(l), width(w) {}
+    // 实现 GetArea 函数
+    double GetArea() const override {
+        return length * width;
+    }
+};
+
+// 派生类 Circle
+class Circle : public Shape {
+private:
+    double radius;
+
+public:
+    Circle(double r) : radius(r) {}
+    // 实现 GetArea 函数
+    double GetArea() const override {
+        return M_PI * radius * radius;
+    }
+};
+
+// 派生类 Square,继承自 Rectangle
+class Square : public Rectangle {
+public:
+    Square(double side) : Rectangle(side, side) {}
+};
+
+int main() {
+    // 动态生成半径为5的圆对象
+    Shape* circle = new Circle(5);
+    std::cout << std::fixed << std::setprecision(1);
+    std::cout << "The area of the Cirele is:" << circle->GetArea() << std::endl;
+
+    // 恢复默认格式
+    std::cout.unsetf(std::ios::fixed);
+    std::cout.precision(6);
+
+    // 动态生成长为4,宽为6的矩形对象
+    Shape* rectangle = new Rectangle(4, 6);
+    std::cout << "The area of the Recanale is:" << rectangle->GetArea() << std::endl;
+
+    // 动态生成边为5的正方形对象
+    Shape* square = new Square(5);
+    std::cout << "The area of the Recanale is:" << square->GetArea() << std::endl;
+
+    // 释放动态分配的内存
+    delete circle;
+    delete rectangle;
+    delete square;
+
+    return 0;
+}

+ 90 - 0
code/定义一个人民币的类(重载+、-、+=、++、输入输出流).cpp

@@ -0,0 +1,90 @@
+#include<iostream>
+#include<iomanip>
+using namespace std;
+
+//人民币类的定义与实现
+class RMB {
+private:
+    int yuan, jiao, fen;
+
+public:
+    RMB(int yuan = 0, int jiao = 0, int fen = 0) : yuan(yuan), jiao(jiao), fen(fen) {}
+
+    friend istream& operator>>(istream& in, RMB& r) {
+        cout << "请分别输入元、角、分(整数之间用空格或回车间隔):" << endl;
+        in >> r.yuan >> r.jiao >> r.fen;
+        return in;
+    }
+
+    friend ostream& operator<<(ostream& out, RMB& r) {
+    	int sum = 100 * yuan + 10 * jiao + fen;
+        out << "¥ " << fixed << setprecision(2) << sum << endl;
+        return out;
+    }
+
+    RMB operator+(const RMB& r) const {
+        RMB result;
+        result.fen = fen + r.fen;
+        result.jiao = jiao + r.jiao + result.fen / 100;
+        result.fen %= 100;
+        result.yuan = yuan + r.yuan + result.jiao / 10;
+        result.jiao %= 10;
+        return result;
+    }
+
+    RMB& operator+=(const RMB& r) {
+        fen += r.fen;
+        jiao += r.jiao + fen / 100;
+        fen %= 100;
+        yuan += r.yuan + jiao / 10;
+        jiao %= 10;
+        return *this;
+    }
+
+    RMB operator-(const RMB& r) const {
+        RMB result;
+        int totalFen = (yuan * 10 + jiao) * 10 + fen;
+        int rTotalFen = (r.yuan * 10 + r.jiao) * 10 + r.fen;
+        totalFen -= rTotalFen;
+        if (totalFen < 0)
+            totalFen = 0;
+        result.fen = totalFen % 10;
+        totalFen /= 10;
+        result.jiao = totalFen % 10;
+        totalFen /= 10;
+        result.yuan = totalFen;
+        return result;
+    }
+
+    RMB& operator++() {
+        fen++;
+        if (fen >= 100) {
+            fen -= 100;
+            jiao++;
+        }
+        if (jiao >= 10) {
+            jiao -= 10;
+            yuan++;
+        }
+        return *this;
+    }
+};
+
+int main()
+{
+    RMB r1(14,  5  ,7),r2,r3;    //定义三个人民币的对象,  其中两个调用缺省的构造,  构造为0
+    cout<<"请输入一个人民币数值:  \n";
+    cin>>r2;    //调用重载的输入流输入对象的数值
+    r3=r1+r2;    //调用重载的加法运算符
+    cout<<r1<<"  +  "<<r2<<"  =  "<<r3<<endl;
+    cout<<r3<<"  +  "<<r1<<"  =  ";  
+    r3+=r1;  //调用重载运算符
+    cout<<r3<<endl;
+    cout<<r3<<"  -  "<<r1<<"  =  ";
+    r3=r3-r1;  //调用重载运算符
+    cout<<r3<<endl;
+    cout<<r3<<"  +  "<<"¥    0.01  =  ";
+    ++r3;  //调用重载运算符
+    cout<<r3<<endl;
+    return  0;
+}

+ 62 - 0
code/建立一个数组类ARR删除数组中的重复元素.cpp

@@ -0,0 +1,62 @@
+#include <iostream>
+#include <iomanip>
+
+using namespace std;
+
+class ARR {
+private:
+    int n; // 数组实际元素个数
+    int a[100]; // 存放原始数组及结果数组
+
+public:
+    // 构造函数,用size初始化n,用x初始化a数组
+    ARR(int x[], int size) : n(size) {
+        for (int i = 0; i < size; ++i) {
+            a[i] = x[i];
+        }
+    }
+
+    // 完成数组a中相同元素的删除工作
+    void delsame() {
+        if (n == 0) return;
+
+        int j = 0; // 新数组的索引
+        for (int i = 1; i < n; ++i) {
+            if (a[i] != a[j]) {
+                a[++j] = a[i];
+            }
+        }
+        n = j + 1; // 更新数组的实际元素个数
+    }
+
+    // 将结果数组以每行10个数的形式输出到屏幕上
+    void show() const {
+        for (int i = 0; i < n; ++i) {
+            cout << setw(5) << a[i];
+            if ((i + 1) % 10 == 0) {
+                cout << endl;
+            }
+        }
+        if (n % 10 != 0) {
+            cout << endl;
+        }
+    }
+};
+
+int main() {
+    int b[] = {1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 8, 9, 10, 10}; // 定义数组
+    int n = sizeof(b) / sizeof(int); // 计算b数组的元素长度
+    ARR v(b, n); // 定义类的对象并用数组b和数据n初始化对象的成员
+
+    cout << "删除前:";
+    v.show(); // 输出对象的数组成员中的数据(还未做删除操作, 输出的是初始化后的数据)
+    cout << endl;
+
+    v.delsame(); // 对对象进行操作, 即将对象的数组成员中的相同的元素删除
+
+    cout << "删除后:";
+    v.show(); // 删除操作后输出对象的数组成员中的数据
+    cout << endl;
+
+    return 0;
+}

+ 79 - 0
code/矩阵类(运算符+-及输入输出重载).cpp

@@ -0,0 +1,79 @@
+#include <iostream>
+#include <iomanip>
+
+class Matrix {
+private:
+    int data[2][3];
+
+public:
+    // 默认构造函数,初始化矩阵为0
+    Matrix() {
+        for (int i = 0; i < 2; ++i)
+            for (int j = 0; j < 3; ++j)
+                data[i][j] = 0;
+    }
+
+    // 重载输入运算符
+    friend std::istream& operator>>(std::istream& in, Matrix& matrix) {
+        for (int i = 0; i < 2; ++i) {
+            for (int j = 0; j < 3; ++j) {
+                in >> matrix.data[i][j];
+            }
+        }
+        return in;
+    }
+
+    // 重载输出运算符
+    friend std::ostream& operator<<(std::ostream& out, const Matrix& matrix) {
+        for (int i = 0; i < 2; ++i) {
+            for (int j = 0; j < 3; ++j) {
+                out << std::setw(7) << matrix.data[i][j]; // 用这个什么setw设置宽度
+            }
+            out << std::endl;
+        }
+        return out;
+    }
+
+    // 重载加法运算符
+    Matrix operator+(const Matrix& other) const {
+        Matrix result;
+        for (int i = 0; i < 2; ++i) {
+            for (int j = 0; j < 3; ++j) {
+                result.data[i][j] = this->data[i][j] + other.data[i][j];
+            }
+        }
+        return result;
+    }
+
+    // 重载减法运算符
+    Matrix operator-(const Matrix& other) const {
+        Matrix result;
+        for (int i = 0; i < 2; ++i) {
+            for (int j = 0; j < 3; ++j) {
+                result.data[i][j] = this->data[i][j] - other.data[i][j];
+            }
+        }
+        return result;
+    }
+};
+
+int main() {
+    Matrix m1, m2;
+
+    std::cout << "请输入2行3列矩阵内容:" << std::endl;
+    std::cin >> m1;
+
+    std::cout << "请输入2行3列矩阵内容:" << std::endl;
+    std::cin >> m2;
+
+    Matrix sum = m1 + m2;
+    Matrix diff = m1 - m2;
+
+    std::cout << "m1+m2=" << std::endl;
+    std::cout << sum;
+
+    std::cout << "m1-m2=" << std::endl;
+    std::cout << diff;
+
+    return 0;
+}

+ 74 - 0
code/车基类派生出自行车类和汽车类再从自行车类和汽车类派生摩托车(多态).cpp

@@ -0,0 +1,74 @@
+#include <iostream>
+using namespace std;
+
+class Vehicle {
+public:
+    void Run() {
+        cout << "Vehicle is running" << endl;
+    }
+
+    void Stop() {
+        cout << "Vehicle stops" << endl;
+    }
+};
+
+class Bicycle : public Vehicle {
+public:
+    void Run() {
+        cout << "Bicycle is running" << endl;
+    }
+
+    void Stop() {
+        cout << "Bicycle stops" << endl;
+    }
+};
+
+class Motorcar : public Vehicle {
+public:
+    void Run() {
+        cout << "Motorcar is running" << endl;
+    }
+
+    void Stop() {
+        cout << "Motorcar stops" << endl;
+    }
+};
+
+class Motorcycle : public Bicycle, public Motorcar {
+public:
+    void Run() {
+        cout << "Motorcycle is running" << endl;
+    }
+
+    void Stop() {
+        cout << "Motorcycle stops" << endl;
+    }
+};
+
+int main() {
+    	Vehicle  v;
+        v.Run();
+        v.Stop();
+        Bicycle  b;
+        b.Run();
+        b.Stop();
+        Motorcar  m;
+        m.Run();
+        m.Stop();
+        Motorcycle  mc;
+        mc.Run();
+        mc.Stop();
+        Vehicle*  vp  =  &v;
+        vp->Run();
+        vp->Stop();
+        vp  =  &b;
+        vp->Run();
+        vp->Stop();
+        vp  =  &m;
+        vp->Run();
+        vp->Stop();
+        vp  =  &mc;
+        vp->Run();
+        vp->Stop();
+        return  0;
+}

BIN
课件/第十五章 输入输出流.ppt