#include #include // strcmp() #include "scanner.h" #include "scannerStructs.h" #include "recognizeEq.h" // checks if input is a number int acceptNumber(List **list) { if (*list != NULL && (*list)->type == NUMBER) { *list = (*list)->next; return 1; } return 0; } // checks if input is an identifier int acceptIdentifier(List **list) { if (*list != NULL && (*list)->type == IDENTIFIER) { *list = (*list)->next; return 1; } return 0; } // checks if input is a specified character int acceptCharacter(char symbol, List **list) { if (*list != NULL && (*list)->type == SYMBOL && ((*list)->token).symbol == symbol) { char symbol = ((*list)->token).symbol; *list = (*list)->next; return symbol; } return 0; } // checks if input follows the grammar | [nat] ['^' ] int acceptTerm(List **list) { if (acceptNumber(list)) { if (acceptIdentifier(list)) { if (acceptCharacter('^', list)) { if (!acceptNumber(list)) { return 0; } } return 1; } return 1; } if (acceptIdentifier(list)) { if (acceptCharacter('^', list)) { if (!acceptNumber(list)) { return 0; } } return 1; } return 0; } // checks if input follows the grammar [-] {'+' || '-' } int acceptExpression(List **list) { if (acceptCharacter('-', list)) { } if (!acceptTerm(list)) { return 0; } while (acceptCharacter('+', list) || acceptCharacter('-', list)) { if (!acceptTerm(list)) { return 0; } } return 1; } // checks if input follows the grammar '=' int acceptEquation(List **list) { if (!acceptExpression(list)) { return 0; } if (!acceptCharacter('=', list)) { return 0; } if (!acceptExpression(list)) { return 0; } if (*list != NULL) { return 0; // the list should be empty at this point } return 1; } // checks if the entire input is a valid equation int isValidEquation(List *list) { return acceptEquation(&list); } // Returns maximum between two values int getMax(int a, int b) { return (a > b ? a : b); } // returns 1 if there is exactly one variable int isSingleVariableEquation(List *list) { char *str = NULL; int found = 0; while (list != NULL) { if (list->type == IDENTIFIER) { if (str == NULL) { str = list->token.identifier; found = 1; } if (strcmp(list->token.identifier, str)) { return 0; } } list = list->next; } return found; } // returns the highest degree of x as an int int getDegree(List *list) { int maxFound = 1; while (list != NULL && list->next != NULL) { if (acceptCharacter('^', &list)) { maxFound = getMax(maxFound, (list->token).number); } list = list->next; } return maxFound; } // evaluates the entire input as an equation double solveEquation(List *list) { int coefficient = 0, constant = 0; int sign = 1; int onLeftSide = 1; while (list != NULL) { switch (list->type) { case NUMBER: if (list->next != NULL && list->next->type == IDENTIFIER) { if (onLeftSide) { coefficient += sign * (list->token.number - 1); } else { coefficient -= sign * (list->token.number + 1); } } else { if (onLeftSide) { constant += sign * list->token.number; } else { constant -= sign * list->token.number; } } sign = 1; break; case IDENTIFIER: if (onLeftSide) { coefficient += sign; } else { coefficient -= sign; } break; case SYMBOL: if (list->token.symbol == '=') { onLeftSide = 0; } else if (list->token.symbol == '-') { sign = -1; } break; } list = list->next; } return (double)-constant / coefficient; }