Skip to content

Commit ab60cea

Browse files
committed
use atof to simplify main routine
1 parent f78b92e commit ab60cea

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

chapter05/5-10.c

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22
* Exercise 5-10. Write the program expr, which evaluates a reverse Polish
33
* expression from the command line, where each operator or operand is a
44
* separate argument. For example,
5+
*
56
* expr 2 3 4 + *
7+
*
68
* evaluates 2 * (3 + 4).
9+
*
710
* By Faisal Saadatmand
811
*/
912

13+
/* NOTE: * and % must be included in equation marks at the command prompt,
14+
* otherwise they will be interpreted by the shell as wildcard characters */
15+
1016
#include <stdio.h>
1117
#include <stdlib.h>
1218
#include <ctype.h>
@@ -15,12 +21,12 @@
1521
#define MAXVAL 100
1622

1723
/* functions */
18-
void push(double);
24+
void push(double);
1925
double pop(void);
2026

2127
/* globals */
22-
int sp = 0; /* next free stack position */
23-
double val[MAXVAL]; /* value stack */
28+
int sp = 0; /* next free stack position */
29+
double val[MAXVAL]; /* value stack */
2430

2531
/* push: push f onto value stack */
2632
void push(double f)
@@ -44,27 +50,23 @@ double pop(void)
4450

4551
int main(int argc, char *argv[])
4652
{
47-
int type, error = 0;
48-
double op2;
53+
int type;
54+
double num, op2;
4955

5056
if (argc < 4) {
51-
printf("Usage: expr operand1 operand2 operator operand3 operator ...\n");
57+
printf("Usage: %s <expr>...\n", *argv);
5258
return -1;
5359
}
5460

5561
while (--argc > 0) {
56-
++argv; /* skip program name */
57-
if (*(argv)[0] == '-' && isdigit(*(*argv + 1)))
58-
type = NUMBER;
59-
if (isdigit(*(argv)[0]))
60-
type = NUMBER;
61-
if (*(argv)[0] == '.' && isdigit(*(*argv + 1)))
62+
++argv; /* skip program name */
63+
if ((num = atof(*argv)))
6264
type = NUMBER;
63-
if (!isdigit(*(argv)[0]) && *(argv)[0] != '-' && *(argv)[0] != '.')
64-
type = *(argv)[0];
65+
else
66+
type = **argv; /* first character in *argv string */
6567
switch (type) {
6668
case NUMBER:
67-
push(atof(*argv));
69+
push(num);
6870
break;
6971
case '*':
7072
push(pop() * pop());
@@ -81,26 +83,25 @@ int main(int argc, char *argv[])
8183
if (op2 != 0.0)
8284
push(pop() / op2);
8385
else {
84-
printf("error: division by zero\n");
85-
error = 1;
86+
printf("error: zero divisor\n");
87+
argc = 1;
8688
}
8789
break;
8890
case '%':
8991
op2 = pop();
9092
if (op2 != 0.0)
9193
push((long) pop() % (long) op2);
9294
else {
93-
printf("error: division by zero\n");
94-
error = 1;
95+
printf("error: zero divisor\n");
96+
argc = 1;
9597
}
9698
break;
9799
default:
98-
printf("error: unknown parameter\n");
99-
error = 1;
100+
printf("error: unknown command %s\n", *argv);
101+
argc = 1;
100102
break;
101103
}
102104
}
103-
if (!error)
104-
printf("%g\n", pop());
105+
printf(" %.8g\n", pop()); /* print result */
105106
return 0;
106107
}

0 commit comments

Comments
 (0)