This is a program for reverse polish calculator (K&R ch 4)
This is a post fix calculator
1.Additions deal with adding functions from math.h to the
calculator.

In anticipation of the following exercise the code deals with an
identifier in the following manner:

If the identifier is recognised as one of the supported mathematical
functions then that function from the library is called. If the
identifier is not one of the supported functions, even if it is a
valid function from math.h it is ignored.

The main changes are the introduction of another define value
(MATHFUNCTION) along with its associated case in the switch statement.
getop has also been changed to deal with reading in alphabetical
characters.

Extra cases have been added to:

2. Swap the top two items on the stack.
3. Duplicate the top item on the stack.

4.??i want the program to support lower case characters
[a-z]. These letters will be used in the program as variables
i.e 3=a..
With this extra option the program will enable the calculation
of expresions like a b +, a c pow (postfix notation)

In anticipation of the following exercise the following characters have
been used for the operations (in the same order as above): ~ for duplicate and $ for swap

Code:
#include "calc.h"

#define MAXOP 100

int main()
{
int type;
double op2;
char s[MAXOP];

	while((type = getop(s)) != EOF) {
	 	   switch(type) {
					case NUMBER:
					
				push(atof(s));
				
					break;
					

					case MATHFUNCTION:  
					  
				CalcExtraMath(s);
				
					break;


					case '+':
					
				push(pop() + pop());
				
					break;


					case '*':
					
				push(pop() * pop());
				
					break;
					

					case '-':
					
				op2 = pop();
				
		push(pop() - op2);
		
					break;
					

					case '/':
					
				op2 = pop();
				
				 if(op2 != 0.0) {
					push(pop() / op2);
				 }
				 else {
		  printf("Division by zero !!\n");
		  }
					break;
					
					
				 case '$':
				 
            SwapTopElements();
            
            break;
            
            
        case '~':
        
            DuplicateTopElement();
            
            break;
            

				case '\n':
				
		printf("\t %.8g\n", pop());
		
				break;
				

				default:
				
		printf("Strange input !!\n");
		
				break;
				
				
		}
	}

return(0);
}
/* stack.c */
#include <stdio.h>

#define MAXVAL 100

static int sp = 0;

static double val[MAXVAL];

	void push(double f)
	 {
		if(sp < MAXVAL) {
		val[sp++] = f;
		} 
		else {
        printf("Stack Overflow !!\n");
        }

	 return;
	}


double pop(void)
 {
   if(sp > 0) {
     return(val[--sp]);
    } 
     else {
      printf("Stack Underflow !!\n");
     }

   return(0.0);
 }

void SwapTopElements(void)
{
    double temp;
    if (sp > 1)
    {
        temp = val[sp-1];
        val[sp-1] = val[sp-2];
        val[sp-2] = temp;
      printf("Top two  elements reversed succesfully..\n");   
    }
    else
        printf("Need at least 2 elements to swap!\n");
}

void DuplicateTopElement(void)
{
    if (sp > 0){
        push(val[sp-1]);
        printf("Top element duplicated succesfully..\n");
      }
    else
        printf("Need at least 1 element to duplicate!\n");

/* getop.c */
#include <stdio.h>
#include <ctype.h>
#include "calc.h"

int getop(char s[])
{
int i, c;
/*remove whitespace*/
 while((s[0] = c = getch()) == ' ' || c == '\t') { ; }
 s[1] = '\0';
/*for expresions like: log, pow, sin, cos.*/
	if(isalpha(c))
    {
      i = 0;
      while(isalpha(s[i++] = c ))
         c = getch();     
      s[i - 1] = '\0';
      if(c != EOF)
         ungetch(c);
      return MATHFUNCTION;
    }
   
   
	if(!isdigit(c) && c != '.') {
		return(c);
	 }

	i = 0;
	if(isdigit(c)) {
		while(isdigit(s[++i] = c = getch())) { ; }
	 }

	if(c == '.') {
		while(isdigit(s[++i] = c = getch())) { ; }
	 }
	s[i] = '\0';


	if(c != EOF) {
		ungetch(c);
	 }

return(NUMBER);
}

/* getch.c */
#include <stdio.h>

#define BUFSZ 100

char buf[BUFSZ];
static int bufp = 0;

	int getch(void)
	{/*when ungetch() make bufp >0 will examine
characters like '\n' or error characters that were entered after
an expresion*/
	 return((bufp > 0) ? buf[--bufp] : getchar());
	}

	void ungetch(int c)
	{
	  if(bufp >= BUFSZ) {
		 printf("ungetch: Buffer Overflow !!");
	   } 
	   else {
             buf[bufp++] = c;
           }

    return;
   }

/*
CalcExtraMath.c
for the calculation of expresions like pow log tan*/

#include <string.h>
#include <stdio.h>
#include <math.h>
#include "calc.h"
void CalcExtraMath(char s[])
{
   double op2;
   
   if( 0 == strcmp(s, "sin")) //epistrefei 0 an tautizontai ta 2 string
      push(sin(pop()));
      
   else if( 0 == strcmp(s, "cos"))
      push(cos(pop()));
      
   else if(0 == strcmp(s, "tan"))
   	push(tan(pop()));
   	
   else if (0 == strcmp(s, "exp"))
      push(exp(pop()));
      
   else if (0 == strcmp(s, "sqrt"))
      push(sqrt(pop()));
      
   else if (0 == strcmp(s, "log"))
      push(log(pop()));
      
   else if (0 == strcmp(s, "log10"))
      push(log10(pop()));
      
   else if(!strcmp(s, "pow"))
   {       op2 = pop(); 
      push(pow(pop(), op2));
   }
   
   else if(0 == strcmp(s, "PI"))
   	push(3.14159L);
   
   else if(0 == strcmp(s, "E"))
   	push(2.71828L);	
   	
   else
      printf("%s  unsupported function entered !!\n",s);
}

/* calc.h */

#ifndef _CALC_H_
#define _CALC_H_

#define NUMBER '0'
#define MATHFUNCTION '1'

void CalcExtraMath(char s[]);
void push(double);
void ungetch(int);
int getch(void);
double pop(void);
int getop(char []);
void SwapTopElements(void);
void DuplicateTopElement(void);

#endif /* _CALC_H */