The Programming Project: pointers
Showing posts with label pointers. Show all posts
Showing posts with label pointers. Show all posts

Wednesday, July 10, 2013

Tokens

Given a program statement in C, list the tokens in the order as they appear in the source statement. Assume that the source statement may contain more than one blank space between two successive tokens and classify the tokens

      

#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>
void  classification(char iden[]);
#define TRUE 1
#define FALSE 0
int main(void)
{

     char *str,ar[10];
    char *token;
     int FLAG=FALSE,count=0,n,len,i,t=0;
    printf("\n Enter the maximum length of the statement:");
     scanf(" %d", &n);
     str=(char *)malloc( n*sizeof(char));
    fflush(stdin);
    printf("\n Enter the C statement:");
     scanf("%[^\n]",str);
     len=strlen(str);
     if(len>n)
        {
        printf("\n Length of the statement is greater than the input length:");
      exit(1);
        }
    token=str;
 //   printf("\n The tokens are:\n");
    while(1)
        {
        while(*token!= ' ')
            {
            if(*token== '\0')
                {
                FLAG=TRUE;
                break;
                }
             //    printf("%c",*token);
                ar[count++]=*token;
                token++;
                }
                ar[count]='\0';
      //      printf(" %s ",ar);
             classification(ar);
      //   printf("\n");
          if(FLAG==TRUE)
            break;
        while(*token== ' ')    
            {
            *token++;
            if(*token== '\0')
                {
                     FLAG=TRUE;
                break;
                }
                }
          if(FLAG==TRUE)
                break;
          count=0;
          }
     return 0;
}
void classification( char ar[])
{

int  FLAG=FALSE,len,count=0,flag=TRUE,numb=0,d,t=0;
char *keyword[]={ "if","for","while","int","float","char"};
char *operatr[]={ "+","-","*","/","=","<",">"};
char *symbols[]={ "{","}",",","(",")",";","?",":"};
int i=0;
len=strlen(ar);
for(i=0;i<len;i++)
            if(ar[i]==' ')
                t++;
            if(t==len)
         return;
if(len>8)
    FLAG=FALSE;
for(i=0;i<=5;i++)
    {
    if(strcmp(keyword[i],ar)==0)
      {
      printf("\n%s\tis a Keyword:",ar);
      FLAG=TRUE;
      flag=FALSE;
      }
    }
    i=0;
for(i=0;i<=6;i++)
    {
    if(strcmp(operatr[i],ar)==0)
        {
        printf("\n%s\tis a Operator:",ar);
        FLAG=TRUE;
        flag=FALSE;
        }
    }
    i=0;
for(i=0;i<=7;i++)
    {
    if(strcmp(symbols[i],ar)==0)
        {
        printf("\n%s\tis a Symbol:",ar);
        FLAG=TRUE;
        flag=FALSE;
        }
    }
if ( ((65 <= ar[0] && ar[0] <= 93 )|| ( 97 <= ar[0] && ar[0] <= 122)) && len <= 8 && FLAG==FALSE)
    {
    printf("\n%s\tis an Identifier:",ar);
    FLAG=TRUE;
    flag=FALSE;
    }
for(i=0;i<len;i++)
    {
    if(49 <= ar[i] && ar[i] <=57 )
        count++;
    else
       continue;
    }
if(count==len && flag==TRUE && FLAG==FALSE)
    {
    printf("\n%s\tis a Constant:",ar);
    FLAG=TRUE;
    }
i=0;
if( FLAG==FALSE)
    {
    if(ar[0]=='-' || ar[0]=='0')
        numb=atoi(ar);
    if(numb <= 0)
    printf("%d\tis not a valid Constant ",numb);
   else
    printf("\n%s\tis an INVALID TOKEN:",ar);
   }
printf("\n");
return;
}

Thursday, March 28, 2013

C Program #18


/* Implementation of queue and few basic operations!*/

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
#define TRUE 1
#define FALSE 0
struct queue{
                        int front;
                        int rear;
                        int items[MAXSIZE];
                        };
struct stack{
                        int top;
                        int items[MAXSIZE];
                        };
