122 lines
2.9 KiB
C
122 lines
2.9 KiB
C
/*
|
|
*
|
|
* driver.c
|
|
*
|
|
* DO NOT MOFIFY OR SUBMIT THE CODE IN THIS FILE
|
|
*
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
#include "stack.h"
|
|
|
|
|
|
/* bar( ) : writes bar of * to stdout */
|
|
void bar(void) { printf("****************************************************************\n"); }
|
|
|
|
/* main( ) : driver function to test */
|
|
int main(int argc, char* argv[])
|
|
{
|
|
FILE* testFile; /* Input file */
|
|
char c; /* First char of line input from file */
|
|
int temp; /* Integer input from file */
|
|
char s[2000]; /* Holds line input from file */
|
|
|
|
struct Node* headPtr = NULL; /* Pointer to top of stack */
|
|
|
|
if (argc != 2)
|
|
{
|
|
printf("Error: missing name of input file\n");
|
|
return 1;
|
|
}
|
|
|
|
testFile = fopen(argv[1], "r");
|
|
|
|
if ( testFile == NULL )
|
|
{
|
|
printf("Error: unable to open file. Exiting now...\n");
|
|
return 1;
|
|
}
|
|
|
|
/* Echo print comment line in input file */
|
|
fgets(s, 2000, testFile);
|
|
printf("Comment: %s\n",s);
|
|
|
|
/* Priming read */
|
|
fgets(s, 2, testFile);
|
|
|
|
while ( !feof(testFile) )
|
|
{
|
|
switch(s[0])
|
|
{
|
|
case 'p': /* Print */
|
|
printf("Print(): top [ ");
|
|
Print(headPtr);
|
|
printf("] bottom\n");
|
|
break;
|
|
|
|
case '+': /* Push */
|
|
fscanf(testFile, "%c", &c);
|
|
fscanf(testFile, "%d", &temp);
|
|
printf("Push(%d)\n", temp);
|
|
Push(&headPtr, temp);
|
|
break;
|
|
|
|
case '-': /* Pop */
|
|
fscanf(testFile, "%c", &c);
|
|
temp = Pop(&headPtr);
|
|
printf("Pop(): %d\n", temp);
|
|
break;
|
|
|
|
case 's': /* Size */
|
|
printf("Size() = %d\n", Size(headPtr));
|
|
break;
|
|
|
|
case 'b': /* Bar */
|
|
bar();
|
|
break;
|
|
|
|
case 'c': /* Clear */
|
|
Clear(&headPtr);
|
|
printf("Clear()\n");
|
|
break;
|
|
|
|
case '\n':
|
|
break;
|
|
|
|
default: /* Handle error */
|
|
printf("Error: unknown command - %c\n", c);
|
|
break;
|
|
};
|
|
|
|
fgets(s, 2, testFile);
|
|
|
|
if (feof(testFile))
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
Clear(&headPtr);
|
|
fclose(testFile);
|
|
|
|
return 0;
|
|
}
|
|
|
|
/* Print(...) : outputs the current contents of the stack top to bottom separated by spaces */
|
|
void Print(struct Node* headPtr)
|
|
{
|
|
struct Node* tempPtr = headPtr;
|
|
|
|
while (tempPtr != NULL)
|
|
{
|
|
printf("%d ", tempPtr->data);
|
|
tempPtr = tempPtr->nextPtr;
|
|
}
|
|
}
|
|
|
|
|