disign

海前 王 - Aug 27 - - Dev Community
embed 

//#include <iostream>
//#include <vector>
//
//// 抽象基类
//class Shape
//{
//public:
//  // 纯虚函数,表示所有子类都必须实现这个函数
//  virtual void draw() const = 0;
//};
//
//// 子类:圆形
//class Circle : public Shape
//{
//public:
//  void draw() const override
//  {
//      std::cout << "Drawing Circle" << std::endl;
//  }
//};
//
//// 子类:矩形
//class Rectangle : public Shape
//{
//public:
//  void draw() const override
//  {
//      std::cout << "Drawing Rectangle" << std::endl;
//  }
//};
//
//// 子类:三角形
//class Triangle : public Shape
//{
//public:
//  void draw() const override
//  {
//      std::cout << "Drawing Triangle" << std::endl;
//  }
//};
//
//// 接口
//class ShapeContainer
//{
//public:
//  // 将 Shape 指针添加到容器中
//  void addShape(Shape* shape)
//  {
//      shapes.push_back(shape);
//  }
//
//  // 遍历容器并调用每个对象的 draw 函数
//  void drawAll() const
//  {
//      for (const auto& shape : shapes)
//      {
//          shape->draw();
//      }
//  }
//
//private:
//  std::vector<Shape*> shapes;
//};
//
//int main()
//{
//  // 创建一个 ShapeContainer 对象
//  ShapeContainer container;
//
//  // 创建 Circle、Rectangle 和 Triangle 对象并将它们添加到容器中
//  Circle circle;
//  Rectangle rectangle;
//  Triangle triangle;
//
//  container.addShape(&circle);
//  container.addShape(&rectangle);
//  container.addShape(&triangle);
//
//  // 遍历容器并调用每个对象的 draw 函数
//  container.drawAll();
//
//  return 0;
//}
#include <iostream>
#include <vector>

// 前向声明 ShapeContainer
class ShapeContainer;

// 抽象基类
class Shape
{
public:
    // 纯虚函数,表示所有子类都必须实现这个函数
    virtual void draw() const = 0;

    // 将自身添加到容器中
    virtual void addToContainer(ShapeContainer* container) = 0;
};

// 接口
class ShapeContainer
{
public:
    // 将 Shape 指针添加到容器中
    void addShape(Shape* shape)
    {
        shapes.push_back(shape);
    }

    // 遍历容器并调用每个对象的 draw 函数
    void drawAll() const
    {
        for (const auto& shape : shapes)
        {
            shape->draw();
        }
    }

private:
    std::vector<Shape*> shapes;
};

// 子类:圆形
class Circle : public Shape
{
public:
    Circle(ShapeContainer* container)
    {
        addToContainer(container);
    }

    void draw() const override
    {
        std::cout << "Drawing Circle" << std::endl;
    }

    // 实现将自身添加到容器中的函数
    void addToContainer(ShapeContainer* container) override
    {
        container->addShape(this);
    }
};

// 子类:矩形
class Rectangle : public Shape
{
public:
    Rectangle(ShapeContainer* container)
    {
        addToContainer(container);
    }

    void draw() const override
    {
        std::cout << "Drawing Rectangle" << std::endl;
    }

    // 实现将自身添加到容器中的函数
    void addToContainer(ShapeContainer* container) override
    {
        container->addShape(this);
    }
};

// 子类:三角形
class Triangle : public Shape
{
public:
    Triangle(ShapeContainer* container)
    {
        addToContainer(container);
    }

    void draw() const override
    {
        std::cout << "Drawing Triangle" << std::endl;
    }

    // 实现将自身添加到容器中的函数
    void addToContainer(ShapeContainer* container) override
    {
        container->addShape(this);
    }
};

