# K & R C Programs Exercise 4-5.

K and R C, Solution to Exercise 4-5:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition) . You can learn and solve K&R C Programs Exercise.
Write a C program to access to library functions like sin, exp, and pow. See “math.h” for more details.Read more about C Programming Language .

`/************************************************************ You can use all the programs on  www.c-program-example.com* for personal and learning purposes. For permissions to use the* programs for commercial purposes,* contact [email protected]* To find more C programs, do visit www.c-program-example.com* and browse!* *                      Happy Coding***********************************************************/#include<stdlib.h>#include<stdio.h>#include<ctype.h>#include<math.h>#include <string.h>#define MAXOP 100#define NUMBER       0#define IDENTIFIER   1#define TRUE 1#define FALSE 0int Getop(char s[]);void push(double val);double pop(void);void showTop(void);void duplicate(void);void swapItems(void);void clearStack();void mathfnc(char s[]);int main(void){ int type; double op2; char s[MAXOP]; int flag = TRUE; while((type = Getop(s)) != EOF) {  switch(type)  {  case NUMBER:   push(atof(s));   break;  case IDENTIFIER:   mathfnc(s);   break;  case '+':  push(pop() + pop());  break;  case '*':   push(pop() * pop());   break;  case '-':   op2 = pop();   push(pop()- op2);   break;  case '/':   op2 = pop();   if(op2)    push(pop() / op2);   else    printf("nError: division by zero!");   break;  case '%':   op2 = pop();   if(op2)    push(fmod(pop(), op2));   else    printf("nError: division by zero!");   break;  case '?':   showTop();   break;  case '#':   duplicate();   break;  case '~':   swapItems();   break;  case '!':   clearStack();  case 'n':   printf("nt%.8gn", pop());   break;  default:   printf("nError: unknown command %s.n", s);   break;  } } return EXIT_SUCCESS;}#define MAXVAL 100int sp = 0;         double val[MAXVAL]; /* push: push f onto stack. */void push(double f){ if(sp < MAXVAL)  val[sp++] = f; else  printf("nError: stack full can't push %gn", f);}/*pop: pop and return top value from stack.*/double pop(void){ if(sp > 0)  return val[--sp]; else {  printf("nError: stack emptyn");  return 0.0; }}void showTop(void){ if(sp > 0)  printf("Top of stack contains: %8gn", val[sp-1]); else  printf("The stack is empty!n");}void duplicate(void){ double temp = pop(); push(temp); push(temp);}void swapItems(void){ double item1 = pop(); double item2 = pop(); push(item1); push(item2);}void clearStack(void){ sp = 0;}/*check string s for supported math functions */void mathfnc(char s[]){ double op2; if( 0 == strcmp(s, "sin"))  push(sin(pop())); else if( 0 == strcmp(s, "cos"))  push(cos(pop())); else if (0 == strcmp(s, "exp"))  push(exp(pop())); else if(!strcmp(s, "pow")) {  op2 = pop();  push(pow(pop(), op2)); } else  printf("%s Error: is not a supported function.n", s);}int getch(void);void unGetch(int);/* Getop: get next operator or numeric operand. */int Getop(char s[]){ int i = 0; int c; int next; /* Skip whitespace */ while((s = c = getch()) == ' ' || c == 't')  ; s = ''; if(isalpha(c)) {  i = 0;  while(isalpha(s[i++] = c ))   c = getch();       s[i - 1] = '';  if(c != EOF)   unGetch(c);  return IDENTIFIER; } /* Not a number but may contain a unary minus. */ if(!isdigit(c) && c != '.' && c != '-')  return c;                  if(c == '-') {  next = getch();  if(!isdigit(next) && next != '.')  {   return c;  }  c = next; } else  c = getch(); while(isdigit(s[++i] = c))  c = getch(); if(c == '.')                 /* Collect fraction part. */  while(isdigit(s[++i] = c = getch()))   ; s[i] = ''; if(c != EOF)  unGetch(c); return NUMBER;}#define BUFSIZE 100char buf[BUFSIZE];int bufp = 0;int getch(void){ return (bufp > 0) ? buf[--bufp]: getchar();}void unGetch(int c){ if(bufp >= BUFSIZE)  printf("nUnGetch: too many charactersn"); else  buf[bufp++] = c;}`
`Read more Similar C ProgramsC BasicC StringsK and R C Programs Exercise`

You can easily select the code by double clicking on the code area above.