added more code
This commit is contained in:
121
CPE455/lab01/driver.c
Normal file
121
CPE455/lab01/driver.c
Normal file
@ -0,0 +1,121 @@
|
||||
/*
|
||||
*
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
BIN
CPE455/lab01/driver.o
Normal file
BIN
CPE455/lab01/driver.o
Normal file
Binary file not shown.
30
CPE455/lab01/input1.txt
Normal file
30
CPE455/lab01/input1.txt
Normal file
@ -0,0 +1,30 @@
|
||||
# input1.txt - test push and clear operations
|
||||
b
|
||||
p
|
||||
b
|
||||
+ 5
|
||||
p
|
||||
b
|
||||
+ 10
|
||||
p
|
||||
b
|
||||
+ 15
|
||||
p
|
||||
b
|
||||
+ 20
|
||||
p
|
||||
b
|
||||
c
|
||||
p
|
||||
b
|
||||
+ 1
|
||||
+ 2
|
||||
+ 3
|
||||
+ 4
|
||||
+ 5
|
||||
p
|
||||
b
|
||||
c
|
||||
p
|
||||
b
|
||||
|
29
CPE455/lab01/input2.txt
Normal file
29
CPE455/lab01/input2.txt
Normal file
@ -0,0 +1,29 @@
|
||||
# input2.txt - test pop operation
|
||||
b
|
||||
p
|
||||
b
|
||||
+ 1
|
||||
+ 2
|
||||
+ 3
|
||||
+ 4
|
||||
+ 5
|
||||
p
|
||||
b
|
||||
-
|
||||
p
|
||||
b
|
||||
-
|
||||
p
|
||||
b
|
||||
-
|
||||
p
|
||||
b
|
||||
-
|
||||
p
|
||||
b
|
||||
-
|
||||
p
|
||||
b
|
||||
p
|
||||
b
|
||||
|
36
CPE455/lab01/input3.txt
Normal file
36
CPE455/lab01/input3.txt
Normal file
@ -0,0 +1,36 @@
|
||||
# input3.txt - test size operation
|
||||
b
|
||||
p
|
||||
s
|
||||
b
|
||||
+ 1
|
||||
+ 2
|
||||
+ 3
|
||||
+ 4
|
||||
+ 5
|
||||
p
|
||||
s
|
||||
b
|
||||
-
|
||||
p
|
||||
s
|
||||
b
|
||||
-
|
||||
p
|
||||
s
|
||||
b
|
||||
-
|
||||
p
|
||||
s
|
||||
b
|
||||
-
|
||||
p
|
||||
s
|
||||
b
|
||||
-
|
||||
p
|
||||
s
|
||||
b
|
||||
p
|
||||
s
|
||||
b
|
36
CPE455/lab01/input4.txt
Normal file
36
CPE455/lab01/input4.txt
Normal file
@ -0,0 +1,36 @@
|
||||
# input4.txt - test clear operation
|
||||
b
|
||||
c
|
||||
p
|
||||
b
|
||||
+ 99
|
||||
p
|
||||
c
|
||||
p
|
||||
b
|
||||
+ 5
|
||||
p
|
||||
b
|
||||
+ 10
|
||||
p
|
||||
b
|
||||
+ 15
|
||||
p
|
||||
b
|
||||
+ 20
|
||||
p
|
||||
b
|
||||
c
|
||||
p
|
||||
b
|
||||
+ 1
|
||||
+ 2
|
||||
+ 3
|
||||
+ 4
|
||||
+ 5
|
||||
p
|
||||
b
|
||||
c
|
||||
p
|
||||
b
|
||||
|
BIN
CPE455/lab01/lab01
Executable file
BIN
CPE455/lab01/lab01
Executable file
Binary file not shown.
20
CPE455/lab01/makefile
Normal file
20
CPE455/lab01/makefile
Normal file
@ -0,0 +1,20 @@
|
||||
#
|
||||
# lab01 makefile - DO NOT MODIFY THIS MAKEFILE
|
||||
#
|
||||
|
||||
CC = gcc
|
||||
CFLAGS = -g -Wall -pedantic
|
||||
|
||||
lab01: driver.o stack.o
|
||||
$(CC) $(CFLAGS) driver.o stack.o -o lab01
|
||||
|
||||
driver.o: driver.c stack.h
|
||||
$(CC) $(CFLAGS) -c driver.c
|
||||
|
||||
stack.o: stack.c stack.h
|
||||
$(CC) $(CFLAGS) -c stack.c
|
||||
|
||||
clean:
|
||||
rm lab01 driver.o stack.o
|
||||
|
||||
|
64
CPE455/lab01/stack.c
Normal file
64
CPE455/lab01/stack.c
Normal file
@ -0,0 +1,64 @@
|
||||
/*
|
||||
*
|
||||
* stack.c
|
||||
*
|
||||
* ADD ALL YOUR CODE HERE AND SUBMIT ONLY stack.c
|
||||
*
|
||||
* Be sure to test functionality with all input files
|
||||
*
|
||||
* and eliminate all memory leaks
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "stack.h"
|
||||
|
||||
|
||||
|
||||
/* Push(...) : adds a new node containing value to the top of the stack assuming stack is not full */
|
||||
void Push(struct Node** headPtr, int value)
|
||||
{
|
||||
struct Node * ptr = (struct Node*) malloc(sizeof(struct Node));
|
||||
ptr->data = value;
|
||||
ptr->nextPtr = *headPtr;
|
||||
*headPtr=ptr;
|
||||
}
|
||||
|
||||
/* Pop(...) : removes and frees node from top of stack, returning the data from the removed node,
|
||||
* assuming the stack is not empty
|
||||
*/
|
||||
int Pop(struct Node** headPtr)
|
||||
{
|
||||
struct Node * ptr = *headPtr;
|
||||
int data = ptr->data;
|
||||
ptr->nextPtr = *headPtr;
|
||||
|
||||
ptr->nextPtr=NULL;
|
||||
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/* Clear(...) : removes and frees all nodes from stack */
|
||||
void Clear(struct Node** headPtr)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/* Size(...) : returns the number of nodes currently stored in the stack */
|
||||
uint32_t Size(struct Node* headPtr)
|
||||
{
|
||||
struct Node * ptr = (struct Node*) malloc(sizeof(struct Node));
|
||||
|
||||
|
||||
return 0; /* Replace this statement with correct code */
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
45
CPE455/lab01/stack.h
Normal file
45
CPE455/lab01/stack.h
Normal file
@ -0,0 +1,45 @@
|
||||
/*
|
||||
*
|
||||
* stack.h
|
||||
*
|
||||
* DO NOT MOFIFY OR SUBMIT THE CODE IN THIS FILE
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef STACK_H
|
||||
#define STACK_H
|
||||
|
||||
|
||||
struct Node
|
||||
{
|
||||
int data; /* Data member of node structure */
|
||||
struct Node* nextPtr; /* Address of next node structure variable in the sequence */
|
||||
};
|
||||
|
||||
extern void Print(struct Node* headPtr);
|
||||
/* Print(...) : outputs the current contents of the stack top to bottom separated by spaces */
|
||||
|
||||
|
||||
extern uint32_t Size(struct Node* headPtr);
|
||||
/* Size(...) : returns the number of nodes currently stored in the stack */
|
||||
|
||||
|
||||
extern void Push(struct Node** headPtr, int value);
|
||||
/* Push(...) : adds a new node containing value to the top of the stack assuming stack is not full */
|
||||
|
||||
|
||||
extern int Pop(struct Node** headPtr);
|
||||
/* Pop(...) : removes and frees node from top of stack, returning the data from the removed node,
|
||||
* assuming the stack is not empty
|
||||
*/
|
||||
|
||||
|
||||
extern void Clear(struct Node** headPtr);
|
||||
/* Clear(...) : removes and frees all nodes from stack */
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
BIN
CPE455/lab01/stack.o
Normal file
BIN
CPE455/lab01/stack.o
Normal file
Binary file not shown.
Reference in New Issue
Block a user