Thursday, 11 June 2015

Program that are completely portable across different operating systems

Code for Program that are completely portable across different operating systems in C++ Programming

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

 int main( )
    {
       clrscr( );

       fstream File("CP-25.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(EXIT_FAILURE);
      }

       char Data[100]={NULL};

       do
      {
         strset(Data,NULL);

         File.getline(Data,100);

         if(strcmp(Data,NULL)==0)
        break;

         long number=atol(Data);

         long byte_1=0;
         long byte_2=0;
         long byte_3=0;
         long byte_4=0;

         byte_1=(number&0x000000FF);

         number>>=8;

         byte_2=(number&0x000000FF);

         number>>=8;

         byte_3=(number&0x000000FF);

         number>>=8;

         byte_4=(number&0x000000FF);

         number=byte_1;

         number<<=8;

         number=(number|byte_2);

         number<<=8;

         number=(number|byte_3);

         number<<=8;

         number=(number|byte_4);

         cout<<Data<<" converts to "<<number<<endl;
      }
       while(1);

       File.close( );

       getch( );
       return 0;
    }

Program to illustrate printing data on the printer


Code for Program to illustrate printing data on the printer in C++ Programming

 #include<iostream.h>
 #include<fstream.h>
 #include<stdlib.h>
 #include<conio.h>

 main(int argc,char *argv[])
    {
       clrscr();

       if(argc!=2)
      {
         cout<<"\n Format : File - Print File Name "<<endl;
         exit(0);
      }

       char ch='\0';

       ifstream input_file;
       ofstream output_file;

       input_file.open(argv[1]);
       output_file.open("PRN");   // or LPT1while(input_file.get(ch)!=0);

       output_file.put(ch);

       getch();
       return 0;
    }

Program that uses this DFA and validates whether an entered string is valid float or not

Code for Program that uses this DFA and validates whether an entered string is valid float or not in C++ Programming

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

int main()
{
    int flag = 0;
    char ch = NULL;
    char state = '0';
    char token[20] = {NULL};
    char stt[5][3] = {NULL, '4', '5',
            '0', '1', '2',
            '1', '1', '2',
            '2', '3', NULL,
            '3', '3', NULL};

    cout<<"Input float data : ";
    cin>>token;

    for(int i=0; token[i] != NULL; i++)
    {
        if(isdigit(token[i]))
        {
            ch = '4';
        }
        elseif (token[i] == '.')
        {
            ch = '5';
        }
        else
        {
            flag=1;
            printf("Invalide value");
            break;
        }

        for(int j=1; j<3 && ch != stt[0][j]; j++);
        for(int k=1; k<5 && state != stt[k][0]; k++);
        state =stt[k][j];
        if(state==NULL)
        {
            flag=1;
            printf("Invalide value");
            break;
        }
    }
    if(flag==0)
    {
        printf("Valide value");
    }
    getch();
    return 0;
}

Comment line in c

Code for Comment line in c in C++ Programming

#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <process.h>
#include <string.h>

int main()
{
    char stt[8][4] = {NULL, '7','8','9',
                        '0', '1', NULL, NULL,
                        '1', '2', '4', NULL,
                        '2', '3', '3', '3',
                        '3', NULL, NULL, '3',
                        '4', '4', '5', '4',
                        '5', '6', '5', '4',
                        '6', NULL, NULL, NULL};
    char ch, state = '0';
    char variable[40];
    int flag=0;

    cout<<"Input c Comment line : ";
    cin>>variable;

    for(int i=0; i<40 && variable[i] != NULL; i++)
    {
        if(variable[i] == '/')
        {
            ch = '7';
            flag=flag+1;
        }
        elseif(variable[i] == '*')
        {
            ch ='8';            
        }
        else
        {
            ch ='9';
        }

        for(int j=1; stt[0][j] != ch; j++);
        for(int k=1; stt[k][0] != state; k++);

        state = stt[k][j];
        if (state == NULL)
        {
            printf("Invalide comment line");
            getchar();
            exit(0);
        }
    }
    if (flag == 1)
    {    
        printf("Invalide comment line");
        getchar();
        exit(0);
    }
    else
    {
        printf("Valide Comment line");
    }
    getchar();
    return 0;
}

Identifer recognisition Integer Unsigned real number with optional integer part

Code for Identifer recognisition Integer Unsigned real number with optional integer part in C++ Programming

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<ctype.h>
int main()
{
    clrscr();
    int flag=0;
    char state = '0';
    char ch = NULL;
    char token[10]={NULL};
    char stt[6][4] = {    NULL,'7','8','9',
                '0','1','2','3',
                '1','1','1',NULL,
                '2',NULL,'2','3',
                '3',NULL,'4',NULL,
                '4',NULL,'4',NULL    };

    cout << "Enter Identifier, Integer or Real Number : ";
    cin >> token;
    for(int i=0; i < strlen(token); i++)
    {
        if (isalpha(token[i]))
            ch='7';
        elseif (isdigit(token[i]))
            ch = '8';
        elseif (token[i] == '.')
            ch = '9';
        for(int j=1; j<=3 && ch != stt[0][j]; j++);
        for(int k=1;k<=5 && state != stt[k][0];k++);
        state = stt[k][j];
            if(state == NULL)
            {
                cout << endl << "Invalid ....";
                flag=1;
                break;
            }
    }
    if (flag==0)
        cout<<"valide string";
    getch();
    return 0;
}

