-
Notifications
You must be signed in to change notification settings - Fork 4
/
exercise4-4.c
48 lines (37 loc) · 897 Bytes
/
exercise4-4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#define MAXVAL 100 /* maximum depth of val stack */
int sp = 0; /* next free stack position */
double val[MAXVAL]; /* val stack */
/* printtop: print the top value from stack */
void printtop(void)
{
if (sp > 0)
printf("The top element of the stack is %g\n", val[sp-1]);
else
printf("error: stack empty\n");
}
double dup[MAXVAL]; /* the duplicated stack
/* duplicate: duplicate the top element of the stack */
void duplicate(void)
{
if (sp > 0) {
val[sp] = val[sp-1];
sp++;
} else
printf("error: stack empty\n");
}
/* swap: swap the top two elements */
void swap(void)
{
double temp;
if (sp > 1) {
temp = val[sp-1];
val[sp-1] = val[sp-2];
val[sp-2] = temp;
} else
printf("error: stack just has less than two elements\n")
}
/* clear: clear the stack */
void clear(void)
{
sp = 0;
}