Monday, April 20, 2009

C RECORD

CQUEUE

#include
#include
#define max 5
int front=-1,rear=-1;
int cq[max];
void main()
{
int rpt=1;
int ch;
clrscr();
while(rpt)
{
printf("1.cqinsert\n");
printf("2.cqdelete\n");
printf("3.cqdisplay\n");
printf("4.exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:cqinsert();
break;
case 2:cqdelete();
break;
case 3:cqdisplay();
break;
case 4:return;
}
printf("Do you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&rpt);
}
}

cqinsert()
{
int num;
if(front==(rear+1)%max)
{
printf("queue is full\n");
return;
}
else
{
printf("enter the element\n");
scanf("%d",&num);
rear=rear+1%max;
cq[rear]=num;
if(front==-1)
front=0;
}
}

cqdelete()
{
int num;
if(front==-1)
{
printf("queue is empty\n");
return;
}
else
{
num=cq[front];
printf("Deleted number is %d",num);
if(front==rear)
front=rear=-1;
else
front=(front+1)%max;
}
}

cqdisplay()
{
int i;
if(front==-1)
{
printf("queue is empty\n");
return;
}
else if(front {
for(i=front;i<=rear;i++)
printf("%d\n",cq[i]);
}
else
{
for(i=front;i printf("%d\n",cq[i]);
for(i=0;i<=rear;i++)
printf("%d\n",cq[i]);
}
}


INFTPOST


/*Infix To Postfix*/

#include
#include
struct stack
{
int top;
char item[30];
};

void push(struct stack *ps,char n)
{
ps->top++;
ps->item[ps->top]=n;
}

void pop(struct stack *ps)
{
ps->top--;
}

int isempty(struct stack *ps)
{
if(ps->top==-1)
{
return(1);
}
else
{
return(0);
}
}

int prcd(char stktop,char op)
{
if(((stktop=='+')||(stktop=='-'))&&((op=='*')||(op=='/')))
{
return(0);
}
if(stktop=='(')
{
return(0);
}
if((stktop!=')')&&(op=='('))
{
return(0);
}
if((stktop!='(')&&(op==')'))
{
return(1);
}
}
void main()
{
struct stack s;
char input[30],output[30];
char optop,ans;
int i=0,j=0,k=0;
s.top=-1;
clrscr();
printf("enter an expression in infix form:\n");
scanf("%s",input);
while(input[i]!='\0')
{
if(isalpha(input[i]))
{
output[j]=input[i];
j++;
}
else
{
while(!isempty(&s)&&(prcd(s.item[s.top],input[i])))
{
optop=s.item[s.top];
pop(&s);
output[j]=optop;
j++;
}
if(isempty(&s)||input[i]!=')')
{
push(&s,input[i]);
}
else
{
pop(&s);
}
}

i++;
}

while(!isempty(&s))
{
optop=s.item[s.top];
pop(&s);
output[j]=optop;
j++;
}
printf("\npostfix form:\t");
output[j]='\0';

while(output[k]!='\0')
{
printf("%c",output[k]);
k++;
}
scanf("%c",&ans);
getch();
}


MERGSOR



#include
#include
void main()
{
int m,n,p,sum=0;
int a[15],b[15],c[45]={0 };
//c[15]={0};
clrscr();
//printf("enter x,y values");
//scanf("%d",&x,&y);
printf("\n enter elements in first list\n");
for(m=0;m<5;m++)
{
scanf("%d",&a[m]);
if(a[m]==0)
m--;
sum=sum+abs(a[m]);
}
printf("\n enter elements in first list\n");
for(n=0;n<5;n++)
{
scanf("%d",&b[n]);
if(a[n]==0)
m--;
sum=sum+abs(b[n]);
}
p=n=m=0;
m=m-sum;
while(m{
for(n=0;n<5;n++)
{
if(m==a[n] || m==b[n])
c[p++]=m;
if(m==a[n] && m==b[n])
c[p++]=m;
}
m++;
}
puts("MERGED SORTED LIST :");
for(n=0;n<10;++n)
printf("\t%d ",c[n]);
}



QLINK


/* Write C programs that implement Queue (its operations) using ii) Pointers */

#define true 1
#define false 0

#include
#include
#include

struct q_point
{
int ele;
struct q_point* n;
};

struct q_point *f_ptr = NULL;

int e_que(void);
void add_ele(int);
int rem_ele(void);
void show_ele();

/*main function*/
void main()
{
int ele,choice,j;
while(1)
{
clrscr();
printf("\n\n****IMPLEMENTATION OF QUEUE USING POINTERS****\n");
printf("==============================================");
printf("\n\t\t MENU\n");
printf("==============================================");
printf("\n\t[1] To insert an element");
printf("\n\t[2] To remove an element");
printf("\n\t[3] To display all the elements");
printf("\n\t[4] Exit");
printf("\n\n\tEnter your choice:");
scanf("%d", &choice);

switch(choice)
{
case 1:
{
printf("\n\tElement to be inserted:");
scanf("%d",&ele);
add_ele(ele);
getch();
break;
}

case 2:
{
if(!e_que())
{
j=rem_ele();
printf("\n\t%d is removed from the queue",j);
getch();
}
else
{
printf("\n\tQueue is Empty.");
getch();
}
break;
}

case 3:
show_ele();
getch();
break;

case 4:
exit(1);
break;

default:
printf("\n\tInvalid choice.");
getch();
break;
}

}
}

/* Function to check if the queue is empty*/
int e_que(void)
{
if(f_ptr==NULL)
return true;
return false;
}

/* Function to add an element to the queue*/
void add_ele(int ele)
{
struct q_point *queue = (struct q_point*)malloc(sizeof(struct q_point));
queue->ele = ele;
queue->n = NULL;
if(f_ptr==NULL)
f_ptr = queue;
else
{
struct q_point* ptr;
ptr = f_ptr;
for(ptr=f_ptr ;ptr->n!=NULL; ptr=ptr->n);
ptr->n = queue;
}
}

/* Function to remove an element from the queue*/
int rem_ele()
{
struct q_point* queue=NULL;
if(e_que()==false)
{
int j = f_ptr->ele;
queue=f_ptr;
f_ptr = f_ptr->n;
free (queue);
return j;
}
else
{
printf("\n\tQueue is empty.");
return -9999;
}
}

/* Function to display the queue*/
void show_ele()
{
struct q_point *ptr=NULL;
ptr=f_ptr;
if(e_que())
{
printf("\n\tQUEUE is Empty.");
return;
}
else
{
printf("\n\tElements present in Queue are:\n\t");
while(ptr!=NULL)
{
printf("%d\t",ptr->ele);
ptr=ptr->n;
}
}
}




SL

#include
#include
#include
#include
struct node
{
int data;
struct node *ptr;
};
typedef struct node NODE;
NODE *head=NULL;
NODE *nn,*temp,*temp1,*temp2;
main()
{
int ch,r=1;
clrscr();
while(r)
{
printf("enter the choice\n");
printf("1.entering from the front\n");
printf("2.entering from the end\n");
printf("3.entering from the middle\n");
printf("4.deleting from the front\n");
printf("5.deleting from the end\n");
printf("6.display\n");
printf("7.deleting from the middle\n");
printf("8.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:enterf();
break;
case 2: entere();
break;
case 3:enterm();
break;
case 4:delf();
break;
case 5:dele();
break;
case 6:display();
break;
case 7:delm();
break;
case 8:return;
}
printf("do you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&r);
}
}
enterf()
{
int item;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
if(head==NULL)
{
nn->data=item;
nn->ptr=NULL;
head=nn;
}
else
{
nn->data=item;
nn->ptr=head;
head=nn;
}
}

display()
{
if(head==NULL)
printf("empty list\n");
else
{
printf("elements in the list are\n");
temp=head;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->ptr;
}
}
}

entere()
{
int item;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
if(head==NULL)
{
nn->data=item;
nn->ptr=NULL;
head=nn;
}
else
{
temp=head;
while(temp->ptr!=NULL)
{
temp=temp->ptr;
}
nn->data=item;
temp->ptr=nn;
nn->ptr=NULL;
}
}
delf()
{
if(head==NULL)
printf("empty list\n");
else if(head->ptr==NULL)
{
printf("deleted element =%d",head->data);
free(head);
head=NULL;
}
else
{
temp=head;
head=head->ptr;
printf("deleted element=%d",temp->data);
free(temp);
}
}
dele()
{
if(head==NULL)
printf("empty list\n");
else if(head->ptr==NULL)
{
printf("deleted element=%d",head->data);
free(head);
head=NULL;
}
else
{
temp1=head;
temp2=NULL;
while(temp1->ptr!=NULL)
{
temp2=temp1;
temp1=temp1->ptr;
}
printf("deleted element=%d",temp1->data);
free(temp1);
temp2->ptr=NULL;
}
}


enterm()
{
int pos,item,i;
if(head==NULL)
{
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
nn->data=item;
nn->ptr=NULL;
head=nn;
}
else
{
printf("enter the position where you want to insert the element\n");
scanf("%d",&pos);
temp1=head;
temp2=NULL;
for(i=1;i {
temp2=temp1;
temp1=temp1->ptr;
}
printf("enter the item to be inserted\n");
scanf("%d",&item);
nn=(NODE*)malloc(sizeof(NODE));
nn->data=item;
temp2->ptr=nn;
nn->ptr=temp1;
}
}

delm()
{
int pos,i;
if(head==NULL)
{
printf("empty list\n");
}
else if(head->ptr==NULL)
{
printf("deleted element=%d",head->data);
free(head);
head=NULL;
}
else
{
printf("enter the position of the element to be deleted\n");
scanf("%d",&pos);
temp1=NULL;
temp2=head;
for(i=1;i {
temp1=temp2;
temp2=temp2->ptr;
}
printf("deleted element=%d\n",temp2->data);
temp1->ptr=temp2->ptr;
}
}



STACKL

/*stack using linked list*/
#include
#include
#include
struct node
{
int data;
struct node *ptr;
};
typedef struct node NODE;
NODE *top=NULL,*nn,*temp;
main()
{
int ch,rpt=1;
clrscr();
while(rpt)
{
printf("1.push\n");
printf("2.pop\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:return;
}
printf("\nDo you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&rpt);
}
}

push()
{
int num;
printf("enter the element to be inserted\n");
scanf("%d",&num);
nn=(NODE*)malloc(sizeof(NODE));
nn->data=num;
if(top==NULL)
{
nn->ptr=NULL;
top=nn;
}
else
{
nn->ptr=top;
top=nn;
}
}

pop()
{
if(top==NULL)
printf("stack is empty\n");
else
{
temp=top;
printf("Deleted element=%d",temp->data);
top=top->ptr;
free(temp);
}
}

display()
{
if(top==NULL)
printf("empty list\n");
else
{
printf("status of the stack is\n");
temp=top;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->ptr;
}
}
}



TRTV

#include
#include
#include
#define newnode (tree_type *)malloc(sizeof(tree_type))
typedef struct tree
{
int info;
struct tree *left,*right;
}tree_type;
tree_type *insert(tree_type *t,int val)
{
if(t==NULL)
{
t=newnode;
t->info=val;
t->left=t->right=NULL;
}
else if(valinfo)
t->left=insert(t->left,val);
else if(val>t->info)
t->right=insert(t->right,val);
else
printf("\n Duplicate number");
return(t);
}
void preorder(tree_type *t)
{
if(t)
{
printf("%4d",t->info);
preorder(t->left);
preorder(t->right);
}
}
void inorder(tree_type *t)
{
if(t)
{
inorder(t->left);
printf("%4d",t->info);
inorder(t->right);
}
}
void postorder(tree_type *t)
{
if(t)
{
postorder(t->left);
postorder(t->right);
printf("%4d",t->info);
}
}
int menu()
{
int opt;
printf("\n 1.Create Tree\n 2. Insert Node\n 3. Delete Node\n");
printf("4.Search Item\n 5. Exit \nEnter Your Option");
scanf("%d",&opt);
return(opt);
}
int search(tree_type *t,int key)
{
if(t==NULL)
return NULL;
else if(keyinfo)
return(search(t->left,key));
else if(key>t->info)
return(search(t->right,key));
else
return(1);
}
tree_type *find(tree_type *t)
{
while(t->right!=NULL)
t=t->right;
return(t);
}
tree_type *Delete(tree_type *t,int val)
{
tree_type *temp,*child;
if(t==NULL)
printf("ELEMENT NOT FOUND");
else if(valinfo)
t->left=Delete(t->left,val);
else if(val > t->info)
t->right=Delete(t->right,val);
else if(t->left!=NULL && t->right!=NULL)
{
temp=find(t->left);
t->info=temp->info;
t->left=Delete(t->left,t->info);
}
else
{
temp=t;
if(t->left==NULL)
child=t->right;
if(t->right==NULL)
child=t->left;
free(temp);
return(child);
}
return(t);
}
void main()
{
tree_type *root=NULL ,*val;
int opt,num;
while(1)
{
clrscr();
printf("\n\n PREORDER :");
preorder(root);
printf("\n\n INORDER :");
inorder(root);
printf("\n\n POST ORDER :");
postorder(root);
opt=menu();
switch(opt)
{
case 1:printf("Enter value or 0 to end\n");
do
{
scanf("%d",&num);
if(num!=0)
root=insert(root,num);
}while(num!=0);
break;
case 2:printf("Enter the number :");
scanf("%d",&num);
root=insert(root,num);
break;
case 3:printf("Enter the number to br deleted:");
scanf("%d",&num);
root=Delete(root,num);
break;
case 4:printf("\n Enter Item to search:");
scanf("%d",&num);
if(search(root,num))
printf("Item %d Found\n",num);
else
printf("NOT FOUND");
getch();
break;
case 5:return;
}
}
}

CIRL


#include
#include
#include
#include
struct node
{
int data;
struct node *ptr;
};
typedef struct node NODE;
NODE *first=NULL,*last=NULL;
NODE *nn,*temp,*temp1,*temp2;
main()
{
int ch,r=1;
clrscr();
while(r)
{
printf("enter the choice\n");
printf("1.entering from the front\n");
printf("2.entering from the end\n");
printf("3.deleting from the front\n");
printf("4.deleting from the end\n");
printf("5.display\n");
printf("6.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:enterf();
break;
case 2: entere();
break;
case 3:delf();
break;
case 4:dele();
break;
case 5:display();
break;
case 6:return;
}
printf("do you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&r);
}
}
enterf()
{
int item;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
nn->data=item;
nn->ptr=nn;
if(first==NULL)
{
first=last=nn;
}
else
{
last->ptr=nn;
nn->ptr=first;
first=nn;
}
}

display()
{
if(first==NULL)
printf("empty list\n");
else if(first->ptr==first)
printf("%d->",first->data);
else
{
temp=first;
while(temp!=last)
{
printf("%d->",temp->data);
temp=temp->ptr;
}
printf("%d->",last->data);
}
}

entere()
{
int item;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
nn->data=item;
nn->ptr=nn;
if(first ==NULL)
{
first=last=nn;
}
else
{
last->ptr=nn;
nn->ptr=first;
last=nn;
}
}
delf()
{
if(first==NULL)
printf("empty list\n");
else if(first->ptr==first)
{
printf("deleted element =%d",first->data);
free(first);
first=last=NULL;
}
else
{
temp=first;
first=first->ptr;
printf("deleted element=%d",temp->data);
free(temp);
last->ptr=first;
}
}
dele()
{
if(first==NULL)
printf("empty list\n");
else if(last->ptr==last)
{
printf("deleted element=%d",first->data);
free(first);
first=last=NULL;
}
else
{
temp1=first;
while(temp1->ptr!=last)
{
temp1=temp1->ptr;
}
printf("deleted element=%d",last->data);
free(last);
last=temp1;
last->ptr=first;
}
}




DOUBLE


#include
#include
#include
struct node
{
int data;
struct node *lptr;
struct node *rptr;
};
typedef struct node NODE;
NODE *head=NULL,*nn,*temp,*temp1,*temp2;
main()
{
int rpt=1,ch;
clrscr();
while(rpt)
{
printf("1.entering the element from the front\n");
printf("2.entering the element from the middle\n");
printf("3.entering the element from the end\n");
printf("4.deleting the element from the front\n");
printf("5.deleting the element from the end\n");
printf("6.deleting the element from the middle\n");
printf("7.display\n");
printf("8.exit\n");
printf("\n\nenter the choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:denterf();
break;
case 2:denterm();
break;
case 3:dentere();
break;
case 4:ddeletef();
break;
case 5:ddeletee();
break;
case 6:ddeletem();
break;
case 7:display();
break;
case 8:return;
}
printf("Do you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&rpt);
}
}

denterf()
{
int item;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
nn->data=item;
if(head==NULL)
{
nn->lptr=nn->rptr=NULL;
head=nn;
}
else
{
nn->rptr=head;
head->lptr=nn;
nn->lptr=NULL;
head=nn;
}
}

dentere()
{
int item;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the element to be inserted\n");
scanf("%d",&item);
nn->data=item;
if(head==NULL)
{
nn->lptr=nn->rptr=NULL;
head=nn;
}
else
{
temp=head;
while(temp->rptr!=NULL)
{
temp=temp->rptr;
}
temp->rptr=nn;
nn->lptr=temp;
nn->rptr=NULL;
}
}

denterm()
{
int item,pos,i;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the element to be inserted\n");
scanf("%d",&item);
nn->data=item;
nn->lptr=nn->rptr=NULL;
if(head==NULL)
head=nn;
else
{
temp1=head;
temp2=NULL;
printf("enter the position where to insert the element\n");
scanf("%d",&pos);
for(i=1;i {
temp2=temp1;
temp1=temp1->rptr;
}
temp2->rptr=nn;
nn->lptr=temp2;
nn->rptr=temp1;
temp1->lptr=nn;
}
}

ddeletef()
{
if(head==NULL)
printf("empty list\n");
else if(head->rptr==NULL)
{
printf("deleted element=%d",head->data);
free(head);
head=NULL;
}
else
{
temp=head;
head=head->rptr;
printf("Deleted element=%d",temp->data);
head->lptr=NULL;
free(temp);
}
}

ddeletee()
{
if(head==NULL)
printf("empty list\n");
else if(head->rptr==NULL)
{
printf("deleted element=%d",head->data);
free(head);
head=NULL;
}
else
{
temp1=NULL;
temp2=head;
while(temp2->rptr!=NULL)
{
temp1=temp2;
temp2=temp2->rptr;
}
printf("deleted element=%d",temp2->data);
temp1->rptr=NULL;
free(temp2);
}
}

ddeletem()
{
int pos,i;
if(head==NULL)
{
printf("empty list\n");
}
else if(head->rptr==NULL)
{
printf("Deleted element=%d",head->data);
free(head);
head=NULL;
}
else
{
printf("enter the position of the element to be inserted\n");
scanf("%d",&pos);
temp1=NULL;
temp2=head;
for(i=1;i {
temp1=temp2;
temp2=temp2->rptr;
}
printf("deleted element=%d",temp2->data);
temp1->rptr=temp2->lptr;
temp2->lptr=temp->rptr;
free(temp2);
}
}

display()
{
if(head==NULL)
printf("empty list\n");
else if(temp->rptr==NULL)
{
temp=head;
printf("%d->\n",temp->data);
}
else
{
temp=head;
while(temp!=NULL)
{
printf("%d->\n",temp->data);
temp=temp->rptr;
}
}
}


LAG

//LAGRANGE INTERPOLATION
#include
#include
#define max 90
void main()
{
float a[max+1],b[max+1],num,den,x,y=0;
int i,j,n;
clrscr();
printf("enter n value:");
scanf("%d",&n);
printf("Enter values of x,y");
for(i=0;i<=n;i++)
scanf("%f %f",&a[i],&b[i]);
printf("Enter value of x at which value of y is to be calculated");
scanf("%f",&x);
for(i=0;i<=n;i++)
{
num=1;
den=1;
for(j=0;jif(j!=i)
{
num*=x-a[j];
den*=a[i]-a[j];
}
y+=(num/den)*b[i];
}
printf("when x=4.1f | y=7.1f\n", x,y);
getch();
}


POSTE



#include
#include
#include
#include
#define MAX 50

char stack[MAX] ;
char infix[MAX] ;
char postfix[MAX] ;
char eval[MAX] ;
char *s, *t ; /*pointers to input and output strings*/
int top; /*Stack top*/

/*Function Prototypes*/
void Initialize (void);
void SetExpression (char *);
char Pop (void );
void Push (char);
int priority (char);
void Convert (void);
int Evaluate(void);

void main( )
{
int m;

clrscr( ) ;
Initialize ( ) ;
printf ( "\nEnter an infix expression: " ) ;
gets ( infix ) ;
SetExpression (infix) ;
Convert( ) ;
printf ( "\nThe Postfix expression is: " ) ;
puts(postfix);
strcpy(eval,postfix);
m=Evaluate( );
printf("answer: %d", m );

getch( ) ;
}

void Initialize (void)
{
top = -1 ;/*Make stack empty*/
}

void SetExpression ( char *str )
{
s = str ;
t = postfix;
}

/* adds operator to the stack */
void Push ( char c )
{
if ( top == MAX - 1 )
printf ( "\nStack is full.\n" ) ;
else
{
top++ ;
stack[top] = c ;
}
}

/* pops an operator from the stack */
char Pop ( void )
{
if ( top == -1 ) /* Stack is empty*/
return -1 ;
else
{
char item = stack[top] ;
top-- ;
return item ;
}
}

int priority(char c)
{
if ( c == '*' || c == '/' || c == '%' )
return 2;
else if ( c == '+' || c == '-' )
return 1;
else
return 0;
}

/* converts the infix expr. to postfix form */
void Convert (void)
{
char x ;

while ( *( s ) )
{ /*Skip white spaces, if any*/
if ( *( s ) == ' ' || *( s ) == '\t' )
{
s++ ;
continue ;
}

if ( isdigit ( *( s ) ) )/*Operands*/
{
while ( isdigit ( *( s ) ) )
{
*( t ) = *( s ) ;
s++ ;
t++ ;
}
}
if ( *( s ) == '(' )/*Opening Parenthesis*/
{
Push ( *( s ) ) ;
s++ ;
}
if ( *( s ) == '*' || *( s ) == '+' || *( s ) == '/' || *( s ) == '%' || *( s )
== '-' ) /*operators*/
{
if ( top != -1 )
{
x = Pop ( ) ;
while ( priority ( x ) >= priority ( *( s ) ) )
{
*( t ) = x ;
t++ ;
x = Pop ( ) ;
}
Push( x ) ;
Push ( *( s ) ) ;
}
else Push( *( s ) ) ;
s++ ;
}
if ( *( s ) == ')' )/*Closing Parenthesis*/
{
x = Pop ( ) ;
while ( x != '(' )
{
*( t ) = x ;
t++ ;
x = Pop ( ) ;
}
s++ ;
}
}
while ( top != -1 )/*While stack is not empty*/
{
x = Pop ( ) ;
*( t ) = x ;
t++ ;
}
t++ ;
}

int Evaluate(void)
{
int i,l,a,b,q,z;
l=strlen(eval);
for(i=0;i{
if ( isdigit ( eval[i] ) )
{
Push(eval[i]);
}
else if ( eval[i] == '*' || eval[i] == '+' || eval[i] == '/' || eval[i] == '%'
|| eval[i] == '-' )
{
a = Pop ( );
b = Pop ( );

switch( eval[i] )
{
case '+' : q=b+a; break;
case '-' : q=b-a; break;
case '*' : q=b*a; break;
case '/' : q=b/a; break;
}
Push ( q );
}
}
z = Pop ( );
return z;
}



QUEUE

/* queue using arrays*/
#include
#include
#define max 5
int front=-1,rear=-1;
int q[max];
void main()
{
int rpt=1;
int ch;
clrscr();
while(rpt)
{
printf("1.qinsert\n");
printf("2.qdelete\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:qinsert();
break;
case 2:qdelete();
break;
case 3:display();
break;
case 4:return;
}
printf("\nDo you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&rpt);
}
}

qinsert()
{
int num;
if(rear==(max-1))
{
printf("queue is full\n");
return;
}
else
{
printf("enter the element\n");
scanf("%d",&num);
rear=rear+1;
q[rear]=num;
if(front==-1)
front=0;
}
}

qdelete()
{
int num;
if(front==-1)
{
printf("queue is empty\n");
return;
}
else
{
num=q[front];
printf("Deleted number is %d",num);
if(front==rear)
front=rear=-1;
else
front++;
}
}

display()
{
int i;
if(front==-1)
{
printf("queue is empty\n");
return;
}
else
{
for(i=front;i<=rear;i++)
{
printf("%d\n",q[i]);
}
}
}




STACK


#include
#include
/*stack using arrays*/
#define max 5
int top=-1;
int stack[max];
void main()
{
int rpt=1;
int ch;
clrscr();
while(rpt)
{
printf("1.push\n");
printf("2.pop\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:return;
}
printf("Do you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&rpt);
}
}

push()
{
int item;
if(top==(max-1))
{
printf("stack is full\n");
return;
}
else
{
printf("enter the element to be inserted\n");
scanf("%d",&item);
top=top+1;
stack[top]=item;
}
}

pop()
{
if(top==-1)
{
printf("stack is empty\n");
return;
}
else
{
printf("Deleted element is %d",stack[top]);
top=top-1;
}
}

display()
{
int i;
if(top==-1)
{
printf("stack is empty\n");
return;
}
else
{
for(i=top;i>=0;i--)
printf("%d\n",stack[i]);
}
}


TREP

//trapezoidal method //
#include
#include
void main()
{
int a[40],i,j,k,n;
float h,x,b=0,a1,c1;
clrscr();
printf("Enter the no of terms\n enter integral values");
scanf("%d %f %f",&n,&a1,&c1);
h=(c1-a1)/n;
printf("ENTER %d VALUES",n+1);
for(i=0;iscanf("%d",&a[i]);
for(j=0;jb+=a[j];
x=(n/2)*(a[0]+a[n])+(2*b);
printf("%f",x);
getch();
}


CMX

/* Write a C program that uses functions to perform the following operations:
i) Reading a complex number
ii) Writing a complex number
iii) Addition of two complex numbers
iv) Multiplication of two complex numbers
(Note: represent complex number using a structure.) */

#include
#include

void arithmetic(int opern);

struct comp
{
double realpart;
double imgpart;
};

void main()
{
int opern;
clrscr();
printf("\n\n \t\t\t***** MAIN MENU *****");
printf("\n\n Select your option: \n 1 : ADD\n 2 : MULTIPLY\n 0 : EXIT \n\n\t\t Enter your Option [ ]\b\b");

scanf("%d",&opern);

switch(opern)
{
case 0:
exit(0);
case 1:
case 2:
arithmetic(opern);
default:
main();
}

}

void arithmetic(int opern)
{

struct comp w1, w2, w;

printf("\n Enter two Complex Numbers (x+iy):\n Real Part of First Number:");
scanf("%lf",&w1.realpart);
printf("\n Imaginary Part of First Number:");
scanf("%lf",&w1.imgpart);
printf("\n Real Part of Second Number:");
scanf("%lf",&w2.realpart);
printf("\n Imaginary Part of Second Number:");
scanf("%lf",&w2.imgpart);


switch(opern)
{

/*addition of complex number*/
case 1:
w.realpart = w1.realpart+w2.realpart;
w.imgpart = w1.imgpart+w2.imgpart;
break;

/*multiplication of complex number*/
case 2:
w.realpart=(w1.realpart*w2.realpart)-(w1.imgpart*w2.imgpart);
w.imgpart=(w1.realpart*w2.imgpart)+(w1.imgpart*w2.realpart);
break;
}


if (w.imgpart>0)
printf("\n Answer = %lf+%lfi",w.realpart,w.imgpart);
else
printf("\n Answer = %lf%lfi",w.realpart,w.imgpart);
getch();
main();
}




INSERT_SUBSTRINGS

/* Write a C program that uses functions to perform the following operations:
To insert a sub-string in to given main string from a given position.
*/

#include
#include
#include

void main()
{
char a[10];
char b[10];
char c[10];
int p=0,r=0,i=0;
int t=0;
int x,g,s,n,o;
clrscr();

puts("Enter First String:");
gets(a);
puts("Enter Second String:");
gets(b);
printf("Enter the position where the item has to be inserted: ");
scanf("%d",&p);
r = strlen(a);
n = strlen(b);
i=0;

// Copying the input string into another array
while(i <= r)
{
c[i]=a[i];
i++;
}
s = n+r;
o = p+n;

// Adding the sub-string
for(i=p;i{
x = c[i];
if(t {
a[i] = b[t];
t=t+1;
}
a[o]=x;
o=o+1;
}

printf("%s", a);
getch();
}


PYRAMID

/* Write a C program to construct a pyramid of numbers. */

#include
#include

void main()
{
int num,i,y,x=35;
clrscr();
printf("\nEnter the number to generate the pyramid:\n");
scanf("%d",&num);

for(y=0;y<=num;y++)
{
/*(x-coordinate,y-coordinate)*/
gotoxy(x,y+1);

/*for displaying digits towards the left and right of zero*/
for(i=0-y;i<=y;i++)

printf("%3d",abs(i));
x=x-3;
}
getch();
}


SEARCH STRING

/* Write a C program that displays the position or index in the string S
where the string T begins, or - 1 if S doesn't contain T.
*/

#include
#include
#include

void main()
{
char s[30], t[20];
char *found;
clrscr();

/* Entering the main string */
puts("Enter the first string: ");
gets(s);

/* Entering the string whose position or index to be displayed */
puts("Enter the string to be searched: ");
gets(t);

/*Searching string t in string s */
found=strstr(s,t);
if(found)
printf("Second String is found in the First String at %d position.\n",found-s);
else
printf("-1");
getch();
}



TOWER OF HANNOI

/* Write C programs that use both recursive and non-recursive functions
To solve Towers of Hanoi problem.*/

#include
#include

/* Non-Recursive Function*/
void hanoiNonRecursion(int num,char sndl,char indl,char dndl)
{
char stkn[100],stksndl[100],stkindl[100],stkdndl[100],stkadd[100],temp;
int top,add;
top=NULL;

one:
if(num==1)
{
printf("\nMove top disk from needle %c to needle %c ",sndl,dndl);
goto four;
}
two:
top=top+1;
stkn[top]=num;
stksndl[top]=sndl;
stkindl[top]=indl;
stkdndl[top]=dndl;
stkadd[top]=3;
num=num-1;
sndl=sndl;
temp=indl;
indl=dndl;
dndl=temp;

goto one;

three:
printf("\nMove top disk from needle %c to needle %c ",sndl,dndl);
top=top+1;
stkn[top]=num;
stksndl[top]=sndl;
stkindl[top]=indl;
stkdndl[top]=dndl;
stkadd[top]=5;
num=num-1;
temp=sndl;
sndl=indl;
indl=temp;
dndl=dndl;

goto one;

four:
if(top==NULL)
return;
num=stkn[top];
sndl=stksndl[top];
indl=stkindl[top];
dndl=stkdndl[top];
add=stkadd[top];
top=top-1;
if(add==3)
goto three;
else if(add==5)
goto four;
}

/* Recursive Function*/
void hanoiRecursion( int num,char ndl1, char ndl2, char ndl3)
{
if ( num == 1 ) {
printf( "Move top disk from needle %c to needle %c.", ndl1, ndl2 );
return;
}

hanoiRecursion( num - 1,ndl1, ndl3, ndl2 );
printf( "Move top disk from needle %c to needle %c.", ndl1, ndl2 );
hanoiRecursion( num - 1,ndl3, ndl2, ndl1 );
}

void main()
{
int no;
clrscr();
printf("Enter the no. of disks to be transferred: ");
scanf("%d",&no);

if(no<1)
printf("\nThere's nothing to move.");
else
printf("Non-Recursive");
hanoiNonRecursion(no,'A','B','C');
printf("\nRecursive");
hanoiRecursion(no,'A','B','C');

getch();
}



COMPLEMENT

/* 2’s complement of a number is obtained by scanning it from right to left and complementing all
the bits after the first appearance of a 1. Thus 2’s complement of 11100 is 00100.
Write a C program to find the 2’s complement of a binary number.*/

#include
#include

void complement (char *a);
void main()
{
char a[16];
int i;
clrscr();
printf("Enter the binary number");
gets(a);
for(i=0;a[i]!='\0';i++)
{
if(a[i]!='0' && a[i]!='1')
{
printf("NOT BINARY NUMBER");
exit(0);
}
}
complement(a);
getch();
}
void complement (char *a)
{
int l, i, c=0;
char b[16];
l=strlen(a);
for (i=l-1; i>=0; i--)
{
if (a[i]=='0')
b[i]='1';
else
b[i]='0';
}
for(i=l-1; i>=0; i--)
{
if(i==l-1)
{
if (b[i]=='0')
b[i]='1';
else
{
b[i]='0';
c=1;
}
}
else
{
if(c==1 && b[i]=='0')
{
b[i]='1';
c=0;
}
else if (c==1 && b[i]=='1')
{
b[i]='0';
c=1;
}
}
}
b[l]='\0';
printf("The 2's complement is %s", b);
}




PASCAL


/* Write a C program to generate Pascal's triangle. */

#include
#include

void main()
{
int bin,p,q,r,x;
clrscr();
bin=1;
q=0;

printf("Rows you want to input:");
scanf("%d",&r);

printf("\nPascal's Triangle:\n");

while(q {
for(p=40-3*q;p>0;--p)
printf(" ");
for(x=0;x<=q;++x)
{
if((x==0)||(q==0))
bin=1;
else
bin=(bin*(q-x+1))/x;
printf("%6d",bin);
}

printf("\n");
++q;
}
getch();
}


ROM_DEC

/* Write a C program to convert a Roman numeral to its decimal equivalent. */

#include
#include
#include
#include

void main()
{

int *a,len,i,j,k;
char *rom;

clrscr();

printf("Enter the Roman Numeral:");
scanf("%s",rom);

len=strlen(rom);

for(i=0;i {
if(rom[i]=='I')
a[i]=1;
else if(rom[i]=='V')
a[i]=5;
else if(rom[i]=='X')
a[i]=10;
else if(rom[i]=='L')
a[i]=50;
else if(rom[i]=='C')
a[i]=100;
else if(rom[i]=='D')
a[i]=500;
else if(rom[i]=='M')
a[i]=1000;
else
{
printf("\nInvalid Value");
getch();
exit(0);
}
}
k=a[len-1];
for(i=len-1;i>0;i--)
{
if(a[i]>a[i-1])
k=k-a[i-1];
else if(a[i]==a[i-1] || a[i] k=k+a[i-1];
}
printf("\nIts Decimal Equivalent is:");
printf("%d",k);
getch();
}



SERIES

//compute sum of geometric progression
// 1+x+x2+x3+....+xn
#include
#include
#include
void main()
{
int sum,i,x,n;
clrscr();
printf("enter values for x,n");
scanf("%d %d",&x,&n);
if(x<=0 ||n<=0)
{
printf("It is valid\n");
sum=1;
for(i=1;i<=n;i++)
{
sum=sum+pow(n,i);
}
printf("sum of series is %d\n",sum);
}
getch();
}

1 comment:

Anonymous said...
This comment has been removed by the author.