-
Notifications
You must be signed in to change notification settings - Fork 1
/
hp.c
173 lines (143 loc) · 2.51 KB
/
hp.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* hp --- reverse Polish notation calculator program */
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "hp.h" /* get declarations */
#define MAXSTACK 100
#define TRUE 1
#define FALSE 0
double stack[MAXSTACK + 1];
int scanptr = -1;
int sp = 0;
char line[BUFSIZ*4];
int
main(int argc, char **argv)
{
int i, l;
if (argc > 1) {
l = 0;
/* concatenate args into one line for input() to use... */
for (i = 1; argv[i] != NULL; i++) {
(void) strcpy(& line[l], argv[i]);
l += strlen(argv[i]);
line[l] = ' ';
l++;
}
line[l] = '\0';
scanptr = -1;
yyparse();
if (sound(1)) {
if(stack[sp] > .01)
printf("%1.3f\n", stack[sp]);
else
printf("%.3e\n",stack[sp]);
}
} else
while (fgets(line, sizeof line, stdin) != NULL) {
scanptr = -1;
yyparse();
}
exit(0);
}
/* ctod --- do atof, but increment pointer into the line buffer */
double
ctod(char text[], int *indx)
{
double result, atof();
char buf[BUFSIZ];
int i = 0;
while (isdigit(text[*indx]) || text[*indx] == ',') {
/* skip commas */
if (text[*indx] != ',')
buf[i++] = text[*indx];
(*indx)++;
}
if (text[*indx] == '.') {
buf[i++] = '.';
(*indx)++;
}
while (isdigit(text[*indx])) {
buf[i++] = text[*indx];
(*indx)++;
}
if (text[*indx] == 'e' || text[*indx] == 'E') {
buf[i++] = text[*indx];
(*indx)++;
if (text[*indx] == '-' || text[*indx] == '+') {
buf[i++] = text[*indx];
(*indx)++;
}
while (isdigit(text[*indx])) {
buf[i++] = text[*indx];
(*indx)++;
}
}
buf[i] = '\0';
result = atof(buf);
return result;
}
/* fmod --- do floating modulus */
double
fmod(double x, double y)
{
extern double modf();
extern double fabs();
double f;
int flag = 0;
double ipart;
if (y == 0.0)
return x;
flag = (x < 0.0);
x = fabs(x);
y = fabs(y);
(void) modf(x / y, & ipart);
f = x - y * ipart;
return (flag ? -f : f);
}
/* push --- push one item onto the stack */
void
push(double stuff)
{
if (sp > MAXSTACK)
fprintf(stderr, "stack overflow\n");
else {
sp++;
stack[sp] = stuff;
}
}
/* sound --- sound out the depth of the stack */
int
sound(int depth)
{
if (sp < depth) {
fprintf(stderr, "stack underflow\n");
return FALSE;
} else
return TRUE;
}
int
input(void)
{
register int c;
again:
scanptr++;
if ((c = line[scanptr]) == '\0') {
scanptr = -1;
return 0;
} else if (isspace(c))
goto again;
else {
return c;
}
}
void
unput(int c)
{
line[--scanptr] = c;
}
void
yyerror(char *s)
{
fprintf(stderr, "hp: %s\n", s);
}