Coders PlayGround

Learn to code and change the future!

Friday, 4 August 2017

Phonebook in C

August 04, 2017 0
Hello guys
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct phonebook
{
 char fName[40];
 char lName[40];
 char phone[10];
 struct phonebook *next;
};

struct phonebook *start=NULL;

struct phonebook* getData()
{
 return ((struct phonebook *)malloc(sizeof(struct phonebook)));
}

void insertData()
{
 struct phonebook *temp,*newNode;
 newNode = NULL;
 temp = start;
 newNode = getData();
 printf("\nEnter First Name: ");
 scanf("%s",&newNode->fName);
 printf("Enter Last Name: ");
 scanf("%s",&newNode->lName);
 printf("Enter Phone Number: ");
 scanf("%s",&newNode->phone);
 if(temp == NULL)
 {
  newNode->next = newNode;
  start = newNode;
 }
 else if(strcmp(temp->fName,newNode->fName)>=0)
 {
  while(temp->next != start)
   temp = temp->next;
  temp->next = newNode;
  newNode->next = start;
  start = newNode;
 }
 else
 {
  while(temp->next != start && strcmp(temp->next->fName,newNode->fName)<=0)
   temp = temp->next;
  newNode->next = temp->next;
  temp->next = newNode;
 }
 
 printf("\n");
}

void displayList(struct phonebook *temp)
{
 if(temp == NULL)
  printf("No Content....");
 else
 {
  printf("\nFirst Name \tLast Name \tPhone Number");
  printf("\n----------------------------------------------");
  while(temp->next != start)
  {
   printf("\n%s",temp->fName);
   printf(" \t%s",temp->lName);
   printf(" \t%s",temp->phone);
   temp = temp->next;
  }
  printf("\n%s",temp->fName);
  printf(" \t%s",temp->lName);
  printf(" \t%s",temp->phone);
 }
 printf("\n"); 
}
void copyToFile(struct phonebook *temp)
{
 FILE *fp;
 fp = fopen("PHONEBOOK.txt","w");
 if(fp == NULL)
 {
  printf("\nFile Not Opened");
 }
 else
 {
  if(fp)
   fprintf(fp,"FName,LName,Number\n");
  do
  {
   fprintf(fp,"%s,%s,%s\n",temp->fName,temp->lName,temp->phone);
   temp = temp->next;
  }while(temp != start);
  fclose(fp);
 }
 printf("\n");
}
int main()
{
 int input,i,n,ch;
 do
 {
  printf("\n-------------PHONEBOOK------------");
  printf("\n1.Insert Records");
  printf("\n2.Display Records\n3.END\n");
  printf("\nEnter your choice: ");
  scanf("%d",&input);
  switch(input)
  {
   case 1:
    printf("Enter number of records required: ");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
     printf("\nInsert Data for Record %d",i);
     insertData();
    }
    copyToFile(start);
    displayList(start);
    break;
   case 2:
    displayList(start);
    break;
   case 3:
    printf("PROGRAM ENDS !!!");
    break;
   default:
    printf("Invalid Choice !!!");
  }
  
 }while(input!=3);
 
 printf("\n");
}

/*


-------------PHONEBOOK------------
1.Insert Records
2.Display Records
3.END

Enter your choice: 1
Enter number of records required: 2

Insert Data for Record 1
Enter First Name: renita
Enter Last Name: lobo
Enter Phone Number: 9856856254


Insert Data for Record 2
Enter First Name: gyanendra 
Enter Last Name: maurya
Enter Phone Number: 9878845235



First Name      Last Name       Phone Number
----------------------------------------------
renita       lobo   9856856254
gyanendra    maurya   9878845235

-------------PHONEBOOK------------
1.Insert Records
2.Display Records
3.END

Enter your choice: 2

First Name      Last Name       Phone Number
----------------------------------------------
renita       lobo   9856856254
gyanendra    maurya   9878845235

-------------PHONEBOOK------------
1.Insert Records
2.Display Records
3.END

Enter your choice: 3
PROGRAM ENDS !!!

--------------------------------
Process exited with return value 10
Press any key to continue . . .


*/

Read More

Queue using Array

August 04, 2017
Hello guys,

