Skip to content

Commit 68e51e4

Browse files
committed
review comments in exercises.
1 parent 8a70dcf commit 68e51e4

File tree

38 files changed

+265
-461
lines changed

38 files changed

+265
-461
lines changed

Chapter 04/4-03/main.c

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ double val[MAXVAL];
2525
int sp = 0;
2626
int bufp = 0;
2727

28+
/* reverse polish calculator */
2829
int main() {
2930
int type;
3031
double op2;

Chapter 04/4-04/main.c

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ double stack[MAXVAL];
2929
int sp = 0; /* next free stack position */
3030
int bufp = 0;
3131

32+
/* reverse polish calculator */
3233
int main() {
3334
int type;
3435
double op1, op2;

Chapter 04/4-05/main.c

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ double stack[MAXVAL];
2727
int sp = 0; /* next free stack position */
2828
int bufp = 0;
2929

30+
/* reverse polish calculator */
3031
int main() {
3132
int type;
3233
double op2;

Chapter 04/4-06/main.c

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ double stack[MAXVAL];
2424
int sp = 0; /* next free stack position */
2525
int bufp = 0;
2626

27+
/* reverse polish calculator */
2728
int main() {
2829
int type, i, var;
2930
double op2, v, variable[26];

Chapter 04/4-07/main.c

+22-129
Original file line numberDiff line numberDiff line change
@@ -5,160 +5,53 @@
55
**/
66

77
#include <stdio.h>
8-
#include <stdlib.h>
9-
#include <ctype.h>
108
#include <string.h>
119

12-
#define MAXOP 100 /* max size of operand or operator */
13-
#define MAXVAL 100 /* maximum depth of val stack */
14-
#define NUMBER '0' /* signal that a number was found */
15-
#define BUFSIZE 100 /* buffer for ungetch */
16-
17-
int getop(char []);
18-
int getch(void);
19-
void ungetch(int);
20-
void push(double);
21-
double pop(void);
22-
void ungets(char []);
10+
#define BUFSIZE 100
2311

2412
char buf[BUFSIZE];
25-
double stack[MAXVAL];
26-
int sp = 0; /* next free stack position */
2713
int bufp = 0;
2814

29-
int main() {
30-
int type, i, var;
31-
double op2, v, variable[26];
32-
char s[MAXOP];
33-
34-
var = 0;
15+
void ungets(char *);
16+
int getch(void);
17+
void ungetch(int c);
3518

36-
while ((type = getop(s)) != EOF){
37-
switch(type){
38-
case NUMBER:
39-
push(atof(s));
40-
break;
41-
case '+':
42-
push(pop() + pop());
43-
break;
44-
case '*':
45-
push(pop() * pop());
46-
break;
47-
case '-':
48-
op2 = pop();
49-
push(pop() - op2);
50-
break;
51-
case '/':
52-
op2 = pop();
53-
if(op2 != 0){
54-
push(pop() / op2);
55-
} else {
56-
printf("error: zero divisor.\n");
57-
}
58-
break;
59-
case '=':
60-
pop();
61-
if(var >= 'A' && var <= 'Z') {
62-
variable[var - 'A'] = pop();
63-
} else {
64-
printf("error: no variable name.\n");
65-
}
66-
break;
67-
case '\n':
68-
v = pop();
69-
printf("\t%.8g\n", pop());
70-
break;
71-
default:
72-
if(type >= 'A' && type <= 'Z'){
73-
push(variable[type - 'A']);
74-
} else if (type == 'v') {
75-
push(v);
76-
} else {
77-
printf("error: unknown command %s\n", s);
78-
}
79-
break;
80-
}
81-
var = type;
82-
}
19+
int main(void) {
20+
int c;
8321

84-
return 0;
85-
}
22+
ungets("String to test \n");
8623

87-
/* push: push f onto value stack */
88-
void push(double f){
89-
if (sp < MAXVAL){
90-
stack[sp++] = f;
91-
} else {
92-
printf("error: stack full, can't push %f\n", f);
24+
while (bufp > 0) {
25+
c = getch();
26+
putchar(c);
9327
}
94-
}
9528

96-
/* pop: pop and return top value from stack */
97-
double pop(void){
98-
if(sp > 0){
99-
return stack[--sp];
100-
} else {
101-
printf("error: stack empty.\n");
102-
return 0.0;
103-
}
29+
return 0;
10430
}
10531

106-
/* getop: get next operator of numeric operand */
107-
int getop(char s[]){
108-
int i, c;
109-
110-
while((s[0] = c = getch()) == ' ' || c == '\t')
111-
;
112-
113-
s[1] = '\0';
114-
if(!isdigit(c) && c != '.'){
115-
return c;
116-
}
117-
118-
i = 0;
119-
120-
if(isdigit(c)){
121-
while (isdigit(s[++i] = c = getch()))
122-
;
123-
}
124-
125-
if(c == '.'){
126-
while (isdigit(s[++i] = c = getch()))
127-
;
128-
}
129-
130-
s[i] = '\0';
32+
/* ungets: put back string */
33+
void ungets(char s[]){
34+
int len = strlen(s);
13135

132-
if(c != EOF){
133-
ungetch(c);
36+
while(len > 0){
37+
ungetch(s[--len]);
13438
}
13539

136-
return NUMBER;
13740
}
13841

139-
/* getch: get a character */
140-
int getch(void){
42+
/* getch: get a (possibly pushed-back) character */
43+
int getch(void) {
14144
return (bufp > 0) ? buf[--bufp] : getchar();
14245
}
14346

14447
/* ungetch: push character back on input */
145-
void ungetch(int c){
146-
if(bufp >= BUFSIZE){
48+
void ungetch(int c) {
49+
if(bufp >= BUFSIZE)
14750
printf("ungetch: too many characters\n");
148-
} else {
51+
else
14952
buf[bufp++] = c;
150-
}
151-
}
152-
153-
/* ungets: push back an entire string onto the input. */
154-
void ungets(char s[]){
155-
int len = strlen(s);
156-
157-
while (len > 0){
158-
ungetch(s[--len]);
159-
}
16053
}
16154

16255
/**
163-
* ungets does not need to know about buf and bufp, the function uungetch handle those variables and error checking.
56+
* ungets does not need to know about buf and bufp, the function ungetch handle those variables and error checking.
16457
**/

Chapter 04/4-08/main.c

+15-144
Original file line numberDiff line numberDiff line change
@@ -3,165 +3,36 @@
33
* Exercise: 4-08 - Suppose that there will never be more than one character of pushback. Modify getch and ungetch accordingly.
44
**/
55

6-
76
#include <stdio.h>
8-
#include <stdlib.h>
9-
#include <ctype.h>
10-
#include <string.h>
7+
#define BUFSIZE 1
118

12-
#define MAXOP 100 /* max size of operand or operator */
13-
#define MAXVAL 100 /* maximum depth of val stack */
14-
#define NUMBER '0' /* signal that a number was found */
9+
char buf[BUFSIZE]; /* buffer for ungetch */
10+
int bufp = 0; /* next free position in buf */
1511

16-
int getop(char []);
1712
int getch(void);
1813
void ungetch(int);
19-
void push(double);
20-
double pop(void);
21-
void ungets(char []);
22-
23-
char buf = 0;
24-
double stack[MAXVAL];
25-
int sp = 0; /* next free stack position */
26-
27-
int main() {
28-
int type, var;
29-
double op2, v, variable[26];
30-
char s[MAXOP];
3114

32-
var = 0;
15+
int main(void) {
16+
int c;
3317

34-
while ((type = getop(s)) != EOF){
35-
switch(type){
36-
case NUMBER:
37-
push(atof(s));
38-
break;
39-
case '+':
40-
push(pop() + pop());
41-
break;
42-
case '*':
43-
push(pop() * pop());
44-
break;
45-
case '-':
46-
op2 = pop();
47-
push(pop() - op2);
48-
break;
49-
case '/':
50-
op2 = pop();
51-
if(op2 != 0){
52-
push(pop() / op2);
53-
} else {
54-
printf("error: zero divisor.\n");
55-
}
56-
break;
57-
case '=':
58-
pop();
59-
if(var >= 'A' && var <= 'Z') {
60-
variable[var - 'A'] = pop();
61-
} else {
62-
printf("error: no variable name.\n");
63-
}
64-
break;
65-
case '\n':
66-
v = pop();
67-
printf("\t%.8g\n", pop());
68-
break;
69-
default:
70-
if(type >= 'A' && type <= 'Z'){
71-
push(variable[type - 'A']);
72-
} else if (type == 'v') {
73-
push(v);
74-
} else {
75-
printf("error: unknown command %s\n", s);
76-
}
77-
break;
78-
}
79-
var = type;
18+
while ((c = getch()) != '\n'){
19+
putchar(c);
8020
}
8121

22+
ungetch(c);
8223
return 0;
8324
}
8425

85-
/* push: push f onto value stack */
86-
void push(double f){
87-
if (sp < MAXVAL){
88-
stack[sp++] = f;
89-
} else {
90-
printf("error: stack full, can't push %f\n", f);
91-
}
92-
}
93-
94-
/* pop: pop and return top value from stack */
95-
double pop(void){
96-
if(sp > 0){
97-
return stack[--sp];
98-
} else {
99-
printf("error: stack empty.\n");
100-
return 0.0;
101-
}
102-
}
103-
104-
/* getop: get next operator of numeric operand */
105-
int getop(char s[]){
106-
int i, c;
107-
108-
while((s[0] = c = getch()) == ' ' || c == '\t')
109-
;
110-
111-
s[1] = '\0';
112-
if(!isdigit(c) && c != '.'){
113-
return c;
114-
}
115-
116-
i = 0;
117-
118-
if(isdigit(c)){
119-
while (isdigit(s[++i] = c = getch()))
120-
;
121-
}
122-
123-
if(c == '.'){
124-
while (isdigit(s[++i] = c = getch()))
125-
;
126-
}
127-
128-
s[i] = '\0';
129-
130-
if(c != EOF){
131-
ungetch(c);
132-
}
133-
134-
return NUMBER;
135-
}
136-
137-
/* getch: get a character */
138-
int getch(void){
139-
int c;
140-
141-
if(buf != 0){
142-
c = buf;
143-
} else {
144-
c = getchar();
145-
}
146-
147-
buf = 0;
148-
return c;
26+
/* getch: get a (possibly pushed-back) character */
27+
int getch(void) {
28+
return (bufp > 0) ? buf[--bufp] : getchar();
14929
}
15030

15131
/* ungetch: push character back on input */
152-
void ungetch(int c){
153-
if(buf != 0){
154-
printf("error: too many characters\n");
32+
void ungetch(int c) {
33+
if (bufp >= BUFSIZE) {
34+
printf("ungetch: too many characters\n");
15535
} else {
156-
buf = c;
157-
}
158-
}
159-
160-
/* ungets: push back an entire string onto the input. */
161-
void ungets(char s[]){
162-
int len = strlen(s);
163-
164-
while (len > 0){
165-
ungetch(s[--len]);
36+
buf[bufp++] = c;
16637
}
16738
}

0 commit comments

Comments
 (0)