// check if the given token is the power symbol '^' int hasPowerSymbol(List *list) { return (list->next != NULL && list->next->type == SYMBOL && list->next->token.symbol == '^'); } // get the node that is 'count' positions ahead of the current node List *getNextNode(List *list, int count) { for (int i = 0; i < count; ++i) { list = list->next; } return list; } // returns a double with the value of the identifier double solveEquation(List *list) { int coefficient = 0, constant = 0; int minus = 1, lh = 1; // for extra1: int bCoefficient = 0; // iterate till the end of the list while (list != NULL) { if (list->type == NUMBER) { // case node is a number // first check if the next node is an identifier // if so, check if the next is a power // update coefficient and constant accordingly // if the next node is not an identifier, just update constant if (list->next != NULL && (list->next)->type == IDENTIFIER) { if (hasPowerSymbol(list->next)) { if (getNextNode(list->next, 2)->token.number == 1) { coefficient += minus * lh * list->token.number; } else if (getNextNode(list->next, 2)->token == 0) { constant -= minus * lh * list->token.number; } else { bCoefficient += } list = getNextNode(list, 3); } else { coefficient += minus * lh * list->token.number; list = list->next; } } else { if (hasPowerSymbol(list)) { if (getNextNode(list, 2)->token.number == 1) { constant -= minus * lh * list->token.number; } else { constant -= minus * lh; } list = getNextNode(list, 2); } else { constant -= minus * lh * list->token.number; } } minus = 1; } else if (list->type == IDENTIFIER) { // case node is an identifier // check for power symbols after the node // update coefficient and constant accordingly if (hasPowerSymbol(list)) { if (getNextNode(list, 2)->token.number == 1) { coefficient += minus * lh; } else { constant -= minus * lh; } list = getNextNode(list, 2); } else { coefficient += minus * lh; } minus = 1; } else if (list->type == SYMBOL) { // case node is a symbol // if node is '=', all operations in the future will be flipped // if node is '-', the next operation will be flipped if (list->token.symbol == '=') { lh = -1; } else if (list->token.symbol == '-') { minus = -1; } } list = list->next; } return (double) constant / coefficient; }