struct stack stk;
struct queue p,q,temp;
int pop(struct stack *);
int remove(struct queue *);
int Isempty(struct queue *);
void push(struct stack *,int);
int isempty_st(struct stack *);
void insert(struct queue *,int);
void reverse_queue(struct queue *);
void display_front_rear(struct queue *);
void display_rear_front(struct queue *);
void append(struct queue *,struct queue *);
void replace(struct queue *,int,int);
void display_front_rear_unchanged(struct queue *);
void display_rear_front_unchanged(struct queue *);
int main()
{
            int choice,x,ins,e;
             stk.top=-1;
            p.front=p.rear=MAXSIZE-1;
            q.front=q.rear=MAXSIZE-1;
            temp.front=temp.rear=MAXSIZE-1;
            do
                        {
                        printf("\n Press 1 to add elements in Queue:");
                        printf("\n Press 2 to remove elements from Queue:");
                        printf("\n Press 3 to append queue Q at the end of queue P:");
                        printf("\n Press 4 to print elements of queue P from front to end with P  becoming empty:");
                        printf("\n Press 5 to print elements of queue P from rear to front with P becoming empty:");
                        printf("\n Press 6 to print elements of queue P from front to end with P remaining same:");
                        printf("\n Press 7 to print elements of queue P from rear to front with P remaining same:");
                        printf("\n Press 8 to reverse the queue P:");
                        printf("\n Press 9 to replace a specific element of a queue:");
                        printf("\n Type 10 to exit:");
                        scanf("%d",&choice);
                        switch(choice)
                                    {
                                    case 1:
                                                printf("\n Enter 1 to insert in queue P, 2 to insert in queue Q:");
                                                            scanf("%d",&x);
                                                                        switch(x)
                                                                                    {
                                                                                    case 1:
                                                                        printf("\n Enter the element to be inserted:");
                                                                                    scanf("%d",&ins);
                                                                                    insert(&p,ins);
                                                                                    break;
                                                                                    case 2:
                                                                        printf("\n Enter the element to be inserted:");
                                                                                    scanf("%d",&ins);
                                                                                    insert(&q,ins);
                                                                                    break;
                                                                                    }
                          break;
                          case 2:
                                    printf("\n Enter 1 to remove in queue P, 2 to remove in queue Q:");
                                                            scanf("%d",&x);
                                                                        switch(x)
                                                                                    {
                                                                                    case 1:
                                                            printf("\n Element removed is: %d\n",remove(&p));
                                                                                    break;
                                                                                    case 2:
                                                            printf("\n Element removed is: %d\n",remove(&p));
                                                                                    break;
                                                                                    }
                                    break;
                                    case 3:
                                    append(&p,&q);
                                    break;
                                    case 4:
                                    display_front_rear(&p);
                                    break;
                                    case 5:
                                    display_rear_front(&p);
                                    break;
                                    case 6:
                                    display_front_rear_unchanged(&p);
                                    break;
                                    case 7:
                                    display_rear_front_unchanged(&p);
                                    break;
                                    case 8:
                                    reverse_queue(&p);
                                    break;
                                    case 9:
                                    printf("\n Enter the element to be replaced:");
                                    scanf("%d",&e);
                                    printf("\n Enter the element to be placed:");
                                    scanf("%d",&x);
                                    replace(&p,e,x);
                                    break;
                                    }
                        }while(choice>=1 && choice<=9);
return 0;
}
void replace(struct queue *p,int e,int x)
            {
            int y;
             while(!Isempty(p))
                        {
                        y=remove(p);
                        if(y==e)
                                    insert(&temp,x);
                        else
                                    insert(&temp,y);
                        }
            printf("\n Elements of the queue after replacing\n");
            while(!Isempty(&temp))
                        {
                        x=remove(&temp);
                        printf("%d ",x);
                        insert(p,x);
                        }
            }
void reverse_queue(struct queue *p)
            {
   int x;
            while(!Isempty(p))
                        {
                        x=remove(p);
                        push(&stk,x);
                        }
            printf("\n Elements of the queue (after reversing)\n");
            while(!isempty_st(&stk))
                        {
                        x=pop(&stk);
                        printf("%d ",x);
                        insert(p,x);
                        }
            }
void display_rear_front_unchanged(struct queue *p)
            {
            int x;
            while(!Isempty(p))
                        {
                        x=remove(p);
                        push(&stk,x);
                        insert(&temp,x);
                        }
            printf("\n Elements of the queue (rear to front)\n");
            while(!isempty_st(&stk))
                        {
                        printf("%d ",pop(&stk));
                        x=remove(&temp);
                        insert(p,x);
                        }
            }
void display_front_rear_unchanged(struct queue *p)
            {
            int x;
            while(!Isempty(p))
                        insert(&temp,remove(p));
            printf("\n Elements of the queue (front to rear)\n");
            while(!Isempty(&temp))
                        {
                        x=remove(&temp);
                        printf("%d ",x);
                        insert(p,x);
                        }
            }
void display_rear_front(struct queue *p)
            {
            while(!Isempty(p))
                        push(&stk,remove(p));
            printf("\n Elements of the queue (rear to front)\n");
            while(!isempty_st(&stk))
                        printf("%d ",pop(&stk));
            }
