16.05.2015 Views

Working with the Unix OS

Working with the Unix OS

Working with the Unix OS

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

C Programming – Basics<br />

}<br />

return 0;<br />

}<br />

/* stack manipulation functions */<br />

#define MAXVAL 100 /* maximum depth of val stack */<br />

int sp=0; /* next free stack position */<br />

double val[MAXVAL]; /* value stack */<br />

void push(double f) /* push: push f onto value stack */<br />

{<br />

if (sp < MAXVAL)<br />

val [sp++] = f;<br />

else<br />

printf("error: stack full, can't push %g\n", f);<br />

}<br />

double pop(void) /* pop: and return top value from stack */<br />

{<br />

if (sp> 0)<br />

return val[--sp];<br />

else<br />

{<br />

printf("error: stack empty\n");<br />

return 0.0;<br />

}<br />

}<br />

#include /* for getch */<br />

#include /* for isdigit */<br />

int getop(char s[]) /* getop: get next operator or operand */<br />

{<br />

int i, c;<br />

while ((s[0] = c = getch()) == ' ', || c == '\t');<br />

s[1] = '\0';<br />

if (!isdigit(c) && c != '.')<br />

return c; /* not a number */<br />

i = 0;<br />

if (isdigit(c)) /* collect integer part */<br />

while (isdigit(s[++i] = c = getch()));<br />

if (c == '.') /* collect fractional part */<br />

while (isdigit(s[++i] = c = getch()));<br />

s[i] = '\0';<br />

if (c != EOF)<br />

ungetch(c);<br />

return NUMBER;<br />

}<br />

Scope Rules<br />

The scope of a name is <strong>the</strong> part of <strong>the</strong> program <strong>with</strong>in which <strong>the</strong> name can be used.<br />

In filel: /* where you require use of sp and val */<br />

extern int sp;<br />

extern double val[];<br />

In file2: /* initial declaration of sp and val */<br />

int sp = 0;<br />

double val [MAXVAL];<br />

Header Files<br />

calc.h<br />

#define NUMBER '0'<br />

void push(double);<br />

double pop(void);<br />

int getop(char []);<br />

31

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!