-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
70 lines (62 loc) · 1.16 KB
/
Copy pathstack.c
File metadata and controls
70 lines (62 loc) · 1.16 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "calc.h"
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXVAL 100 /* maximum depth of val stack */
int sp = 0; /* next free stack position */
double val[MAXVAL]; /* value stack */
/**
* @brief Push f into the stack.
*
* @param f floating point number to push
*/
void push(double f) {
if (sp < MAXVAL)
val[sp++] = f;
else
printf("error: stack full, can't push %g\n", f);
}
/**
* @brief Pop and return the top value from the stack.
*
* @return double top value from the stack
*/
double pop(void) {
if (sp > 0)
return val[--sp];
else {
printf("error: stack empty\n");
return 0.0;
}
}
/**
* @brief print top of the stack with popping it.
*
*/
void printtop(void) {
printf("\t%.8g\n", val[sp - 1]);
}
/**
* @brief duplicate top element of the stack.
*
*/
void duplicatetop(void) {
push(val[sp - 1]);
}
/**
* @brief swap top two elements of the stack.
*
*/
void swaptoptwo(void) {
double temp = val[sp - 1];
val[sp - 1] = val[sp - 2];
val[sp - 2] = temp;
}
/**
* @brief clear the stack.
*
*/
void clearstack(void) {
sp = 0;
}