P1+M+Cpp

$ cat stdafx.cpp //stdafx.cpp : source file that includes just the standard includes// Stack.pch will be the pre-compiled header //stdafx.obj will contain the pre-compiled type information//

//#include "stdafx.h"//

TODO: reference any additional headers you need in STDAFX.H //and not in this file//

//$ cat stdafx.h// stdafx.h : include file for standard system include files, //or project specific include files that are used frequently, but// are changed infrequently

//#pragma once//

//#include "targetver.h"// //#include // //#include // //#include //

//#include //

TODO: reference additional headers your program requires here

$ cat Stack.cpp //Stack.cpp : Defines the entry point for the console application.//


 * 1) include "stdafx.h"


 * 1) define MAX_STRLEN 256
 * 2) define INITIAL_DEPTH 5
 * 3) define STACK_ELEMENT(b,x) b + x * MAX_STRLEN

typedef struct __stack { TCHAR * base; int depth; int index; } STACK, *PSTACK;

BOOL Grow(PSTACK pStack) { TCHAR * temp; //Allocate a new stack which is double the size of the current stack and copy everything over//

//temp = (TCHAR *)calloc(pStack->depth * 2, MAX_STRLEN * sizeof(TCHAR));// //if (NULL == temp)// //{// //_ftprintf(stderr, _T("Failed allocating new memory for stack.\n"));// //return false;// //}//

Copy everything over for (int n = 0; n < pStack->index; n++) _tcscpy_s(STACK_ELEMENT(temp, n), MAX_STRLEN, STACK_ELEMENT(pStack->base, n));

free(pStack->base); pStack->base = temp; pStack->depth *= 2; return true; }

BOOL Push(PSTACK pStack, TCHAR * string, int length) { TCHAR * element;

if (pStack->index == pStack->depth) //Stack is full// //if (!Grow(pStack))// //return false;//

Should have space once we're here element = STACK_ELEMENT(pStack->base, pStack->index++); _tcscpy_s(element, MAX_STRLEN, string); return true; }

TCHAR * Pop(PSTACK pStack) { if (0 == pStack->index) return NULL;

return STACK_ELEMENT(pStack->base, --pStack->index); }

void Show(PSTACK pStack) { if (0 == pStack->index) _tprintf(_T("Stack is empty\n\n")); else for (int n = 0; n < pStack->index; n++) _tprintf(_T("location: %d\tcontent: %s\n"), n, STACK_ELEMENT(pStack->base, n)); }

BOOL ParseInput(PSTACK pStack) { TCHAR input[MAX_STRLEN]; TCHAR * output = NULL; int length = 0;

switch(_tscanf_s(_T("%255[^\n]"), input, MAX_STRLEN)) { case (EOF): _ftprintf(stderr, _T("Error, EOF encountered\n")); return true; case (0): //No input// //return false;// //default:// //length = _tcsnlen(input, MAX_STRLEN);// //if (MAX_STRLEN == length)// //input[MAX_STRLEN -1] = _T('\0');// //}//

//switch (input[0])// //{// //case _T('P')://

//output = Pop(pStack);// //if (NULL != output)// //_tprintf(_T("location: %d\n content: %s\n\n"), pStack->index, output);// //else// //_tprintf(_T("Stack is empty\n\n"));// //break;//

//case _T('Q'):// //return true;//

//case _T('S'):// //Show(pStack);// //break;//

//case _T('+')://

//if (length > 1)// //if (!Push(pStack, &input[1], length - 1))// //return true;//

Success or nothing to push break;

default: if (!Push(pStack, input, length)) return true;

}

fflush(stdin); return false; }

int _tmain(int argc, _TCHAR* argv[]) { STACK MyStack; MyStack.index = 0; MyStack.depth = INITIAL_DEPTH;

BOOL done = false;

MyStack.base = (TCHAR *)calloc(INITIAL_DEPTH, MAX_STRLEN * sizeof(TCHAR));

while (!done) { done = ParseInput(&MyStack); }

free(MyStack.base);

return 0; }

$ cat output.txt D:\Users\Michael Bock\Documents\Visual Studio 2010\Projects\Stack\Release>Stack The stack has exactly 5 elements. S location: 0 content: The location: 1 content: stack location: 2 content: has exactly location: 3 content: 5 location: 4 content: elements. Q

D:\Users\Michael Bock\Documents\Visual Studio 2010\Projects\Stack\Release>Stack This time, the stack will Pop location: 2 content: will

Show location: 0 content: This time, location: 1 content: the stack should not overflow if you implement growing. Show location: 0 content: This time, location: 1 content: the stack location: 2 content: should location: 3 content: not location: 4 content: overflow location: 5 content: if you location: 6 content: implement location: 7 content: growing. Quit

D:\Users\Michael Bock\Documents\Visual Studio 2010\Projects\Stack\Release>stack This time, the stack would overflow unless..... Show location: 0 content: This location: 1 content: time, location: 2 content: the location: 3 content: stack location: 4 content: would location: 5 content: overflow location: 6 content: unless..... Pop location: 6 content: unless.....

Pop location: 5 content: overflow

Pop location: 4 content: would

Pop location: 3 content: stack

Pop location: 2 content: the

Pop location: 1 content: time,

Pop location: 0 content: This

Pop Stack is empty

Show Stack is empty

Quit

D:\Users\Michael Bock\Documents\Visual Studio 2010\Projects\Stack\Release>528:

This is a nice job. I'd be nitpicking or arguing style to criticize this for assignment 1.