Long Integer Addition Using Three Wide Character Stacks by James Pate Williams, Jr.

// LongIntSum.cpp : Defines the entry point for the application.
// Translated from the Pascal code found in the textbook
// "Applied Data Structures Using Pascal" by Guy J. Hale and
// Richard J. Easton pages 98 and 99.

#include "stdafx.h"
#include "CharArrayStack.h"
#include "LongIntSum.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;                                // current instance
WCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name

// Forward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    AddDialog(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: Place code here.

    // Initialize global strings
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_LONGINTSUM, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_LONGINTSUM));

    MSG msg;

    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}

//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_LONGINTSUM));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_LONGINTSUM);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // Store instance handle in our global variable

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND  - process the application menu
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // Parse the menu selections:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
			case ID_FILE_LONGINTADDITION:
				DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hWnd, AddDialog);
				break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: Add any drawing code that uses hdc here...
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

#define BUFFER_MAX 8192

WCHAR buffer1[BUFFER_MAX];
WCHAR buffer2[BUFFER_MAX];
WCHAR buffer3[BUFFER_MAX];
CHARARRAYSTACK stack1;
CHARARRAYSTACK stack2;
CHARARRAYSTACK stack3;

void Add(
	HWND hwnd,
	int id1,
	int id2,
	int id3)
{
	WCHAR ch1, ch2, ch3;
	int carry = 0, i, i1, i2, i3, count = 0;

	GetDlgItemText(hwnd, id1, buffer1, BUFFER_MAX);
	GetDlgItemText(hwnd, id2, buffer2, BUFFER_MAX);

	for (i = 0; i < (int)wcslen(buffer1); i++)
		PushCharArrayStack(&stack1, buffer1[i]);

	for (i = 0; i < (int)wcslen(buffer2); i++)
		PushCharArrayStack(&stack2, buffer2[i]);

	while (
		!EmptyCharArrayStack(&stack1) &&
		!EmptyCharArrayStack(&stack2))
	{
		PopCharArrayStack(&stack1, &ch1);
		PopCharArrayStack(&stack2, &ch2);
		i1 = (int)(ch1 - '0');
		i2 = (int)(ch2 - '0');
		i3 = i1 + i2 + carry;
		carry = i3 / 10;
		i3 %= 10;
		ch3 = (char)(i3 + '0');
		PushCharArrayStack(&stack3, ch3);
	}

	while (!EmptyCharArrayStack(&stack1))
	{
		PopCharArrayStack(&stack1, &ch1);
		i1 = (int)(ch1 - '0');
		i3 = i1 + carry;
		carry = i3 / 10;
		i3 %= 10;
		ch3 = (char)(i3 + '0');
		PushCharArrayStack(&stack3, ch3);
	}

	while (!EmptyCharArrayStack(&stack2))
	{
		PopCharArrayStack(&stack2, &ch2);
		i2 = (int)(ch2 - '0');
		i3 = i2 + carry;
		carry = i3 / 10;
		i3 %= 10;
		ch3 = (char)(i3 + '0');
		PushCharArrayStack(&stack3, ch3);
	}

	if (carry != 0)
	{
		ch3 = (char)(carry + '0');
		PushCharArrayStack(&stack3, ch3);
	}

	while (!EmptyCharArrayStack(&stack3))
	{
		PopCharArrayStack(&stack3, &ch3);
		buffer3[count++] = ch3;
	}

	buffer3[count] = '\0';
	SetDlgItemText(hwnd, id3, buffer3);
}

// Message handler for long int addition dialog box.
INT_PTR CALLBACK AddDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	WORD word = LOWORD(wParam);

	switch (message)
	{
	case WM_INITDIALOG:
		InitCharArrayStack(&stack1);
		InitCharArrayStack(&stack2);
		InitCharArrayStack(&stack3);
		return (INT_PTR)TRUE;
	case WM_COMMAND:
		if (word == IDOK || word == IDCANCEL)
		{
			EndDialog(hDlg, word);
			return (INT_PTR)TRUE;
		}

		if (word == IDC_BUTTON1)
			Add(hDlg, IDC_EDIT1, IDC_EDIT2, IDC_EDIT3);

		break;
	}
	return (INT_PTR)FALSE;
}