int isempty_st(struct stack *stk)
            {
             return((stk->top==-1)?TRUE:FALSE);
            }
int pop(struct stack *stk)
            {
            return(stk->items[(stk->top)--]);
            }
void push(struct stack *stk,int x)
            {
            stk->items[++stk->top]=x;
            }
void display_front_rear(struct queue *q)
            {
            printf("\n Elements of the queue (front to end)\n");
            while(!Isempty(q))
                        printf("%d ",remove(q));
            }
void append(struct queue *p, struct queue *q)
            {
            while(!Isempty(q))
                        insert(p,remove(q));
            printf("\n Appended........\n");
            }
int remove(struct queue *q)
            {
                        if(Isempty(q))
                                    {
                                    printf("\n Queue underflow:");
                                    exit(1);
                                    }
                        if(q->front==MAXSIZE-1)
                                    q->front=0;
                        else
                                    (q->front)++;
                        return(q->items[q->front]);
            }
int Isempty(struct queue *q)
            {
            return((q->front==q->rear)?TRUE:FALSE);
            }
void insert(struct queue *q,int x)
            {
            if(q->rear==MAXSIZE-1)
                        q->rear=0;
            else
                        (q->rear)++;
            if(q->front==q->rear)
                        printf("\n Queue Overflow...:");
                        q->items[q->rear]=x;
   return;
            }

Thursday, March 21, 2013

C Program #15


/*Dynamic Implementation of Stack With Basic operations!*/

#include<stdio.h>
#include<malloc.h>
#define TRUE 1
#define FALSE 0

struct stack
                        {
                        int item;
                        struct stack *right;
                        };
typedef struct stack node;
node *push(node *);
node *push1(node *,int);
void display(node *);
int Isempty(node *);
void top2bottomu(node *);
void top2bottom(node *);
void pop(node *);
int pop1(node *);
void duplicate(node *);
void filestack(node *, FILE *fp);
void stack2(node *);
int Isequal(node *,node *);
int length(node*);
int main()
{
int choice;
FILE *fp;
fp=fopen("data.txt","w");
node *root,*data,*root1,*data1,*root2,*data2;
root=(node *)malloc(sizeof(node));
root->right=NULL;
data=root;
root1=(node *)malloc(sizeof(node));
root1->right=NULL;
data1=root1;
root2=(node *)malloc(sizeof(node));
root2->right=NULL;
data2=root2;
            do
            {
                        printf("\n Press 1 to push a element:");
                        printf("\n Press 2 for bottom to top:");
                        printf("\n Press 3 to check for empty:");
                        printf("\n Press 4 to print elements from top to bottom,stack becoming empty:");
                        printf("\n Press 5 to pop:");
                        printf("\n Press 6 to print elements from top to bottom,stack unchanged:");
                        printf("\n Press 7 to duplicate the top element of the stack:");
                        printf("\n Press 8 to read from a file and print in reverse order:");
                        printf("\n Press 9 to push element in second stack:");
                        printf("\n Press 10 to display second stack:");
                        printf("\n Press 11 to check if two stacks are equal:");
                        printf("\n Enter choice:");
                        printf("\n Type 12 to exit:");
                        scanf("%d",&choice);
                        switch(choice)
                                    {
                                                case 1:
                                                data=push(data);
                                                data->right=NULL;
                                                break;
                                                case 2:
                                                display(root);
                                                break;
                                                case 3:
                                                if(Isempty(root))
                                                            printf("\n Stack is empty:\n");
                                                else
                                                            printf("\n Stack is not empty:\n");
                                                break;
                                                case 4:
                                                top2bottom(root);
                                                data=root;
                                                break;
                                                case 5:
                                                pop(root);
                                                data=root;
                                                break;
                                                case 6:
                                                top2bottomu(root);
                                                break;
                                                case 7:
                                                duplicate(root);
                                                break;
                                                case 8:
                                                filestack(root1,fp);
                                                break;
                                                case 9:
                                                data2=push(data2);
                                                data2->right=NULL;
                                                break;
                                                case 10:
                                                display(root2);
                                                break;
                                                case 11:
                                                if(Isequal(root,root2))
                                                            printf("\n Stack are equal:\n");
                                                else
                                                            printf("\n Stack are not equal:\n");
                                                break;
                                                default:
                                                break;

                                    }
                        }
                        while(1<=choice && choice<=11);
            return 0;
 }
