Thursday, April 22, 2010

c++ - insertion operator (<<) overloading

#include"iostream"
using namespace std;
class x
{
int a,b;

public:
x()
{
a=5;
b=9;
}

friend ostream& operator<<(ostream &, x);
};

ostream& operator<<(ostream &t, x x1)
{
t << x1.a << endl;
t << x1.b;
return t;
}
int main()
{
x x1;
cout << x1;
system("pause");
return 0;
}

c++ - access private member of a class by member function of another class

#include"iostream"
using namespace std;
class b;
class a
{
int x;
public:
void set()
{x=30;}
void show(b);
};
class b
{
int y;
friend void a::show(b);
public:
void set()
{y=20;}

};
void a::show(b bb)
{
cout "x=" x endl;
cout "y=" bb.y;
}
int main()
{
a a1;
a1.set();
b b1;
b1.set();
a1.show(b1);
system("pause");
return 0;
}

c++ - find number of days upto date from 1 jan 1970

//find number of days upto date from 1 jan 1970 //
//date-21/01/10 //
//programmer=amrendra kumar //
///////////////////////////////////////////////////////
#include"iostream"
using namespace std;
int leap(int);
int main()
{
long unsigned int d,m,y;
long unsigned int s=0,i;
cout"enter date(dd mm yyyy)";
cin>>d;
cin>>m;
cin>>y;
cout"ur date is " d " " m " " y endl;

//calculate of days
s=s+d;
for(i=1;i {
if(i==4||i==6||i==9||i==11)
s=s+30;
else if(i==1||i==3||i==5||i==7||i==8||i==10)
s=s+31;
else
s=s+28;
}
if(m>2)
s=s+leap(y);
if(y>1970)
s=s+(365*(y-1970));
cout<<"total days "< system("pause");
return 0;
}

int leap(int y)
{
int i,n=0;
for(i=1700;i<=y;i++)
{
if(i%400==0||(i%4==0&&i%100!=0))
n++;
}
return n;
}

c++ - conversion from class hour to minute and vice-versa

#include"iostream"
using namespace std;
class year;
class month
{
int m;
public:
//month(){m=0;}
month(int month){m=month;}
month(year);

operator year();
void show()
{
cout "month= " m endl;
}
};
class year
{
int y;
public:
year(){y=0;}
year(int year){y=year;}
int ye(){return y;}
void show()
{
cout "year=" y endl;
}


};
month::operator year()
{
int a;
a=m/12;
return year(a);
}
month::month(year oy)
{
int i;
i=oy.ye();
m=i*12;
}


int main()
{
year o;
month o1(36);
o1.show();
o=o1;
o.show();
o=2;
o.show();
o1=o;
o1.show();
system("pause");
return 0;
}

Tuesday, April 13, 2010

ds - expression tree creation and evaluation

#include"stdio.h"
#include"conio.h"
#include"alloc.h"
#define N 20
typedef struct tree
{
int data;
struct tree *left;
struct tree *right;
}tree;

typedef struct stack
{
char data[N];
int top;
}stack;

typedef struct stack_num
{
tree *data[N];
int top;
}stack_num;


void convert(char *exp,tree **t);
void push(stack *st,char ch);
void push_num(stack_num *st,tree *t);
char pop(stack *st);
tree *pop_num(stack_num *st);
int prec(char c);
int prec_stack(char c);
void display(tree *t);
int eval(tree *t);

int main()
{
char *exp;
tree *t;
clrscr();
printf("Enter the Expression >");
scanf("%s",exp);

convert(exp,&t);
display(t);
printf("\nEval = %d",eval(t));
getch();
return(0);
}

void convert(char *exp,tree **t)
{
stack st_op;
stack_num st_num;
char u;
st_op.top=-1;
st_num.top=-1;
push(&st_op,'(');
u=*exp;
while(u!='\0')
{
if((u>=48 && u <=57) || (u>=65 && u<=122))
{
*t=(tree *)malloc(sizeof(tree));
(*t)->data=u;
(*t)->left=(*t)->right=NULL;
push_num(&st_num,*t);
}
else if(u==')')
{
while(st_op.data[st_op.top]!='(')
{

*t=(tree *)malloc(sizeof(tree));
(*t)->data=pop(&st_op);
(*t)->left=pop_num(&st_num);
(*t)->right=pop_num(&st_num);
push_num(&st_num,*t);
}
}
else if(prec(u)< prec_stack(st_op.data[st_op.top]))
{
*t=(tree *)malloc(sizeof(tree));
(*t)->data=pop(&st_op);
(*t)->left=pop_num(&st_num);
(*t)->right=pop_num(&st_num);
push_num(&st_num,*t);
push(&st_op,u);
}
else
push(&st_op,u);

exp++;
u=*exp;
}

}

void push(stack *st,char ch)
{
if(st->top==N-1)
{
printf("\nStack is Full!!!");
return;
}
st->top++;
st->data[st->top]=ch;
}

char pop(stack *st)
{
char ch;
if(st->top==-1)
{
printf("\nStack is empty!!!");
return(' ');
}
ch=st->data[st->top];
st->top--;
return(ch);
}

void push_num(stack_num *st,tree *t)
{
if(st->top==N-1)
{
printf("\nStack is Full!!!");
return;
}
st->top++;
st->data[st->top]=t;
}

tree *pop_num(stack_num *st)
{
tree *t;
if(st->top==-1)
{
printf("\nStack is empty!!!");
return(NULL);
}
t=st->data[st->top];
st->top--;
return(t);
}



int prec(char c)
{
if(c=='+'||c=='-')
return(1);
else if(c=='*'||c=='/')
return(2);
else if(c=='(')
return(9);
else
return(7);
}

int prec_stack(char c)
{
if(c=='+'||c=='-')
return(2);
else if(c=='*'||c=='/')
return(4);
else if(c=='(')
return(0);
else
return(8);
}


void display(tree *t)
{
if(t!=NULL)
{
display(t->left);
printf("%c ",t->data);
display(t->right);
}
}


int eval(tree *t)
{
if(t->data>=48 && t->data <=57)
{
return(t->data-48);
}
else if(t->data=='+')
{
return(eval(t->left)+eval(t->right));
}
else if(t->data=='-')
{
return(eval(t->left)-eval(t->right));
}
else if(t->data=='*')
{
return(eval(t->left)*eval(t->right));
}
else if(t->data=='/')
{
return(eval(t->left)/eval(t->right));
}
else
{
printf("\nInvalid Operation!!!");
return(0);
}

}

Sunday, April 11, 2010

c - create personal library in TurboC

first create a header file in which function prototype is written
/* ex.h */
int fact(int);

source file in which function is defined
/* ex.c */
int fact(int x)
{
int i,f;
for(i=x;i>1;i--)
f=f*i;
return f;
}

now in main program we can call fact() function use ex.h header file
/* factorial.c */
#include"ex.h"
void main()
{
int n;
printf("Enter number:");
scanf("%d",&n);
printf("factorial of %d is %d",n,fact(n));
}

/*library file creation */
goto command prompt
c:\tc\bin
/* first create obj file of source file command is as */
c:\tc\bin\tcc -c ex.c /* object file ex.obj created */
c:\tc\bin\tlib ex + ex.obj /* ex.lib library file created */
c:\tc\bin\tcc ex.lib factorial.c /* library file with main program file is connected */
c:\tc\bin\tcc -c factorial.c /*program compile */
factorial /* program run simple writing program and then enter output come out */
enter number 5
factorial of 5 is 120

//* this program is checked and successfully run

Friday, April 9, 2010

ds - insertion and deletion in heap

#include"stdio.h"
#include"conio.h"
#include"alloc.h"
#include"stdlib.h"
#define n 15
struct node
{
int index,parent;
int leaf[n];
};
typedef struct node node;
void heapinsert(node *,int);
void reheapup(node *,int);
void deleteheap(node *); //delete root node
void reheapdown(node *,int);
void display(node);
void main()
{
node heap;
heap.index=-1;
clrscr();
heapinsert(&heap,17);
heapinsert(&heap,52);
heapinsert(&heap,32);
heapinsert(&heap,16);
display(heap);
deleteheap(&heap);
printf("\n");
display(heap);
getch();
}
void heapinsert(node *heap,int x)
{
//heap->parent=0;
if(heap->index==n-1)
{printf("memory overflow");
return;}
heap->index=heap->index+1;
heap->leaf[heap->index]=x;
reheapup(heap,heap->index);
}
void reheapup(node *heap,int x)
{
int t;
if(x!=0)
{
heap->parent=(x-1)/2;
if(heap->leaf[x]>heap->leaf[heap->parent])
{
t=heap->leaf[x];
heap->leaf[x]=heap->leaf[heap->parent];
heap->leaf[heap->parent]=t;
reheapup(heap,heap->parent);
}}
}
void display(node heap)
{
int i,x;
i=heap.index;
for(x=0;x<=i;x++)
printf("%d ",heap.leaf[x]);
}
void deleteheap(node *heap)
{
if(heap->index<0)
{printf("memory underflow");
return;}
heap->leaf[0]=heap->leaf[heap->index];
heap->index--;
reheapdown(heap,0);
}
void reheapdown(node *heap,int root)
{ int t,leftkey,rightkey,largekey,largeindex;
if(root*2+1 <= heap->index)
{
leftkey=heap->leaf[root*2+1];
if(root*2+2<=heap->index)
rightkey=heap->leaf[root*2+2];
else
rightkey=-1;
if(leftkey>rightkey)
{largekey=leftkey;
largeindex=root*2+1; }
else{
largekey=rightkey;
largeindex=root*2+2;
}
if(heap->leaf[root]{
t=heap->leaf[root];
heap->leaf[root]=heap->leaf[largeindex];
heap->leaf[largeindex]=t;
reheapdown(heap,largeindex);
}
}}

ds - conversion of general tree to binary tree

#include"stdio.h"
#include"conio.h"
#include"alloc.h"
#include"stdlib.h"
#define n 15
struct node
{
int data;
struct node *lptr;
struct node *rptr;
};
typedef struct node node;
struct stack
{
int top;
int number[n];
struct node *location[n];
};
typedef struct stack stack;
void convert(node **);
void display(node *);
void main()
{
node *root=NULL;
clrscr();
convert(&root);
printf("\ndata in binary tree :\n");
display(root);
getch();
}
void convert(node **root)
{
char c;
int pl,level,x;
stack s;
node *p=NULL,*ploc=NULL;
node *newnode=(node *)malloc(sizeof(node));
s.top=-1;
if(newnode==NULL)
{
printf("memory overflow");
return;
}
if(*root==NULL)
{
*root=newnode;
(*root)->lptr=(*root)->rptr=NULL;
printf("enter level and data>");
scanf("%d%d",&level,&x);
(*root)->data=x;
s.location[0]=*root;
s.number[0]=level;
s.top=0;
}
l:printf("to enter a data(y/n)?");
c=getch();
if(c=='y')
{
printf("\nenter level and data>");
scanf("%d%d",&level,&x);
p=(node *)malloc(sizeof(node));
p->lptr=p->rptr=NULL;
p->data=x;
pl=s.number[s.top];
ploc=s.location[s.top];
if(level>pl)
ploc->lptr=p;
else
{
while(pl>level)
{
s.top=s.top+1;
pl=s.number[s.top];
ploc=s.location[s.top];
}
ploc->rptr=p;
s.top=s.top-1;
}
s.top=s.top-1;
s.number[s.top]=level;
s.location[s.top]=p;
goto l;
}}
void display(node *p)
{
if(p!=NULL)
{
printf(" %d ",p->data);
display(p->lptr);
display(p->rptr);
}
}

ds - AVL Tree insertion and deletion

//there may be some mistakes but it works.

#include"stdio.h"
#include"conio.h"
#include"stdlib.h"
#include"alloc.h"
struct node
{
int data;
struct node *right;
struct node *left;
int bal;
};
typedef struct node node;
void avlinsert(node **,int,int *);
void rotateleft(node **);
void rotateright(node **);
void leftbal(node **,int *);
void rightbal(node **,int *);
void display(node *);
void delete_node(node **,int,int *);
void delbalright(node **,int *);
void delballeft(node **,int *);
void main()
{
node *root=NULL;
int taller=0;
int shorter=0;
clrscr();
avlinsert(&root,15,&taller);
avlinsert(&root,12,&taller);
avlinsert(&root,23,&taller);
avlinsert(&root,27,&taller);
printf("display in inorder:\n");
display(root);
delete_node(&root,23,&shorter);
printf("\nafter deletion :\n");
display(root);
getch();
}
void avlinsert(node **root,int x,int *taller)
{
node *newnode=(node *)malloc(sizeof(node));
if(*root==NULL)
{
*root=newnode;
(*root)->left=(*root)->right=NULL;
(*root)->data=x;
(*root)->bal=0;
*taller=1;
return;
}
if(x<(*root)->data)
{
avlinsert(&(*root)->left,x,taller);
if(*taller==1)
if((*root)->bal==-1)
leftbal(&(*root),taller);
else{
if((*root)->bal==0)
(*root)->bal=-1;
else
{(*root)->bal=0;
taller=0;
}
}}
else
{
avlinsert(&(*root)->right,x,taller);
if(*taller==1)
if((*root)->bal==0)
rightbal(&(*root),taller);
else{
if((*root)->bal==0)
(*root)->bal=1;
else
{(*root)->bal=1;
taller=0;
}
}return;
}}
void leftbal(node **root,int *taller)
{
node *l=NULL,*r=NULL;
l=(*root)->left;
if(l->bal==-1)
{ (*root)->bal=0;
l->bal=0;
rotateright(&(*root));
taller=0;
}
else
{r=l->right;
if((l->bal)==-1)
{(*root)->left=0;
l->bal=0;}
else{
if(r->bal==0)
l->bal=0;
else
{(*root)->bal=0;
l->bal=-1;}
}
r->bal=0;
rotateleft(&(*root)->left);
rotateright(&(*root));
*taller=0;}
}
void rightbal(node **root,int *taller)
{
node *r=NULL,*l=NULL;
r=(*root)->right;
if(r->bal==0)
{(*root)->bal=0;
r->bal=0;
rotateleft(&(*root));
*taller=0;
}
else
{
l=r->left;
if(l->bal==0)
{(*root)->bal=-1;
r->bal=0;}
else{
if(l->bal==0)
(*root)->bal=r->bal=0;
else
{(*root)->bal=0;
r->bal=1;}
}l->bal=0;
rotateright(&(*root)->right);
rotateleft(&(*root));
*taller=0;
}return;
}
void rotateleft(node **root)
{
node *temp=NULL;
temp=(*root)->right;
(*root)->right=temp->left;
temp->left=*root;
*root=temp;
}
void rotateright(node **root)
{
node *temp=NULL;
temp=(*root)->left;
(*root)->left=temp->right;
temp->right=*root;
*root=temp;
}
void display(node *p)
{
if(p!=NULL)
{
display(p->left);
printf("%d ",p->data);
display(p->right);
}
}
void delete_node(node **root,int x,int *shorter)
{

node *p=NULL,*n=NULL,*q=NULL;
if(*root==NULL)
{printf("list is empty/data not found");
return;
}
if(x==(*root)->data)
{
p=*root;
if((*root)->left==NULL)
(*root)=(*root)->right;
else if((*root)->right==NULL)
(*root)=(*root)->left;
if(p!=(*root))
{
free(p);
*shorter=1;
return;
}
q=*root;
n=(*root)->right;
while(n->left!=NULL)
{q=n;
n=n->left;}
if(q==(*root))
{(*root)->right=n->right;
*shorter=1;}
else
{
q->left=n->right;
if(*shorter==1)
delbalright(&(*root)->right,shorter);
}
n->left=(*root)->left;
n->right=(*root)->right;
free(*root);
*root=n;
if(*shorter==1)
delballeft(&(*root),shorter);
return;
}
if(x<(*root)->data)
{
delete_node(&(*root)->left,x,shorter);
if(*shorter==1)
delbalright(&(*root),shorter);
}
else{
delete_node(&(*root)->right,x,shorter);
if(*shorter==1)
delballeft(&(*root),shorter);
}}
void delbalright(node **root,int *shorter)
{
node *r=NULL,*l=NULL;
if((*root)->bal==-1)
(*root)->bal=0;
else
{
if((*root)->bal==0) {
(*root)->bal=1;
*shorter=0; }
else{
r=(*root)->right;
if(r->bal==0)
{
l=r->left;
if(l->bal==-1)
{r->bal=1;
(*root)->bal=1;}
else{
if(l->bal==0)
r->bal=(*root)->bal=0;
else{
(*root)->bal=1;
r->bal=0;}
}
l->bal=0;
rotateright(&(*root)->right);
rotateleft(&(*root));
}
else{
if(r->bal==1)
(*root)->bal=r->bal=0;
else{
if(r->bal==0)
{(*root)->bal=1;
r->bal=-1;
*shorter=0;}}
rotateleft(&(*root));
}} }}
void delballeft(node **root,int *shorter)
{
node *r=NULL,*l=NULL;
if((*root)->bal==1)
(*root)->bal=0;
else
{
if((*root)->bal==0) {
(*root)->bal=-1;
*shorter=0; }
else{
l=(*root)->left;
if(l->bal==-1)
{
r=l->right;
if(r->bal==1)
{l->bal=-1;
(*root)->bal=0;}
else{
if(l->bal==0)
l->bal=(*root)->bal=0;
else{
(*root)->bal=1;
l->bal=0;}
}
r->bal=0;
rotateleft(&(*root)->left);
rotateright(&(*root));
}
else{
if(l->bal==-1){
(*root)->bal=0;
l->bal=1;}
else{
//if(r->bal==0)
{(*root)->bal=-1;
l->bal=0;
*shorter=0;}}
rotateright(&(*root));
}} }}

ds - parsing operation for paranthesis ( )

#include"stdio.h"
#include"conio.h"
#include"alloc.h"
#include"stdlib.h"
#define n 20
struct stack
{
char c[n];
int top;
};
typedef struct stack stack;
void push(stack *,char);
char pop(stack *);
void parse(char *,stack *);
void main()
{
stack *st=NULL;
char s[15];
clrscr();
st->top=-1;
printf("enter equation :");
scanf("%s",s);
parse(s,st);
getch();

}
void push(stack *st,char data)
{
if(st->top==n-1)
{
printf("\nList is Already FULL");
return;
}

st->c[++st->top]=data;

}
char pop(stack *st)
{
char res;
if(st->top==-1)
{
printf("\nList is Empty");
return NULL;
}
res=st->c[st->top];

st->top--;
return res;
}
void parse(char *s,stack *st)
{
int i=0,f=0;
char c;
while(s[i]!=0)
{
c=s[i];
if(c=='(')
push(st,c);
else
{ if(c==')')
{
if(st->top==-1) {f=1;
printf("error:closing paranthesis not matched."); }
else
pop(st); }
} i++;
}
if(st->top!=-1)
printf("error:opening paranthesis not matched.");
else if(f!=1)
printf("no error:paranthesis are matched.");
}

ds - push,pop operation in stack (linked list implementation)

#include"stdio.h"
#include"conio.h"
#include"alloc.h"
#include"stdlib.h"
struct stack
{
int data;
int count;
struct stack *next;
};
typedef struct stack stack;
void push(stack **,int);
void pop(stack **);
void display(stack *);
void main()
{
stack *s=NULL;
clrscr();
s->count=0;
push(&s,5);
push(&s,10);
push(&s,15);
display(s);
pop(&s);
printf("\n");
display(s);
getch();
}
void push(stack **s,int d)
{
stack *newnode=(stack *)malloc(sizeof(stack));
if(newnode==NULL)
{printf("memory overflow");
exit(0);}
(*s)->count++;
newnode->data=d;
newnode->next=(*s);
(*s)=newnode;

}
void pop(stack **s)
{
stack *p;
if(*s==NULL)
{printf("underflow");
exit(0);}
p=*s;
(*s)->count--;
*s=(*s)->next;

free(p);
}
void display(stack *s)
{
while(s!=NULL)
{printf("%d ",s->data);
s=s->next;}
}

ds - push,pop,peek operation in stack (array implementation)

#include"stdio.h"
#include"conio.h"
#include"alloc.h"
#include"stdlib.h"
#define n 10
struct stack
{
int x[10];
int top;
};
typedef struct stack stack;
void push(stack *,int);
void peek(stack *,int);
void display(stack);
void change(stack *,int,int);
void pop(stack *);
void main()
{
stack s;
clrscr();
s.top=0;
push(&s,2);
push(&s,4);
push(&s,6);
push(&s,8);
display(s);
peek(&s,1);
change(&s,2,3);
display(s);
pop(&s);
display(s);
getch();
}
void push(stack *s,int x)
{
if(s->top==(n-1))
{printf("overflow");
return; }
s->top++;
s->x[s->top]=x;
}
void display(stack s)
{
int i=1;
printf("\n");
while(i<=s.top)
{printf("%d",s.x[i]);
i++;}
}
void pop(stack *s)
{
if((s->top)<0) {
printf("underflow");
return;}
s->x[s->top]=0;
s->top--;
}
void peek(stack *s,int p)
{
if((s->top)-p<0) {
printf(" underflow");
return;}
printf("\n%d",s->x[p]);
}
/* change ith element */
void change(stack *s,int p,int x)
{
if((s->top)-p<0) {
printf(" underflow");
return;}
s->x[p]=x;
}

Tuesday, April 6, 2010

ds - enqueue and dequeue implementation in circular queue

#include"stdio.h"
#include"conio.h"
#include"alloc.h"
#include"stdlib.h"
#define n 8
struct queue
{
int f;
int r;
int a[n];
int data;
};
typedef struct queue queue;
void enqueue(queue *,int);
void dequeue(queue *);
void display(queue);
void main()
{
queue q;
clrscr();
q.f=-1;
q.r=-1;
enqueue(&q,11);
enqueue(&q,22);
enqueue(&q,33);
enqueue(&q,44);
enqueue(&q,55);
enqueue(&q,66);
display(q);
dequeue(&q);
display(q);
getch();
}
void enqueue(queue *q,int d)
{
if(q->r==n-1&&q->f==0||q->r+1==q->f)
{printf("memory overflow");
exit(0); }
if(q->f==-1&&q->r==-1)
{q->r=0;
q->f=0;}
else{
if(q->r==n-1)
q->r=0;
else
q->r=q->r+1;
}
q->a[q->r]=d;
}
void display(queue q)
{
int i=q.f;
while(i {printf("%d ",q.a[i]);
i++;}
}
void dequeue(queue *q)
{
int x;
if(q->r==-1&&q->f==-1)
{printf("underflow");
exit(0);}
x=q->a[q->f];
q->a[q->f]=0;
if(q->f==q->r) {
q->r=-1;
q->f=-1;}
else{
if(q->f==n-1)
q->f=0;
else
q->f++;
}
printf("\n%d deleted\n",x);
}

ds - enqueue and dequeue implementation in linear queue

#include"stdio.h"
#include"conio.h"
#include"alloc.h"
#include"stdlib.h"
#define n 8
struct queue
{
int f;
int r;
int data;
int a[n];
};
typedef struct queue queue;
void display(queue *);
void enqueue(queue **,int);
void dequeue(queue **);
void main()
{
queue *q=NULL;
clrscr();
q->f=-1;
q->r=-1;
enqueue(&q,10);
enqueue(&q,20);
enqueue(&q,30);
enqueue(&q,40);
enqueue(&q,50);
enqueue(&q,60);
display(q);
dequeue(&q);
display(q);
enqueue(&q,70);
printf("\n");
display(q);
getch();
}
void enqueue(queue **q,int d)
{
int i;
if((*q)->r==n-1&&(*q)->f==0)
{printf("memory overflow");
exit(0);}
if((*q)->f==-1&&(*q)->r==-1)
{
(*q)->r=0;
(*q)->f=0;
}
else{
if((*q)->r==n-1)
{
i=(*q)->f;
while(i<=(*q)->r) {
(*q)->a[i-(*q)->f]=(*q)->a[i];
i++; }
(*q)->r=(*q)->r-(*q)->f+1;
(*q)->f=0;
}
else
(*q)->r=(*q)->r+1;
}
(*q)->a[(*q)->r]=d;
}
void display(queue *q)
{ int t=q->f;
while(t {printf("%d ",q->a[t]);
t++;
}
}
void dequeue(queue **q)
{
int y;
if((*q)->f==-1&&(*q)->r==-1)
{printf("memory overflow");
exit(0);}
y=(*q)->a[(*q)->f];
if((*q)->f==(*q)->r)
{(*q)->a[(*q)->f]=0;
(*q)->f=-1;
(*q)->r=-1; }
else{
(*q)->a[(*q)->f]=0;
(*q)->f=(*q)->f+1; }
printf("\n%d deleted\n",y);
}