#include "graphics.h"
#include "conio.h"
#include "iostream.h"
#include "time.h"
#include "stddef.h"
#include "stdio.h"
#include "dos.h"
#include "string.h"
#include "ctype.h"
#include "stdlib.h"
const size=1000;
//***********************************//
static int choise,driver=DETECT,mode=0,i;
//***********************************//
// Start Graphic class
class Graphic
{
     public:
   void Graph();
   int BackGround();
   void Title();
   void Head();
   void Time();
   void Sign();
   void PostfixMenu();
   //void PrefixMenu();
   void HelpMenu();
     private:
   static void box(int,int,int,int,int);
   static void fill_box(int,int,int,int,int);
}GraphObject;
//***********************************// End Class Graphic

class EventKey
{
     public:
   void KeyEvent();
}EventKeyObject;
//**********************************
class PostfixStack
{
 public:
  void stack();
  void push( char ); // push an element onto stack
  char pop(); // pop an element off the stack
  int isOperand( char );
  void convert();
  void copy( char * , char * );
 private:
  char top;
  char poItem[size];
}conSta;
//*********************************
class InfixStack
{
 public:
  void Instack();
  void Inpush( char ); // push an element onto stack
  char Inpop(); // pop an element off the stack
 private:
  char Intop;
  char InItem[size];
}InfixSta;
//*********************************
void InfixStack::Instack()
{
 Intop = -1;
}

void InfixStack::Inpush(char Ins)
{
 InItem[++Intop] = Ins;
}
char InfixStack::Inpop()
{
 return InItem[Intop--];

}
//********************************
void PostfixStack::stack()
{
 top = -1;
}

void PostfixStack::push(char s)
{
 poItem[++top] = s;
}
char PostfixStack::pop()
{
 return poItem[top--];

}
void PostfixStack::copy( char * s1, char *s2 )
{
 for ( ; ( *s1 = *s2 ) != '\0'; s1++ )
 ;
}
void PostfixStack::convert( )
{
 char postfix[size] , symbol , top1 , top2 , InfixTop , PostfixTop;
 int i = 0 , b = 0 , d = 0 , len = 0 , f = 3;
 gotoxy(29,15);
 cin.getline( postfix , size);
 for ( i = 0; i<=strlen(postfix); i++ )
 {
  symbol = postfix[i];
  if ( conSta.isOperand( symbol ) )
  {
   d = d+1;
   len = (strlen(postfix) - d);
   conSta.push( symbol );
  }
  else
  {
   b = b+1;
   if (b==1)
   {
    top1 = conSta.pop();
    top2 = conSta.pop();
    InfixSta.Inpush( top2 );
    InfixSta.Inpush( symbol );
    InfixSta.Inpush( top1 );
   }
   else
   {
    top1 = conSta.pop();
    InfixSta.Inpush( symbol );
    InfixSta.Inpush( top1 );
   }
  } // end else
 } // end for
 for ( int j = 0; j <= strlen(postfix); j++ )
 {
  InfixTop = InfixSta.Inpop();
  conSta.push( InfixTop );
 }
 gotoxy(29,19);
 for (int c = 0; c <= len; c++ )
 {
  cout<<"(";
 }
 for ( int l = 0; l <= strlen(postfix); l++ )
 {
  PostfixTop = conSta.pop();
  cout << PostfixTop;
  switch ( l )
  {
   case 0,1,2:
    break;
  }
  if ( l == f )
  {
   cout<<")";
   f= f+2;
  }
 }
}
int PostfixStack::isOperand( char symbol )
{
 switch ( symbol )
 {
  case '^':
  case '+':
  case '-':
  case '*':
  case '/':
  case '%':
   return 0;
  default:
   return 1;
 }
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@// Start class Convert Postfix to infix
int main()
{
 char EvenKey;
 GraphObject.Graph();
 while ((EvenKey=getch())!='Q')
 {
  switch ( EvenKey )
  {
   case 'H':
    EventKeyObject.KeyEvent();
    break;
   case 'N':
    closegraph();
    restorecrtmode();
    GraphObject.Graph();
    break;
   case 'C':
    conSta.convert();
    break;

  }
 }
 closegraph();
 restorecrtmode();
 return 0;
}
//***********************************// Start Graphic Object Class
void Graphic::Graph()
{
     BackGround();
     Title();
     Head();
     Sign();
     PostfixMenu();
     //PrefixMenu();
     HelpMenu();
     Time();
}
//***********************************//
int Graphic::BackGround()
{
     initgraph(&driver,&mode,"c:\\tc\\bgi");
     setbkcolor(6);
     box(5,5,632,470,WHITE);
     setviewport(20,20,630,470,1);
     box(5,135,590,430,WHITE);
     return driver,mode;
}
//***********************************//
void Graphic::Title()
{
     box(5,110,590,135,WHITE);
     settextstyle(4,HORIZ_DIR,3);
     outtextxy(140,108,"WWW.CSHARP.BLOGFA.COM!");
}
//***********************************//
void Graphic::Sign()
{
     fill_box(19,159,179,390,12);
     box(19,159,179,390,WHITE);
     settextstyle(0,HORIZ_DIR,1);
     outtextxy(25,170,"Programer :");
     outtextxy(25,190,"Hamid & Ali Ahmadi");
     outtextxy(25,210,"Ver : 1.1");
     outtextxy(25,230,"Company : Galaxy");
     outtextxy(25,250,"Master Name :");
     outtextxy(25,270,"Master Fadavi");
     outtextxy(25,290,"Univercity :");
     outtextxy(25,310,"Shomal");
     outtextxy(25,330,"Programer Tell:");
     outtextxy(25,350,"0935-794-5949");
     outtextxy(25,370,"0911-909-5720");
}
//***********************************//
void Graphic::PostfixMenu()
{
     fill_box(185,159,580,391,12);
     box(185,159,580,391,WHITE);
     settextstyle(0,HORIZ_DIR,1);
     outtextxy(195,175,"Input Postfix For Convert to Infix:");
     fill_box(195,201,570,221,7);
     box(195,201,570,221,WHITE);
     outtextxy(195,240,"Ansower is:");
     fill_box(195,265,570,285,7);
     box(195,265,570,285,WHITE);
}
//***********************************//
/*void Graphic::PrefixMenu()
{
     fill_box(185,280,580,391,12);
     box(185,280,580,391,WHITE);
     settextstyle(0,HORIZ_DIR,1);
     outtextxy(195,290,"Input Prefix For Convert to Infix:");
     fill_box(195,310,570,330,7);
     box(195,310,570,330,WHITE);
     outtextxy(195,340,"Ansower is:");
     fill_box(195,360,570,380,7);
     box(195,360,570,380,WHITE);
} /*
//***********************************/
void Graphic::Head()
{
     fill_box(5,5,590,110,13);
     box(5,5,590,110,WHITE);
     settextstyle(GOTHIC_FONT,HORIZ_DIR,8);
     outtextxy(70,14,"Galaxy Group");
}
//***********************************//
void Graphic::HelpMenu()
{
     fill_box(19,400,580,420,12);
     box(19,400,580,420,WHITE);
     settextstyle(0,HORIZ_DIR,1);
     outtextxy(32,408,"Shift+H = Help |");
     outtextxy(170,408,"Shift+N = New |");
     outtextxy(300,408,"Shift+C = Convert |");
     outtextxy(460,408,"Shift+Q = Exit");
}
//***********************************//
void Graphic::box(int startx, int starty,int endx,int endy,int color)
{
     setcolor(color);
     line(startx,starty,startx,endy);
     line(startx,starty,endx,starty);
     line(endx,starty,endx,endy);
     line(endx,endy,startx,endy);
}
//***********************************//
void Graphic::fill_box(int startx, int starty,int endx,int endy,int color)
{
     register int i,begin,end;
     begin = startx < endx ? startx : endx;
     end = startx > endx ? startx : endx;
     for (i = begin; i < end ; i++)
     {
   setcolor(color);
   line(i,starty,i,endy);
     }
}
//***********************************//
void Graphic::Time()
{
     time_t t ;
     struct time dos_time ;
     struct date dos_date ;
     struct tm *local ;
     getdate(&dos_date) ;
     gettime(&dos_time) ;
     t = dostounix(&dos_date, &dos_time) ;
     local = localtime(&t) ; // braye eshare be yek sakhteman az noe tm
     settextstyle(2,HORIZ_DIR,5);
     outtextxy(385,432,asctime(local)); // asctime braye format dadan be sakhtemane time   return driver,mode;
}
//End Graphic Object Class
void EventKey::KeyEvent()
{
 gotoxy(29,21);
 cout<< "You write postfix in << Input postfix for ";
 gotoxy(29,22);
 cout<< "Convert to Infix:>> Box! And you See To ";
 gotoxy(29,23);
 cout<<"Ansower is Box";
}