设为首页收藏本站

新微赢技术网

 找回密码
 注册
搜索
热搜: 回贴
查看: 790|回复: 7
打印 上一主题 下一主题

C++的计算器怎么做

[复制链接]
跳转到指定楼层
1#
发表于 2009-11-4 00:49:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
C++的计算器怎么做,那位大哥帮帮忙啊!
2#
发表于 2009-11-4 00:49:19 | 只看该作者
windwows api 写的....如果用c++写呢, 数据结构上都有描述
HEXCALC.C

/*------------------------------------------------------------------------

HEXCALC.C -- Hexadecimal Calculator

(c) Charles Petzold, 1998

-------------------------------------------------------------------------*/

#include <windows.h>

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,

PSTR szCmdLine, int iCmdShow)

{

static TCHAR szAppName[] = TEXT ("HexCalc") ;

HWND hwnd ;

MSG msg ;

WNDCLASS wndclass ;



wndclass.style = CS_HREDRAW | CS_VREDRAW;

wndclass.lpfnWndProc = WndProc ;

wndclass.cbClsExtra = 0 ;

wndclass.cbWndExtra = DLGWINDOWEXTRA ; // Note!

wndclass.hInstance = hInstance ;

wndclass.hIcon = LoadIcon (hInstance, szAppName) ;

wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;

wndclass.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1) ;

wndclass.lpszMenuName = NULL ;

wndclass.lpszClassName = szAppName ;



if (!RegisterClass (&wndclass))

{

MessageBox ( NULL, TEXT ("This program requires Windows NT!"),

szAppName, MB_ICONERROR) ;

return 0 ;

}



hwnd = CreateDialog (hInstance, szAppName, 0, NULL) ;

ShowWindow (hwnd, iCmdShow) ;

while (GetMessage (&msg, NULL, 0, 0))

{

TranslateMessage (&msg) ;

DispatchMessage (&msg) ;

}

return msg.wParam ;

}

void ShowNumber (HWND hwnd, UINT iNumber)

{

TCHAR szBuffer[20] ;

wsprintf (szBuffer, TEXT ("%X"), iNumber) ;

SetDlgItemText (hwnd, VK_ESCAPE, szBuffer) ;

}

DWORD CalcIt (UINT iFirstNum, int iOperation, UINT iNum)

{

switch (iOperation)

{

case '=': return iNum ;

case '+': return iFirstNum + iNum ;

case '-': return iFirstNum - iNum ;

case '*': return iFirstNum * iNum ;

case '&': return iFirstNum & iNum ;

case '|': return iFirstNum | iNum ;

case '^': return iFirstNum ^ iNum ;

case '<': return iFirstNum << iNum ;

case '>': return iFirstNum >> iNum ;

case '/': return iNum ? iFirstNum / iNum: MAXDWORD ;

case '%': return iNum ? iFirstNum % iNum: MAXDWORD ;

default : return 0 ;

}

}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)

{

static BOOL bNewNumber = TRUE ;

static int iOperation = '=' ;

static UINT iNumber, iFirstNum ;

HWND hButton ;



switch (message)

{

case WM_KEYDOWN: // left arrow --> backspace

if (wParam != VK_LEFT)

break ;

wParam = VK_BACK ;

// fall through

case WM_CHAR:

if ((wParam = (WPARAM) CharUpper ((TCHAR *) wParam)) == VK_RETURN)

wParam = '=' ;



if (hButton = GetDlgItem (hwnd, wParam))

{

SendMessage (hButton, BM_SETSTATE, 1, 0) ;

Sleep (100) ;

SendMessage (hButton, BM_SETSTATE, 0, 0) ;

}

else

{

MessageBeep (0) ;

break ;

}

// fall through

case WM_COMMAND:

SetFocus (hwnd) ;



if (LOWORD (wParam) == VK_BACK) //backspace

ShowNumber (hwnd, iNumber /= 16) ;



else if (LOWORD (wParam) == VK_ESCAPE) // escape

ShowNumber (hwnd, iNumber = 0) ;



else if (isxdigit (LOWORD (wParam))) // hex digit

{

if (bNewNumber)

{

iFirstNum = iNumber ;

iNumber = 0 ;

}

bNewNumber = FALSE ;

if (iNumber <= MAXDWORD >> 4)

ShowNumber (hwnd, iNumber = 16 * iNumber + wParam -

(isdigit (wParam) ? '0': 'A' - 10)) ;

else

MessageBeep (0) ;

}

else // operation

{

if (!bNewNumber)

ShowNumber (hwnd, iNumber =

CalcIt (iFirstNum, iOperation, iNumber)) ;

bNewNumber = TRUE ;

iOperation = LOWORD (wParam) ;

}

return 0 ;

case WM_DESTROY:

PostQuitMessage (0) ;

return 0 ;

}

return DefWindowProc (hwnd, message, wParam, lParam) ;

}

