You are on page 1of 7

// program 7 : program to implement date manipulation.

#include<iostream.h>
class date
{
int dd,mm,yy;

public:
void getdate();
int operator-(date);
date operator+(int);
};

void date::getdate()
{
start: cout<<"\nenter a valid date [dd mm yy] :\n";
cin>>dd>>mm>>yy;
if((mm==2) && (dd>29))
{
cout<<"\nwrong input\n";
goto start;
}
if((mm>12) && (dd>31))
{
cout<<"\nwrong input\n";
goto start;
}
if((mm==4||mm==6||mm==9||mm==11) && (dd>30))
{
cout<<"\nwrong input\n";
goto start;
}
if((yy%4==0) && (mm==2) && (dd>28))
{
cout<<"\nwrong input\n";
goto start;
}
}

int date::operator-(date d2)


{
int i,nod1,nod2,nody,lc,nod;
nod1=nod2=lc=0;
for(i=1;i<mm;i++)
{
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
nod1=nod1+31;
else if(i==2)
nod1+=28;
else
nod1+=30;
}
nod1+=dd;
for(i=1;i<d2.mm;i++)
{
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
nod2=nod2+31;
else if(i==2)
nod2+=28;
else
nod2+=30;
}
nod2+=dd;
nody=(yy-d2.yy)*365;
for(i=d2.yy;i<yy;i++)
{
if((i%4)==0)
lc++;
}
int y4=yy-d2.yy;
while(y4>400)
{
lc++;
y4-=400;
}
if(mm>2 && (yy%4)==0)
lc++;
if((d2.mm>2) && (d2.yy%4)==0)
lc--;
nod=nody+nod1-nod2+lc;
return(nod);
}

date date::operator+(int nd)


{
while(nd>365)
{
yy++;
nd-=365;
}
while(nd>30)
{
if(mm==1||mm==3||mm==5||mm==7||mm==8||mm==10||mm==12)
{
nd=nd-31;
mm++;
}
else if(mm==2)
{
nd-=28;
mm++;
}
else
{
nd-=30;
mm++;
}
if(mm>12)
{
yy++;
mm=1;
}
}
dd+=nd;
if(dd>30)
{
if(mm==4||mm==6||mm==9||mm==11)
{
dd=dd-30;
mm++;
}
else if(mm==2)
{
dd-=28;
mm++;
}
else if(dd>31)
{
dd-=31;
mm++;
}
}
cout<<"\nnew date is: ";
cout<<dd<<"-"<<mm<<"-"<<yy<<endl<<endl;
return *this;
}

void main()
{
date dd1,dd2;
int num,res;
dd1.getdate();
dd2.getdate();
res=dd1-dd2;
cout<<"\nno of days : "<<res<<"\n";
cout<<"\nenter the no of days to be added to the first date: ";
cin>>num;
dd2=dd1+num;
}

/* ---------- output -----------

enter a valid date [dd mm yy] :


10 03 2006

enter a valid date [dd mm yy] :


10 02 2006

no of days : 28

enter the no of days to be added to the first date: 10

new date is: 20-3-2006

*/
plain text attachment [ scan and save to computer | save to yahoo! briefcase ]

//program 11:doubly linked list


#include<iostream.h>
#include<stdlib.h>
class node
{
public:
int data;
node *prev;
node *next;
};
class dlist
{
node *first;
int count,ch;
public:
dlist()
{
first=null;
count=0;
}
void create();
void add(int);
void display();
void delet(int);
};
void dlist::create()
{
node *p,*t;
p=t=null;
do
{
t=p;
p=new node;
cout<<"enter data of the node:\n";
cin>>p->data;
if(first==null)
first=p;
else
t->next=p;
p->prev=t;
count++;
cout<<"any more nodes to be created 1-yes or 2-no\n";
cin>>ch;
}while(ch==1);
p->next=null;
}
void dlist::add(int pos)
{
int num=1;
node *t1,*t2,*p;
t1=t2=null;
p=first;
if((pos<1) || (pos>count+1))
{
cout<<"invalid position:\n";
return;
}
while((p!=null) && (num!=pos))
{
t1=p;
p=p->next;
num++;
}
t2=p;
p=new node;
cout<<"enter the info of the node:\n";
cin>>p->data;
if(t1==null)
first=p;
else
t1->next=p;
p->prev=t1;
p->next=t2;
if(t2!=null)
t2->prev=p;
count++;
}
void dlist::delet(int pos)
{
int num=1;
node *p,*t1,*t2;
p=first;
t1=t2=null;
if(pos<1 || pos>count)
{
if(count==0)
cout<<"the list is empty cannot delete:\n";
else
cout<<"invalid position:\n";
return;
}
while((p!=null) && (num!=pos))
{
t1=p;
p=p->next;
num++;
}
t1=p->prev;
t2=p->next;
if(t1==null)
first=first->next;
else
t1->next=t2;
if(t2!=null)
t2->prev=t1;
cout<<"the deleted node is:"<<p->data<<endl;
delete(p);
count--;
}
void dlist::display()
{
node *p;
p=first;
if(p==null)
{
cout<<"the list is empty cannot display:\n";
return;
}
else
{
cout<<"the nodes in the list are:\n";
while(p!=null)
{
cout<<p->data<<"\t";
p=p->next;
}
}
}

void main()
{
dlist d;
int c,pos;
cout<<"creation of the list:\n";
d.create();
while(1)
{
cout<<"\n----------------menu------------------\n";
cout<<"\n1-insert\n2-delete\n3-display\n4-exit\n";
cout<<"\nenter your choice\n";
cin>>c;
switch(c)
{
case 1:cout<<"enter position where node is to be inserted:\n";
cin>>pos;
d.add(pos);
break;
case 2:cout<<"enter position of the node to be deleted:\n";
cin>>pos;
d.delet(pos);
break;
case 3:d.display();
break;
case 4:exit(0);
}
}
}
/* output
creation of the list:
enter data of the node:
2
any more nodes to be created 1-yes or 2-no
1
enter data of the node:
5
any more nodes to be created 1-yes or 2-no
1
enter data of the node:
6
any more nodes to be created 1-yes or 2-no
1
enter data of the node:
7
any more nodes to be created 1-yes or 2-no
1
enter data of the node:
9
any more nodes to be created 1-yes or 2-no
2
----------------menu------------------
1-insert
2-delete
3-display
4-exit
enter your choice
3
the nodes in the list are:
2 5 6 7 9
----------------menu------------------
1-insert
2-delete
3-display
4-exit
enter your choice
2
enter position of the node to be deleted:
3
the deleted node is:6
----------------menu------------------
1-insert
2-delete
3-display
4-exit
enter your choice
1
enter position where node is to be inserted:
3
enter the info of the node:
4
----------------menu------------------
1-insert
2-delete
3-display
4-exit
enter your choice
3
the nodes in the list are:
2 5 4 7 9
----------------menu------------------
1-insert
2-delete
3-display
4-exit
enter your choice
4

You might also like