DEV Community

海前 王
海前 王

Posted on

enmu

#include <iostream>
#include <string>

#ifdef _WIN32 // 在 Windows 平台下的实现

#include <afxwin.h>
class time_watch
{
public:
    time_watch();
    ~time_watch();

public:
    void start();
    void stop();
    void restart();
    double elapsed_us();
    double elapsed_ms();
    double elapsed_s();
    CString elapsed_time_string();

private:
    LARGE_INTEGER freq_;
    LARGE_INTEGER begin_time_;
    long long elapsed_;
};

time_watch::time_watch()
{
    elapsed_ = 0;
    QueryPerformanceFrequency(&freq_);
}

time_watch::~time_watch()
{

}

void time_watch::start()
{
    QueryPerformanceCounter(&begin_time_);
}

void time_watch::stop()
{
    LARGE_INTEGER end_time;
    QueryPerformanceCounter(&end_time);
    elapsed_ = (end_time.QuadPart - begin_time_.QuadPart) * 1000000 / freq_.QuadPart;
}

void time_watch::restart()
{
    elapsed_ = 0;
    start();
}

CString time_watch::elapsed_time_string()
{
    int hours = static_cast<int>(elapsed_ / 3600000000);
    int minutes = static_cast<int>((elapsed_ % 3600000000) / 60000000);
    int seconds = static_cast<int>((elapsed_ % 60000000) / 1000000);

    CString elapsedTime;
    elapsedTime.Format(_T("%dh %dm %ds"), hours, minutes, seconds);
    return elapsedTime;
}

#else // 在其他平台下的实现

// TODO: 在其他平台上实现计时功能

class time_watch
{
public:
    time_watch() {}
    ~time_watch() {}

    void start() {}
    void stop() {}
    void restart() {}
    double elapsed_us() { return 0.0; }
    double elapsed_ms() { return 0.0; }
    double elapsed_s() { return 0.0; }
    string elapsed_time_string() { return _T(""); }
};

#endif

int main()
{
    time_watch timer;
    timer.start();

    // 模拟一些耗时操作
    for (UINT64 i = 0; i < 10000000000; ++i)
    {
    }

    timer.stop();



    CString elapsedTime = timer.elapsed_time_string();
    std::wcout << "Elapsed time: " << static_cast<const wchar_t*>(elapsedTime) << std::endl;

    getchar();
    return 0;
}

#include <cstdlib>
#include <iostream>

class Singleton
{
public:
    static Singleton* getInstance()
    {
        if (instance == nullptr)
        {
            instance = new Singleton();
            return instance;
        }
        else
            return  instance;
    }
    static void print_instance()
    {
        std::cout << name << " " << age << std::endl;
    }
    ~Singleton() {};
private:
    Singleton() {};
    static Singleton* instance;
    static int age;
    static std::string name;
};

int  Singleton::age = 20;
std::string Singleton::name = "lisan";
Singleton* Singleton::instance = nullptr;

class A
{

public:
    void print_a()
    {
        Singleton::getInstance()->print_instance();
    }

};
int main()
{
    A a;
    a.print_a();
    system("pause");
    return 0;
}

#include <windows.h>

#include <iostream>

int main()
{
    SYSTEM_INFO sysInfo;

    GetSystemInfo(&sysInfo);

    std::cout << "机器属性:" << std::endl;

    std::cout << "页大小=" << sysInfo.dwPageSize << std::endl;

    std::cout << "分配粒度=" << sysInfo.dwAllocationGranularity << std::endl;

    std::cout << "用户区最小值=" << sysInfo.lpMinimumApplicationAddress << std::endl;

    std::cout << "用户区最大值="

        << sysInfo.lpMaximumApplicationAddress << std::endl << std::endl;
}
#include <iostream>
#include <thread>
#include <vector>
#include <atomic>

bool is_solution(const std::vector<int>& data)
{
    int n = data.size();
    int sum = 0;
    for (int i = 0; i < n; ++i)
    {
        sum += data[i];
    }
    return sum == n * (n + 1) / 2;
}