Tuesday, 9 June 2015

Program to illustrate the functions returning pointers


Code for Program to illustrate the functions returning pointers in C++ Programming



 



 
 #include<iostream.h>
 #include<conio.h>

 int *function();

 main()
    {
       clrscr();

       int *p;

       p=function();

       cout<<"\n Value return by the function returning pointer :"<<endl;
       cout<<"\t value of *p = "<<*p<<endl;

       getch();
       return 0;
    }


 /*************************************************************************///---------------------------  function( )  -----------------------------///*************************************************************************/int *function()
    {
       int i=10;
       int *j;

       j=&i;

       return j;
    }



 

Program to convert an Infix Expression into a Postfix Expression using Linked List as a Stack


Code for Program to convert an Infix Expression into a Postfix Expression using Linked List as a Stack in C++ Programming



 



 



 
 # include <iostream.h>
 # include   <string.h>
 # include   <stdlib.h>
 # include    <conio.h>

 struct node
 {
    char data;

    node *next;
 };

 node *top=NULL;
 node *bottom=NULL;
 node *entry;
 node *last_entry;
 node *second_last_entry;

 void push(constchar);
 constchar pop( );

 void infix_to_postfix(constchar *);


 int main( )
    {
       clrscr( );

       char Infix_expression[100]={NULL};

       cout<<"\n\n Enter the Infix Expression : ";
       cin>>Infix_expression;

       infix_to_postfix(Infix_expression);

       getch( );
       return 0;
    }

 /*************************************************************************//*************************************************************************///------------------------  Function Definitions  -----------------------///*************************************************************************//*************************************************************************//*************************************************************************///-----------------------------  push(const char)  ----------------------///*************************************************************************/void push(constchar Symbol)
    {
       entry=new node;

       if(bottom==NULL)
      {
         entry->data=Symbol;
         entry->next=NULL;
         bottom=entry;
         top=entry;
      }

       else
      {
         entry->data=Symbol;
         entry->next=NULL;
         top->next=entry;
         top=entry;
      }
    }

 /*************************************************************************///--------------------------------  pop( )  -----------------------------///*************************************************************************/constchar pop( )
    {
       char Symbol=NULL;

       if(bottom==NULL)
      cout<<"\n\n\n\t ***  Error : Stack is empty. \n"<<endl;

       else
      {
         for(last_entry=bottom;last_entry->next!=NULL;
                         last_entry=last_entry->next)
        second_last_entry=last_entry;

         if(top==bottom)
        bottom=NULL;

         Symbol=top->data;

         delete top;

         top=second_last_entry;
         top->next=NULL;
      }

       return Symbol;
    }

 /*************************************************************************///---------------------  infix_to_postfix(const char *)  ----------------///*************************************************************************/void infix_to_postfix(constchar *Infix)
    {
       char Infix_expression[100]={NULL};
       char Postfix_expression[100]={NULL};

       strcpy(Infix_expression,"(");
       strcat(Infix_expression,Infix);
       strcat(Infix_expression,")");

       char Symbol[5]={NULL};
       char Temp[5]={NULL};

       for(int count=0;count<strlen(Infix_expression);count++)
      {
         Symbol[0]=Infix_expression[count];

         if(Symbol[0]=='(')
        push(Symbol[0]);

         elseif(Symbol[0]==')')
        {
           Symbol[0]=pop( );

           while(Symbol[0]!='(')
              {
             strcat(Postfix_expression,Symbol);

             Symbol[0]=pop( );
              }
        }

         elseif(Symbol[0]=='^' || Symbol[0]=='*' || Symbol[0]=='/'
                    || Symbol[0]=='+' || Symbol[0]=='-')
        {
           if(Symbol[0]=='*' || Symbol[0]=='/')
              {
             Temp[0]=pop( );

             while(Temp[0]=='^' || Temp[0]=='*' || Temp[0]=='/')
                {
                   strcat(Postfix_expression,Temp);

                   Temp[0]=pop( );
                }

             push(Temp[0]);
              }

           elseif(Symbol[0]=='+' || Symbol[0]=='-')
              {
             Temp[0]=pop( );

             while(Temp[0]!='(')
                {
                   strcat(Postfix_expression,Temp);

                   Temp[0]=pop( );
                }

             push(Temp[0]);
              }

           push(Symbol[0]);
        }

         else
        strcat(Postfix_expression,Symbol);
      }

       cout<<"\n\n Postfix Expression : "<<Postfix_expression;
    }