netx_iit

challenging program using operator overloading

Jan 14th, 2021 (edited)
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.03 KB | None | 0 0
  1. /*solution 1 define c++ solution 2 define using C programming*/
  2. /*
  3. Challenging program MCA(Neel)-VNSGU
  4. For the class Date with properties int month; int day; int year, overload  the following operators.
  5. a. + operator [a+b (a is of date type and b is an integer), use the assumption that all years have 360
  6. days and months 30 days.
  7. b. – operator [a-b(same as above)]
  8. c. = operator [ supporting a=b=c=d]
  9. d. <,<=,>,>=
  10. e. ++, --[post and pre both]
  11.  
  12. */
  13.  
  14. /*
  15. Test Case
  16. Date : 14/01/2021
  17. Input : +1000
  18. Output : 24/10/2023
  19.  
  20. Input : -1000
  21. Output : 4-4-2018
  22.  
  23. Date : 1-1-2022
  24. Input : -1
  25. Output : 30-12-2021
  26.  
  27. Date : 30-12-2021
  28. Input : +1
  29. Output : 1-1-2022
  30.  
  31. */
  32. /*
  33. Solution - 1
  34. */
  35. #include<iostream>
  36. #include<stdlib.h>
  37. using namespace std;
  38. class date
  39. {
  40.     int day,month,year;
  41.     int tday,tmonth,tyear;
  42.     int d1,m1,y1;//geting 2nd date for compairing
  43.     public:
  44.         void get_data()
  45.         {
  46.             cout<<"Enter day";
  47.             cin>>day;
  48.             cout<<"Enter month";
  49.             cin>>month;
  50.             cout<<"Enter year";
  51.             cin>>year;
  52.         }
  53.         void disp()
  54.         {
  55.             cout<<"\n Enter Date is "<<day<<"-"<<month<<"-"<<year;
  56.             tday=day;
  57.             tmonth=month;
  58.             tyear=year;
  59.         }
  60.        
  61.         void op()
  62.         {
  63.             cout<<"\n Enter Date is "<<tday<<"-"<<tmonth<<"-"<<tyear;
  64.             cout<<"\n New Date is "<<day<<"-"<<month<<"-"<<year;
  65.             day=tday;
  66.             month=tmonth;
  67.             year=tyear;
  68.         }
  69.         void operator +(int d)
  70.         {
  71.            
  72.             int y=((d/30)/12);
  73.             year=year+y;
  74.             int m=((d/30)%12);
  75.             month=month+m;
  76.             day=day+(d%30);
  77.             if(day>30)
  78.             {
  79.                 if(month+(day/30)>12)
  80.                 {
  81.                     year=year+1;
  82.                     month=month+(month/12)-12;
  83.                     day=day%30;
  84.                 }
  85.                 else
  86.                 {
  87.                     month=month+(day/30);
  88.                     day=day%30;
  89.                 }
  90.             }
  91.             if(month>12)
  92.             {
  93.                 year=year+1;
  94.                 month=month-12;
  95.             }
  96.         }
  97.        
  98.         void operator -(int d)
  99.         {
  100.             int y=d/360;
  101.             year=year-y;
  102.             d=d%360;
  103.             int m=d/30;
  104.             if(m>=month)
  105.             {
  106.                 year=year-1;
  107.                 month=13-m;
  108.             }
  109.             else
  110.             {
  111.                 month=month-m;
  112.             }
  113.             d=d%30;
  114.             if(d>=day)
  115.             {
  116.                 if(day==d)
  117.                 {
  118.                     day=30;
  119.                 }
  120.                 else
  121.                 {
  122.                     day=31-d;
  123.                 }
  124.                 month=month-1;
  125.                 if(month==0)
  126.                 {
  127.                     year=year-1;
  128.                     month=12;
  129.                 }          
  130.             }
  131.             else
  132.             {
  133.                 day=day-d;
  134.             }
  135.         }
  136.        
  137.         void operator =(int d)
  138.         {
  139.             switch(d)
  140.             {
  141.                 case 1:
  142.                     cout<<day<<"-"<<month<<"-"<<year%100;
  143.                     break;
  144.                
  145.                 case 2:
  146.                     cout<<month<<"-"<<day<<"-"<<year%100;
  147.                     break;
  148.                    
  149.                 case 3:
  150.                     cout<<day<<"-"<<month<<"-"<<year;
  151.                     break; 
  152.                
  153.                 case 4:
  154.                     cout<<month<<"-"<<day<<"-"<<year;
  155.                     break;
  156.                
  157.                 default:
  158.                     cout<<"\n Invalid selection of format";
  159.             }
  160.         }
  161.        
  162.         void get_secnd_date()
  163.         {
  164.             cout<<"\n Enter 2nd date for compairing";
  165.             cout<<"\n Enter date";
  166.             cin>>d1;
  167.             cout<<"\n Enter Month";
  168.             cin>>m1;
  169.             cout<<"\n Enter Year";
  170.             cin>>y1;
  171.         }
  172.    
  173.         int compare()
  174.         {
  175.             if(day==d1 && month==m1 && year==y1)
  176.             {
  177.                 return 0;//both date is same
  178.             }
  179.             else if(year == y1)
  180.             {
  181.                 if(month==m1)
  182.                 {
  183.                     if(day > d1)
  184.                     {
  185.                         return 1;//1st date is big
  186.                     }
  187.                     else
  188.                     {
  189.                         return 2;//2nd date is big
  190.                     }
  191.                 }
  192.                 else if(month > m1)
  193.                 {
  194.                     return 1;
  195.                 }
  196.                 else
  197.                 {
  198.                     return 2;
  199.                 }
  200.             }
  201.             else
  202.             {
  203.                 if(year > y1)
  204.                 {
  205.                     return 1;//1st date is big
  206.                 }
  207.                 else
  208.                 {
  209.                     return 2;//second date is big
  210.                 }
  211.             }
  212.         }
  213.        
  214.         void operator <(int temp)
  215.         {
  216.             cout<<"\nFirst Date is "<<day<<"-"<<month<<"-"<<year;
  217.             cout<<"\nSecond Date is "<<d1<<"-"<<m1<<"-"<<y1;
  218.             cout<<"\n First date is less than second date=";
  219.             compare()==2?cout<<"True":cout<<"False";
  220.         }
  221.        
  222.         void operator <=(int temp)
  223.         {
  224.             cout<<"\n First date is less than or equal to second date=";
  225.             compare()==0?cout<<"True":cout<<"False";   
  226.         }
  227.        
  228.         void operator >(int temp)
  229.         {
  230.             cout<<"\n First date is greter than second date=";
  231.             compare()==1?cout<<"True":cout<<"False";   
  232.         }
  233.        
  234.         void operator >=(int temp)
  235.         {
  236.             cout<<"\n First date is greter than or equal to second date=";
  237.             compare()==0?cout<<"True":cout<<"False";   
  238.         }
  239.        
  240.        
  241.        
  242. };
  243. int main()
  244. {
  245.     date d1;
  246.     int day,ch;
  247.     d1.get_data();
  248.     d1.disp();
  249.     do
  250.     {
  251.         cout<<"\n1.Add day(using +)";
  252.         cout<<"\n2.Subtract(using -)";
  253.         cout<<"\n3.Change format of given date(=)";
  254.         cout<<"\n4.Date compairing(<,<=,>,>=)";
  255.         cout<<"\n5.Increment and Decrement in date";
  256.         cout<<"\n Enter your choice";
  257.         cin>>ch;
  258.         switch(ch)
  259.         {
  260.             case 1:
  261.                 system ("CLS");//clear screen
  262.                 cout<<"\n How many day you want to insert";
  263.                 cin>>day;
  264.                 d1+day;
  265.                 cout<<"\n After adding days into date new date is ";
  266.                 d1.op();
  267.                 break;
  268.                
  269.             case 2:
  270.                 system ("CLS");//clear screen
  271.                 cout<<"\n How many day you want to subtract";
  272.                 cin>>day;
  273.                 d1-day;
  274.                 cout<<"\n After subtracting days into date new date is ";
  275.                 d1.op();
  276.                 break;
  277.                
  278.             case 3:
  279.                 system ("CLS");//clear screen
  280.                 int f;
  281.                 cout<<"\n which format you want";
  282.                 cout<<"\n1.dd-mm-yy";
  283.                 cout<<"\n2.mm-dd-yy";
  284.                 cout<<"\n3.dd-mm-Y";
  285.                 cout<<"\n4.mm-dd-Y";
  286.                 cout<<"\nEnter your format";
  287.                 cin>>f;
  288.                 d1=f;
  289.                 break;
  290.                
  291.             case 4:
  292.                 system ("CLS");//clear screen
  293.                 d1.get_secnd_date();
  294.                 d1<0;//0 is taking for temporary demo arg
  295.                 d1<=0;
  296.                 d1>0;
  297.                 d1>=0;
  298.                 break;
  299.                
  300.             case 5:
  301.                 system ("CLS");//clear screen
  302.                 cout<<"\nDate increment (pre ++)";
  303.                 d1+1;
  304.                 d1.op();
  305.                 cout<<"\nMonth increment (post ++)";
  306.                 d1+30;
  307.                 d1.op();
  308.                 cout<<"\nDate decrement(pre --)";
  309.                 d1-1;
  310.                 d1.op();
  311.                 cout<<"\nMonth decrement(post --)";
  312.                 d1-30;
  313.                 d1.op();
  314.             break;
  315.                
  316.         }
  317.     }while(ch>=1 && ch<=5);
  318.    
  319. }
  320.  
  321. /*
  322. Solution - 2
  323. */
  324. #include<stdio.h>
  325. #include<conio.h>
  326. void calculate_date(int,int,int,int,int);
  327. int main()
  328. {
  329.     int d,m,y,ch,add_day,sub_day,sum;
  330.     printf("\n Enter day");
  331.     scanf("%d",&d);
  332.     printf("\n Enter month");
  333.     scanf("%d",&m);
  334.     printf("\n Enter year");
  335.     scanf("%d",&y);
  336.     do
  337.     {
  338.         printf("\n 1. Add day(s)");
  339.         printf("\n 2. Subtract day(s)");
  340.         printf("\n Enter your choice");
  341.         scanf("%d",&ch);
  342.         switch(ch)
  343.         {
  344.             case 1:
  345.             printf("\n How many days you want to insert");
  346.             scanf("%d",&add_day);
  347.             printf("Input date is %d - %d - %d",d,m,y);
  348.             calculate_date(d,m,y,add_day,1);//1 means add days
  349.             break;
  350.            
  351.             case 2:
  352.             printf("\n How many days you want to cut");
  353.             scanf("%d",&add_day);
  354.             printf("Input date is %d - %d - %d",d,m,y);
  355.             calculate_date(d,m,y,add_day,2);//2 means cut days
  356.             break;
  357.         }
  358.     }while(ch>=1 && ch<=2);
  359. }
  360. void calculate_date(int day,int month,int year,int d,int op)
  361. {
  362.     int tempy1,tempy2;
  363.     int sum;
  364.     tempy1=year%100;
  365.     tempy2=year/100;
  366.     if(op==1)//add days
  367.     {
  368.         sum=(tempy1*360)+(month*30)+day;
  369.         sum=sum+d;
  370.     }
  371.     else//op2 means subtract days
  372.     {
  373.         sum=(tempy1*360)+(month*30)+day;
  374.         sum=sum-d;
  375.     }
  376.     day=sum%30;
  377.     if(day==0)
  378.     {
  379.         day=30;
  380.         sum=sum-1;
  381.     }
  382.     sum=sum/30;
  383.     year=sum/12;
  384.     month=sum%12;
  385.     if(month==0)
  386.     {
  387.         year=year-1;
  388.         month=12;
  389.     }
  390.    
  391.     printf("\n After %d days new date is=",d);
  392.     printf("%d - %d - %d",day,month,year+(100*tempy2));
  393.    
  394. }
  395.  
Advertisement
Add Comment
Please, Sign In to add comment