void search(std::vector<int> data, std::vector<bool>& used, int level,
            std::vector<std::vector<int>>& results, std::atomic<bool>& found)
{
    if (found) return;
    if (level == data.size() && is_solution(data))
    {
        found = true;
        results.push_back(data);
        return;
    }
    for (int i = 0; i < data.size(); ++i)
    {
        if (!used[i])
        {
            data[level] = i + 1;
            used[i] = true;
            if (found) return;
            std::thread t(search, data, std::ref(used), level + 1, std::ref(results), std::ref(found));
            t.join();
            used[i] = false;
        }
    }
}

std::vector<std::vector<int>> parallel_search(int n, int num_threads)
{
    std::atomic<bool> found = false;
    std::vector<std::vector<int>> results;
    std::vector<int> data(n);
    std::vector<bool> used(n, false);
    std::vector<std::thread> threads(num_threads);
    for (int i = 0; i < num_threads; ++i)
    {
        threads[i] = std::thread(search, data, std::ref(used), 0, std::ref(results), std::ref(found));
    }
    for (int i = 0; i < num_threads; ++i)
    {
        threads[i].join();
    }
    return results;
}

int main()
{
    std::vector<std::vector<int>> results = parallel_search(6, 4);
    std::cout << "Results found: " << results.size() << std::endl;
    for (auto& r : results)
    {
        std::cout << "Solution: ";
        for (auto& e : r)
        {
            std::cout << e << " ";
        }
        std::cout << std::endl;
    }
    return 0;
}
#include <iostream>

template<typename T, typename U>
T my_cast(U value) {
    return static_cast<T>(value);
}

int main() {
    int num = 10;
    double convertedNum = my_cast<double>(num);

    std::cout << "Converted number: " << convertedNum << std::endl;

    return 0;
}

//#include <iostream>
//#include <fstream>
//
//int main() {
//    // Parameters
//    const int num_sets = 10;      // Number of sets to generate
//    const int grid_size = 100;    // Fixed grid size
//    const int time_steps = 50;    // Fixed time steps
//    const double isolevel = 0.5;  // Fixed isolevel
//
//    // Open file for writing
//    std::ofstream outfile("data.txt");
//    if (!outfile) {
//        std::cerr << "Error: Could not open the file for writing!" << std::endl;
//        return 1;
//    }
//
//    // Write data for each set
//    for (int i = 0; i < num_sets; ++i) {
//        outfile /*<< "Set " << i + 1 << ": "*/
//            << "grid_size=" << grid_size << ", "
//            << "time_steps=" << time_steps << ", "
//            << "isolevel=" << isolevel << std::endl;
//    }
//
//    // Close the file
//    outfile.close();
//
//    std::cout << "Data has been written to data.txt" << std::endl;
//
//    return 0;
//}
#include <iostream>
#include <cstdlib> // For atoi() and atof() functions

struct Parameters {
    int grid_size;
    int time_steps;
    double isolevel;
};

int main(int argc, char* argv[]) {
    // Check if the number of arguments is correct
    if (argc != 4) {
        std::cerr << "Usage: " << argv[0] << " <grid_size> <time_steps> <isolevel>" << std::endl;
        return 1;
    }

    // Parse command line arguments
    Parameters params;
    params.grid_size = atoi(argv[1]);
    params.time_steps = atoi(argv[2]);
    params.isolevel = atof(argv[3]);

    // Output the parameters read
    std::cout << "grid_size=" << params.grid_size
        << ", time_steps=" << params.time_steps
        << ", isolevel=" << params.isolevel << std::endl;

    return 0;
}
#include <windows.h>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <ctime>

// 生成 UUID
std::string generateUUID() {
    auto now = std::chrono::system_clock::now();
    auto now_time_t = std::chrono::system_clock::to_time_t(now);
    auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;

    std::tm bt{};
    localtime_s(&bt, &now_time_t); // 使用更安全的 localtime_s 函数

    std::ostringstream oss;
    oss << std::put_time(&bt, "%Y%m%d%H%M%S")
        << std::setw(3) << std::setfill('0') << now_ms.count();
    return oss.str();
}