int Isequal(node *data1,node *data2)
            {
            int flag=FALSE,i;
            if(Isempty(data1) || Isempty(data2))
                        {
                        if(Isempty(data1) && Isempty(data2))
                                    return (TRUE);
                        else
                                    return (FALSE);
                        }
            else
                        {
                         if(length(data1)!=length(data2))
                         //         {printf("{%d,%d}\n",length(data1),length(data2));
                                    return (FALSE);//}
                         else
                                    {
                                    for(i=0;i<=length(data1);i++)
                                                {
                                                if(data1->item!=data2->item)
                                                            flag=TRUE;
                                                data1=data1->right;
                                                data2=data2->right;
                                                }
                                    if(flag==FALSE)
                                                return (TRUE);
                                    else
                                                return (FALSE);
                                    }
                         }
            }
int length(node *data)
            {
            int k=0;
            while(data->right!=NULL)
                        {
                        k++;
                        data=data->right;
                        }
            return(k-1);
            }
void filestack(node *data, FILE *fp)
            {
            node *p;
            p=data;
            fclose(fp);
            fp=fopen("data.txt","w");
            int n,i,x;
            printf("\n Enter the number of elements to be entered in file:");
            scanf("%d",&n);
            for(i=1;i<=n;i++)
                        {
                        printf("\n Enter the %d# elemet:",i);
                        scanf("%d",&x);
                        fprintf(fp,"%d ",x);
      }
            fclose(fp);
            fp=fopen("data.txt","r");
            printf("\n Elements in the file:");
            for(i=1;i<=n;i++)
                        {
                        fscanf(fp,"%d",&x);
                        printf("%d ",x);
                        data=push1(data,x);
                        data->right=NULL;
                        }
            fclose(fp);
            printf("\n Elements from the file in reverse order:");
            top2bottomu(p);
            printf("\n");
            }
node *push1(node *data,int x)
            {
  //        node *p;
            while(data->right!=NULL)
                        data=data->right;
            data->item=x;
            data->right=(node *)malloc(sizeof(node));
            data=data->right;
            return(data);
            }
void duplicate(node *data)
            {
            node *p,*q;
            q=(node *)malloc(sizeof(node));
            p=data;
            int x;
            while(p->right->right!=NULL)
                        p=p->right;
            x=p->item;
            p->right->item=x;
            p->right->right=q;
            q->right=NULL;
   }
void top2bottom(node *data)
            {
                        while(!Isempty(data))
                                    {
                                    pop(data);
                                    }
            }
void pop(node *data)
            {
            node *p;
            if(Isempty(data))
                        {
                                    printf("\n Stack is empty, cannot be popped:\n");
                        }
            else
                        {
                                    if(data->right->right==NULL)
                                                {
                                                printf("\nElement popped out is %d \n",data->item);
                                                p=data;
                                                free(data);
                                                p->right=NULL;
                                                }
                                    else
                                                {
                                                while(data->right->right->right!=NULL)
                                                            data=data->right;
                                                            p=data->right;
                                                            printf("\nElement popped out is %d \n",p->item);
                                                            free(p);
                                                            data->right->right=NULL;

                                                }
                        }
            }
void top2bottomu(node *data)
            {
            node *q;
            q=data;
            int k=0,i,*a;
            while(data->right!=NULL)
                        {
                        k++;
                        data=data->right;
                        }
            //printf("%d.....\n",k);
            a=(int*)malloc(k*sizeof(int));
            for(i=0;i<k;i++)
                        {
                        *(a+i)=q->item;
                        q=q->right;
                        }
            for(i=k-1;i>=0;i--)
                        printf("%d ",*(a+i));

   free(a);
            printf("\n");
            }
int Isempty(node *data)
            {
                        if(data->right==NULL)
                                    return (TRUE);
                        else
                                    return (FALSE);
            }
node *push(node *data)
            {
  //        node *p;
            while(data->right!=NULL)
                        data=data->right;
            printf("\n Enter the element to be inserted:");
            scanf("%d",&data->item);
            data->right=(node *)malloc(sizeof(node));
            data=data->right;
            return(data);
            }
void display(node *data)
            {
                        if(Isempty(data))
                                    printf("\n Stack is empty, nothing to display:\n");
            else{    while(data->right!=NULL)
                                    {
                                    printf("%d ",data->item);
                                    data=data->right;
                                    }
                         }
            printf("\n");
            }
int pop1(node *data)
            {
            node *p;
            if(Isempty(data))
                        {
                                    printf("\n Stack is empty, cannot be popped:\n");
                        }
            else
                        {
                                    if(data->right->right==NULL)
                                                {
            p=data;
                                                p->right=NULL;
            return(data->item);
                                                }
                                    else
                                                {
                                                while(data->right->right->right!=NULL)
                                                            data=data->right;
                                                            p=data->right;
                                                            data->right->right=NULL;
                                                            return(p->item);
                                                            }
                        }
            }