Skip to content

Commit 536dbcc

Browse files
committed
improve algorithm
1 parent e5c08ef commit 536dbcc

File tree

4 files changed

+133
-138
lines changed

4 files changed

+133
-138
lines changed

chapter04/4-3.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ int getch(void);
2121
void ungetch(int);
2222

2323
/* globals */
24-
int sp = 0; /* next free stack position */
24+
int sp; /* next free stack position */
2525
double val[MAXVAL]; /* value stack */
2626
char buf[BUFSIZE]; /* buffer from ungetch */
27-
int bufp = 0; /* next free position in buf */
27+
int bufp; /* next free position in buf */
2828

2929
/* push: push f onto value stack */
3030
void push(double f)
@@ -56,19 +56,19 @@ int getop(char s[])
5656
s[1] = '\0';
5757

5858
i = 0;
59-
if (c == '-') /* check sign */
59+
if (c == '-') /* check sign */
6060
if (!isdigit(s[++i] = c = getch())) {
6161
ungetch(c);
62-
c = s[0]; /* not a sign */
62+
c = s[0]; /* not a sign */
6363
}
6464

6565
if (!isdigit(c) && c != '.')
66-
return c; /* not a number */
66+
return c; /* not a number */
6767

6868
if (isdigit(c))
6969
while (isdigit(s[++i] = c = getch()))
7070
;
71-
if( c == '.') /* collect fraction part */
71+
if( c == '.') /* collect fraction part */
7272
while (isdigit(s[++i] = c = getch()))
7373
;
7474
s[i] = '\0';

chapter04/4-4.c

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#define MAXOP 100 /* max size of operand or operator */
1313
#define NUMBER '0' /* signal that a number was found */
14-
#define MAXVAL 100
14+
#define MAXVAL 100 /* max depth of val stack */
1515
#define BUFSIZE 100
1616
#define TOP val[sp - 1] /* top element in stack */
1717

@@ -21,7 +21,7 @@ void push(double);
2121
double pop(void);
2222
int getch(void);
2323
void ungetch(int);
24-
void printTop(void);
24+
void printTOP(void);
2525
void duplicateTop(void);
2626
void swapTopTwo(void);
2727
void clearStack(void);
@@ -31,7 +31,7 @@ int sp; /* next free stack position */
3131
double val[MAXVAL]; /* value stack */
3232
char buf[BUFSIZE]; /* buffer from ungetch */
3333
int bufp; /* next free position in buf */
34-
int stackcmd; /* stack commands flag */
34+
int peak; /* flag to peak at top of the stack */
3535

3636
/* push: push f onto value stack */
3737
void push(double f)
@@ -63,19 +63,19 @@ int getop(char s[])
6363
s[1] = '\0';
6464

6565
i = 0;
66-
if (c == '-') /* check sign */
66+
if (c == '-') /* check sign */
6767
if (!isdigit(s[++i] = c = getch())) {
6868
ungetch(c);
69-
c = s[0]; /* not a sign */
69+
c = s[0]; /* not a sign */
7070
}
7171

7272
if (!isdigit(c) && c != '.')
73-
return c; /* not a number */
73+
return c; /* not a number */
7474

7575
if (isdigit(c))
7676
while (isdigit(s[++i] = c = getch()))
7777
;
78-
if( c == '.') /* collect fraction part */
78+
if( c == '.') /* collect fraction part */
7979
while (isdigit(s[++i] = c = getch()))
8080
;
8181
s[i] = '\0';
@@ -100,44 +100,47 @@ void ungetch(int c)
100100
buf[bufp++] = c;
101101
}
102102

103-
/* printTop: prints the top element in the stack */
104-
void printTop(void)
103+
/* printTOP: print top of the stack without pop */
104+
void printTOP(void)
105105
{
106-
if (sp > 0) {
107-
printf("\t%.8g\n", TOP);
108-
stackcmd = 1;
109-
}
106+
if (sp < 1)
107+
printf("stack empty\n");
108+
printf("\t%.8g\n", TOP);
110109
}
111110

112-
/* deleteTop: deletes the top element in the stack */
111+
/* duplicateTop: duplicate the top element in the stack */
113112
void duplicateTop(void)
114113
{
115-
if (sp > 0) {
116-
push(TOP);
117-
printTop();
118-
}
114+
double top;
115+
116+
if (sp < 1)
117+
return;
118+
119+
push(top = pop());
120+
push(top);
119121
}
120122

