Grading Log for CS113 -- HW4 Name: Tuck, Laura SCRIPT FILES ====== ===== h4.scr =================================================== > Script started on Thu Apr 01 17:29:31 1999 > %cat h4calc.c > /* > * File name: h4calc.c > * Name: Laura Tuck > * Assignment: 4 > * Problem: 1 > * Date: April 1, 1999 > */ > > /* > * Use: > * This program uses the stack functions defined in "h4stack.c" to > * implement a calculator. Numbers and operations are entered by > * the user as they call the program. It runs automatically and > * returns the result or an error message if need be. > */ > > /* Included C Library Functions */ > #include > #include > #include > #include > #include > > /* Included interface file for the stack module */ > #include "h4stack.h" > > /* > * Function: Calculator > * Usage: Calculator(&s1, &command); > * -------------------------- > * This function holds all of the subroutines needed when for > * the calculator operations. The operation is passed to the > * function and the new value is placed on top of the stack. > */ > int Calculator(stackT *s1, char *command); > > /*** THE MAIN FUNCTION ***/ > int main(int argc, char *argv[]) > { > /* Initializes a stack */ > stackT s1; > > /* Initializes the loop counters and an error detector */ > int i,j,error=0; > > /* > *Initializes a pointer to access characters enterd by the user > * when they start the program > */ > char *input; > > /* > * Initializes operand to be a double for operands entered by > * user to be convered into the same type held by the stack. > */ > double operand; > > /* Initializes a stack s1 */ > StackInitialize(&s1); > > /* > * Loops through the characters entered by the user when the > * program is called > */ > for(i=1; i<(argc); i++) > { > /*Sets input equal to the string entered by the user */ > input = argv[i]; > > /* Tests to see if inputted string is an operation */ > if ( strcmp(input,"+") == 0 || > strcmp(input,"-") == 0 || > strcmp(input,"x") == 0 || > strcmp(input,"/") == 0 || > strcmp(input,"^") == 0 || > strcmp(input,"neg") == 0) > { > /* If it is pass that operation to the calculator */ > error = Calculator(&s1, input); > } > > /* If not an operation entered string is an operand */ > else > { > /* Sets the inputted string to type double */ > operand = atof(input); > > /* Places the double on the stack */ > StackPush(&s1, operand); > } > > /* Test to see if an error was returned from the Calculator function */ > if(error > 0) > { > /* If there was an error print error message */ > printf("Error with '"); > > /* > * Prints out all of the entered values that the stack was able > * to process. > */ > for(j=1; j<=i; j++) > printf("%s ",argv[j]); > > /* > * Determines if the error was because there were not enough > * values in the stack. > */ > if(error == 1) /* Use constants for error conditions. -TF */ > { > printf("'\nNot enought operands for '%s'\n",argv[i]); > > /* Quits the program automatically */ > exit (1); > } > > /* Determines if the error was due to division by zero */ > if(error == 2) > { > printf("'\nAttempt to divide by zero!\n"); > > /* Quits the program automatically */ > exit (1); > } > } > } > > /* > * Determines if the user entered anyvalue when calling the program > * If not will print an appropriate error message and exit. > */ > if(StackIsEmpty(&s1)) > printf("\nYou didn't enter any values.\n"); > > /* Prints the result of the values entered by the user */ > else > printf("The result is %f\n", StackPop(&s1)); ^^ result should have been printed with %g > exit (1); /* Use "return 0" from main() upon success. -TF */ > } > > /* > * Function: Calcualtor > * -------------------------- > * This function takes the command entered by the user and manipulates > * the stack so the desired operation is completed. Returns error > * messages if there are not enough operands on the stack or if > * the user is tring to divide by zero. > */ > int Calculator(stackT *s1, char *command) > { > /* > *Initializes memory so can manipulate values when they come off the > * stack. > */ > stackElementT temp1; > stackElementT temp2; > stackElementT answer; > > /* Checks to make sure that the stack has a operand, if not returns error */ > if(StackIsEmpty(s1)) > { > return 1; > } > > /* Checks to see if the operation is negation */ > if(strcmp(command,"neg") == 0) > { > /* answer becomes the negated value on the top of the stack. */ > answer = StackPop(s1) * -1; > > /* returns the negated value */ > StackPush(s1, answer); > > /* returns 0 - no error */ > return 0; > } > > /* Pops the first operand off the stack */ > temp1 = StackPop(s1); > > /* > * Checks to make sure that the stack has another operand, if not > * returns error */ > if(StackIsEmpty(s1)) > { > return 1; > } > > /* Checks to see if the operation is addition */ > if(strcmp(command,"+") == 0) > { > temp2 = StackPop(s1); > > /* answer is addition of top two operand of the stack */ > answer = temp1 + temp2; > StackPush(s1, answer); > > /* returns 0 - no error */ > return 0; > } > > /* Checks to see if the operation is subtaction */ > if(strcmp(command,"-") == 0) > { > temp2 = StackPop(s1); > > /* answer is subtraction of top two operand of the stack */ > answer = temp2 - temp1; > StackPush(s1, answer); > > /* returns 0 - no error */ > return 0; > } > > /* Checks to see if the operation is multiplication */ > if(strcmp(command,"x") == 0) > { > temp2 = StackPop(s1); > > /* answer is multiplication of top two operand of the stack */ > answer = temp1 * temp2; > StackPush(s1, answer); > > /* returns 0 - no error */ > return 0; > } > > /* Checks to see if the operation is division */ > if(strcmp(command,"/") == 0) > { > /* Determines if attempting to divide by zero - returns error */ > if(temp1 == 0) > { > return 2; > } > temp2 = StackPop(s1); > > /* answer is division of top two operand of the stack */ > answer = temp2 / temp1; > StackPush(s1, answer); > > /* returns 0 - no error */ > return 0; > } > > /* Checks to see if the operation is raise to a power */ > if(strcmp(command,"^") == 0) > { > temp2 = StackPop(s1); > > /* > * answer is the second value of the stack raise to the first > * value from the stack > */ > answer = pow(temp2, temp1); > StackPush(s1, answer); > > /* returns 0 - no error */ > return 0; > } > > /* returns error becuase no operation match */ > return 1; > } > > > %cat h4stack.h > /* > * File name: h4stack.h > * Name: Laura Tuck > * Assignment: 4 > * Problem: 1 > * Date: April 1, 1999 > */ > > /* > * USE: > * This is the interface file for the stack module that creates > * and manipulates a stack. The functions prototyped below allow the > * user to create and destroy a stack as well as push and pop elements > * on the stack. > */ > > /* > * This wraps the header to solve the problem of multiple inclusion > */ > > #ifndef _h4stack_h > #define _h4stack_h > > /*** TYPE DEFINITIONS ***/ > > /* Defines stackElementT to be of type double */ > typedef double stackElementT; > > /* > * Defines a structure to be called stackNodeT. That will be used > * like a link list. The first element is a double that stores > * a value on the stack. The second is a pointer to the next element > * in the stack. The last node in the stacks pointer is = NULL. > */ > typedef struct stackNodeTag > { > stackElementT stackElement; > struct stackNodeTag *linkP; > } stackNodeT; > > /* > * Defines a structure to be called stackT. It contains a pointer > * to the top of the stack, the last value added to the stack. > */ > typedef struct > { > /* Top of stack, i.e., pointer to 1st node in list. */ > stackNodeT *top; > } stackT; > > /*** FUNCTION PROTOTYPES ***/ > > /* > * Function: StackInitialize > * Usage: StackInitialize(&s1); > * -------------------------- > * This procedure initializes an empty stack. > */ > void StackInitialize(stackT *listP); > > /* > * Function: StackDestroy > * Usage: StackDestroy(&s1); > * -------------------------- > * This procedure destorys a stack. It removes all values from the stack > * and then "frees" the dynamically allocated memory. > */ > void StackDestroy(stackT *listP); > > /* > * Function: StackPush > * Usage: StackPush(&s1, value); > * -------------------------- > * This function takes a value that is of type double and places > * it on the stack. > */ > void StackPush(stackT *listP, stackElementT value); > > /* > * Function: StackPop > * Usage: StackPop(&s1); > * -------------------------- > * This function takes the top value on the stack and returns it. > */ > stackElementT StackPop(stackT *listP); > > /* > * Function: StackIsEmpty > * Usage: StackIsEmpty(&s1); > * -------------------------- > * This procedure determines if the stack holds anything, if it > * is empty. > */ > int StackIsEmpty(stackT *listP); > > /* This is for wrapping, solve the problem of multiple inclusion.*/ > #endif > %cat h4stack.c > /* > * File name: h4stack.c > * Name: Laura Tuck > * Assignment: 4 > * Problem: 1 > * Date: April 1, 1999 > */ > > /* > * Use: > * This is the implementation file for the stack module. It is use > * to define all of the functions used in creating and manipulating > * the stack. The function prototypes are give in "h4stack.h" > * which is included in this file > */ > > /* Included C Library Functions */ > #include > #include > #include > #include > #include > > /* Included interface file for the stack module */ > #include "h4stack.h" > > /* > * Function: StackInitialize > * -------------------------- > * Initializes a stack. > */ > void StackInitialize(stackT *listP) > { > /* > * Sets the first node pointer = NULL, which corresponds to an > * empty stack > */ > listP->top = NULL; > } > > /* > * Function: StackIsEmpty > * -------------------------- > * Determines if the stack has anything in it. > */ > int StackIsEmpty(stackT *listP) > { > /* If the stack is empty the pointer top will = NULL */ > return(listP->top == NULL); > } > > /* > * Function: StackDestroy > * -------------------------- > * Empties the stack and releases the dynamic memory allocated to it. > */ > void StackDestroy(stackT *listP) > { > /* Pops values off of the stack until it is empty */ > while (!StackIsEmpty(listP)) > StackPop(listP); > > /* Resets the pointer for the top of the stack */ > listP->top = NULL; > > /* Frees the memory */ > free(listP); > } > > /* > * Function: StackPush > * -------------------------- > * Places the value that is of type stackElementT onto the stack. > */ > void StackPush(stackT *listP, stackElementT value) > { > /* Creates a new node for the link list */ > stackNodeT *temp; > > /* Allocates memory for the new node */ > temp = (stackNodeT *)malloc(sizeof(stackNodeT)); > /* Make sure malloc() got space. -TF */ > > /* > * Sets the pointer of the new node to the pointer to the top of > * the stack > */ > temp->linkP=listP->top; > > /* Sets the node value to be the element passed to the function. */ > temp->stackElement=value; > > /* Sets the last nodes pointer to the new node */ > listP->top=temp; > } > > /* > * Function: StackPop > * -------------------------- > * Removes the top item in the stack. > */ > stackElementT StackPop(stackT *listP) > { > /* Initializes a new node */ > stackNodeT *temp; > stackElementT element; > > /* Error checking, you can't pop an element off an empty stack. */ > if (listP->top == NULL) > { > printf("Can not POP an empty stack!"); /* Send to *stderr*. -TF */ > return NULL; > /* > * Exit program since returning NULL won't work with all types > * of elements. -TF > */ > } > > /* If Pop is a legal operation of the node continues */ > else > { > /* Sets the tempory node to the pointer to the top */ > temp = listP->top; > > /* Sets element equal to the element in the top node */ > element = temp->stackElement; > > /* Sets the pointer to the previous node in the stack */ > listP->top = temp->linkP; > > /* Frees the temporary node used by function */ > free(temp); > } > > /* Returns the element from the stack */ > return(element); > > } > %cat h4makefile > # File name: h4makefile > # Name: Laura Tuck > # Assignment: 4 > # Date April 1, 1999 > > # Makefile for CS113 Homework 4 > > # ***************************************************** > # Parameters to control Makefile operation > > CC = gcc > CFLAGS = -ansi -pedantic -Wall -g > > # **************************************************** > # Entries to bring the executable up to date > > h4calc: h4calc.o h4stack.o > $(CC) $(CFLAGS) -lm -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 -c h4stack.c > 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.000000 > %h4calc -6.7 4 x + 5.7 3 - 11.2 x neg > Error with '-6.7 4 x + ' > Not enought 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 150 100 75 + 12 2 ^ neg - / > The result is 0.470219 > %h4 calc 150 100 75 + 10 0 / > Error with '150 100 75 + 10 0 / ' > Attempt to divide by zero! > %h4calc 4.2 -6.2 +8.6 + 7.7 / x 2 ^ + - > Error with '4.2 -6.2 +8.6 + 7.7 / x 2 ^ + ' > Not enought operands for '+' > %exit > exit > > script done on Thu Apr 01 17:33:22 1999