diff --git a/k&r/04-funcs-and-prog-struct/04-calc.c b/k&r/04-funcs-and-prog-struct/04-calc.c index 839595d..48dac3f 100644 --- a/k&r/04-funcs-and-prog-struct/04-calc.c +++ b/k&r/04-funcs-and-prog-struct/04-calc.c @@ -23,7 +23,7 @@ #define POP_PRINT_F "pop -> %.8g\n" #define PEEK_PRINT_F "peek -> %.8g\n" #define INITIAL_PROMPT_F \ - "A simple calculator using polish notation.\n" \ + "A simple polish notation calculator.\n" \ "Type 'help' for a list of available commands\n" \ PROMPT_F @@ -38,7 +38,7 @@ double val[MAXVAL]; #define length() sp /* push: push f onto value stack */ #define push(x) \ - ((sp < MAXVAL) ? val[sp++] = x : printerr(FULL_STACK_ERR, (double) (x))) + ((sp < MAXVAL) ? val[sp++] = x : printerr(FULL_STACK_ERR, (double) (x))) /* pop: pop and return top value from stack */ double pop(void) { @@ -114,8 +114,8 @@ void print_vars(void) /* commutative operator */ #define COMM_OP(OP) push(pop() OP pop()) /* non-commutative operator */ -#define NONC_OP(OP, T, ...) \ - do { T op = pop(); __VA_ARGS__ push(((T) pop()) OP op); } while (0) +#define NONC_OP(OP, ...) \ + do { double op = pop(); __VA_ARGS__ push(pop() OP op); } while (0) /* unary function */ #define UNARY_FUN(F) push(F(pop())) /* unary function with tests */ @@ -124,9 +124,10 @@ void print_vars(void) /* commutative function */ #define COMM_FUN(F) push(F(pop(), pop())) /* non-commutative function */ -#define NONC_FUN(F, T, ...) \ - do { T op = pop(); __VA_ARGS__ push(F(pop(), op)); } while (0) +#define NONC_FUN(F, ...) \ + do { double op = pop(); __VA_ARGS__ push(F(pop(), op)); } while (0) +/* OPERAND TESTS */ /* nonzero operand */ #define NONZ_SND_OP if (!op) printerr(ZERODIV_ERR); else /* operator in range */ @@ -149,31 +150,33 @@ void print_vars(void) X(help, "print all available commands", COMMAND) \ X(vars, "print defined variables and their values", CALL, print_vars, COMMAND) +#define MOD(x, y) ((int) (x) % (int) (y)) + #define OPERATORS \ X(+, "add operands", COMM_OP) \ X(*, "multiply operands", COMM_OP) \ - X(-, "substact operands", NONC_OP, double) \ - X(/, "divide operands", NONC_OP, double, NONZ_SND_OP) \ - X(%, "modulo operands", NONC_OP, int, NONZ_SND_OP) \ - X(>, "greater than", NONC_OP, double) \ - X(<, "less than", NONC_OP, double) \ - X(<=, "less than or equal", NONC_OP, double) \ - X(>=, "greater than or equal", NONC_OP, double) \ + X(-, "substact operands", NONC_OP) \ + X(/, "divide operands", NONC_OP, NONZ_SND_OP) \ + X(%, "apply modulo to operands", CALL, MOD, NONC_FUN, NONZ_SND_OP) \ + X(>, "greater than", NONC_OP) \ + X(<, "less than", NONC_OP) \ + X(<=, "less than or equal", NONC_OP) \ + X(>=, "greater than or equal", NONC_OP) \ X(==, "test equality", COMM_OP) #define MATH_H_BINDINGS \ X(sin, "sine", UNARY_FUN) \ X(cos, "cosine", UNARY_FUN) \ X(tan, "tangent", UNARY_FUN) \ - X(acos, "arc cosine", UNARY_T_FUN, double, IN_RANGE(-1, 1)) \ - X(asin, "arc sine", UNARY_T_FUN, double, IN_RANGE(-1, 1)) \ + X(acos, "arc cosine", UNARY_T_FUN, IN_RANGE(-1, 1)) \ + X(asin, "arc sine", UNARY_T_FUN, IN_RANGE(-1, 1)) \ X(atan, "arc tangent", UNARY_FUN) \ X(exp, "exponentional function", UNARY_FUN) \ - X(log, "natural logarithm", UNARY_T_FUN, double, NON_NEG) \ - X(log10, "logarithm of base 10", UNARY_T_FUN, double, NON_NEG) \ - X(log2, "logarithm of base 2", UNARY_T_FUN, double, NON_NEG) \ - X(pow, "power function", NONC_FUN, double) \ - X(sqrt, "square root", UNARY_T_FUN, double, NON_NEG) \ + X(log, "natural logarithm", UNARY_T_FUN, NON_NEG) \ + X(log10, "logarithm of base 10", UNARY_T_FUN, NON_NEG) \ + X(log2, "logarithm of base 2", UNARY_T_FUN, NON_NEG) \ + X(pow, "power function", NONC_FUN) \ + X(sqrt, "square root", UNARY_T_FUN, NON_NEG) \ X(ceil, "round up value", UNARY_FUN) \ X(floor, "round down value", UNARY_FUN) \ X(round, "round to nearest", UNARY_FUN) \