Joshua
April 19th, 1999, 12:35 AM
Hey there people,
I've been looking at this program for quite a while now and I need some help. PLEASE?? If a bunch of people could look at this code which i provided you can help me figure out why it doesn't work. Everything on it works except for the brackets (which if you have any suggestuoions it would be nice), but mostly my problem lies within the math functions of this calculator. I can add numbers straight, or just mulitply or just subtract and have no problwm. BUT if I start to mix functions like add 2 and then subtract 5 and times 7 i come out with really large or messed up numbers. Here's all the code so if you cut and paste it you can see what i mean for yourself. I really could use some help, so if this is a place for helping people with code, i could really appreciate it. Thanks!
ccode
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define BUTTON01 101
#define BUTTON02 102
#define BUTTON03 103
#define BUTTON04 104
#define BUTTON05 105
#define BUTTON06 106
#define BUTTON07 107
#define BUTTON08 108
#define BUTTON09 109
#define BUTTON10 110
#define BUTTON11 111
#define BUTTON12 112
#define BUTTON13 113
#define BUTTON14 114
#define BUTTON15 115
#define BUTTON16 116
#define BUTTON17 117
#define BUTTON18 118
#define BUTTON19 119
#define BUTTON20 120
#define BUTTON21 121
#define BUTTON22 122
#define BUTTON23 123
#define BUTTON24 124
#define BUTTON25 125
#define BUTTON26 126
#define BUTTON27 127
#define BUTTON28 128
#define BUTTON29 129
#define BUTTON30 130
#define BUTTON31 131
#define BUTTON32 132
#define BUTTON33 133
#define BUTTON34 134
#define BUTTON35 135
#define BUTTON36 136
#define BUTTON37 137
#define BUTTON38 138
#define BUTTON39 139
typedef unsigned int UNIT;
HINSTANCE hInst;
int nCmd;
long FAR PASCAL WndProc( HWND hwnd, UNIT msg, UNIT wParam, LONG lParam );
int PASCAL WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpszCmdLineArgs,
int nCmdShow
)
{
char szAppName[]="Calculator";
HWND hwnd, button;
MSG msg;
WNDCLASS wc;
hInst = hInstance;
nCmd = nCmdShow;
if( !hPrevInstance )
{
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = GetStockObject( WHITE_BRUSH );
wc.lpszMenuName = NULL;
wc.lpszClassName = szAppName;
RegisterClass( &wc );
}
hwnd = CreateWindow( szAppName,
"Calculator", //Gives the calculator a name
WS_OVERLAPPEDWINDOW,
200, //This sets the size of the window
200,
340,
250,
NULL,
NULL,
hInstance,
NULL );
ShowWindow( hwnd, nCmdShow );
UpdateWindow( hwnd );
//The following 18 statements are the button creations.
//They give the size of the button, put the respectable number on top of each.
//button = CreateWindow ("button", " ", WS_CHILD | BS_PUSHBUTTON,
// 10, 60, 30, 30, hwnd, (HMENU) BUTTON20,
// hInstance, NULL);
//ShowWindow (button, nCmdShow);
//UpdateWindow(button);
button = CreateWindow ("button", "(", WS_CHILD | BS_PUSHBUTTON,
40, 60, 30, 30, hwnd, (HMENU) BUTTON21,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", ")", WS_CHILD | BS_PUSHBUTTON,
70, 60, 30, 30, hwnd, (HMENU) BUTTON22,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "MC", WS_CHILD | BS_PUSHBUTTON,
100, 60, 30, 30, hwnd, (HMENU) BUTTON23,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
//button = CreateWindow ("button", " ", WS_CHILD | BS_PUSHBUTTON,
// 10, 90, 30, 30, hwnd, (HMENU) BUTTON24,
// hInstance, NULL);
//ShowWindow (button, nCmdShow);
//UpdateWindow(button);
button = CreateWindow ("button", "Exp", WS_CHILD | BS_PUSHBUTTON,
40, 90, 30, 30, hwnd, (HMENU) BUTTON25,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "ln", WS_CHILD | BS_PUSHBUTTON,
70, 90, 30, 30, hwnd, (HMENU) BUTTON26,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "MR", WS_CHILD | BS_PUSHBUTTON,
100, 90, 30, 30, hwnd, (HMENU) BUTTON27,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "sin", WS_CHILD | BS_PUSHBUTTON,
10, 120, 30, 30, hwnd, (HMENU) BUTTON28,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "x^y", WS_CHILD | BS_PUSHBUTTON,
40, 120, 30, 30, hwnd, (HMENU) BUTTON29,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "log", WS_CHILD | BS_PUSHBUTTON,
70, 120, 30, 30, hwnd, (HMENU) BUTTON30,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "MS", WS_CHILD | BS_PUSHBUTTON,
100, 120, 30, 30, hwnd, (HMENU) BUTTON31,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "cos", WS_CHILD | BS_PUSHBUTTON,
10, 150, 30, 30, hwnd, (HMENU) BUTTON32,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "x^3", WS_CHILD | BS_PUSHBUTTON,
40, 150, 30, 30, hwnd, (HMENU) BUTTON33,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "n!", WS_CHILD | BS_PUSHBUTTON,
70, 150, 30, 30, hwnd, (HMENU) BUTTON34,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "M+", WS_CHILD | BS_PUSHBUTTON,
100, 150, 30, 30, hwnd, (HMENU) BUTTON35,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "tan", WS_CHILD | BS_PUSHBUTTON,
10, 180, 30, 30, hwnd, (HMENU) BUTTON36,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "x^2", WS_CHILD | BS_PUSHBUTTON,
40, 180, 30, 30, hwnd, (HMENU) BUTTON37,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "1/x", WS_CHILD | BS_PUSHBUTTON,
70, 180, 30, 30, hwnd, (HMENU) BUTTON38,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "PI", WS_CHILD | BS_PUSHBUTTON,
100, 180, 30, 30, hwnd, (HMENU) BUTTON39,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
// *********************************************************************
button = CreateWindow ("button", "1", WS_CHILD | BS_PUSHBUTTON,
160, 60, 30, 30, hwnd, (HMENU) BUTTON01,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "2", WS_CHILD | BS_PUSHBUTTON,
190, 60, 30, 30, hwnd, (HMENU) BUTTON02,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "3", WS_CHILD | BS_PUSHBUTTON,
220, 60, 30, 30, hwnd, (HMENU) BUTTON03,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "/", WS_CHILD | BS_PUSHBUTTON,
250, 60, 30, 30, hwnd, (HMENU) BUTTON04,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "4", WS_CHILD | BS_PUSHBUTTON,
160, 90, 30, 30, hwnd, (HMENU) BUTTON05,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "5", WS_CHILD | BS_PUSHBUTTON,
190, 90, 30, 30, hwnd, (HMENU) BUTTON06,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "6", WS_CHILD | BS_PUSHBUTTON,
220, 90, 30, 30, hwnd, (HMENU) BUTTON07,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "*", WS_CHILD | BS_PUSHBUTTON,
250, 90, 30, 30, hwnd, (HMENU) BUTTON08,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "7", WS_CHILD | BS_PUSHBUTTON,
160, 120, 30, 30, hwnd, (HMENU) BUTTON09,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "8", WS_CHILD | BS_PUSHBUTTON,
190, 120, 30, 30, hwnd, (HMENU) BUTTON10,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "9", WS_CHILD | BS_PUSHBUTTON,
220, 120, 30, 30, hwnd, (HMENU) BUTTON11,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "-", WS_CHILD | BS_PUSHBUTTON,
250, 120, 30, 30, hwnd, (HMENU) BUTTON12,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "+/-", WS_CHILD | BS_PUSHBUTTON,
160, 150, 30, 30, hwnd, (HMENU) BUTTON13,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "0", WS_CHILD | BS_PUSHBUTTON,
190, 150, 30, 30, hwnd, (HMENU) BUTTON14,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", ".", WS_CHILD | BS_PUSHBUTTON,
220, 150, 30, 30, hwnd, (HMENU) BUTTON15,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "+", WS_CHILD | BS_PUSHBUTTON,
250, 150, 30, 30, hwnd, (HMENU) BUTTON16,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "=", WS_CHILD | BS_PUSHBUTTON,
285, 60, 40, 90, hwnd, (HMENU) BUTTON17,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "Clear", WS_CHILD | BS_PUSHBUTTON,
285, 160, 40, 20, hwnd, (HMENU) BUTTON18,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
while( GetMessage( &msg, NULL, 0, 0) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return( msg.wParam );
}
//******************************************************************************
//* Name: FAR PASCAL
//******************************************************************************
//*
//* Description: This function takes in the windows functions, such as the buttons
//* and their values. It also calculates the users input as
//* requested using switch statments.
//* Passed In: hwnd, msg, wParam, lParam
//* Passed Out: hwnd, msg, wParam, lParam
//* Assumtions/Side Effects: none
//******************************************************************************
long FAR PASCAL WndProc( HWND hwnd, UNIT msg, UNIT wParam, LONG lParam )
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
static float num1, num2, subtotal, tempVal, temp_mem; //declares the varibles
static int count=1, i=0;
static HWND edit, button;
static char tempStr[100], op;
switch( msg )
{
case WM_CREATE: //this creates the text box
edit = CreateWindow("edit", " ", WS_CHILD|WS_BORDER,
120, 10, 200, 30,
hwnd, (HMENU)119, hInst, NULL);
ShowWindow (edit, SW_SHOW);
UpdateWindow(edit);
break;
case WM_PAINT: hdc = BeginPaint( hwnd, &ps );
GetClientRect( hwnd, &rect );
EndPaint( hwnd, &ps );
break;
case WM_DESTROY: PostQuitMessage( 0 );
break;
case WM_COMMAND: switch( wParam )
{
case 101: GetWindowText (edit, tempStr, 101);
strcat (tempStr, "1");
SetWindowText (edit, tempStr);
break;
case 102: GetWindowText (edit, tempStr, 102);
strcat (tempStr, "2");
SetWindowText (edit, tempStr);
break;
case 103: GetWindowText (edit, tempStr, 103);
strcat (tempStr, "3");
SetWindowText (edit, tempStr);
break;
case 105: GetWindowText (edit, tempStr, 105);
strcat (tempStr, "4");
SetWindowText (edit, tempStr);
break;
case 106: GetWindowText (edit, tempStr, 106);
strcat (tempStr, "5");
SetWindowText (edit, tempStr);
break;
case 107: GetWindowText (edit, tempStr, 107);
strcat (tempStr, "6");
SetWindowText (edit, tempStr);
break;
case 109: GetWindowText (edit, tempStr, 109);
strcat (tempStr, "7");
SetWindowText (edit, tempStr);
break;
case 110: GetWindowText (edit, tempStr, 110);
strcat (tempStr, "8");
SetWindowText (edit, tempStr);
break;
case 111: GetWindowText (edit, tempStr, 111);
strcat (tempStr, "9");
SetWindowText (edit, tempStr);
break;
case 114: GetWindowText (edit, tempStr, 114);
strcat (tempStr, "0");
SetWindowText (edit, tempStr);
break;
case 113: GetWindowText (edit, tempStr, 113);
/* +/- Button */ sscanf (tempStr, "%f", &tempVal);
tempVal = tempVal * -1;
sprintf (tempStr, "%.3f", tempVal);
SetWindowText (edit, tempStr);
break;
case 115: if (count <= 1)
{
/* Decimal Button */ GetWindowText (edit, tempStr, 115);
strcat (tempStr, ".");
SetWindowText (edit, tempStr);
count++;
}
break;
case 118: GetWindowText (edit, tempStr, 118);
SetWindowText (edit, " ");
/* Clear Button */ count = 1;
num1 = 0;
tempVal = 0;
subtotal = 0;
for(i=0; i <= 100; i++)
tempStr[i]=0;
temp_mem = 0;
op = NULL;
break;
case 104: GetWindowText (edit, tempStr, 104);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
/* Divide Button */ SetWindowText (edit, " ");
if (subtotal == 0)
subtotal = num1;
else
subtotal = subtotal / num1;
count = 1;
op = '/';
break;
case 108: GetWindowText (edit, tempStr, 108);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
/* Multiply Button */ SetWindowText (edit, " ");
if (subtotal == 0)
subtotal = num1;
else
subtotal = subtotal * num1;
count = 1;
op = '*';
break;
case 112: GetWindowText (edit, tempStr, 112);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
/* Subtract Button */ SetWindowText (edit, " ");
subtotal = subtotal - num1;
count = 1;
op = '-';
break;
case 116: GetWindowText (edit, tempStr, 116);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
/* Add Button */ subtotal = subtotal + num1;
count = 1;
SetWindowText (edit, " ");
op = '+';
break;
case 117: GetWindowText (edit, tempStr, 117);
sscanf (tempStr, "%f", &tempVal);
/* Equal Button */
switch (op)
{
case '$': num2 = tempVal;
subtotal = pow(num1, num2);
break;
case '+': num1 = tempVal;
subtotal = subtotal + num1;
count = 1;
break;
case '-': num1 = tempVal;
subtotal = subtotal - num1;
count = 1;
break;
case '*': num1 = tempVal;
if (subtotal == 0)
subtotal = num1;
else
subtotal = subtotal * num1;
count = 1;
break;
case '/': num1 = tempVal;
if (subtotal == 0)
subtotal = num1;
else
subtotal = subtotal / num1;
count = 1;
break;
}
sprintf (tempStr, "%f", subtotal);
SetWindowText (edit, tempStr);
for(i=0; i <= 100; i++)
tempStr[i]=0;
break;
case 133: GetWindowText (edit, tempStr, 133);
sscanf (tempStr, "%f", &tempVal);
/* x^3 */ num1 = tempVal;
num1 = pow(num1, 3);
sprintf (tempStr, "%.3f", num1);
SetWindowText (edit, tempStr);
break;
case 137: GetWindowText (edit, tempStr, 137);
sscanf (tempStr, "%f", &tempVal);
/* x^2 */ num1 = tempVal;
num1 = pow(num1, 2);
sprintf (tempStr, "%.3f", num1);
SetWindowText (edit, tempStr);
break;
case 138: GetWindowText (edit, tempStr, 138);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
/* 1/x */ num1 = 1 / num1;
sprintf (tempStr, "%.3f", num1);
SetWindowText (edit, tempStr);
break;
case 134: GetWindowText (edit, tempStr, 134);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
/* n! */ subtotal=1;
while (num1 != 1)
{
subtotal = subtotal * num1;
num1 = num1 - 1;
}
sprintf (tempStr, "%f", subtotal);
SetWindowText (edit, tempStr);
break;
case 139: num1 = (double)22/7;
sprintf (tempStr, "%.9f", num1);
/* PI */ SetWindowText (edit, tempStr);
break;
case 123: temp_mem = 0;
/* MC */ break;
case 127: sprintf (tempStr, "%.3f", temp_mem);
/* MR */ SetWindowText (edit, tempStr);
break;
case 131: GetWindowText (edit, tempStr, 131);
sscanf (tempStr, "%f", &tempVal);
/* MS */ temp_mem = tempVal;
SetWindowText (edit, " ");
break;
case 135: GetWindowText (edit, tempStr, 135);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
/* M+ */ temp_mem = temp_mem + num1;
SetWindowText (edit, " ");
break;
case 130: GetWindowText (edit, tempStr, 130);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
/* log10 x */ subtotal = log10(num1);
sprintf (tempStr, "%f", subtotal);
SetWindowText (edit, tempStr);
break;
case 126: GetWindowText (edit, tempStr, 126);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
subtotal = log(num1);
/* logx */ sprintf (tempStr, "%f", subtotal);
SetWindowText (edit, tempStr);
break;
case 125: GetWindowText (edit, tempStr, 125);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
subtotal = exp(num1);
/* EXP */ sprintf (tempStr, "%f ", subtotal);
SetWindowText (edit, tempStr);
break;
case 128: GetWindowText (edit, tempStr, 128);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
subtotal = sin(num1);
/* sin */ sprintf (tempStr, "%.9f", subtotal);
SetWindowText (edit, tempStr);
break;
case 132: GetWindowText (edit, tempStr, 132);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
/* cos */ subtotal = cos(num1);
sprintf (tempStr, "%.9f", subtotal);
SetWindowText (edit, tempStr);
break;
case 136: GetWindowText (edit, tempStr, 136);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
/* tan */ subtotal = tan(num1);
sprintf (tempStr, "%.9f", subtotal);
SetWindowText (edit, tempStr);
break;
case 129: GetWindowText (edit, tempStr, 129);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
/* x^y */ SetWindowText (edit, " ");
op = '$';
break;
}
default: return( DefWindowProc( hwnd, msg, wParam, lParam ));
}
}
/ccode
I've been looking at this program for quite a while now and I need some help. PLEASE?? If a bunch of people could look at this code which i provided you can help me figure out why it doesn't work. Everything on it works except for the brackets (which if you have any suggestuoions it would be nice), but mostly my problem lies within the math functions of this calculator. I can add numbers straight, or just mulitply or just subtract and have no problwm. BUT if I start to mix functions like add 2 and then subtract 5 and times 7 i come out with really large or messed up numbers. Here's all the code so if you cut and paste it you can see what i mean for yourself. I really could use some help, so if this is a place for helping people with code, i could really appreciate it. Thanks!
ccode
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define BUTTON01 101
#define BUTTON02 102
#define BUTTON03 103
#define BUTTON04 104
#define BUTTON05 105
#define BUTTON06 106
#define BUTTON07 107
#define BUTTON08 108
#define BUTTON09 109
#define BUTTON10 110
#define BUTTON11 111
#define BUTTON12 112
#define BUTTON13 113
#define BUTTON14 114
#define BUTTON15 115
#define BUTTON16 116
#define BUTTON17 117
#define BUTTON18 118
#define BUTTON19 119
#define BUTTON20 120
#define BUTTON21 121
#define BUTTON22 122
#define BUTTON23 123
#define BUTTON24 124
#define BUTTON25 125
#define BUTTON26 126
#define BUTTON27 127
#define BUTTON28 128
#define BUTTON29 129
#define BUTTON30 130
#define BUTTON31 131
#define BUTTON32 132
#define BUTTON33 133
#define BUTTON34 134
#define BUTTON35 135
#define BUTTON36 136
#define BUTTON37 137
#define BUTTON38 138
#define BUTTON39 139
typedef unsigned int UNIT;
HINSTANCE hInst;
int nCmd;
long FAR PASCAL WndProc( HWND hwnd, UNIT msg, UNIT wParam, LONG lParam );
int PASCAL WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpszCmdLineArgs,
int nCmdShow
)
{
char szAppName[]="Calculator";
HWND hwnd, button;
MSG msg;
WNDCLASS wc;
hInst = hInstance;
nCmd = nCmdShow;
if( !hPrevInstance )
{
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = GetStockObject( WHITE_BRUSH );
wc.lpszMenuName = NULL;
wc.lpszClassName = szAppName;
RegisterClass( &wc );
}
hwnd = CreateWindow( szAppName,
"Calculator", //Gives the calculator a name
WS_OVERLAPPEDWINDOW,
200, //This sets the size of the window
200,
340,
250,
NULL,
NULL,
hInstance,
NULL );
ShowWindow( hwnd, nCmdShow );
UpdateWindow( hwnd );
//The following 18 statements are the button creations.
//They give the size of the button, put the respectable number on top of each.
//button = CreateWindow ("button", " ", WS_CHILD | BS_PUSHBUTTON,
// 10, 60, 30, 30, hwnd, (HMENU) BUTTON20,
// hInstance, NULL);
//ShowWindow (button, nCmdShow);
//UpdateWindow(button);
button = CreateWindow ("button", "(", WS_CHILD | BS_PUSHBUTTON,
40, 60, 30, 30, hwnd, (HMENU) BUTTON21,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", ")", WS_CHILD | BS_PUSHBUTTON,
70, 60, 30, 30, hwnd, (HMENU) BUTTON22,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "MC", WS_CHILD | BS_PUSHBUTTON,
100, 60, 30, 30, hwnd, (HMENU) BUTTON23,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
//button = CreateWindow ("button", " ", WS_CHILD | BS_PUSHBUTTON,
// 10, 90, 30, 30, hwnd, (HMENU) BUTTON24,
// hInstance, NULL);
//ShowWindow (button, nCmdShow);
//UpdateWindow(button);
button = CreateWindow ("button", "Exp", WS_CHILD | BS_PUSHBUTTON,
40, 90, 30, 30, hwnd, (HMENU) BUTTON25,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "ln", WS_CHILD | BS_PUSHBUTTON,
70, 90, 30, 30, hwnd, (HMENU) BUTTON26,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "MR", WS_CHILD | BS_PUSHBUTTON,
100, 90, 30, 30, hwnd, (HMENU) BUTTON27,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "sin", WS_CHILD | BS_PUSHBUTTON,
10, 120, 30, 30, hwnd, (HMENU) BUTTON28,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "x^y", WS_CHILD | BS_PUSHBUTTON,
40, 120, 30, 30, hwnd, (HMENU) BUTTON29,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "log", WS_CHILD | BS_PUSHBUTTON,
70, 120, 30, 30, hwnd, (HMENU) BUTTON30,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "MS", WS_CHILD | BS_PUSHBUTTON,
100, 120, 30, 30, hwnd, (HMENU) BUTTON31,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "cos", WS_CHILD | BS_PUSHBUTTON,
10, 150, 30, 30, hwnd, (HMENU) BUTTON32,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "x^3", WS_CHILD | BS_PUSHBUTTON,
40, 150, 30, 30, hwnd, (HMENU) BUTTON33,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "n!", WS_CHILD | BS_PUSHBUTTON,
70, 150, 30, 30, hwnd, (HMENU) BUTTON34,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "M+", WS_CHILD | BS_PUSHBUTTON,
100, 150, 30, 30, hwnd, (HMENU) BUTTON35,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "tan", WS_CHILD | BS_PUSHBUTTON,
10, 180, 30, 30, hwnd, (HMENU) BUTTON36,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "x^2", WS_CHILD | BS_PUSHBUTTON,
40, 180, 30, 30, hwnd, (HMENU) BUTTON37,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "1/x", WS_CHILD | BS_PUSHBUTTON,
70, 180, 30, 30, hwnd, (HMENU) BUTTON38,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "PI", WS_CHILD | BS_PUSHBUTTON,
100, 180, 30, 30, hwnd, (HMENU) BUTTON39,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
// *********************************************************************
button = CreateWindow ("button", "1", WS_CHILD | BS_PUSHBUTTON,
160, 60, 30, 30, hwnd, (HMENU) BUTTON01,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "2", WS_CHILD | BS_PUSHBUTTON,
190, 60, 30, 30, hwnd, (HMENU) BUTTON02,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "3", WS_CHILD | BS_PUSHBUTTON,
220, 60, 30, 30, hwnd, (HMENU) BUTTON03,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "/", WS_CHILD | BS_PUSHBUTTON,
250, 60, 30, 30, hwnd, (HMENU) BUTTON04,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "4", WS_CHILD | BS_PUSHBUTTON,
160, 90, 30, 30, hwnd, (HMENU) BUTTON05,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "5", WS_CHILD | BS_PUSHBUTTON,
190, 90, 30, 30, hwnd, (HMENU) BUTTON06,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "6", WS_CHILD | BS_PUSHBUTTON,
220, 90, 30, 30, hwnd, (HMENU) BUTTON07,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "*", WS_CHILD | BS_PUSHBUTTON,
250, 90, 30, 30, hwnd, (HMENU) BUTTON08,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "7", WS_CHILD | BS_PUSHBUTTON,
160, 120, 30, 30, hwnd, (HMENU) BUTTON09,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "8", WS_CHILD | BS_PUSHBUTTON,
190, 120, 30, 30, hwnd, (HMENU) BUTTON10,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "9", WS_CHILD | BS_PUSHBUTTON,
220, 120, 30, 30, hwnd, (HMENU) BUTTON11,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "-", WS_CHILD | BS_PUSHBUTTON,
250, 120, 30, 30, hwnd, (HMENU) BUTTON12,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "+/-", WS_CHILD | BS_PUSHBUTTON,
160, 150, 30, 30, hwnd, (HMENU) BUTTON13,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "0", WS_CHILD | BS_PUSHBUTTON,
190, 150, 30, 30, hwnd, (HMENU) BUTTON14,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", ".", WS_CHILD | BS_PUSHBUTTON,
220, 150, 30, 30, hwnd, (HMENU) BUTTON15,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "+", WS_CHILD | BS_PUSHBUTTON,
250, 150, 30, 30, hwnd, (HMENU) BUTTON16,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "=", WS_CHILD | BS_PUSHBUTTON,
285, 60, 40, 90, hwnd, (HMENU) BUTTON17,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
button = CreateWindow ("button", "Clear", WS_CHILD | BS_PUSHBUTTON,
285, 160, 40, 20, hwnd, (HMENU) BUTTON18,
hInstance, NULL);
ShowWindow (button, nCmdShow);
UpdateWindow(button);
while( GetMessage( &msg, NULL, 0, 0) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return( msg.wParam );
}
//******************************************************************************
//* Name: FAR PASCAL
//******************************************************************************
//*
//* Description: This function takes in the windows functions, such as the buttons
//* and their values. It also calculates the users input as
//* requested using switch statments.
//* Passed In: hwnd, msg, wParam, lParam
//* Passed Out: hwnd, msg, wParam, lParam
//* Assumtions/Side Effects: none
//******************************************************************************
long FAR PASCAL WndProc( HWND hwnd, UNIT msg, UNIT wParam, LONG lParam )
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
static float num1, num2, subtotal, tempVal, temp_mem; //declares the varibles
static int count=1, i=0;
static HWND edit, button;
static char tempStr[100], op;
switch( msg )
{
case WM_CREATE: //this creates the text box
edit = CreateWindow("edit", " ", WS_CHILD|WS_BORDER,
120, 10, 200, 30,
hwnd, (HMENU)119, hInst, NULL);
ShowWindow (edit, SW_SHOW);
UpdateWindow(edit);
break;
case WM_PAINT: hdc = BeginPaint( hwnd, &ps );
GetClientRect( hwnd, &rect );
EndPaint( hwnd, &ps );
break;
case WM_DESTROY: PostQuitMessage( 0 );
break;
case WM_COMMAND: switch( wParam )
{
case 101: GetWindowText (edit, tempStr, 101);
strcat (tempStr, "1");
SetWindowText (edit, tempStr);
break;
case 102: GetWindowText (edit, tempStr, 102);
strcat (tempStr, "2");
SetWindowText (edit, tempStr);
break;
case 103: GetWindowText (edit, tempStr, 103);
strcat (tempStr, "3");
SetWindowText (edit, tempStr);
break;
case 105: GetWindowText (edit, tempStr, 105);
strcat (tempStr, "4");
SetWindowText (edit, tempStr);
break;
case 106: GetWindowText (edit, tempStr, 106);
strcat (tempStr, "5");
SetWindowText (edit, tempStr);
break;
case 107: GetWindowText (edit, tempStr, 107);
strcat (tempStr, "6");
SetWindowText (edit, tempStr);
break;
case 109: GetWindowText (edit, tempStr, 109);
strcat (tempStr, "7");
SetWindowText (edit, tempStr);
break;
case 110: GetWindowText (edit, tempStr, 110);
strcat (tempStr, "8");
SetWindowText (edit, tempStr);
break;
case 111: GetWindowText (edit, tempStr, 111);
strcat (tempStr, "9");
SetWindowText (edit, tempStr);
break;
case 114: GetWindowText (edit, tempStr, 114);
strcat (tempStr, "0");
SetWindowText (edit, tempStr);
break;
case 113: GetWindowText (edit, tempStr, 113);
/* +/- Button */ sscanf (tempStr, "%f", &tempVal);
tempVal = tempVal * -1;
sprintf (tempStr, "%.3f", tempVal);
SetWindowText (edit, tempStr);
break;
case 115: if (count <= 1)
{
/* Decimal Button */ GetWindowText (edit, tempStr, 115);
strcat (tempStr, ".");
SetWindowText (edit, tempStr);
count++;
}
break;
case 118: GetWindowText (edit, tempStr, 118);
SetWindowText (edit, " ");
/* Clear Button */ count = 1;
num1 = 0;
tempVal = 0;
subtotal = 0;
for(i=0; i <= 100; i++)
tempStr[i]=0;
temp_mem = 0;
op = NULL;
break;
case 104: GetWindowText (edit, tempStr, 104);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
/* Divide Button */ SetWindowText (edit, " ");
if (subtotal == 0)
subtotal = num1;
else
subtotal = subtotal / num1;
count = 1;
op = '/';
break;
case 108: GetWindowText (edit, tempStr, 108);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
/* Multiply Button */ SetWindowText (edit, " ");
if (subtotal == 0)
subtotal = num1;
else
subtotal = subtotal * num1;
count = 1;
op = '*';
break;
case 112: GetWindowText (edit, tempStr, 112);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
/* Subtract Button */ SetWindowText (edit, " ");
subtotal = subtotal - num1;
count = 1;
op = '-';
break;
case 116: GetWindowText (edit, tempStr, 116);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
/* Add Button */ subtotal = subtotal + num1;
count = 1;
SetWindowText (edit, " ");
op = '+';
break;
case 117: GetWindowText (edit, tempStr, 117);
sscanf (tempStr, "%f", &tempVal);
/* Equal Button */
switch (op)
{
case '$': num2 = tempVal;
subtotal = pow(num1, num2);
break;
case '+': num1 = tempVal;
subtotal = subtotal + num1;
count = 1;
break;
case '-': num1 = tempVal;
subtotal = subtotal - num1;
count = 1;
break;
case '*': num1 = tempVal;
if (subtotal == 0)
subtotal = num1;
else
subtotal = subtotal * num1;
count = 1;
break;
case '/': num1 = tempVal;
if (subtotal == 0)
subtotal = num1;
else
subtotal = subtotal / num1;
count = 1;
break;
}
sprintf (tempStr, "%f", subtotal);
SetWindowText (edit, tempStr);
for(i=0; i <= 100; i++)
tempStr[i]=0;
break;
case 133: GetWindowText (edit, tempStr, 133);
sscanf (tempStr, "%f", &tempVal);
/* x^3 */ num1 = tempVal;
num1 = pow(num1, 3);
sprintf (tempStr, "%.3f", num1);
SetWindowText (edit, tempStr);
break;
case 137: GetWindowText (edit, tempStr, 137);
sscanf (tempStr, "%f", &tempVal);
/* x^2 */ num1 = tempVal;
num1 = pow(num1, 2);
sprintf (tempStr, "%.3f", num1);
SetWindowText (edit, tempStr);
break;
case 138: GetWindowText (edit, tempStr, 138);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
/* 1/x */ num1 = 1 / num1;
sprintf (tempStr, "%.3f", num1);
SetWindowText (edit, tempStr);
break;
case 134: GetWindowText (edit, tempStr, 134);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
/* n! */ subtotal=1;
while (num1 != 1)
{
subtotal = subtotal * num1;
num1 = num1 - 1;
}
sprintf (tempStr, "%f", subtotal);
SetWindowText (edit, tempStr);
break;
case 139: num1 = (double)22/7;
sprintf (tempStr, "%.9f", num1);
/* PI */ SetWindowText (edit, tempStr);
break;
case 123: temp_mem = 0;
/* MC */ break;
case 127: sprintf (tempStr, "%.3f", temp_mem);
/* MR */ SetWindowText (edit, tempStr);
break;
case 131: GetWindowText (edit, tempStr, 131);
sscanf (tempStr, "%f", &tempVal);
/* MS */ temp_mem = tempVal;
SetWindowText (edit, " ");
break;
case 135: GetWindowText (edit, tempStr, 135);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
/* M+ */ temp_mem = temp_mem + num1;
SetWindowText (edit, " ");
break;
case 130: GetWindowText (edit, tempStr, 130);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
/* log10 x */ subtotal = log10(num1);
sprintf (tempStr, "%f", subtotal);
SetWindowText (edit, tempStr);
break;
case 126: GetWindowText (edit, tempStr, 126);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
subtotal = log(num1);
/* logx */ sprintf (tempStr, "%f", subtotal);
SetWindowText (edit, tempStr);
break;
case 125: GetWindowText (edit, tempStr, 125);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
subtotal = exp(num1);
/* EXP */ sprintf (tempStr, "%f ", subtotal);
SetWindowText (edit, tempStr);
break;
case 128: GetWindowText (edit, tempStr, 128);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
subtotal = sin(num1);
/* sin */ sprintf (tempStr, "%.9f", subtotal);
SetWindowText (edit, tempStr);
break;
case 132: GetWindowText (edit, tempStr, 132);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
/* cos */ subtotal = cos(num1);
sprintf (tempStr, "%.9f", subtotal);
SetWindowText (edit, tempStr);
break;
case 136: GetWindowText (edit, tempStr, 136);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
/* tan */ subtotal = tan(num1);
sprintf (tempStr, "%.9f", subtotal);
SetWindowText (edit, tempStr);
break;
case 129: GetWindowText (edit, tempStr, 129);
sscanf (tempStr, "%f", &tempVal);
num1 = tempVal;
/* x^y */ SetWindowText (edit, " ");
op = '$';
break;
}
default: return( DefWindowProc( hwnd, msg, wParam, lParam ));
}
}
/ccode