#include<stdio.h>
#define MAX 5
int front= -1,rear = -1, queue[MAX];
void disp()
{
 int i;
 if(front==-1&&rear==-1)
 {
  printf("\n Queue is empty!!!");
 }
 else
 {
  printf("\n Queue elements are : ");
  for(i=front;i<=rear;i++)
   printf("%d ",queue[i]);  
 }
}
void enqueue(int x)
{
 if(rear == MAX-1)
  printf("\nQueue overflow");
 else
 {
  if(front==-1&&rear==-1)
  {
   front = 0;
   rear = 0;
   queue[rear] = x;
   printf("\n %d successfully enqueued!!!",x);
   disp();
  }
  
  else
  {
   rear++;
   queue[rear] = x;
   printf("\n %d successfully enqueued!!!",x);
   disp();
  }
 }
}
void dequeue()
{
 //front
 int a;
 a=queue[front];
 if(front==-1&&rear==-1)
 {
  printf("\n Queue underflow!!!");
 }
 else if(front == rear)
 {
  front = -1;
  rear = -1;
 } 
 else
 {
  front++;
  printf("\n %d successfully dequeued",a);
  disp();
 }
}
void main()
{
 int ch,e;
 do
 {
  printf("\n===MENU===");
  printf("\n 1. ENQUEUE");
  printf("\n 2. DEQUEUE");
  printf("\n 3. DISPLAY");
  printf("\n 4. EXIT");
  printf("\n Enter your choice : ");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1 : if(rear == MAX-1)
      printf("\n Element cannot be inserted \n Queue overflow");
      else{
      printf("\n Enter the element : ");
      scanf("%d",&e);
      enqueue(e);}
      break;
   case 2 : dequeue();
      break;
   case 3 : disp();
      break;
   case 4 : printf("\n Thank you!!! \n ");
      break;
   default : printf("\n Invalid Input ");
      break;
  }
 
 }while(ch!=4);

}
/*
Output : 

===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 1

 Enter the element : 56

 56 successfully enqueued!!!
 Queue elements are : 56 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 1

 Enter the element : 27

 27 successfully enqueued!!!
 Queue elements are : 56 27 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 1

 Enter the element : 44

 44 successfully enqueued!!!
 Queue elements are : 56 27 44 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 1

 Enter the element : 48

 48 successfully enqueued!!!
 Queue elements are : 56 27 44 48 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 1

 Enter the element : 29

 29 successfully enqueued!!!
 Queue elements are : 56 27 44 48 29 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 1

 Element cannot be inserted 
 Queue overflow
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 2

 56 successfully dequeued
 Queue elements are : 27 44 48 29 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 2

 27 successfully dequeued
 Queue elements are : 44 48 29 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 2

 44 successfully dequeued
 Queue elements are : 48 29 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 2

 48 successfully dequeued
 Queue elements are : 29 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 2

===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 2

 Queue underflow!!!
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 3

 Queue is empty!!!
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 4

 Thank you!!! 
 lobo@lobo-Compaq-Presario-CQ60-Notebook-PC:~/Desktop$ 

*/

Read More

Postfix and Prefix

August 04, 2017 0
Hello guys,

#include<stdio.h>
void prefix();
void postfix();
char stack[50];
int s[50];
char post[50],pre[50];
int p = -1;
int top = -1;
int rtop = -1;
int xtop=-1;      
 
xpush(int elem)
{     
 s[++xtop]=elem;                
}
 
int xpop()
{                      
 return(s[xtop--]);
}



void push(char x)
{
        stack[++top] = x;
}
 
char pop()
{
        if(top == -1)
                return -1;
        else
            return stack[top--];
}
 
int priority(char x)
{
    if(x == '(')
        return 0;
    if(x == '+' || x == '-')
        return 1;
    if(x == '*' || x == '/')
        return 2;
}


void main()
{
 int choice;
 do
 {
  printf("\n\n----MENU----\n1.INFIX - POSTFIX\n2.INFIX - PREFIX\n3.EXIT\n");
  printf("Enter your choice\n");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1 : postfix();break;
   case 2 : prefix();break;
   case 3 : printf("~~~~~ Thank You ~~~~~\n");break;
   default: printf("INVALID INPUT !!!!\n");
  }
 }while(choice!=3);
}

