Debian Codegraph @

listings/appendix-a/calculator.c

raw
1 /***********************************************************************
2 * Code listing from "Advanced Linux Programming," by CodeSourcery LLC *
3 * Copyright (C) 2001 by New Riders Publishing *
4 * See COPYRIGHT for license information. *
5 ***********************************************************************/
6
7 /* Calculate using unary numbers. */
8
9 /* Enter one-line expressions using reverse postfix notation, e.g.,
10 602 7 5 - 3 * +
11 Nonnegative numbers are entered using decimal notation. The
12 operators "+", "-", and "*" are supported. The unary operators
13 "even" and "odd" return the number one iff its one operand is even
14 or odd, respectively. Spaces must separate all words. Negative
15 numbers are not supported. */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <ctype.h>
21 #include "definitions.h"
22
23
24 /* Apply the binary function with operands obtained from the stack,
25 pushing the answer on the stack. Return nonzero upon success. */
26
27 int apply_binary_function (number (*function) (number, number),
28 Stack* stack)
29 {
30 number operand1, operand2;
31 if (empty_stack (*stack))
32 return 0;
33 operand2 = pop_stack (stack);
34 if (empty_stack (*stack))
35 return 0;
36 operand1 = pop_stack (stack);
37 push_stack (stack, (*function) (operand1, operand2));
38 destroy_number (operand1);
39 destroy_number (operand2);
40 return 1;
41 }
42
43 /* Apply the unary function with an operand obtained from the stack,
44 pushing the answer on the stack. Return nonzero upon success. */
45
46 int apply_unary_function (number (*function) (number),
47 Stack* stack)
48 {
49 number operand;
50 if (empty_stack (*stack))
51 return 0;
52 operand = pop_stack (stack);
53 push_stack (stack, (*function) (operand));
54 destroy_number (operand);
55 return 1;
56 }
57
58 int main ()
59 {
60 char command_line[1000];
61 char* command_to_parse;
62 char* token;
63 Stack number_stack = create_stack ();
64
65 while (1) {
66 printf ("Please enter a postfix expression:\n");
67 command_to_parse = fgets (command_line, sizeof (command_line), stdin);
68 if (command_to_parse == NULL)
69 return 0;
70
71 token = strtok (command_to_parse, " \t\n");
72 command_to_parse = 0;
73 while (token != 0) {
74 if (isdigit (token[0]))
75 push_stack (&number_stack, string_to_number (token));
76 else if (((strcmp (token, "+") == 0) &&
77 !apply_binary_function (&add, &number_stack)) ||
78 ((strcmp (token, "-") == 0) &&
79 !apply_binary_function (&subtract, &number_stack)) ||
80 ((strcmp (token, "*") == 0) &&
81 !apply_binary_function (&product, &number_stack)) ||
82 ((strcmp (token, "even") == 0) &&
83 !apply_unary_function (&even, &number_stack)) ||
84 ((strcmp (token, "odd") == 0) &&
85 !apply_unary_function (&odd, &number_stack)))
86 return 1;
87 token = strtok (command_to_parse, " \t\n");
88 }
89 if (empty_stack (number_stack))
90 return 1;
91 else {
92 number answer = pop_stack (&number_stack);
93 printf ("%u\n", number_to_unsigned_int (answer));
94 destroy_number (answer);
95 clear_stack (&number_stack);
96 }
97 }
98
99 return 0;
100 }