在VC状态下如何编程求2至1000之间的完数?


打开VC++6.0,点击工具栏上的“新建”图标建立一个空白文档(注意不是点击菜单File―New),把以下代码复制进去:

#include
#include
void main()
{
int m,i,s=0;
printf("2~1000的数中:\n");
for(m=2;m<=1000;m++)
{
for (i=1;i {
if(m%i==0)
s=s+i;
}
if(s==m)
{
printf("%d为一衡配个完数,因子为",m);
for(i=1;i {
if(m%i==0)
printf("%d,",i);
}
printf("\n");
}
s=0;
}
}
点击“编译”,弹出的对话框选择“是”(将为你自动建立一个工程),咐厅指将文件名改为“Text1.c”,选择伏巧一个工程的存放位置,点击“保存”。然后编译运行即可。

不知道你还有没有特殊要求,比如想要基于MFC而不仅仅是基于VC环境?
你把下边这个程序改改就是你要的
//hookdll.cpp

#define _WIN32_WINNT 0x0500

#include
#include
#include "hookdll.h"

#pragma data_seg("SHARED")
HHOOK g_hHook = NULL;
HINSTANCE g_hInstance = NULL;
HWND g_hWnd = NULL;
const int WND_WIDTH = 100;
const int WND_HEIGHT = 100;
const int WND_CAP_X = 0;
const int WND_CAP_Y = 0;
#pragma data_seg()

#pragma comment(linker, "/section:SHARED,rws")

LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam);

int WINAPI DllMain (HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved)
{
// 保虚滑存模块实例句柄
g_hInstance = hInstance;

// 在进程结束或线程结束悉塌时卸载钩子
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_PROCESS_DETACH:
case DLL_THREAD_DETACH:
if (g_hHook != NULL)
UnhookWindowsHookEx(g_hHook);
break;
}
return TRUE;
}

//导出函数
DLLEXPORT BOOL CALLBACK BeginHook(HWND hWnd)
{
g_hWnd = hWnd;
if(NULL == g_hWnd)
{
MessageBox(NULL, "窗口句柄为空!", "hook", MB_OK);
return FALSE;
}

g_hHook = SetWindowsHookEx(WH_MOUSE_LL, LowLevelMouseProc, g_hInstance, 0);
if(NULL == g_hHook)
{
TCHAR str[256];
wsprintf(str, "创建失败err_code: %d", GetLastError());
MessageBox(NULL, str, NULL, MB_OK);
return FALSE;
}

return TRUE;
}

//导睁誉圆出函数
DLLEXPORT BOOL CALLBACK EndHook()
{
BOOL ret = FALSE;

if(g_hHook != NULL)
ret = UnhookWindowsHookEx(g_hHook);

g_hHook = NULL;

return ret;
}

LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if(nCode >= HC_ACTION)
{
if(NULL == g_hWnd)
{
goto NEXT;
}

if((wParam == WM_MOUSEMOVE) || (wParam == WM_NCMOUSEMOVE))
{
MOUSEHOOKSTRUCT mhs;

mhs = *((MOUSEHOOKSTRUCT*)lParam);

MoveWindow(g_hWnd, mhs.pt.x + WND_CAP_X, mhs.pt.y + WND_CAP_Y, WND_WIDTH, WND_HEIGHT, TRUE);
SetWindowPos(g_hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
}
}

NEXT:
return CallNextHookEx(g_hHook, nCode, wParam, lParam);
}

//hookproc.h

#ifdef __cplusplus
#define DLLEXPORT extern "C" __declspec (dllexport)
#else
#define DLLEXPORT __declspec (dllexport)
#endif

DLLEXPORT BOOL CALLBACK BeginHook(HWND hWnd);
DLLEXPORT BOOL CALLBACK EndHook();

#define WM_GHOOK_MOUSEPOS (WM_USER + 13)
在VC状态下如何编程求2至1000之间的完数?
← 上一篇: 下一篇: →