C++ Programming Tutorial

 
 
 
 

Image

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


 typedef enum { false=0,true=1 } bool;



 //-----------------------  Function Prototypes  -------------------------//



 const bool isPositiveNumber(const char*);
 const bool isNegativeNumber(const char*);
 const bool isPositiveDigit(const char*);
 const bool isNegativeDigit(const char*);
 const bool isIdentifier(const char*);
 const bool isKeyword(const char*);



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



 void main( )
    {
       clrscr( );

       char Token[25]={NULL};

       cout<<\"Enter a token : \";
       gets(Token);

       if(isKeyword(Token)==true)
      cout<<\"The given Token is a Keyword.\";

       else if(isIdentifier(Token)==true)
      cout<<\"The given Token is an Identifier.\";

       else if(isPositiveDigit(Token)==true)
      cout<<\"The given Token is a Positive Digit.\";

       else if(isNegativeDigit(Token)==true)
      cout<<\"The given Token is a Negative Digit.\";

       else if(isPositiveNumber(Token)==true)
      cout<<\"The given Token is a Positive Number.\";

       else if(isNegativeNumber(Token)==true)
      cout<<\"The given Token is a Negative Number.\";

       else
      cout<<\"Error : The given Token is invalid.\";

       getch( );
    }



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




 //-----------------------  isPositiveNumber( )  -------------------------//


 const bool isPositiveNumber(const char* Token)
 {
    if((!isdigit(Token[0]) && Token[0]!=\'+\') || Token[0]==\'0\')
       return false;

    if(Token[0]==\'+\' && (Token[1]==\'0\' || !isdigit(Token[1])))
       return false;

    int length=strlen(Token);

    for(int count=1;count<length;count++)
    {
       if(!isdigit(Token[count]))
      return false;
    }

    return true;
 }


 //-----------------------  isNegativeNumber( )  -------------------------//


 const bool isNegativeNumber(const char* Token)
 {
    if(Token[0]!=\'-\' || !isdigit(Token[1]) || Token[1]==\'0\')
       return false;

    int length=strlen(Token);

    for(int count=2;count<length;count++)
    {
       if(!isdigit(Token[count]))
      return false;
    }

    return true;
 }


 //------------------------  isPositiveDigit( )  -------------------------//


 const bool isPositiveDigit(const char* Token)
 {
    if(strlen(Token)>2 || strlen(Token)==0)
       return false;

    else if(strlen(Token)==2 && (Token[0]!=\'+\' ||
                       Token[1]==\'0\' || !isdigit(Token[1])))
       return false;

    else if(strlen(Token)==1 && (Token[0]==\'0\' || !isdigit(Token[0])))
       return false;

    return true;
 }


 //------------------------  isNegativeDigit( )  -------------------------//


 const bool isNegativeDigit(const char* Token)
 {
    if(strlen(Token)!=2 || Token[0]!=\'-\' ||
                       Token[1]==\'0\' || !isdigit(Token[1]))
       return false;

    return true;
 }


 //-------------------------  isIdentifier( )  ---------------------------//


 const bool isIdentifier(const char* Token)
 {
    if(!isalpha(Token[0]) && Token[0]!=\'_\')
       return false;

    int length=strlen(Token);

    for(int count=1;count<length;count++)
    {
       if(!isalpha(Token[count]) && !isdigit(Token[count]) &&
                             Token[count]!=\'_\')
      return false;
    }

    return true;
 }


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


 const bool isKeyword(const char* Token)
 {
    if(strlen(Token)>16 || strlen(Token)==0)
       return false;

    const char Keywords[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 count=0;count<64;count++)
    {
       if(strcmpi(Keywords[count],Token)==0)
      return true;
    }

    return false;
 }



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

 
 
Didn't find what you were looking for? Find more on Program that will read a token from a user and find its type