Ejemplo n.º 1
0
int main (int argc, char **argv)
{

 char *input;
 token inputToken;

 //DEBUGGING MODE
    int debuggingMode = 0;
    int i;
    for(i = 1; i < argc; i++) {
        if(argv[i] != NULL) {
            if(strcmp(argv[1], "-d\n")) {
                debuggingMode = 1;
            }
        }
    }
    //DEBUGGING MODE

 printf ("Starting Expression Evaluation Program\n\n");
 printf ("Enter Expression: ");

 inputToken = getInputToken (stdin);
 while (inputToken.type != QUIT)
   {
    /* check first Token on Line of input */
    if(inputToken.type == HELP)
      {
       printCommands();
       clearToEoln(stdin);
      }
    else if(inputToken.type == ERROR)
      {
       printf ("Invalid Input - For a list of valid commands, type ?\n");
       clearToEoln(stdin);
      }
    else if(inputToken.type == EOLN)
      {
       printf ("Blank Line - Do Nothing\n");
       /* blank line - do nothing */
      }
    else
      {
       processExpression(inputToken, stdin, debuggingMode);
      }

    printf ("\nEnter Expression: ");
    inputToken = getInputToken (stdin);
   }

 printf ("Quitting Program\n");
 return 1;
}
Ejemplo n.º 2
0
int main (int argc, char **argv)
{

  char *input;
  token inputToken;

// ************START ADDITION
  int debug = 0;
  gen_parse_args( argc, argv, &debug);
// ************END ADDITON


  printf ("Starting Expression Evaluation Program\n\n");
  printf ("Enter Expression: ");

  inputToken = getInputToken (stdin);
  while (inputToken.type != QUIT)
  {
    /* check first Token on Line of input */
    if(inputToken.type == HELP)
    {
      printCommands();
      clearToEoln(stdin);
    }
    else if(inputToken.type == ERROR)
    {
      printf ("Invalid Input - For a list of valid commands, type ?\n");
      clearToEoln(stdin);
    }
    else if(inputToken.type == EOLN)
    {
      printf ("Blank Line - Do Nothing\n");
      /* blank line - do nothing */
    }
    else 
    {
      processExpression(inputToken, stdin, debug);
    } 

    printf ("\nEnter Expression: ");
    inputToken = getInputToken (stdin);
  }

  printf ("Quitting Program\n");
  return 1;
}
Ejemplo n.º 3
0
void processExpression (token inputToken, FILE *in, int debugMode) {

    /**********************************************/
    /* Declare both stack head pointers here      */
    LIST *opList = lst_create();
    LIST *valList = lst_create();

    /* Loop until the expression reaches its End */
    while (inputToken.type != EOLN) {

        /* The expression contains an OPERATOR */
        if (inputToken.type == OPERATOR) {

            //DEBUGGING
            if(debugMode) {
                printf ("OP:%c, " ,inputToken.op);
            }
            //DEBUGGING

            if(inputToken.op == '(') {
                push(opList, inputToken.op);
            }

            if(inputToken.op == '+' || inputToken.op == '-') {
                while(!lst_is_empty(opList) && top(opList) == '+' ||  top(opList) == '-' || top(opList) == '*' || top(opList) == '/') {
                    pop_eval(opList, valList);
                }
                push(opList, inputToken.op);
            }

            if(inputToken.op == '*' || inputToken.op == '/') {
                while(!lst_is_empty(opList) && top(opList) == '+' ||  top(opList) == '-' || top(opList) == '*' || top(opList) == '/') {
                    pop_eval(opList, valList);
                }
                push(opList, inputToken.op);
            }

            if(inputToken.op == ')') {
                while(!lst_is_empty(opList) && top(opList) != '(') {
                    pop_eval(opList, valList);
                }
                if(lst_is_empty(opList)) {
                    printf("\nERROR. OP Stack EMPTY!\n");
                } else {
                    pop(opList);
                }
            }

        } else if (inputToken.type == VALUE) {

            //DEBUGGING
            if(debugMode) {
                printf ("Val: %d, ", inputToken.val);
            }
            //DEBUGGING

            push(valList, inputToken.val);

        }

        /* get next token from input */
        inputToken = getInputToken (in);

    }

    /* The expression has reached its end */

    // add code to perform this operation here

    while(!lst_is_empty(opList)) {
        pop_eval(opList, valList);
    }

    printf("\nRESULT: %d", top(valList));
    pop(valList);

    if(!lst_is_empty(valList)) {
        printf("\nERROR: ValueStack is not EMPTY %d\n", top(valList));
    }

    printf ("\n");

}
Ejemplo n.º 4
0
void processExpression (token inputToken, FILE *in, int d)
{
  /**********************************************/
  /* Declare both stack head pointers here      */
  Stack ops = stack_init( ops);
  Stack nums = stack_init( nums);
  /* Loop until the expression reaches its End */
  while (inputToken.type != EOLN)
  {
    /* The expression contains an OPERATOR */
    if (inputToken.type == OPERATOR)
    {
      /* make this a debugMode statement */
      if( d) printf ("OP:%c, " ,inputToken.op);
      // add code to perform this operation here
      //if the op is a open paren
      if( inputToken.op == '(' )
        stack_push_ch( ops, inputToken.op);
      //if the op is + or -
      else if( inputToken.op == '+' || inputToken.op == '-'){
        while( !stack_isEmpty(ops) && (stack_peek_ch(ops) == '+' 
                                  ||  stack_peek_ch(ops) == '-'
                                  ||  stack_peek_ch(ops) == '/'
                                  ||  stack_peek_ch(ops) == '*') ){
          stack_pop_eval_push( ops, nums);
        }
        stack_push_ch( ops,inputToken.op);
      }
      //if the op is * or /
      else if( inputToken.op == '*' || inputToken.op == '/'){
        while( !stack_isEmpty(ops) && (stack_peek_ch(ops) == '/' 
                                  ||  stack_peek_ch(ops) == '*') ){
          stack_pop_eval_push( ops, nums);
        }
        stack_push_ch( ops,inputToken.op);
      }
      //if the op is a closing paren
      else if( inputToken.op == ')' ){
        while( !stack_isEmpty(ops) && stack_peek_ch(ops) != '(' ){
          stack_pop_eval_push( ops, nums);
        }
        if( stack_isEmpty( ops)) printf("ERROR, no closing parenthesis!\n");
        else stack_pop_ch( ops); //pop the open parenthesis
      }
    }

    /* The expression contain a VALUE */
    else if (inputToken.type == VALUE)
    {
      /* make this a debugMode statement */
      if(d) printf ("Val: %d, ", inputToken.val);
      // add code to perform this operation here
      stack_push_i( nums, inputToken.val);
    }

    /* get next token from input */
    inputToken = getInputToken (in);
  } 

  /* The expression has reached its end */
  // add code to perform this operation here
  while( !stack_isEmpty( ops) ){
    stack_pop_eval_push( ops, nums);
  }
  printf("The top of the val stack: %d\n", stack_peek_i( nums) );
  stack_pop_i( nums);
  if( !stack_isEmpty( nums) ) printf("ERROR, leftover nums on num stack\n");
  
  stack_free( ops);
  stack_free( nums);
  printf ("\n");
}