// 创建目录
void createDirectory(const std::string& folderPath) {
    // 将 std::string 转换为 std::wstring
    std::wstring wideFolderPath(folderPath.begin(), folderPath.end());

    // 尝试创建目录
    if (CreateDirectory(wideFolderPath.c_str(), NULL) || GetLastError() == ERROR_ALREADY_EXISTS) {
        std::cout << "Directory created or already exists: " << folderPath << std::endl;
    }
    else {
        std::cerr << "Failed to create directory. Error: " << GetLastError() << std::endl;
    }
}

int main() {
    // 生成 UUID 作为目录名
    std::string uuid = generateUUID();
    std::string folderPath = "E:\\" + uuid;
    createDirectory(folderPath);

    return 0;
}

#include <iostream>
#include <fstream>
#include <string>

int main() {
    std::ifstream inputFile("E:\\param.txt");
    std::ofstream outputFile("E:\\param_test.txt");

    if (!inputFile.is_open()) {
        std::cerr << "Failed to open input file!" << std::endl;
        return 1;
    }

    if (!outputFile.is_open()) {
        std::cerr << "Failed to open output file!" << std::endl;
        return 1;
    }

    std::string line;
    int lineNumber = 1;

    while (std::getline(inputFile, line)) {
        if (!line.empty()) {
            outputFile << lineNumber << " " << line << std::endl;
            ++lineNumber;
        }
    }

    inputFile.close();
    outputFile.close();

    return 0;
}
#include <iostream>
#include <thread>
#include <chrono>
#include <windows.h>

struct EnumData {
    const wchar_t* targetTitle;
    bool foundWindow;
};

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
    EnumData* data = reinterpret_cast<EnumData*>(lParam);
    wchar_t windowTitle[256];

    // Get the window title
    GetWindowText(hwnd, windowTitle, sizeof(windowTitle) / sizeof(wchar_t));

    // Check if the window title matches the target title
    if (wcscmp(windowTitle, data->targetTitle) == 0) {
        // Bring the window to the foreground
        SetForegroundWindow(hwnd);

        // Send space key
        PostMessage(hwnd, WM_KEYDOWN, VK_SPACE, 0);
        Sleep(5);
        PostMessage(hwnd, WM_KEYUP, VK_SPACE, 0);

        data->foundWindow = true;
    }

    return TRUE; // Continue enumeration
}