HEXCALC.RC (摘录)

//Microsoft Developer Studio generated resource script.

#include "resource.h"

#include "afxres.h"

/////////////////////////////////////////////////////////////////////////////

// Icon

HEXCALC ICON DISCARDABLE "HexCalc.ico"

/////////////////////////////////////////////////////////////////////////////

#include "hexcalc.dlg"

HEXCALC.DLG

/*--------------------------------

HEXCALC.DLG dialog script

----------------------------------*/

HexCalc DIALOG -1, -1, 102, 122

STYLE WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX

CLASS "HexCalc"

CAPTION "Hex Calculator"

{

PUSHBUTTON "D", 68, 8, 24, 14, 14

PUSHBUTTON "A", 65, 8, 40, 14, 14

PUSHBUTTON "7", 55, 8, 56, 14, 14

PUSHBUTTON "4", 52, 8, 72, 14, 14

PUSHBUTTON "1", 49, 8, 88, 14, 14

PUSHBUTTON "0", 48, 8, 104,14, 14

PUSHBUTTON "0", 27, 26, 4, 50, 14

PUSHBUTTON "E", 69, 26, 24, 14, 14

PUSHBUTTON "B", 66, 26, 40, 14, 14

PUSHBUTTON "8", 56, 26, 56, 14, 14

PUSHBUTTON "5", 53, 26, 72, 14, 14

PUSHBUTTON "2", 50, 26, 88, 14, 14

PUSHBUTTON "Back", 8, 26, 104,32, 14

PUSHBUTTON "C", 67, 44, 40, 14, 14

PUSHBUTTON "F", 70, 44, 24, 14, 14

PUSHBUTTON "9", 57, 44, 56, 14, 14

PUSHBUTTON "6", 54, 44, 72, 14, 14

PUSHBUTTON "3", 51, 44, 88, 14, 14

PUSHBUTTON "+", 43, 62, 24, 14, 14

PUSHBUTTON "-", 45, 62, 40, 14, 14

PUSHBUTTON "*", 42, 62, 56, 14, 14

PUSHBUTTON "/", 47, 62, 72, 14, 14

PUSHBUTTON "%", 37, 62, 88, 14, 14

PUSHBUTTON "Equals", 61, 62, 104,32, 14

PUSHBUTTON "&&",38, 80, 24, 14, 14

PUSHBUTTON "|", 124, 80, 40, 14, 14

PUSHBUTTON "^", 94, 80, 56, 14, 14

PUSHBUTTON "<", 60, 80, 72, 14, 14

PUSHBUTTON ">", 62, 80, 88, 14, 14

}
回复 支持 反对

使用道具 举报

3#
发表于 2009-11-4 00:49:20 | 只看该作者
呼呼,大哥,
厉害^_^
回复 支持 反对

使用道具 举报

4#
发表于 2009-11-4 00:49:21 | 只看该作者
请教下这段代码放在什么编译器才能执行,为什么我放在VC有错误呢
回复 支持 反对

使用道具 举报

5#
发表于 2009-11-4 00:49:22 | 只看该作者
这个只是 书上 copy下来的...
在windows 程序设计的 配套光盘中有
回复 支持 反对

使用道具 举报

6#
发表于 2009-11-4 00:49:23 | 只看该作者
take a look there:
http://blog.bc-cn.net/user1/121/archives/2006/882.shtml
回复 支持 反对

使用道具 举报

7#
发表于 2009-11-4 00:49:24 | 只看该作者
好长/。。。。。
回复 支持 反对

使用道具 举报

8#
发表于 2009-11-4 00:49:25 | 只看该作者
谢谢各位啊!但我急需一个C++编的!!!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

申请友链|小黑屋|最新主题|手机版|新微赢技术网 ( 苏ICP备08020429号 )  

GMT+8, 2024-11-18 19:32 , Processed in 0.153642 second(s), 9 queries , Gzip On, Memcache On.

Powered by xuexi

© 2001-2013 HaiAn.Com.Cn Inc. 寰耽

快速回复 返回顶部 返回列表