void postfix()
{
  p=-1;
     char exp[20];
        char *e, x;
        printf("Enter the expression :: ");
        scanf("%s",exp);
        printf("\nPostfix expression :: ");
        e = exp;
        while(*e != '\0')
        {
                if(isalnum(*e))
                {
     printf("%c",*e);
                    post[++p]=*e;
                }
                else if(*e == '(')
              push(*e);
              
          else if(*e == ')')
                {
                    while((x = pop()) != '(')
                 {
      printf("%c", x);
                 post[++p]=x;
                 }
                }
         else
         {
             while(priority(stack[top]) >= priority(*e))
             {
              char a;
              a= pop();
        printf("%c",a);
                 post[++p]=a;
             }
             push(*e);
         }
                e++;
        }
    while(top != -1)
    {
     char b;
     b=pop();
        printf("%c",b);
        post[++p]=b;
    }
        
 int i=0,op1,op2; /*Evaluation*/
 char ch;
 while(i != (p+1))
 {
  ch=post[i];
  i++;
  if(isdigit(ch))
  xpush(ch-'0'); 
  else
  {        
   op2=xpop();
   op1=xpop();
   switch(ch)
   {
   case '+':xpush(op1+op2);break;
   case '-':xpush(op1-op2);break;
   case '*':xpush(op1*op2);break;
   case '/':xpush(op1/op2);break;
   }
  }
 }
  printf("\n\nResult after Evaluation: %d\n",s[xtop]);
    

}

void prefix()
{
 xtop=-1;rtop=-1;p=-1;
 int i=0,j=0;
 char exp[50],rexp[50];
    char *e,*r, x;
    printf("Enter the expression :: ");
    scanf("%s",exp);
    while(exp[i]!='\0')
    i++;
    i--;
    while(i>=0)
    {
     if(exp[i]==')')
     {
      rexp[j++]='(';
      i--;
  }
  else if(exp[i]=='(')
  {
   rexp[j++]=')';
   i--;
  }
  else
  rexp[j++]=exp[i--];
    
 }
 
 rexp[j]='\0'; 
 
 r=rexp;
 while(*r != '\0')
 {
  if(isalnum(*r))
        {
            pre[++p]=*r;
        }
        else if(*r=='(')
        {
         push(*r);
  }
  else if(*r==')')
  {
   while((x=pop())!='(')
   pre[++p]=x;
  }
  else
  {
   while(priority(stack[top])>=priority(*r))
   pre[++p]=pop();
   push(*r);
  }
  r++;
 }
 while(top != -1)
    {
        pre[++p]=pop();
    }
    int print=p;
    printf("\nPrefix expression :: ");
    while(print!=-1)
    printf("%c",pre[print--]);
    /*evaluation*/
 i=0;
 j=0;
    int op1,op2;
    char ch;
    while(i!=(p+1))
    {
     i++;
     ch=pre[j++];
     if(isdigit(ch))
      xpush(ch-'0');
     else
     {
      op1=xpop();
      op2=xpop();
      switch(ch)
      {
       case '+':xpush(op1+op2);break;
      case '-':xpush(op1-op2);break;
      case '*':xpush(op1*op2);break;
      case '/':xpush(op1/op2);break;
   }
  }
 }
 printf("\n\nResult after Evaluation: %d\n",s[xtop]);
}

/*
OUTPUT

----MENU----
1.INFIX - POSTFIX
2.INFIX - PREFIX
3.EXIT
Enter your choice
1
Enter the expression :: 8*5*(10-3)

Postfix expression :: 85*103-*

Result after Evaluation: 280


----MENU----
1.INFIX - POSTFIX
2.INFIX - PREFIX
3.EXIT
Enter your choice
2
Enter the expression :: 8*8*(10-3)

Prefix expression :: *8*8-103

Result after Evaluation: 448


----MENU----
1.INFIX - POSTFIX
2.INFIX - PREFIX
3.EXIT
Enter your choice
1
Enter the expression :: 5*5-4*(4-3)

Postfix expression :: 55*443-*-

Result after Evaluation: 21


----MENU----
1.INFIX - POSTFIX
2.INFIX - PREFIX
3.EXIT
Enter your choice
2
Enter the expression :: 5*5-4*(4-3)

Prefix expression :: -*55*4-43

Result after Evaluation: 21


----MENU----
1.INFIX - POSTFIX
2.INFIX - PREFIX
3.EXIT
Enter your choice
10
INVALID INPUT !!!!


----MENU----
1.INFIX - POSTFIX
2.INFIX - PREFIX
3.EXIT
Enter your choice
3
~~~~~ Thank You ~~~~~

*/


Read More
PropellerAds