// Function to send space key to all matching windows until all are closed
void sendSpaceKeyToAllWindows(const wchar_t* windowTitle) {
    while (true) {
        EnumData data;
        data.targetTitle = windowTitle;
        data.foundWindow = false;

        // Enumerate all top-level windows
        EnumWindows(EnumWindowsProc, reinterpret_cast<LPARAM>(&data));

        if (!data.foundWindow) {
            // No more windows found, exit loop
            break;
        }

        // Sleep for a short time before the next enumeration
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
}

int main() {
    const wchar_t* targetTitle = L"libigl viewer";

    std::wcout << L"Sending space key to all windows with title: " << targetTitle << std::endl;
    sendSpaceKeyToAllWindows(targetTitle);

    return 0;
}

#include <iostream>
#include <cmath>
#include <random>
#include <array>

// 常量 PI
const double PI = 3.14159265358979323846;

// 简单的 BRDF 函数,假设为常数
double brdf(const std::array<double, 3>& v, const std::array<double, 3>& l) {
    return 1.0;
}

// 简单的入射光函数,假设为常数
double Li(const std::array<double, 3>& l) {
    return 1.0;
}

// 计算入射角余弦
double cos_theta(const std::array<double, 3>& l) {
    // 假设表面法线为 (0, 0, 1)
    return l[2];
}

// 生成随机方向向量
std::array<double, 3> random_direction() {
    static std::mt19937 gen(std::random_device{}());
    static std::uniform_real_distribution<> dist(-1.0, 1.0);

    std::array<double, 3> l;
    do {
        l = { dist(gen), dist(gen), dist(gen) };
    } while (l[0] * l[0] + l[1] * l[1] + l[2] * l[2] >= 1.0 || l[2] <= 0.0);

    double norm = std::sqrt(l[0] * l[0] + l[1] * l[1] + l[2] * l[2]);
    l[0] /= norm;
    l[1] /= norm;
    l[2] /= norm;

    return l;
}

// 计算出射光 Lo
double integrate_radiance(const std::array<double, 3>& v, int num_samples = 1000) {
    double total_radiance = 0.0;

    for (int i = 0; i < num_samples; ++i) {
        auto l = random_direction();
        total_radiance += brdf(v, l) * Li(l) * cos_theta(l);
    }

    // 归一化
    total_radiance /= num_samples;
    return total_radiance;
}

int main() {
    // 出射方向 v,假设为 (0, 0, 1)
    std::array<double, 3> v = { 0.0, 0.0, 1.0 };

    // 计算出射光
    double Lo = integrate_radiance(v);
    std::cout << "出射光 Lo: " << Lo << std::endl;

    return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include<math.h>
#include<Windows.h>
#include <iostream>
#include <cmath>
#include<stdio.h> //X86
#include<GL/glut.h>
#define PI 3.1415926
void Init() {
    //glClearColor(0.3f, 0.3f, 0.3f, 0.0f);  //灰色
    glClearColor(1.0f, 0.97f, 0.86f, 0.0f);
}
void Reshape(int w, int h) {
    //
        glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glOrtho(-w / 2, w / 2, -h / 2, h / 2, -800, 800);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
void XY() {
    glLineWidth(1);
    //glColor3f(0.0, 0.0, 0.0);//坐标轴
    glColor3f(0.0, .0, 0.0);
    glBegin(GL_LINES);
    glVertex2i(-800, 0);
    glVertex2i(800, 0);

    glVertex2i(0, -600);
    glVertex2i(0, 600);
    glEnd();
    glFlush();
}
void XYZ() {
    gluLookAt(1, 1, 1, 0, 0, 0, 0, 1, 0);
    glLineWidth(1);
    //glColor3f(0.0, 0.0, 0.0);//坐标轴
    glColor3f(1.0, 0.97, 0.86);
    glBegin(GL_LINES);
    glVertex3i(-1000, 0, 0);
    glVertex3i(1000, 0, 0);
    glVertex3i(0, -1000, 0);
    glVertex3i(0, 1000, 0);
    glVertex3i(0, 0, -1000);
    glVertex3i(0, 0, 1000);
    glEnd();
    glFlush();
}

//int x[12] = { 0, 0, 2, 2, 4, 4, 4, 4, 2, 2, 0, 0 };
//int y[12] = { 0,-1, 0,-1, 0,-1, 0,-1 ,0,-1, 0,-1 };
//int z[16] = { 0, 0, 0, 0,  2, 2, 3, 3, 5, 5, 5, 5 };
//int ID[51] =   { 0,1,2,3,4,5,0,6,7,8,9,10,11,6,0,6,1,7,2,8,3,9,4,10,5,11};
//int flog[51] = { 0,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,0,1,0,1,0,1,0,1, 0, 1};

int x[12] = { 0, 2, 4, 4, 2,  0, 0, 2, 4, 4, 2, 0 };
int y[12] = { 0, 0, 0, 0, 0,  0,-1,-1,-1,-1,-1,-1 };
int z[12] = { 0, 0, 2, 3, 5,  5, 0, 0, 2, 3, 5, 5 };
int ID[26] = { 0,1,2,3,4,5,0,6,7,8,9,10,11,6,0,6,1,7,2,8,3,9,4,10,5,11 };
int flog[26] = { 0,1,1,1,1,1,1,0,1,1,1, 1,1, 1,0,1,0,1,0,1,0,1,0,1, 0, 1 };

/*int x[10] = { 0, 100, 100, 40, 0, 0, 100, 100, 40, 0 };
int y[10] = { 80, 80, 80, 80, 80, 0, 0,0, 0, 0 };
int z[10] = { 0, 0, 40, 40, 60, 0, 0, 40, 40, 60 };
int ID[22] = { 0, 1, 2, 3, 4, 0, 5, 6, 7, 8, 9, 5, 0, 5, 1, 6, 2, 7, 3, 8, 4, 9 };
int flog[22] = {0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1};*/

void Frontview(int tx, int ty) {
    int j, k;
    glBegin(GL_LINES);

    for (int i = 0; i < 51; i++) {
        j = ID[i];
        k = ID[i + 1];
        if (flog[i + 1] == 1) {
            glVertex3i(-x[j] * 50 - tx, z[j] * 50 + ty, 0);
            glVertex3i(-x[k] * 50 - ty, z[k] * 50 + ty, 0);
        }
    }
    glEnd();
    glFlush();

}
void Leftview(int tx, int ty) {
    int j, k;
    glBegin(GL_LINES);

    for (int i = 0; i < 51; i++) {
        j = ID[i];
        k = ID[i + 1];
        if (flog[i + 1] == 1) {
            glVertex3i(y[j] * 50 + tx, z[j] * 50 + ty, 0);
            glVertex3i(y[k] * 50 + ty, z[k] * 50 + ty, 0);
        }
    }
    glEnd();
    glFlush();

}
void Toptview(int tx, int ty) {
    int j, k;
    glBegin(GL_LINES);

    for (int i = 0; i < 51; i++) {
        j = ID[i];
        k = ID[i + 1];
        if (flog[i + 1] == 1) {
            glVertex3i(-x[j] * 50 - tx, -y[j] * 50 - ty, 0);
            glVertex3i(-x[k] * 50 - ty, -y[k] * 50 - ty, 0);
        }
    }

    glEnd();
    glFlush();
}

void allview(int tx, int ty) {
    int j, k;
    glColor3f(1.0, 0.0, 0.0);
    gluLookAt(1, 1, 1, 0, 0, 0, 0, 1, 0);
    glTranslatef(tx, ty, 0);
    glBegin(GL_LINES);
    for (int i = 0; i < 51; i++) {
        j = ID[i];
        k = ID[i + 1];
        if (flog[i + 1] == 1) {
            glVertex3i(x[j] * 50, y[j] * 50, z[j] * 50);
            glVertex3i(x[k] * 50, y[k] * 50, z[k] * 50);

        }
    }

    glEnd();
    glFlush();
}
void myDisplay() {
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    //int j, k;
    //glBegin(GL_LINES);
    //for (int i = 0; i < 51; i++) {
    //  j = ID[i];
    //  k = ID[i + 1];
    //  if (flog[i + 1] == 1) {
    //      /*glVertex3i(x[j], y[j], z[j]);
    //      glVertex3i(x[k], y[k], z[k]);*/
    //      glVertex3i(x[j]*50, y[j]*50, z[j]*50);
    //      glVertex3i(x[k]*50, y[k]*50, z[k]*50);

    //  }
    //}
    //glEnd();
    //glFlush();
    Sleep(100);
    allview(300, -100);
    glLoadIdentity();

    XY();
    glLineWidth(2);
    glColor3f(1.0, 0.0, 0.0);

    Frontview(100, 100);
    Leftview(100, 100);
    Toptview(100, 100);


}
int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(800, 800);
    glutCreateWindow("the forth plus menu");

    Init();
    glutReshapeFunc(Reshape);
    glutDisplayFunc(myDisplay);
    //glutMouseFunc(mouse_hit);
    glutMainLoop();
    return 0;
}
// ConsoleApplication12.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include <iostream>
#include <atlstr.h>

using namespace std;

//by zhaocl
int main()
{
    CString strSouce = "2221", strLoad;

    if (strSouce.SpanIncluding(_T("0123456789"))==strSouce)
    {
        cout << "all number." << endl;
    }
    else
    {
        cout << "not all number." << endl;
    }

    return 0;
}


Enter fullscreen mode Exit fullscreen mode

Top comments (0)