Grading Log for CS113 -- HW4 Name: Ostrowski, Steven SCRIPT FILES ====== ===== h4.scr =================================================== > Script started on Mon Mar 29 16:59:47 1999 > %cat h4calc.c > /* > * File name: h4calc.c > * Name: Steven Ostrowski > * Assignment: 4 > * Problem: 1 > * Date: March 21, 1999 > */ > > /* This is a postfix calculator program that accepts arguments > * from the command line for its operation. It utilizes a stack > * implemented as a linked list to perform the postfix operations. > * Errors are handled before they reach the stack module. > */ > > #include "h4stack.h" > #include > #include > #include > #include /* to compare argument strings */ > > /* Function : ScanArguments() > * Usage : ScanArguments(stackT *stack, int argc, char *argv[]) > * ---------------------- > * This program is the main action of the calculator. > * It scans the arguments and performs the necessary stack > * operations on them > */ > void ScanArguments(stackT *stack, int argc, char *argv[]); > > /* Function : DoOperation() > * Usage : DoOperation(stackT *stack, char *argv[], int i) > * ---------------------- > * This function performs the correct operation based on > * the operator passed to it, and checks for errors. > */ > double DoOperation(stackT *stack, char *argv[], int i); > > /* Function : PrintError() > * Usage : PrintError(char *argv[], int i, int code) > * ----------------- > * This function prints the error message based on the code passed, > * and reports where the error occurred. > */ > void PrintError(char *argv[], int i, int code); > > int main(int argc, char *argv[]) > { > /* Create stack variable */ > stackT stack; > > /* Initialize Stack */ > StackInitialize(&stack); > > /* Scan arguments */ > ScanArguments(&stack, argc, argv); > > return 0; > } > > /* Function ScanArguments() */ > void ScanArguments(stackT *stack, int argc, char *argv[]) > { > int i; > > /* Go through the arguments performing the operations */ > for(i=1; i { > /* Check if string is a operand or operator */ > /* If it is a number, push it on stack */ > if(strcmp(argv[i],"+")!=0&&strcmp(argv[i],"-")!=0&& > strcmp(argv[i],"/")!=0&&strcmp(argv[i],"x")!=0&& > strcmp(argv[i],"^")!=0&&strcmp(argv[i],"neg")!=0) > { > StackPush(stack, atof(argv[i])); > } > else /* It is not a number, i.e. it is operator */ > { > /* Push the result from DoOperation, that function will > handle errors */ > StackPush(stack, DoOperation(stack, argv, i)); > }; /* End if-else */ > }; /* end for(i..) */ > > /* Print end result, if there is one */ > if(!StackIsEmpty(stack)) > printf("The result is %g\n", StackPop(stack)); > else > printf("No result was obtained.\n"); > } > > /* Function : DoOperation() */ > double DoOperation(stackT *stack, char *argv[], int i) > { > char tempop = *(argv[i]); /* hold the operator first char */ > double tempnum[2]; /* used for holding elements */ > int j; /* used for loop to test for enough elements for operation */ > > /* Perform operation based on operator, and error check */ > switch(tempop) > { > case '+': for(j=0; j<2; ++j) > { > if(!StackIsEmpty(stack)) > tempnum[j] = StackPop(stack); > else PrintError(argv, i, 1); > }; > return (tempnum[0]+tempnum[1]); > break; > case '-': for(j=0; j<2; ++j) > { > if(!StackIsEmpty(stack)) > tempnum[j] = StackPop(stack); > else PrintError(argv, i, 1); > }; > return (tempnum[1]-tempnum[0]); > break; > case '/': for(j=0; j<2; ++j) > { > if(!StackIsEmpty(stack)) > tempnum[j] = StackPop(stack); > else PrintError(argv, i, 1); > }; > /* Check for division by 0 */ > if(tempnum[0]==0) > PrintError(argv, i, 2); > else > return (tempnum[1] / tempnum[0]); > break; > case 'x': for(j=0; j<2; ++j) > { > if(!StackIsEmpty(stack)) > tempnum[j] = StackPop(stack); > else PrintError(argv, i, 1); > }; > return (tempnum[0] * tempnum[1]); > break; > case 'n': for(j=0; j<1; ++j) > { > if(!StackIsEmpty(stack)) > tempnum[j] = StackPop(stack); > else PrintError(argv, i, 1); > }; > return (-1 * tempnum[0]); > break; > case '^': for(j=0; j<2; ++j) > { > if(!StackIsEmpty(stack)) > tempnum[j] = StackPop(stack); > else PrintError(argv, i, 1); > }; > return (pow(tempnum[1],tempnum[0])); > break; > /* if none of cases reached, some error occurred, exit */ > default: break; > }; /* end switch */ > > /* If function has not returned anything or exited via error, there > is an error, so exit */ > if(tempop=='n') > printf("Error: operator 'neg' not handled. "); > else > printf("Error: operator '%c' not handled. ", tempop); > printf("Exiting.\n"); > exit(1); > } > > /* Function PrintError() */ > void PrintError(char *argv[], int i, int code) > { > int j; /* counter to print out each argument used */ > > /* Generically, switch statement for code > * in this program, only two codes, 1 for not enough operands, > * 2 for divide by zero. > */ > switch(code) > { /* Use constants for error codes. -TF */ > case 1: printf("Error with \""); > /* Print out all arguments used so far, this shows > * where error occurred. > */ > for(j=1;j<=i-1;++j) > printf("%s ", argv[j]); > printf("%s", argv[i]); > printf("\": not enough operands for '%s'.\n", argv[i]); > exit(1); > break; > case 2: printf("Error with \""); > /* Print out all arguments used so far, this shows > * where error occurred. > */ > for(j=1;j<=i-1;++j) > printf("%s ", argv[j]); > printf("%s", argv[i]); > printf("\": attempt to divide by zero.\n"); > exit(1); > break; > default: break; > }; /* end switch() */ > > /* if program reaches here, some error */ > printf("Error not handled correctly. Exiting.\n"); > exit(1); > } > %cat h4stack.h > /* > * File name: h4stack.h > * Name: Steven Ostrowski > * Assignment: 4 > * Problem: 1 > * Date: March 18, 1999 > */ > > /* > * This is the header for the stack module. This contains the basic > * stack operations such as push, pop, isempty, initialize, and > * destroy, so the stack may be implemented in other programs. > */ > > #ifndef _h4stack_h > #define _h4stack_h > > typedef double stackElementT; > > /* This holds the number and link for each element in list */ > typedef struct stackNodeTag { > stackElementT data; /* Hold the double */ > struct stackNodeTag *next; /* Hold the link */ > } stackNodeT; > > /* This structure contains the general info for the list */ > typedef struct { > /* Pointer to the first node */ > stackNodeT *top; > } stackT; > > /* Function Declarations */ > > /* Function : StackInitialize > * Usage : StackInitialize(&stackP); > * --------------------- > * > * This function sets up a new stack to be empty > */ > void StackInitialize(stackT *); > > /* Function : StackDestroy > * Usage : StackDestroy(&stackP); > * ------------------ > * > * This function removes any remaining nodes in list, which > * deletes the list. > */ > void StackDestroy(stackT *); > > /* Function : StackPush > * Usage : StackPush(&stackP, stackElement) > * -------------------- > * > * This function places an element at the end of the list, or > * at the top of the stack. > */ > void StackPush(stackT *, stackElementT); > > /* Function : StackPop > * Usage : StackPop(&stackP); > * ---------------- > * > * This function removes an element from the top of the list, > * and stores it in an element variable. > */ > stackElementT StackPop(stackT *); > > /* Function : StackIsEmpty > * Usage : StackIsEmpty(&stackP); > * ----------------- > * > * This function reports true or false based on if the > * stack is empty or not. > */ > int StackIsEmpty(stackT *); > > #endif > > %cat h4stack.c > /* > * File name: h4stack.c > * Name: Steven Ostrowski > * Assignment: 4 > * Problem: 1 > * Date: March 21, 1999 > */ > > #include "h4stack.h" > #include > #include /* For exit() function */ > > /* Function : StackInitialize() */ > void StackInitialize(stackT *stack) > { > /* Set up the top field of the stack to null so it's empty */ > stack->top = NULL; > } > > /* Function : StackDestroy() */ > void StackDestroy(stackT *stack) > { > stackNodeT *current = stack->top; > stackNodeT *temp; > /* These are used to trace through the list and delete nodes > * initialized to top of stack > */ > > /* Loop through list until the end, freeing each element */ > while(current != NULL) > { > temp = current; > current = current->next; > free(temp); /* Delete the node */ > }; /* end while(current..) */ > > /* Set top field of stack to NULL to show emptiness */ > stack->top = NULL; > } > > /* Function : StackPush() */ > void StackPush(stackT *stack, stackElementT element) > { > /* temp pointer to hold address of current top field */ > stackNodeT *temp = stack->top; > > /* Create the new node */ > stack->top = (stackNodeT *) malloc(sizeof(stackNodeT)); > /* Make sure malloc() got space. -TF */ > /* Initialize it to element, and set next field to previous top */ > stack->top->data = element; > stack->top->next = temp; > } > > /* Function StackPop() */ > stackElementT StackPop(stackT *stack) > { > /* Holds the value of the data of the top element to pop */ > stackElementT tempel; > > /* Holds the previous top element so prog can free it */ > stackNodeT *tempnode; /* It's a pointer, so add "P" to name. -TF */ > > /* If the stack is not empty, pop off the top element */ > if(!StackIsEmpty(stack)) > { > /* Store the top and the top data */ > tempnode = stack->top; > tempel = stack->top->data; > /* Move the top pointer to the next element */ > stack->top = stack->top->next; > free(tempnode); /* Delete the popped node */ > return tempel; /* Return the data value */ > } > else /* Stack is empty, so return error */ > { > printf("There are no stack elements remaining "); > printf("to pop. Exiting...\n"); > exit(1); /* Exit with error code */ > }; > } > > /* Function : StackIsEmpty() */ > int StackIsEmpty(stackT *stack) > { > /* Return true if top = null, signalling empty stack */ > return (stack->top == NULL); > } > %cat h4makefile > # Makefile for CS113 - HW3 > > # ***************************************************** > # Parameters to control Makefile operation > > CC = gcc > CFLAGS = -ansi -pedantic -Wall -g -lm > # Put -lm just in linking command below. -TF > > # **************************************************** > # Entries to bring the executable up to date > > h4calc: h4calc.o h4stack.o > $(CC) $(CFLAGS) -o h4calc h4calc.o h4stack.o > > h4calc.o: h4calc.c h4stack.h > $(CC) $(CFLAGS) -c h4calc.c > > h4stack.o: h4stack.c h4stack.h > $(CC) $(CFLAGS) -c h4stack.c > %make -f h4makefile > gcc -ansi -pedantic -Wall -g -lm -c h4calc.c > gcc: -lm: linker input file unused since linking not done > gcc -ansi -pedantic -Wall -g -lm -c h4stack.c > gcc: -lm: linker input file unused since linking not done > gcc -ansi -pedantic -Wall -g -lm -o h4calc h4calc.o h4stack.o > %h4calc 45.6 2.0 / 10.97 + neg 9 0.5 ^ x .69 - > The result is -102 > %h4calc -6.7 4 x + 5.7 3 - 11.2 x neg > Error with "-6.7 4 x +": not enough operands for '+'. > %h4calc 76.0 45.6 89 - 5.0 x 217 + / 42 neg - > Error with "76.0 45.6 89 - 5.0 x 217 + /": attempt to divide by zero. > %h4calc +12 4 - 8 x 4 / neg 20 + 3 ^ > The result is 64 > %h4calc 32 2 / 12 + - > Error with "32 2 / 12 + -": not enough operands for '-'. > %h4calc 100 10 / 5 x 50 - 2 + 3 3 / 1 - / > Error with "100 10 / 5 x 50 - 2 + 3 3 / 1 - /": attempt to divide by zero. > %quit > quit: Command not found. > %exit > exit > > script done on Mon Mar 29 17:07:31 1999