int main()
{
    // 创建一个 ShapeContainer 对象

Enter fullscreen mode Exit fullscreen mode
    ShapeContainer container;

    // 创建 Circle、Rectangle 和 Triangle 对象并将它们添加到容器中
    Circle circle(&container);
    Rectangle rectangle(&container);
    Triangle triangle(&container);

    // 遍历容器并调用每个对象的 draw 函数
    container.drawAll();

    return 0;
}
// ConsoleApplication28.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <vector>
//#define row 10
//#define col 10
//const int row = 10;
//const int col = 10;
int add(int a,int b)
{
    return a + b;
}

int des(int a, int b)
{
    return a - b;
}
int mul(int a, int b)
{
    return a * b;
}
typedef  std::vector<void*> funx;


typedef int (*fun)(int, int);

typedef int (*fn[3])(int, int);
using fx = int (*)(int, int);


std::istream& sdd(std::istream& is,int i)
{
    int j = i;
    is >> i;
    std::cout << "hello";
    return is;
}

int main()
{
    int arr[10][10];
  //  int(&row)[10] = arr[1];
    for (auto& i : arr) {
        for (auto& j : i)
        {
            static int jk = 0;
            j = ++jk;
        }
    }
    //std::istream cin();
    //sdd(std::cin,10);
    fun f = add;
    std::cout<<f(2, 3);

    fn d = { add,des };

    using std::cout;
    std::cout<< " ---------\n "<<d[0](2, 3)<<" "<<d[1](2,3);

    fx g = add;
    g(2, 3);

    std::vector<int (*)(int, int)> funx;

    // 添加函数指针到vector
    funx.push_back(&add);
    funx.push_back(&des);
    funx.push_back(&mul);

    // 使用函数指针调用函数
    int result1 = funx[0](10, 5); // 调用 add(10, 5)
    int result2 = funx[1](10, 5); // 调用 des(10, 5)
    int result3 = funx[2](10, 5); // 调用 mul(10, 5)

    // 打印结果
    std::cout << "Result of add: " << result1 << std::endl;
    std::cout << "Result of des: " << result2 << std::endl;
    std::cout << "Result of mul: " << result3 << std::endl;
}

#include <valarray>
#include <iostream>

int main() {
    int ar[] = { 1, 3, 4, 5, 4, 6 };
    std::valarray<int> arr(ar, 6);

    // 循环移位3个位置
    arr = arr.shift(3);

    // 输出移位后的数组
    for (int i = 0; i < arr.size(); ++i) {
        std::cout << arr[i] << " ";
    }
    std::cout << std::endl;

    // 对每个元素应用某个函数
    arr.apply([](int x) { std::cout << ++x; return x * 2; });

    // 输出应用函数后的数组
    for (int i = 0; i < arr.size(); ++i) {
        std::cout << arr[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}
// ConsoleApplication29.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

//#include <iostream>
//#include<assert.h>
//
//int main()
//{
//  try
//  {
//      assert(0);
//  }
//  catch (...)
//  {
//  }
//  
//    //abort();
//    std::cout << "Hello World!\n";
//}
#include <array>
#include <cassert>
#include <csetjmp>
#include <csignal>
#include <iostream>
#include <list>
#include <valarray>
#include <vector>
std::jmp_buf env;

static void my_assert_handler(int a) {
    std::cout << "Assertion failed!" << std::endl;
    std::longjmp(env, 1);
}

static void do_work()
{
    assert(0);
}

int main() {

    std::setjmp(env);
    std::signal(SIGABRT, my_assert_handler);


    int a;
    int b;
    int c;

    do_work();

    for (int i = 9 - 1; i >= 0; --i)
    {


    }
    std::vector<int> ve{ 13,4,5,6 };
    for (int ve1 : ve)
    {

    }
    std::list<int> z{ 1, 2, 3, 4, 5, 6, 7 };

    for (int z1 : z)
    {

    }
    int ar[]{ 1,3,4,5,4,6 };
     std::valarray<int> arr(ar,10);
    // ReSharper disable once CppExpressionWithoutSideEffects
    arr.cshift(3);
    arr.apply()

    return 0;
}


// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件

Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player