/* This program uses code from "Algorithms in C, Third Edition," * by Robert Sedgewick, Addison-Wesley, 1998. */ #include #include #include "lists.h" #include "stacks.h" #include "tokens.h" int applyOperation(char operator, int number1, int number2) { int result; if (operator == '+') { result = number1 + number2; } else if (operator == '-') { result = number1 - number2; } else if (operator == '*') { result = number1 * number2; } else if (operator == '/') { result = number1 / number2; } return result; } list infixToPostfix(list infix_tokens); int evaluatePostfix(list tokens); int main(int argc, char ** argv) { char * input_file = argv[1]; // input_file = "in1.txt"; FILE * handle = fopen(input_file, "rb"); while(1) { char * line = 0; int allocated = 0; int items = getline(&line, &allocated, handle); if (items == -1) { break; } printf("--------------------------------\n"); printf("processing: %s\n", line); list infix_tokens = tokenize(line); if (listEmpty(infix_tokens)) { free(line); free(infix_tokens); continue; } printf("infix tokens:"); printTokenList(infix_tokens); list postfix_tokens = infixToPostfix(infix_tokens); printf("\npostfix tokens:"); printTokenList(postfix_tokens); int result = evaluatePostfix(postfix_tokens); printf("\nresult: %d\n", result); destroyList(infix_tokens); destroyList(postfix_tokens); free(line); } fclose(handle); int * a; char * b = (char *) a; return 0; }