121123
/* swapTopTwo: swaps top two elements */
122124
void swapTopTwo(void)
123125
{
124126
double top1, top2;
125127

126-
if (sp > 1) {
127-
top1 = pop();
128-
top2 = pop();
129-
push(top1);
130-
push(top2);
131-
printTop();
128+
if (sp < 2) {
129+
if (sp == 1)
130+
printf("error: 1 element in stack\n");
131+
return;
132132
}
133+
top1 = pop();
134+
top2 = pop();
135+
push(top1);
136+
push(top2);
133137
}
134138

135139
/* clear: clears the entire stack */
136140
void clearStack(void)
137141
{
138-
while (sp > 0)
142+
while (sp > 1)
139143
pop();
140-
printTop();
141144
}
142145

143146
/* reverse Polish Calculator */
@@ -177,7 +180,7 @@ int main(void)
177180
printf("error: zero divisor\n");
178181
break;
179182
case '!':
180-
printTop();
183+
peak = 1;
181184
break;
182185
case '#':
183186
duplicateTop();
@@ -189,9 +192,11 @@ int main(void)
189192
clearStack();
190193
break;
191194
case '\n':
192-
if (!stackcmd)
195+
if (peak) {
196+
printTOP();
197+
peak = 0;
198+
} else
193199
printf("\t%.8g\n", pop());
194-
stackcmd = 0;
195200
break;
196201
default:
197202
printf("error: unknown command %s\n", s);

chapter04/4-5.c

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@
55
*/
66

77
#include <stdio.h>
8-
#include <stdlib.h> /* for atof() */
8+
#include <stdlib.h> /* for atof() */
99
#include <ctype.h>
10-
#include <string.h> /* for strcmp() */
11-
#include <math.h> /* for math commands */
10+
#include <string.h> /* for strcmp() */
11+
#include <math.h> /* for math commands */
1212

13-
#define MAXOP 100 /* max size of operand or operator */
14-
#define NUMBER '0' /* signal that a number was found */
15-
#define MATH '1' /* signal that an operation was found */
16-
#define MAXVAL 100 /* maximum depth of val stack */
17-
#define BUFSIZE 100
18-
#define TOP val[sp - 1] /* top element in stack */
13+
#define MAXOP 100 /* max size of operand or operator */
14+
#define NUMBER '0' /* signal that a number was found */
15+
#define NAME '1' /* signal that a string command was found */
16+
#define MAXVAL 100 /* maximum depth of val stack */
17+
#define BUFSIZE 100
18+
#define TOP val[sp - 1] /* top element in stack */
1919

2020
/* functions */
2121
int getop(char []);
2222
void push(double);
2323
double pop(void);
2424
int getch(void);
2525
void ungetch(int);
26-
void printTop(void);
26+
void printTOP(void);
2727
void duplicateTop(void);
2828
void swapTopTwo(void);
2929
void clearStack(void);
@@ -34,7 +34,7 @@ int sp; /* next free stack position */
3434
double val[MAXVAL]; /* value stack */
3535
char buf[BUFSIZE]; /* buffer from ungetch */
3636
int bufp; /* next free position in buf */
37-
int stackcmd; /* stack commands flag */
37+
int peak; /* flag: peak at top of the stack */
3838

3939
/* push: push f onto value stack */
4040
void push(double f)
@@ -66,27 +66,27 @@ int getop(char s[])
6666
s[1] = '\0';
6767

6868
i = 0;
69-
if (c == '-') /* check sign */
69+
if (c == '-') /* check sign */
7070
if (!isdigit(s[++i] = c = getch())) {
7171
ungetch(c);
72-
c = s[0]; /* not a sign */
72+
c = s[0]; /* not a sign */
7373
}
7474

75-
if (isalpha(c)) { /* math functions */
75+
if (isalpha(c)) { /* string command */
7676
while (isalpha(s[++i] = c = getch()))
7777
;
7878
s[i] = '\0';
7979
ungetch(c);
80-
return MATH;
80+
return NAME;
8181
}
8282

8383
if (!isdigit(c) && c != '.')
84-
return c; /* not a number */
84+
return c; /* not a number */
8585

8686
if (isdigit(c))
8787
while (isdigit(s[++i] = c = getch()))
8888
;
89-
if( c == '.') /* collect fraction part */
89+
if( c == '.') /* collect fraction part */
9090
while (isdigit(s[++i] = c = getch()))
9191
;
9292
s[i] = '\0';
@@ -111,60 +111,64 @@ void ungetch(int c)
111111
buf[bufp++] = c;
112112
}
113113

114-
/* printTop: prints the top element in the stack */
115-
void printTop(void)
114+
/* printTOP: print top of the stack without pop */
115+
void printTOP(void)
116116
{
117-
if (sp > 0) {
118-
printf("\t%.8g\n", TOP);
119-
stackcmd = 1;
120-
}
117+
if (sp < 1)
118+
printf("stack empty\n");
119+
printf("\t%.8g\n", TOP);
121120
}
122121

123-
/* deleteTop: deletes the top element in the stack */
122+
/* duplicateTop: duplicate the top element in the stack */
124123
void duplicateTop(void)
125124
{
126-
if (sp > 0) {
127-
push(TOP);
128-
printTop();
129-
}
125+
double top;
126+
127+
if (sp < 1)
128+
return;
129+
130+
push(top = pop());
131+
push(top);
130132
}
131133

132134
/* swapTopTwo: swaps top two elements */
133135
void swapTopTwo(void)
134136
{
135137
double top1, top2;
136138

137-
if (sp > 1) {
138-
top1 = pop();
139-
top2 = pop();
140-
push(top1);
141-
push(top2);
142-
printTop();
139+
if (sp < 2) {
140+
if (sp == 1)
141+
printf("error: 1 element in stack\n");
142+
return;
143143
}
144+
top1 = pop();
145+
top2 = pop();
146+
push(top1);
147+
push(top2);
144148
}
145149

146150
/* clear: clears the entire stack */
147151
void clearStack(void)
148152
{
149-
while (sp > 0)
153+
while (sp > 1)
150154
pop();
151-
printTop();
152155
}
153156

154-
/* mathf: call the appropriate math function according to value of s */
157+
/* mathfunction: call the appropriate math function according to value of s,
158+
* return 1 on success 0 on failure. */
155159
int mathfunction(char s[])
156160
{
157161
double op2;
158162

159-
if (strcmp(s, "sin") == 0)
163+
if (!strcmp(s, "sin"))
160164
push(sin(pop()));
161-
else if (strcmp(s, "cos") == 0)
165+
else if (!strcmp(s, "cos"))
162166
push(cos(pop()));
163-
else if (strcmp(s, "exp") == 0)
167+
else if (!strcmp(s, "exp"))
164168
push(exp(pop()));
165-
else if (strcmp(s, "sqrt") == 0)
169+
else if (!strcmp(s, "sqrt"))
166170
push(sqrt(pop()));
167-
else if (strcmp(s, "pow") == 0) {
171+
else if (!strcmp(s, "pow")) {
168172
op2 = pop();
169173
push(pow(pop(), op2));
170174
} else
@@ -184,6 +188,10 @@ int main(void)
184188
case NUMBER:
185189
push(atof(s));
186190
break;
191+
case NAME:
192+
if (!mathfunction(s))
193+
printf("error: unkown command %s\n", s);
194+
break;
187195
case '+':
188196
push(pop() + pop());
189197
break;
@@ -209,7 +217,7 @@ int main(void)
209217
printf("error: zero divisor\n");
210218
break;
211219
case '!':
212-
printTop();
220+
peak = 1; /* don't pop */
213221
break;
214222
case '#':
215223
duplicateTop();
@@ -221,13 +229,11 @@ int main(void)
221229
clearStack();
222230
break;
223231
case '\n':
224-
if (!stackcmd)
232+
if (peak) {
233+
printTOP();
234+
peak = 0;
235+
} else
225236
printf("\t%.8g\n", pop());
226-
stackcmd = 0;
227-
break;
228-
case MATH:
229-
if (!mathfunction(s))
230-
printf("error: unknown command %s\n", s);
231237
break;
232238
default:
233239
printf("error: unknown command %s\n", s);

0 commit comments

Comments
 (0)