예제 #1
0
int arg(const char *from, const char *to, void *aux)
{
  stack_t *stk = aux;
  int val;
  val = atoi(from);
  calc_op(stk,val);
  printf("%d",val);
  return 0;
}
예제 #2
0
int close(const char *from, const char *to, void *aux)
{
  stack_t *stk = aux;
  int val;
  val = stack_top(stk).val;
  stack_drop(stk);
  calc_op(stk,val);
  printf(")");
  return 0;
}
예제 #3
0
파일: 4-4.c 프로젝트: markmontymark/knr-c
void impl( )
{

	stk_t * k = stk_new();	

	char cur;
	int lhs;
	int rhs;
	int val;

	int total = 0;

	while( (cur = fgetc(stdin)) != EOF )
	{
		if(isdigit(cur))
		{
			int digits[MAXDIGITS];
			int n_digits = 0;
			digits[ n_digits++ ] = cur - '0';
			while( (cur = fgetc(stdin)) != EOF && isdigit(cur) && n_digits < MAXDIGITS)
				digits[ n_digits++ ] = cur - '0';
			int val =  0;
			for(int i =  0, j = n_digits - 1; i < n_digits; i ++, j-- )
				val += digits[i] * pow(10, j);
			stk_push(k, val);
		}

		switch(cur)
		{
			// handle operators
			case '%': 
			case '+': 
			case '-': 
			case '*': 
			case '/':
				//printf("got op, %c\n",cur);
				if(! stk_is_empty(k)	)
					if( (lhs = stk_pop(k)) == -1 )
					{
						printf("Bad lhs pop\n");
						return;
					}
				if(! stk_is_empty(k)	)
				{
					if( (rhs = stk_pop(k)) == -1 )
					{
						printf("Bad rhs pop\n");
						return;
					}
					fprintf(stdout, "lhs %d, rhs %d, op %c\n",lhs,rhs,cur);
					stk_push(k, calc_op(cur, lhs, rhs));
				}
				else 
				{
					stk_push(k,lhs);
					stk_push(k,cur);
				}
				break;

			// clear stack with _ underscore
			case '_': 
				calc_clear(k); 
				break;
			case '.': 
				calc_print_top(k,stdout); 
				break;

			case '>': 
				calc_swap_top(k); 
				break;
		}
	
	}
	stk_dump(k,stdout);
	stk_free(k);
}