C++ program (for beginners)



All this time we have been blogging about various incidents happening in an around us and you. But, today i realized that we should not just concentrate upon or talk about tech all the time cause, it would make us seem monotonous. When we started this blog, we wanted it to be universal in terms of content. So, shouldn't we also have fun?
 
Here is  a C++ program that you could type on to your C++ compiler to "Calculate difference between two date " .

If you are thinking how would you do it if you do not have a C(or C++) compiler then here is the download link: Click to DOWNLOAD


#include<iostream.h>
#include<conio.h>
class date
{
    int day,month,year;
    public:
         date()
         {
            cout<<"Enter day month year...";
            cin>>day>>month>>year;
         }
         date(int a,int b,int c)
         {
              day=a;
              month=b;
              year=c;
         }
    int greater(date &d1,date &d2)//calculates the date which occurs after the other
        {
            date d(0,0,0);
            if(d1.year<d2.year)
            {
                 d=d1;
                 d1=d2;
                 d2=d;
                 return 0;
            }
            else if(d1.year==d2.year)
            {
                 if(d1.month<d2.month)
                 {
                    d=d1;
                    d1=d2;
                    d2=d;
                 }
                 else if(d1.month==d2.month)
                 {
                    if(d1.day<d2.day)
                    {
                        d=d1;
                        d1=d2;
                        d2=d;
                    }
                 }

            }
            else
            {
                    d=d1;
                    d1=d2;
                    d2=d;
                    return 0;
            }
        }
         int check_month()//checks whether the month has 31,30 or 29 (or 28) days
         {
                  if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
                    return 1;
                else if(year%4==0)
                 {
                    if(month==2)
                        return 2;
                 }
                 else if(month==4||month==6||month==9||month==11)
                    return 3;
                 else
                    return 4;
         }
         int diff(date d1,date d2)//calculates difference between two dates
         {
                int month1,month2,temp=d1.day,a=d1.month;
                if(d1.month==d2.month)
                {
                    temp=d2.day-d1.day;
                }
                while(d1.month!=d2.month)
                {
                        temp++;
                        if( d1.check_month()==1 )
                        {
                            if(temp==32)
                            {
                                d1.month++;
                                if(d1.month==13)
                                {
                                    d1.month=1;
                                    d1.year++;
                                }
                             }
                        }
                        else if( d1.check_month()==2 )
                        {
                            if(temp==30)
                            {
                                d1.month++;
                             }
                        }
                        else if( d1.check_month()==3 )
                        {
                            if(temp==31)
                            {
                                d1.month++;
                             }
                        }
                        else
                            {
                                if(temp==29)
                                    d1.month++;
                            }
                        if(d1.month==d2.month)
                        {
                            temp=temp-d1.day+d2.day+1;
                            break;
                        }
                }
                if(d1.year!=d2.year)
                {
                    temp+=360;
                    d1.year++;
                }
                return temp;
        }
        void add(int a)
        {
            date d(day,month,year);
            int month1;
            day=day+a;
            month1=d.check_month();
            if(month1==1)
            {
                if(day>31)
                 {
                    month++;
                    day=day-31;
                    if(month>12)
                    {
                        month=1;
                        year++;
                    }
                 }
            }
            else if(month1==2)
            {
                if(day>29)
                 {
                    month++;
                    day=day-29;
                 }
            }
            else if(month1==3)
            {
                if(day>30)
                 {
                    month++;
                    day=day-30;
                 }
            }
            else
            {
                if(day>28)
                 {
                    month++;
                    day=day-28;
                 }
            }
        }
        void display()
        {
            cout<<day<<"/"<<month<<"/"<<year;
        }
};
void main()
    {
        clrscr();
        date d1,d2;
        int n,val;
        d1.greater(d1,d2);
        n=d1.diff(d1,d2);
        cout<<"Difference="<<n<<"\n";
        cout<<"Enter the value to be added...";
        cin>>val;
        d1.add(val);
        d1.display();
        getch();
    }



Enjoy programming..................

Comments

Popular posts from this blog

Ingress : Google's Augmented Reality Game

DotA 2 Vs League of Legends

City Lens: Nokia's Augmented Reality Browser