#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 80
struct stack
{
 int top;
 double items[MAX];
};
struct cstack
{
  int top;
  char items[MAX];
};
double inf_eval(char in_fix[]);
void cpostfix(char in[],char post[]);
void cpush(struct cstack*,char);
int cpop(struct cstack*);
int iscempty(struct cstack*);
char at_s_top(struct cstack*);
double eval(char postfix[]);
int is_digit(char);
void push(struct stack*,double);
double pop(struct stack*);
double oper(int,double,double);
void space(int);
void shape();
void result();
void clean(int,int);
void main()
{
 clrscr();
 shape();
 gotoxy(20,8);printf("Sin(S)");
 gotoxy(30,8);printf("Cos(C)");
 gotoxy(40,8);printf("Tan(T)");
 gotoxy(50,8);printf("Log10(L)");
 gotoxy(20,10);printf("1/x(i)");
 gotoxy(30,10);printf("x^2(s)");
 gotoxy(40,10);printf("x^3(q)");
 gotoxy(50,10);printf("x^y(p)");
 gotoxy(20,12);printf("Sqrt(r)");
 gotoxy(30,12);printf("e^x(e)");
 gotoxy(40,12);printf("Ln(n)");
 gotoxy(50,12);printf("Arith(A)");
 gotoxy(50,22);printf("OFF(o)");
// gotoxy(50,4);printf("Ans:");
 result();
 getch();
}
void shape()
{
 int i,j;
 clrscr();
 printf("\n\t\tÉ");
 for(i=0;i<40;i++)
   printf("Í");
 printf("»\n");
 for(i=0;i<3;i++)
 { printf("\t\tº");space(40);printf("º\n");}
 printf("\t\tÌ");
 for(i=0;i<40;i++)
   printf("Í");
 printf("¹\n");
 for(i=0;i<17;i++)
 { printf("\t\tº");space(40);printf("º\n");}
   printf("\t\tÈ");
 for(i=0;i<40;i++)
   printf("Í");
 printf("¼\n");
}
void clean(int x,int y)
{
 int i;
 for(i=x;i<x+20;i++)
 { gotoxy(i,4);printf(" ");}
}
void space(int n)
{
 int i;
 for(i=0;i<n;i++)
   printf(" ");
}
void result()
{
 char c;
 int i;
 char *p;
 double n,m;
 gotoxy(20,4);
 while(1)
 {
  c=getch();
  if(c==' '||c=='\t')
  {  clrscr();
    textcolor(WHITE);
    sound(1000);
    cprintf("\n\n\tWRONG KEY PRESSED.USE THE CALCULATOR CORRECTLY");
    nosound();
    getch();
    main();
  }
  gotoxy(19,4);
  for(i=19;i<56;i++)
   printf(" ");
nar:  gotoxy(20,4);
  switch(c)
  {
   case 'S':printf("Sin ");gets(p);
	    if(*p=='\0')
	      {c='S';goto nar;}
	    n=atof(p);
	    gotoxy(40,4);
	    printf("%0.3lf",sin(n*4*atan(1)/180.0));
	    break;
   case 'C':printf("Cos ");gets(p);
	    if(*p=='\0')
	      {c='C';goto nar;}
	    n=atof(p);
	    gotoxy(40,4);
	    printf("%0.3lf",cos(n*4*atan(1)/180.0));
	    break;
   case 'T':printf("Tan ");gets(p);
	    if(*p=='\0')
	      {c='T';goto nar;}
	    n=atof(p);
	    gotoxy(40,4);
	    if(n==90.000)
	     { printf("Not Defined.");break;}
	    printf("%0.3lf",tan(n*4*atan(1)/180.0));
	    break;
   case 'L':printf("Log10 ");gets(p);
	    if(*p=='\0')
	      {c='L';goto nar;}
	    n=atof(p);
	    gotoxy(40,4);
	    if(n==0.00000)
	       {printf("Not Defined.");break;}
	    printf("%0.3lf",log10(n));
	    break;
   case 'n':printf("Log e ");gets(p);
	    if(*p=='\0')
	      {c='n';goto nar;}
	    n=atof(p);
	    gotoxy(40,4);
	    if(n==0.00000)
	       {printf("Not Defined.");break;}
	    printf("%0.3lf",log(n));
	    break;
   case 'e':printf("e pow ");gets(p);
	    if(*p=='\0')
	      {c='e';goto nar;}
	    n=atof(p);
	    gotoxy(40,4);
	    printf("%0.3lf",exp(n));
	    break;
   case 'i':printf("Inverse of ");gets(p);
	    if(*p=='\0')
	      {c='i';goto nar;}
	    n=atof(p);
	    gotoxy(40,4);
	    if(n==0.00000)
	      { printf("Not Defined.");break;}
	    printf("%0.3lf",1/n);
	    break;
   case 's':printf("Square of ");gets(p);
	    if(*p=='\0')
	      {c='s';goto nar;}
	    n=atof(p);
	    gotoxy(40,4);
	    printf("%0.3lf",n*n);
	    break;
   case 'q':printf("Cube of ");gets(p);
	    if(*p=='\0')
	      {c='q';goto nar;}
	    n=atof(p);
	    gotoxy(40,4);
	    printf("%0.3lf",n*n*n);
	    break;
   case 'p':printf("x and y :");scanf("%lf%lf",&m,&n);
	    gotoxy(43,4);
	    printf("x^y: %0.3lf",pow(m,n));
	    break;
   case 'r':printf("Square root of ");gets(p);
	    if(*p=='\0')
	      {c='r';goto nar;}
	    n=atof(p);
	    gotoxy(40,4);
	    printf("%0.3lf",sqrt(n));
	    break;
   case 'A':printf("Expression(infix): ");delay(500);
	    clean(20,4);
	    gotoxy(20,4);
	    gets(p);
	    if(*p=='\0')
	      {c='A';goto nar;}
	    gotoxy(40,4);
	    printf("%0.8e",inf_eval(p));
	    break;
   case 'o':exit(0);
  }
  fflush(stdin);
 }
}
double eval(char postfix[])
{
  int i;
  char c;
  struct stack s;
  double op1,op2,val;
  s.top=-1;
  for(i=0;(c=postfix[i])!='\0';i++)
  {
   if(isdigit(c))
     push(&s,(double)(c-'0'));
   else
   {
    op2=pop(&s);
    op1=pop(&s);
    val=oper(c,op1,op2);
    push(&s,val);
   }
  }
  return(pop(&s));
}
int is_digit(char ch)
{
 return(ch>='0' && ch<='9');
}
void push(struct stack *s,double x)
{
 s->items[++s->top]=x;
}
double pop(struct stack *s)
{
 return(s->items[s->top--]);
}
double oper(int c,double op1,double op2)
{
 switch(c)
 {
  case '+':return(op1+op2);
  case '-':return(op1-op2);
  case '*':return(op1*op2);
  case '/':return(op1/op2);
  case '$':return(pow(op1,op2));
  default :printf("\n invalid option");
  getch();
 }
}
double inf_eval(char in_fix[])
{
 char post[MAX];
 double a;
 cpostfix(in_fix,post);
 a=eval(post);
 return a;
}
void cpostfix(char in[MAX],char post[MAX])
{
  int i=0,j=0;
  char ts,ch;
  struct cstack *s;
  s->top=-1;
  while((ch=in[i++])!='\0')
  {
   if(isalnum(ch))
   post[j++]=ch;
   else
   {
    while(!iscempty(s) && (prec(at_s_top(s))>=prec(ch)) && (ch!='('))
    {
      ts=cpop(s);
      if(ts!='(')
	post[j++]=ts;
    }
   if(ch!=')')
     cpush(s,ch);
   }
  }
 while(!iscempty(s))
   post[j++]=cpop(s);
 post[j]='\0';
}
void cpush(struct cstack *s,char c)
{
   s->items[++(s->top)]=c;
}
int cpop(struct cstack *s)
{
  return(s->items[(s->top)--]);
}
int iscempty(struct cstack *s)
{
  return(s->top==-1);
}
char at_s_top(struct cstack *s)
{
  return(s->items[s->top]);
}
int prec(char c)
{
 switch(c)
 {
   case '(':case ')':return 0;
   case '+':case '-':return 1;
   case '*':case '/':return 2;
   case '$':return 3;
 }
}



