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  -------------------------------//

    Related Post:
  1. Program that calculates area of triangle and rectangle using inheritance

  2. Program to illustrate the use of call-by-value method in functions

  3. Program to show the projection of 3D objects using Cavalier Oblique Parallel Projection onto xy-plane (i.e. angle=45 deg)

  4. Identifer recognisition Integer Unsigned real number with optional integer part

  5. Program to determine whether the given function is a Cubic Spline or not

  6. Program to create a queue using static memory allocation

  7. Program that are completely portable across different operating systems

  8. Implement a Matrix class representing 2-dimensional matrix with the following functionalities i.e. operators + , - , * , == , += &lt;&lt; , &gt;&gt; etc

  9. Program to display string triangle

  10. Program of reflection in x axis

  11. Program to show the projection of 3D objects using Orthographics Parallel Projection w.r.t. xy-plane and along vector v

  12. Program to implement a Translator that reads an Infix Expression translates it into a Postfix Expression and evaluate the Postfix Expression

  13. Write a function power() to raise a number m to a power n

  14. Program of telephone directory

  15. Program of LL parser1

  16. Program to illustrate the implementation of Stack as an Arithmetic Expression Evaluater

  17. Program to illusrate data conversion b/w built-in data types and user defined data types(in char)

  18. Program to illustrate unary operator(decrement operator) overloading without return type

  19. Program to illustrate the use of nested structures

  20. Program to show the projection of 3D objects using Orthographics Projection Parallel onto xy-plane

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