#pragma once
#include "stdafx.h"

#define STACK_SIZE 8192

typedef struct myCharArrayStack {
	WCHAR data[STACK_SIZE];
	int top;
} CHARARRAYSTACK, *PCHARARRAYSTACK;

void InitCharArrayStack(
	PCHARARRAYSTACK stack);

BOOL EmptyCharArrayStack(
	PCHARARRAYSTACK stack);

BOOL FullCharArrayStack(
	PCHARARRAYSTACK stack);

BOOL PushCharArrayStack(
	PCHARARRAYSTACK stack,
	WCHAR data);

BOOL PopCharArrayStack(
	PCHARARRAYSTACK stack,
	WCHAR* data);

#include "stdafx.h"
#include "CharArrayStack.h"

void InitCharArrayStack(
	PCHARARRAYSTACK stack)
{
	int i;

	for (i = 0; i < stack->top; i++)
		stack->data[i] = '\0';

	stack->data[0] = 0;
	stack->top = -1;
}

BOOL EmptyCharArrayStack(
	PCHARARRAYSTACK stack)
{
	return stack->top == -1 ? TRUE : FALSE;
}

BOOL FullCharArrayStack(
	PCHARARRAYSTACK stack)
{
	return stack->top == STACK_SIZE - 1 ? TRUE : FALSE;
}

BOOL PushCharArrayStack(
	PCHARARRAYSTACK stack,
	WCHAR data)
{
	BOOL result = FALSE;

	if (!FullCharArrayStack(stack))
	{
		stack->top++;
		stack->data[stack->top] = 0;
		stack->data[stack->top] = data;
		result = TRUE;
	}

	return result;
}

BOOL PopCharArrayStack(
	PCHARARRAYSTACK stack,
	WCHAR* data)
{
	BOOL result = FALSE;

	if (!EmptyCharArrayStack(stack))
	{
		*data = stack->data[stack->top];
		stack->top--;
		result = TRUE;
	}

	return result;
}

Unknown's avatar

Author: jamespatewilliamsjr

My whole legal name is James Pate Williams, Jr. I was born in LaGrange, Georgia approximately 70 years ago. I barely graduated from LaGrange High School with low marks in June 1971. Later in June 1979, I graduated from LaGrange College with a Bachelor of Arts in Chemistry with a little over a 3 out 4 Grade Point Average (GPA). In the Spring Quarter of 1978, I taught myself how to program a Texas Instruments desktop programmable calculator and in the Summer Quarter of 1978 I taught myself Dayton BASIC (Beginner's All-purpose Symbolic Instruction Code) on LaGrange College's Data General Eclipse minicomputer. I took courses in BASIC in the Fall Quarter of 1978 and FORTRAN IV (Formula Translator IV) in the Winter Quarter of 1979. Professor Kenneth Cooper, a genius poly-scientist taught me a course in the Intel 8085 microprocessor architecture and assembly and machine language. We would hand assemble our programs and insert the resulting machine code into our crude wooden box computer which was designed and built by Professor Cooper. From 1990 to 1994 I earned a Bachelor of Science in Computer Science from LaGrange College. I had a 4 out of 4 GPA in the period 1990 to 1994. I took courses in C, COBOL, and Pascal during my BS work. After graduating from LaGrange College a second time in May 1994, I taught myself C++. In December 1995, I started using the Internet and taught myself client-server programming. I created a website in 1997 which had C and C# implementations of algorithms from the "Handbook of Applied Cryptography" by Alfred J. Menezes, et. al., and some other cryptography and number theory textbooks and treatises.

Leave a comment