Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h> /* printf */
- #include <stdlib.h> /* malloc, free */
- #include <assert.h> /* assert */
- #include <string.h>
- #include "scanner.h"
- #include "recognizeExp.h"
- #include "evalExp.h"
- #include "infixExp.h"
- int isOperatorAddSub(char c) {
- return ( c == '+' || c == '-' );
- }
- //Function that checks if an operator seperates 'expressions'.
- int valueOperatorExp(List *lp, char *cp) {
- if ( *lp != NULL && (*lp)->tt == Symbol && isOperatorAddSub(((*lp)->t).symbol) ) {
- *cp = ((*lp)->t).symbol;
- *lp = (*lp)->next;
- return 1;
- }
- return 0;
- }
- int isOperatorMulDiv(char c) {
- return ( c == '*' || c == '/' );
- }
- //Function that checks if an operator seperates 'terms'
- int valueOperatorTerm(List *lp, char *cp) {
- if (*lp != NULL && (*lp)->tt == Symbol && isOperatorMulDiv(((*lp)->t).symbol) ) {
- *cp = ((*lp)->t).symbol;
- *lp = (*lp)->next;
- return 1;
- }
- return 0;
- }
- //Main function that converts the infix expression 'List'
- //into an expression 'Tree'
- ExpTree treeExpression(List *lp) {
- char c;
- Token t;
- ExpTree tr = NULL;
- //Base case to check if the list is empty (meaning we've reached the end)
- if (*lp == NULL) {
- return NULL;
- }
- //Create a tree for the left term.
- tr = treeTerm(lp);
- //If a + or - is found, the right term is evaluated and
- //given its own treenode
- while (valueOperatorExp(lp, &c)) {
- t.symbol = c;
- tr = newExpTreeNode(Symbol, t, tr, NULL);
- tr->right = treeTerm(lp);
- }
- return tr;
- }
- ExpTree treeTerm(List *lp) {
- char c;
- Token t;
- ExpTree tr = NULL;
- if(*lp == NULL) {
- return NULL;
- }
- //identically to expressionToTree, a tree is made for the first factor
- tr = treeFactor(lp);
- while(valueOperatorTerm(lp, &c)) {
- t.symbol = c;
- tr = newExpTreeNode(Symbol, t, tr, NULL);
- tr->right = treeFactor(lp);
- }
- return tr;
- }
- ExpTree treeFactor(List *lp) {
- double w;
- char *s;
- Token t;
- ExpTree tr = NULL;
- if (*lp == NULL) {
- return NULL;
- }
- //If the factor is a number, a new tree is created
- //with this number as the root.
- if (valueNumber(lp, &w)) {
- t.number = (int)w;
- tr = newExpTreeNode(Number, t, NULL, NULL);
- return tr;
- }
- //If the factor is an identifier, the same happens
- //but instead with an identifier as the root.
- else if (valueIdentifier(lp, &s)) {
- t.identifier = s;
- tr = newExpTreeNode(Identifier, t, NULL, NULL);
- return tr;
- }
- //Neither a number nor identifier results in a new expression
- //(possibly with brackets).
- acceptCharacter(lp, '(');
- tr = treeExpression(lp);
- acceptCharacter(lp, ')');
- return tr;
- }
- void infixSolver() {
- char *ar;
- List tl, tl1, tl2;
- ExpTree t = NULL;
- printf("give an expression: ");
- ar = readInput();
- while (ar[0] != '!') {
- tl = tokenList(ar);
- printList(tl);
- tl1 = tl;
- tl2 = tl;
- //Check if the expression is valid, if not skip the solver
- //and request a new expression.
- if (acceptExpression(&tl2) && tl2 == NULL) {
- t = treeExpression(&tl1);
- printf("in infix notation: ");
- printExpTreeInfix(t);
- printf("\n");
- if (isNumerical(t)) {
- printf("the value is %g\n", valueExpTree(t));
- } else {
- printf("this is not a numerical expression\n");
- }
- } else {
- printf("this is not an expression\n");
- }
- freeExpTree(t);
- t = NULL;
- freeTokenList(tl);
- free(ar);
- printf("\ngive an expression: ");
- ar = readInput();
- }
- free(ar);
- printf("good bye\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement