C++ Programming Tutorial

 
 
 
 

Image

 # include <iostream.h>
 # include <fstream.h>
 # include <string.h>
 # include <stdlib.h>
 # include <stdio.h>
 # include <conio.h>
 # include <ctype.h>


 //--------------------------  Global Variables  -------------------------//


 const int iRows = 34;
 const int iCols = 15;

 int iTT[iRows][iCols] = {0};


 //------------------------  Function Definitions  -----------------------//


 //------------------------  loadTransitionTable( )  ---------------------//

 void loadTransitionTable( )
 {
    fstream File(\"tt.txt\", ios::in|ios::nocreate);

    if (!File)
    {
       cout << \"\\n Unable to open the input file.\" << endl;
       cout << \"\\n Press any key to exit.\";

       getch( );
       exit(0);
    }

    char sInput[100]={NULL};

    for (int i = 0; i < iRows; i ++)
    {
       strset(sInput, NULL);

       File.getline(sInput, 80);

       char *sPtr=NULL;

       sPtr = strtok(sInput, \" \");

       iTT[i][0] = atoi(sPtr);

       for(int j = 1; j < iCols; j ++)
       {
      sPtr=strtok(NULL, \" \");

      iTT[i][j] = atoi(sPtr);
       }
    }

    File.close( );
 }

 //---------------------------  getNextState( )  -------------------------//

 int getNextState(int iState, char cChar)
 {
    if (isalpha(cChar))
       return iTT[iState][1];

    else if (isdigit(cChar))
       return iTT[iState][2];

    else if (cChar == \'.\')
       return iTT[iState][3];

    else if (cChar == \'\"\')
       return iTT[iState][4];

    else if (cChar == \'_\')
       return iTT[iState][6];

    else if (cChar == \'+\')
       return iTT[iState][7];

    else if (cChar == \'=\')
       return iTT[iState][8];

    else if (cChar == \'-\')
       return iTT[iState][9];

    else if (cChar == \'%\')
       return iTT[iState][10];

    else if (cChar == \'!\')
       return iTT[iState][11];

    else if (cChar == \'>\')
       return iTT[iState][12];

    else if (cChar == \'<\')
       return iTT[iState][13];

    else if (cChar == \'/\')
       return iTT[iState][14];

    return iTT[iState][0];
 }

 //----------------------------  isKeyword( )  ----------------------------//

 int isKeyword(char* sToken)
 {
    if (strlen(sToken) > 16 || strlen(sToken) == 0)
       return 0;

    char sKeywords[64][20] = {
                   \"asm\",\"auto\",\"bool\",\"break\",\"case\",\"catch\",
                   \"char\",\"class\",\"const\",\"const_cast\",
                   \"continue\",\"default\",\"delete\",\"do\",\"double\",
                   \"dynamic_cast\",\"else\",\"enum\",\"explicit\",
                   \"export\",\"extern\",\"false\",\"float\",\"for\",
                   \"friend\",\"goto\",\"if\",\"inline\",\"int\",\"long\",
                   \"main\",\"mutable\",\"namespace\",\"new\",
                   \"operator\",\"private\",\"protected\",\"public\",
                   \"register\",\"reinterpret_cast\",\"return\",
                   \"short\",\"signed\",\"sizeof\",\"static\",
                   \"static_cast\",\"struct\",\"switch\",\"template\",
                   \"this\",\"throw\",\"true\",\"try\",\"typedef\",
                   \"typeid\",\"typename\",\"union\",\"unsigned\",\"using\",
                   \"virtual\",\"void\",\"volatile\",\"wchar_t\",\"while\"
                 };

    for(int iCount = 0; iCount < 64; iCount ++)
    {
       if (strcmpi(sKeywords[iCount], sToken) == 0)
      return 1;
    }

    return 0;
 }


 //-------------------------------  main( )  -----------------------------//


 int main( )
 {
    clrscr( );

    loadTransitionTable( );

    fstream File(\"input.txt\", ios::in|ios::nocreate);

    if (!File)
    {
       cout<<\"\\n Unable to open the input file.\"<<endl;
       cout<<\"\\n Press any key to exit.\";

       getch( );
       exit(0);
    }

    char sToken[255] = {NULL};
    int iTokenIndex = 0;

    char cChar = NULL;
    int iState = 0;
    int iFlag = 0;

    char cTemp = File.get( );

    do
    {
       Start:

       if (iFlag == 0)
       {
      cChar = cTemp;
      cTemp = File.get( );
       }

       else
      iFlag = 0;

       if (cChar == \'/\' && cTemp == \'/\')
       {
      while(File.get( ) != \'\\n\')
      {
         if (File.eof( ))
        goto End;
      }

      cout<<\'\\r\';

      cTemp = File.get( );

      goto Start;
       }

       if (cChar == \'/\' && cTemp == \'*\')
       {
      cTemp = File.get( );

      do
      {
         cChar = cTemp;
         cTemp = File.get( );

         if (File.eof( ))
        goto End;
      }
      while(cChar != \'*\' && cTemp != \'/\');

      cout<<\'\\r\';

      cTemp = File.get( );

      goto Start;
       }

       iState = getNextState(iState, cChar);

       switch (iState)
       {
      case  0 :  cout << cChar;

             iState = 0;
             iTokenIndex = 0;

             strset(sToken, NULL);

             break;

      case  1 :
      case  3 :
      case  5 :
      case  7 :
      case 10 :
      case 14 :
      case 18 :
      case 25 :
      case 26 :  sToken[iTokenIndex] = cChar;
             iTokenIndex ++;

             break;

      case  2 :  if (isKeyword(sToken))
            cout << sToken;

             else
            cout << \"<ID>\";

             iState = 0;
             iTokenIndex = 0;
             iFlag = 1;

             strset(sToken, NULL);

             break;

      case  4 :  cout << \"<INT>\";

             iState = 0;
             iTokenIndex = 0;
             iFlag = 1;

             strset(sToken, NULL);

             break;

      case  6 :  cout << \"<FLOAT>\";

             iState = 0;
             iTokenIndex = 0;
             iFlag = 1;

             strset(sToken, NULL);

             break;

      case  8 :  cout << \"<STR>\";

             iState = 0;
             iTokenIndex = 0;

             strset(sToken, NULL);

             break;

      case  9 :
      case 11 :
      case 12 :
      case 13 :
      case 15 :
      case 16 :
      case 17 :
      case 19 :
      case 20 :
      case 21 :
      case 22 :
      case 23 :
      case 24 :
      case 27 :
      case 28 :  cout << \"<OPR>\";

             if (cChar != \'+\' && cChar != \'-\' && cChar != \'/\'
                && cChar != \'>\' && cChar != \'<\' && cChar != \'=\')
            iFlag = 1;

             iState = 0;
             iTokenIndex = 0;

             strset(sToken, NULL);

             break;

      case 30 :
      case 33 :  iState = 0;
             iTokenIndex = 0;

             strset(sToken, NULL);

             break;
       }
    }
    while(!File.eof( ));

    End:

    getch( );
    return 0;
 }


 //------------------------------  THE END  ------------------------------//

    Related Post:
  1. Program of class and objects using private and public data members

  2. Program to illustrate the implementation of arrays as a Stack in graphics

  3. Represent a polynomial in terms of a singly linked list and then add two three variables polynomials

  4. Program that reads a number and displays it with its digits in reverse order (using while loop)

  5. Program that reads a number ,coumputes and displays its factorial ( using for loop )

  6. Program of DDA line drawing algorithm

  7. Program to draw a Rectangle

  8. Program to construct Newtons Forward Difference Interpolation Formula from the given distinct equally spaced data points

  9. Program to illustrate strings as member of classes

  10. Program to construct Natural Cubic Spline Interpolant from the given data

  11. Program to draw different kinds of Dashed Lines

  12. EXAMPLE OF WRITE FUNCTION

  13. Program to estimate the Integral value of the function at the given points from the given data using Romberg Method

  14. Program to estimate the Integral value of the function at the given points from the given data using Simpsons 1/3 Rule

  15. Program to estimate the value of First Derivative of the function at the given points from the given data using Central Difference Formula

  16. Program to illustrate the implementation of Scaling Transformation along Arbitrary Direction

  17. Program to show the 3D Reflection Transformation along yz-plane

  18. Program to fill different types of geometric shapes using Boundary Fill Algorithm

  19. Program to draw a 3D Piece-Wise Bezier Curve of nth degree with Zeroth Order Continuity

  20. Program to estimate the Integral value of a given function using Gussian Quadrature Rule

 
 
Didn't find what you were looking for? Find more on Program to implement Lexical Analyzer