struct linked_list * get_stack(struct linked_list *item) { struct object *obj; char buf[2 * LINELEN]; int i = 0, j; struct linked_list *ll; mpos = 0; obj = OBJPTR(item); ll = obj->next_obj; sprintf(buf, "You are standing on top of the following items: "); add_line(buf); sprintf(buf, "%d) -- %s", i, inv_name(obj, TRUE)); add_line(buf); while (ll) { i++; obj = OBJPTR(ll); sprintf(buf, "%d) -- %s", i, inv_name(obj, TRUE)); add_line(buf); ll = next(ll); } end_line(); msg("Which one do you want to pick up? [* for all] "); switch(get_string(buf, cw)) { case NORM: break; case QUIT: /* pick up nothing */ msg(""); return (NULL); } if (buf[0] == '*') { get_all(item); msg(""); return(NULL); } else { i = atoi(buf); if (i) { obj = OBJPTR(item); ll = obj->next_obj; j = 1; while (ll && (j != i)) { ll = next(ll); j++; } if (ll) { swap_top(item, ll); return(ll); } else { debug("Got past last item while picking up."); return(item); } } else return (item); } }
/* K&R Ex4_3: Extends reverse polish calculator to add modulus operator and provisions for negative numbers */ int main() { int type, n; double op2,op1,isint; char s[MAXOP]; while((type = getop(s)) != EOF){ switch(type) { case NUMBER: push(atof(s)); break; case '+': push(pop() + pop()); break; case '*': push(pop() * pop()); break; case '-': op2 = pop(); push(pop() - op2) ; break; case '/': op2 = pop(); if (op2 != 0.0) push(pop() / op2); else printf("error: zero divisor \n"); break; case '%': op2 = pop(); op1 = pop(); isint = op2 - floor(op2) + op1 - floor(op1); if (op2 != 0.0 && isint ==0) push((int)op1 %(int)op2); else printf("illegal operands in % \n"); break; case '?': print_top(); break; case '#': duplicate(); break; case '~': swap_top(); break; case '!': clear_stack(); case '\n': printf("\t%g\n",pop()); break; default: if( (n = getmath(type))!= -1) lib_function(n); else printf("error: unknown commmand %s\n",s); break; } } return 0; }
int main() { int type; double op2; char tmp; char s[MAXOP]; while ((type = getop(s)) != EOF) { switch (type) { case NUMBER: push(atof(s)); break; case VARIABLE: push(var[s[0] - 'a']); vpush(s[0]); break; case '=': tmp = vpop(); duplicate_stack_top(); var[tmp - 'a'] = pop(); break; case '+': push(pop() + pop()); break; case '*': push(pop() * pop()); break; case '-': op2 = pop(); push(pop() - op2); break; case '/': op2 = pop(); if (op2 != 0.0) push(pop() / op2); else printf("error: zero divisor\n"); break; case '%': op2 = pop(); push((int)pop() % (int)op2); break; case 'S': push(sin(pop())); break; case 'E': push(exp(pop())); break; case 'P': op2 = pop(); push(pow(pop(),op2)); break; case '?': print_stack_top(); break; case '~': swap_top(); break; case '#': duplicate_stack_top(); break; case '@': clear(); break; case '\n': var['z' - 'a'] = pop(); printf("\t%.8g\n", var['z' - 'a']); break; default: printf("error: unknown command %s\n", s); break; } } return 0; }