使用WINDOWS任务管理器绘制正弦曲线

    xiaoxiao2022-06-29  51

    最近有点时间,想起了之前听说的一道微软面试题,就写了个这个小东西玩了玩,分享出来和大家同乐吧!水平有限,大牛勿笑。

    代码使用 vs2015 编译

    // stdafx.h : 标准系统包含文件的包含文件, // 或是经常使用但不常更改的 // 特定于项目的包含文件 // #pragma once #include "targetver.h" #include <windows.h> #include <stdio.h> #include <tchar.h> #include <math.h> // TODO: 在此处引用程序需要的其他头文件 #include <string> #include <iostream> #include <thread> #include <vector> #include <algorithm> #include <memory // ConsoleApplication1.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" void DrawPoint(DWORD dwHigh) { DWORD dwStartTick = GetTickCount(); while (GetTickCount() - dwHigh < dwStartTick) { } if (dwHigh < 1000) { Sleep(1000 - dwHigh); } } int main() { std::vector<std::shared_ptr<std::thread>> arrThread; Sleep(3 * 1000); const double PIE = 3.141592657; auto func = [&](int nProcessIndex) { SetThreadAffinityMask(GetCurrentThread(), 0x1 << nProcessIndex); double dStep = 0.0; while (true) { DWORD dwValue = (DWORD)(sin(PIE * dStep) * 1000); //std::cout << dwValue << std::endl; DrawPoint(dwValue); dStep += 0.03; // 调整这个参数值,修正曲线增长速度 dStep = dStep > 1 ? 0 : dStep; } }; // 根据 CPU 数量修改循环次数 for (int i = 0; i < 4; i++) { auto thread = std::make_shared<std::thread>(func, i); arrThread.push_back(thread); } std::for_each(arrThread.begin(), arrThread.end(), [](auto pThread) { pThread->join(); }); return 0; }

    运行效果

    如果曲线不够规整,可以调整任务管理器窗口大小,或者代码中 dStep 步进参数。

    转载请注明原文地址: https://ju.6miu.com/read-1124821.html

    最新回复(0)