Beispiel #1
0
main()
{
	char *pc;
	char buf[256];
	double result;
	
	while(1)
	{
		printf("Enter Expression. Example: 2 3 4 + *(= (3+4)*2). ctrl + C to exit:\n");
		fgets(buf, 256, stdin);
		eval_rpn(&result, buf);
		printf("result = %f\n", result);
	}

}
Beispiel #2
0
int evaluate_RPN_expression(const char *expression)
{
  int i;

  for (i = 0; expression[i] != '\0'; i++) {
    printf("%c\n", expression[i]);
    if (expression[i]  >= '0' && expression[i] <= '9') {
      push(expression[i] - '0');
    }
    else if (expression[i] == ' ')
      continue;
    else if (expression[i] == '=') {
      return pop();
    }
    else {
      eval_rpn(expression[i]);
    }
  }
  return pop();
}
Beispiel #3
0
static double eval_rpn(int *equat) {
  int i;
  double temx, temy, temz;
  union /*  WARNING  -- ASSUMES 32 bit int  and 64 bit double  */
  {
    struct {
      int int1;
      int int2;
    } pieces;
    struct {
      double z;
    } num;
  } encoder;

  while ((i = *equat++) != ENDEXP) {
    switch (i) {
    case NUMSYM:
      encoder.pieces.int2 = *equat++;
      encoder.pieces.int1 = *equat++;
      PUSH(encoder.num.z);
      break;
    case ENDFUN:
      i = *equat++;
      uptr -= i;
      break;
    case MYIF: {
      int ijmp;

      temx = POP;
      ijmp = *equat++;
      if (temx == 0.0)
        equat += ijmp;
      break;
    }
    case MYTHEN: {
      int ijmp = *equat++;

      equat += ijmp;
      break;
    }
    case MYELSE:
      break;
    case ENDDELSHFT:
      temx = POP;
      temy = POP;
      temz = POP;
      PUSH(do_delay_shift(temx, temy, temz));
      break;
    case ENDDELAY:
      temx = POP;
      temy = POP;
      PUSH(do_delay(temx, temy));
      break;
    case ENDSHIFT:
      temx = POP;
      temy = POP;
      PUSH(do_shift(temx, temy));
      break;
    case ENDISHIFT:
      temx = POP;
      temy = POP;
      PUSH(do_ishift(temx, temy));
      break;
    case SUMSYM: {
      int high;
      int low;
      int ijmp;
      double sum;

      temx = POP;
      high = (int)temx;
      temx = POP;
      low = (int)temx;
      ijmp = *equat++;
      sum = 0.0;
      if (low <= high) {
        for (int is = low; is <= high; is++) {
          constants.elems[SumIndex] = (double)is;
          sum += eval_rpn(equat);
        }
      }
      equat += ijmp;
      PUSH(sum);
      break;
    }
    case ENDSUM:
      return (POP);
    case INDXCOM:
      PUSH(0.0);
      break;
    default: {
      int it = i / MAXTYPE;
      int in = i % MAXTYPE;
      switch (it) {
      case FUN1TYPE:
        PUSH(expr_fun1[in](POP));
        break;
      case FUN2TYPE:
        switch (in) {
        case 0:
          temx = POP;
          temy = POP;
          PUSH(temx + temy);
          break;
        case 1:
          temx = POP;
          temy = POP;
          PUSH(temy - temx);
          break;
        case 2:
          temx = POP;
          temy = POP;
          PUSH(temx * temy);
          break;
        case 3:
          temx = POP;
          if (temx == 0.0)
            temx = DOUB_EPS;
          temy = POP;
          PUSH(temy / temx);
          break;
        default:
          temx = POP;
          temy = POP;
          PUSH(expr_fun2[in](temy, temx));
          break;
        }
        break;
      case CONTYPE:
        PUSH(constants.elems[in]);
        break;
      case NETTYPE:
        PUSH(network_value(POP, in));
        break;
      case TABTYPE:
        PUSH(lookup(POP, in));
        break;
      case USTACKTYPE:
        /* ram: so this means ustacks really do need to be of USTACKTYPE */
        PUSH(ustack[uptr - 1 - in]);
        break;
      case KERTYPE:
        PUSH(ker_val(in));
        break;
      case VARTYPE:
        PUSH(variables.elems[in]);
        break;
      /* indexes for shift and delay operators... */
      case SCONTYPE:
        PUSH((double)(COM(CONTYPE, in)));
        break;
      case SVARTYPE:
        PUSH((double)(COM(VARTYPE, in)));
        break;
      case UFUNTYPE:
        i = *equat++;
        for (int j = 0; j < i; j++) {
          ustack[uptr] = POP;
          uptr++;
        }
        PUSH(eval_rpn(ufuns.elems[in].rpn));
        break;
      }
      break;
    }
    }
  }
  return (POP);
}
Beispiel #4
0
double evaluate(int *equat) {
  uptr = 0;
  stack_pointer = 0;
  return (eval_rpn(equat));
}