"Fossies" - the Fresh Open Source Software Archive 
Member "hevea-2.35/examples/hp.c" (16 Jan 2021, 2161 Bytes) of package /linux/www/hevea-2.35.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 typedef struct cell {
5 int val;
6 struct cell *next;
7 } Cell, *Stack;
8
9 Stack stack = NULL;
10
11 Cell *cons(int val,Cell *next)
12 {
13 Cell *r;
14
15 r = (Cell *)malloc(sizeof(Cell));
16 if ( r == NULL) {
17 fprintf(stderr,"Plus de memoire\n");
18 exit(-1);
19 }
20 r->val = val;
21 r->next = next;
22 return r;
23 }
24
25 void afficher(void)
26 {
27 Stack p;
28
29 printf("[");
30 for (p=stack ; p != NULL ; p = p->next) {
31 printf("%d",p->val);
32 if (p->next != NULL)
33 printf(", ");
34 }
35 printf("]");
36 }
37
38 Tree pop(void)
39 {
40 Tree r;
41 Stack tmp;
42
43 if (stack == NULL) {
44 fprintf(stderr,"Tentative de depiler une pile vide, adieu\n");
45 exit(-1);
46 }
47 r = stack->val;
48 tmp = stack;
49 stack = stack->next;
50 free(tmp);
51 return r;
52 }
53
54
55 void push(i)
56 {
57 stack = cons(i,stack);
58 }
59
60 void swap(void)
61 {
62 int t;
63
64 if (stack == NULL || stack->next == NULL)
65 return;
66 t = stack->val;
67 stack->val = stack->next->val;
68 stack->next->val = t;
69 return;
70 }
71
72 void down(void)
73 {
74 Stack p,q;
75
76 if (stack == NULL)
77 return;
78 for (p = stack ; p->next->next != NULL ; p = p->next)
79 ;
80 q = p->next;
81 p->next = NULL;
82 q->next = stack;
83 stack = q;
84 }
85
86 void up(void)
87 {
88 Stack p,q;
89
90 if (stack == NULL || stack->next == NULL)
91 return;
92 for (p = stack ; p->next != NULL ; p = p->next)
93 ;
94 q = stack;
95 stack = stack->next;
96 q->next = NULL;
97 p->next = q;
98 }
99
100 void main(void)
101 {
102 char pgm[] = "4321ud";
103 char c;
104 int i;
105 int r1,r2;
106
107 printf(" ");
108 afficher();
109 printf("\n");
110 for(i=0; pgm[i] != '\0'; i++) {
111 c = pgm[i];
112 switch (c) {
113 case '+':
114 r1 = pop();
115 r2 = pop();
116 push(r2+r1);
117 break;
118 case '-':
119 r1 = pop();
120 r2 = pop();
121 push(r2-r1);
122 break;
123 case '*':
124 r1 = pop();
125 r2 = pop();
126 push(r2*r1);
127 break;
128 case '/':
129 r1 = pop();
130 r2 = pop();
131 push(r2/r1);
132 break;
133 case 's':
134 swap();
135 break;
136 case 'd':
137 down();
138 break;
139 case 'u':
140 up();
141 break;
142 default:
143 if ('0' <= c && c <= '9')
144 push(c - '0');
145 }
146 printf("%c : ",c);
147 afficher();
148 printf("\n");
149 }
150 }