Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*solution 1 define c++ solution 2 define using C programming*/
- /*
- Challenging program MCA(Neel)-VNSGU
- For the class Date with properties int month; int day; int year, overload the following operators.
- a. + operator [a+b (a is of date type and b is an integer), use the assumption that all years have 360
- days and months 30 days.
- b. – operator [a-b(same as above)]
- c. = operator [ supporting a=b=c=d]
- d. <,<=,>,>=
- e. ++, --[post and pre both]
- */
- /*
- Test Case
- Date : 14/01/2021
- Input : +1000
- Output : 24/10/2023
- Input : -1000
- Output : 4-4-2018
- Date : 1-1-2022
- Input : -1
- Output : 30-12-2021
- Date : 30-12-2021
- Input : +1
- Output : 1-1-2022
- */
- /*
- Solution - 1
- */
- #include<iostream>
- #include<stdlib.h>
- using namespace std;
- class date
- {
- int day,month,year;
- int tday,tmonth,tyear;
- int d1,m1,y1;//geting 2nd date for compairing
- public:
- void get_data()
- {
- cout<<"Enter day";
- cin>>day;
- cout<<"Enter month";
- cin>>month;
- cout<<"Enter year";
- cin>>year;
- }
- void disp()
- {
- cout<<"\n Enter Date is "<<day<<"-"<<month<<"-"<<year;
- tday=day;
- tmonth=month;
- tyear=year;
- }
- void op()
- {
- cout<<"\n Enter Date is "<<tday<<"-"<<tmonth<<"-"<<tyear;
- cout<<"\n New Date is "<<day<<"-"<<month<<"-"<<year;
- day=tday;
- month=tmonth;
- year=tyear;
- }
- void operator +(int d)
- {
- int y=((d/30)/12);
- year=year+y;
- int m=((d/30)%12);
- month=month+m;
- day=day+(d%30);
- if(day>30)
- {
- if(month+(day/30)>12)
- {
- year=year+1;
- month=month+(month/12)-12;
- day=day%30;
- }
- else
- {
- month=month+(day/30);
- day=day%30;
- }
- }
- if(month>12)
- {
- year=year+1;
- month=month-12;
- }
- }
- void operator -(int d)
- {
- int y=d/360;
- year=year-y;
- d=d%360;
- int m=d/30;
- if(m>=month)
- {
- year=year-1;
- month=13-m;
- }
- else
- {
- month=month-m;
- }
- d=d%30;
- if(d>=day)
- {
- if(day==d)
- {
- day=30;
- }
- else
- {
- day=31-d;
- }
- month=month-1;
- if(month==0)
- {
- year=year-1;
- month=12;
- }
- }
- else
- {
- day=day-d;
- }
- }
- void operator =(int d)
- {
- switch(d)
- {
- case 1:
- cout<<day<<"-"<<month<<"-"<<year%100;
- break;
- case 2:
- cout<<month<<"-"<<day<<"-"<<year%100;
- break;
- case 3:
- cout<<day<<"-"<<month<<"-"<<year;
- break;
- case 4:
- cout<<month<<"-"<<day<<"-"<<year;
- break;
- default:
- cout<<"\n Invalid selection of format";
- }
- }
- void get_secnd_date()
- {
- cout<<"\n Enter 2nd date for compairing";
- cout<<"\n Enter date";
- cin>>d1;
- cout<<"\n Enter Month";
- cin>>m1;
- cout<<"\n Enter Year";
- cin>>y1;
- }
- int compare()
- {
- if(day==d1 && month==m1 && year==y1)
- {
- return 0;//both date is same
- }
- else if(year == y1)
- {
- if(month==m1)
- {
- if(day > d1)
- {
- return 1;//1st date is big
- }
- else
- {
- return 2;//2nd date is big
- }
- }
- else if(month > m1)
- {
- return 1;
- }
- else
- {
- return 2;
- }
- }
- else
- {
- if(year > y1)
- {
- return 1;//1st date is big
- }
- else
- {
- return 2;//second date is big
- }
- }
- }
- void operator <(int temp)
- {
- cout<<"\nFirst Date is "<<day<<"-"<<month<<"-"<<year;
- cout<<"\nSecond Date is "<<d1<<"-"<<m1<<"-"<<y1;
- cout<<"\n First date is less than second date=";
- compare()==2?cout<<"True":cout<<"False";
- }
- void operator <=(int temp)
- {
- cout<<"\n First date is less than or equal to second date=";
- compare()==0?cout<<"True":cout<<"False";
- }
- void operator >(int temp)
- {
- cout<<"\n First date is greter than second date=";
- compare()==1?cout<<"True":cout<<"False";
- }
- void operator >=(int temp)
- {
- cout<<"\n First date is greter than or equal to second date=";
- compare()==0?cout<<"True":cout<<"False";
- }
- };
- int main()
- {
- date d1;
- int day,ch;
- d1.get_data();
- d1.disp();
- do
- {
- cout<<"\n1.Add day(using +)";
- cout<<"\n2.Subtract(using -)";
- cout<<"\n3.Change format of given date(=)";
- cout<<"\n4.Date compairing(<,<=,>,>=)";
- cout<<"\n5.Increment and Decrement in date";
- cout<<"\n Enter your choice";
- cin>>ch;
- switch(ch)
- {
- case 1:
- system ("CLS");//clear screen
- cout<<"\n How many day you want to insert";
- cin>>day;
- d1+day;
- cout<<"\n After adding days into date new date is ";
- d1.op();
- break;
- case 2:
- system ("CLS");//clear screen
- cout<<"\n How many day you want to subtract";
- cin>>day;
- d1-day;
- cout<<"\n After subtracting days into date new date is ";
- d1.op();
- break;
- case 3:
- system ("CLS");//clear screen
- int f;
- cout<<"\n which format you want";
- cout<<"\n1.dd-mm-yy";
- cout<<"\n2.mm-dd-yy";
- cout<<"\n3.dd-mm-Y";
- cout<<"\n4.mm-dd-Y";
- cout<<"\nEnter your format";
- cin>>f;
- d1=f;
- break;
- case 4:
- system ("CLS");//clear screen
- d1.get_secnd_date();
- d1<0;//0 is taking for temporary demo arg
- d1<=0;
- d1>0;
- d1>=0;
- break;
- case 5:
- system ("CLS");//clear screen
- cout<<"\nDate increment (pre ++)";
- d1+1;
- d1.op();
- cout<<"\nMonth increment (post ++)";
- d1+30;
- d1.op();
- cout<<"\nDate decrement(pre --)";
- d1-1;
- d1.op();
- cout<<"\nMonth decrement(post --)";
- d1-30;
- d1.op();
- break;
- }
- }while(ch>=1 && ch<=5);
- }
- /*
- Solution - 2
- */
- #include<stdio.h>
- #include<conio.h>
- void calculate_date(int,int,int,int,int);
- int main()
- {
- int d,m,y,ch,add_day,sub_day,sum;
- printf("\n Enter day");
- scanf("%d",&d);
- printf("\n Enter month");
- scanf("%d",&m);
- printf("\n Enter year");
- scanf("%d",&y);
- do
- {
- printf("\n 1. Add day(s)");
- printf("\n 2. Subtract day(s)");
- printf("\n Enter your choice");
- scanf("%d",&ch);
- switch(ch)
- {
- case 1:
- printf("\n How many days you want to insert");
- scanf("%d",&add_day);
- printf("Input date is %d - %d - %d",d,m,y);
- calculate_date(d,m,y,add_day,1);//1 means add days
- break;
- case 2:
- printf("\n How many days you want to cut");
- scanf("%d",&add_day);
- printf("Input date is %d - %d - %d",d,m,y);
- calculate_date(d,m,y,add_day,2);//2 means cut days
- break;
- }
- }while(ch>=1 && ch<=2);
- }
- void calculate_date(int day,int month,int year,int d,int op)
- {
- int tempy1,tempy2;
- int sum;
- tempy1=year%100;
- tempy2=year/100;
- if(op==1)//add days
- {
- sum=(tempy1*360)+(month*30)+day;
- sum=sum+d;
- }
- else//op2 means subtract days
- {
- sum=(tempy1*360)+(month*30)+day;
- sum=sum-d;
- }
- day=sum%30;
- if(day==0)
- {
- day=30;
- sum=sum-1;
- }
- sum=sum/30;
- year=sum/12;
- month=sum%12;
- if(month==0)
- {
- year=year-1;
- month=12;
- }
- printf("\n After %d days new date is=",d);
- printf("%d - %d - %d",day,month,year+(100*tempy2));
- }
Advertisement
Add Comment
Please, Sign In to add comment